|
| 1 | +package cdn |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 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 | + |
| 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 | +// @API CDN POST /v1.0/cdn/configuration/templates/{tml_id}/apply |
| 19 | +func ResourceDomainTemplateApply() *schema.Resource { |
| 20 | + return &schema.Resource{ |
| 21 | + CreateContext: resourceDomainTemplateApplyCreate, |
| 22 | + ReadContext: resourceDomainTemplateApplyRead, |
| 23 | + UpdateContext: resourceDomainTemplateApplyUpdate, |
| 24 | + DeleteContext: resourceDomainTemplateApplyDelete, |
| 25 | + |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + // Required parameters. |
| 28 | + "template_id": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + Description: `The ID of the domain template.`, |
| 32 | + }, |
| 33 | + "resources": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Required: true, |
| 36 | + Description: `The list of domain names to apply the template. Multiple domain names are separated by commas.`, |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func applyDomainTemplate(client *golangsdk.ServiceClient, templateId, resources string) error { |
| 43 | + httpUrl := "v1.0/cdn/configuration/templates/{tml_id}/apply" |
| 44 | + applyPath := client.Endpoint + httpUrl |
| 45 | + applyPath = strings.ReplaceAll(applyPath, "{tml_id}", templateId) |
| 46 | + |
| 47 | + applyOpt := golangsdk.RequestOpts{ |
| 48 | + KeepResponseBody: true, |
| 49 | + MoreHeaders: map[string]string{ |
| 50 | + "Content-Type": "application/json", |
| 51 | + }, |
| 52 | + JSONBody: map[string]interface{}{ |
| 53 | + "resources": resources, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + requestResp, err := client.Request("POST", applyPath, &applyOpt) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + respBody, err := utils.FlattenResponse(requestResp) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + if utils.PathSearch("status", respBody, nil) == "success" { |
| 67 | + return nil |
| 68 | + } |
| 69 | + |
| 70 | + jobDetails := utils.PathSearch("detail", respBody, make([]interface{}, 0)).([]interface{}) |
| 71 | + if len(jobDetails) < 1 { |
| 72 | + log.Printf("[ERROR] Unable to find the job details (with field `detail`) in the API response: %+v", respBody) |
| 73 | + return nil |
| 74 | + } |
| 75 | + |
| 76 | + for _, jobDetail := range jobDetails { |
| 77 | + if utils.PathSearch("status", jobDetail, nil) == "success" { |
| 78 | + continue |
| 79 | + } |
| 80 | + log.Printf("[ERROR] The job (about domain `%v`) is applied failed, the error message is: %v", |
| 81 | + utils.PathSearch("domain_name", jobDetail, ""), utils.PathSearch("error_msg", jobDetail, "")) |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func resourceDomainTemplateApplyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 87 | + cfg := meta.(*config.Config) |
| 88 | + client, err := cfg.NewServiceClient("cdn", "") |
| 89 | + if err != nil { |
| 90 | + return diag.Errorf("error creating CDN client: %s", err) |
| 91 | + } |
| 92 | + |
| 93 | + err = applyDomainTemplate(client, d.Get("template_id").(string), d.Get("resources").(string)) |
| 94 | + if err != nil { |
| 95 | + return diag.Errorf("error applying CDN domain template: %s", err) |
| 96 | + } |
| 97 | + |
| 98 | + randomUUID, err := uuid.GenerateUUID() |
| 99 | + if err != nil { |
| 100 | + return diag.Errorf("unable to generate ID: %s", err) |
| 101 | + } |
| 102 | + d.SetId(randomUUID) |
| 103 | + |
| 104 | + return resourceDomainTemplateApplyRead(ctx, d, meta) |
| 105 | +} |
| 106 | + |
| 107 | +func resourceDomainTemplateApplyRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +func resourceDomainTemplateApplyUpdate(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func resourceDomainTemplateApplyDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics { |
| 116 | + errorMsg := `This resource is a one-time action resource used to apply CDN domain template. Deleting this resource |
| 117 | + will not clear the corresponding request record, but will only remove the resource information from the tf state |
| 118 | + file.` |
| 119 | + return diag.Diagnostics{ |
| 120 | + diag.Diagnostic{ |
| 121 | + Severity: diag.Warning, |
| 122 | + Summary: errorMsg, |
| 123 | + }, |
| 124 | + } |
| 125 | +} |
0 commit comments