|
| 1 | +package eps |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/hashicorp/go-uuid" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 11 | + |
| 12 | + "github.com/chnsz/golangsdk" |
| 13 | + |
| 14 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" |
| 15 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" |
| 16 | +) |
| 17 | + |
| 18 | +var actionNonUpdatableParams = []string{ |
| 19 | + "enterprise_project_id", |
| 20 | + "action", |
| 21 | +} |
| 22 | + |
| 23 | +// @API ER POST /v1.0/enterprise-projects/{enterprise_project_id}/action |
| 24 | +func ResourceAction() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + CreateContext: resourceActionCreate, |
| 27 | + UpdateContext: resourceActionUpdate, |
| 28 | + ReadContext: resourceActionRead, |
| 29 | + DeleteContext: resourceActionDelete, |
| 30 | + |
| 31 | + CustomizeDiff: config.FlexibleForceNew(actionNonUpdatableParams), |
| 32 | + |
| 33 | + Schema: map[string]*schema.Schema{ |
| 34 | + "enterprise_project_id": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + Description: `The ID of enterprise project to be operated.`, |
| 38 | + }, |
| 39 | + "action": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Required: true, |
| 42 | + Description: `The action type.`, |
| 43 | + }, |
| 44 | + "enable_force_new": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Optional: true, |
| 47 | + ValidateFunc: validation.StringInSlice([]string{"true", "false"}, false), |
| 48 | + Description: utils.SchemaDesc("", utils.SchemaDescInput{Internal: true}), |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func resourceActionCreate(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 55 | + cfg := meta.(*config.Config) |
| 56 | + client, err := cfg.NewServiceClient("eps", cfg.Region) |
| 57 | + if err != nil { |
| 58 | + return diag.Errorf("error creating EPS client: %s", err) |
| 59 | + } |
| 60 | + |
| 61 | + var ( |
| 62 | + httpUrl = "v1.0/enterprise-projects/{enterprise_project_id}/action" |
| 63 | + epsId = d.Get("enterprise_project_id").(string) |
| 64 | + action = d.Get("action").(string) |
| 65 | + ) |
| 66 | + createPath := client.Endpoint + httpUrl |
| 67 | + createPath = strings.ReplaceAll(createPath, "{enterprise_project_id}", epsId) |
| 68 | + |
| 69 | + createOpt := golangsdk.RequestOpts{ |
| 70 | + KeepResponseBody: true, |
| 71 | + MoreHeaders: map[string]string{ |
| 72 | + "Content-Type": "application/json", |
| 73 | + }, |
| 74 | + OkCodes: []int{204}, |
| 75 | + JSONBody: map[string]interface{}{ |
| 76 | + "action": action, |
| 77 | + }, |
| 78 | + } |
| 79 | + _, err = client.Request("POST", createPath, &createOpt) |
| 80 | + if err != nil { |
| 81 | + return diag.Errorf("unable to %s the enterprise project (%s): %s", action, epsId, err) |
| 82 | + } |
| 83 | + |
| 84 | + randUUID, err := uuid.GenerateUUID() |
| 85 | + if err != nil { |
| 86 | + return diag.Errorf("unable to generate ID: %s", err) |
| 87 | + } |
| 88 | + d.SetId(randUUID) |
| 89 | + |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +func resourceActionRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func resourceActionUpdate(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +func resourceActionDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 102 | + errorMsg := `This resource is only a one-time action resource for operating the enterprise project. Deleting this |
| 103 | +resource will not clear the corresponding request record, but will only remove the resource information from the tfstate |
| 104 | +file.` |
| 105 | + return diag.Diagnostics{ |
| 106 | + diag.Diagnostic{ |
| 107 | + Severity: diag.Warning, |
| 108 | + Summary: errorMsg, |
| 109 | + }, |
| 110 | + } |
| 111 | +} |
0 commit comments