|
| 1 | +package eg |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/hashicorp/go-uuid" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 12 | + |
| 13 | + "github.com/chnsz/golangsdk" |
| 14 | + |
| 15 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" |
| 16 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" |
| 17 | +) |
| 18 | + |
| 19 | +var eventSubscriptionNonUpdateParams = []string{"subscription_ids", "operation", "enterprise_project_id"} |
| 20 | + |
| 21 | +// @API EG POST /v1/{project_id}/subscriptions/operation |
| 22 | +func ResourceEventSubscriptionBatchAction() *schema.Resource { |
| 23 | + return &schema.Resource{ |
| 24 | + CreateContext: resourceEventSubscriptionBatchActionCreate, |
| 25 | + ReadContext: resourceEventSubscriptionBatchActionRead, |
| 26 | + UpdateContext: resourceEventSubscriptionBatchActionUpdate, |
| 27 | + DeleteContext: resourceEventSubscriptionBatchActionDelete, |
| 28 | + |
| 29 | + CustomizeDiff: config.FlexibleForceNew(eventSubscriptionNonUpdateParams), |
| 30 | + |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "region": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Optional: true, |
| 35 | + Computed: true, |
| 36 | + ForceNew: true, |
| 37 | + Description: `The region where the event subscriptions are located.`, |
| 38 | + }, |
| 39 | + "subscription_ids": { |
| 40 | + Type: schema.TypeList, |
| 41 | + Required: true, |
| 42 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 43 | + Description: `The list of subscription IDs to be operated.`, |
| 44 | + }, |
| 45 | + "operation": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Required: true, |
| 48 | + Description: `Whether to enable the event subscription.`, |
| 49 | + }, |
| 50 | + "enterprise_project_id": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Optional: true, |
| 53 | + Description: `The ID of the enterprise project.`, |
| 54 | + }, |
| 55 | + // Internal parameters. |
| 56 | + "enable_force_new": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Optional: true, |
| 59 | + ValidateFunc: validation.StringInSlice([]string{"true", "false"}, false), |
| 60 | + Description: utils.SchemaDesc("", utils.SchemaDescInput{Internal: true}), |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func buildEventSubscriptionBatchActionBodyParams(d *schema.ResourceData) map[string]interface{} { |
| 67 | + return map[string]interface{}{ |
| 68 | + "subscription_ids": d.Get("subscription_ids"), |
| 69 | + "operation": d.Get("operation"), |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func buildEventSubscriptionBatchActionQueryParams(d *schema.ResourceData) string { |
| 74 | + res := "" |
| 75 | + if v, ok := d.GetOk("enterprise_project_id"); ok { |
| 76 | + res += fmt.Sprintf("?enterprise_project_id=%s", v.(string)) |
| 77 | + } |
| 78 | + return res |
| 79 | +} |
| 80 | + |
| 81 | +func checkEventSubscriptionBatchActionResult(respBody interface{}) error { |
| 82 | + failedCount := utils.PathSearch("failed_count", respBody, 0).(float64) |
| 83 | + if failedCount != 0 { |
| 84 | + events := utils.PathSearch("events", respBody, make([]interface{}, 0)).([]interface{}) |
| 85 | + var failedSubscriptions []string |
| 86 | + for _, event := range events { |
| 87 | + if errorCode := utils.PathSearch("error_code", event, nil); errorCode != nil { |
| 88 | + subscriptionId := utils.PathSearch("subscription_id", event, "").(string) |
| 89 | + errorMsg := utils.PathSearch("error_msg", event, "").(string) |
| 90 | + failedSubscriptions = append(failedSubscriptions, fmt.Sprintf("subscription %s: %s (%s)", subscriptionId, errorMsg, errorCode)) |
| 91 | + } |
| 92 | + } |
| 93 | + return fmt.Errorf("failed to operate %v subscription(s): %s", failedCount, strings.Join(failedSubscriptions, "; ")) |
| 94 | + } |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func resourceEventSubscriptionBatchActionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 99 | + var ( |
| 100 | + cfg = meta.(*config.Config) |
| 101 | + region = cfg.GetRegion(d) |
| 102 | + httpUrl = "v1/{project_id}/subscriptions/operation" |
| 103 | + ) |
| 104 | + |
| 105 | + client, err := cfg.NewServiceClient("eg", region) |
| 106 | + if err != nil { |
| 107 | + return diag.Errorf("error creating EG client: %s", err) |
| 108 | + } |
| 109 | + |
| 110 | + createPath := client.Endpoint + httpUrl |
| 111 | + createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID) |
| 112 | + createPath += buildEventSubscriptionBatchActionQueryParams(d) |
| 113 | + createOpt := golangsdk.RequestOpts{ |
| 114 | + KeepResponseBody: true, |
| 115 | + MoreHeaders: map[string]string{ |
| 116 | + "Content-Type": "application/json", |
| 117 | + }, |
| 118 | + JSONBody: buildEventSubscriptionBatchActionBodyParams(d), |
| 119 | + } |
| 120 | + |
| 121 | + resp, err := client.Request("POST", createPath, &createOpt) |
| 122 | + if err != nil { |
| 123 | + return diag.Errorf("error creating EG event subscription action: %s", err) |
| 124 | + } |
| 125 | + |
| 126 | + respBody, err := utils.FlattenResponse(resp) |
| 127 | + if err != nil { |
| 128 | + return diag.FromErr(err) |
| 129 | + } |
| 130 | + |
| 131 | + randomUUID, err := uuid.GenerateUUID() |
| 132 | + if err != nil { |
| 133 | + return diag.Errorf("unable to generate ID: %s", err) |
| 134 | + } |
| 135 | + d.SetId(randomUUID) |
| 136 | + |
| 137 | + err = checkEventSubscriptionBatchActionResult(respBody) |
| 138 | + if err != nil { |
| 139 | + return diag.FromErr(err) |
| 140 | + } |
| 141 | + |
| 142 | + return resourceEventSubscriptionBatchActionRead(ctx, d, meta) |
| 143 | +} |
| 144 | + |
| 145 | +func resourceEventSubscriptionBatchActionRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func resourceEventSubscriptionBatchActionUpdate(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 150 | + return nil |
| 151 | +} |
| 152 | + |
| 153 | +func resourceEventSubscriptionBatchActionDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 154 | + errorMsg := `This resource is only a one-time action resource for operate event subscription status. Deleting this |
| 155 | +resource will not clear the corresponding request record, but will only remove the resource information from the tfstate |
| 156 | + file.` |
| 157 | + return diag.Diagnostics{ |
| 158 | + diag.Diagnostic{ |
| 159 | + Severity: diag.Warning, |
| 160 | + Summary: errorMsg, |
| 161 | + }, |
| 162 | + } |
| 163 | +} |
0 commit comments