Skip to content

Commit 8c042ce

Browse files
committed
feat(cdn): add new resource to apply template to domains
1 parent 61c5e02 commit 8c042ce

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
subcategory: "Content Delivery Network (CDN)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_cdn_domain_template_apply"
5+
description: |-
6+
Manages a CDN domain template apply resource within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_cdn_domain_template_apply
10+
11+
Manages a CDN domain template apply resource within HuaweiCloud.
12+
13+
-> This resource is a one-time action resource for applying CDN domain template to domains. Deleting this resource will
14+
not clear the corresponding request record, but will only remove the resource information from the tfstate file.
15+
16+
## Example Usage
17+
18+
```hcl
19+
variable "template_id" {}
20+
variable "apply_domain_names" {
21+
type = list(string)
22+
}
23+
24+
resource "huaweicloud_cdn_domain_template_apply" "test" {
25+
template_id = var.template_id
26+
resources = join(",", var.apply_domain_names)
27+
}
28+
```
29+
30+
## Argument Reference
31+
32+
The following arguments are supported:
33+
34+
* `template_id` - (Required, String) Specifies the ID of the domain template to apply.
35+
36+
* `resources` - (Required, String) Specifies the list of domain names to apply the template.
37+
Multiple domain names are separated by commas (,).
38+
A maximum of 50 domains can be applied in a single operation.
39+
40+
## Attribute Reference
41+
42+
In addition to all arguments above, the following attributes are exported:
43+
44+
* `id` - The resource ID.

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,6 +2623,7 @@ func Provider() *schema.Provider {
26232623
"huaweicloud_cdn_domain_owner_verify": cdn.ResourceDomainOwnerVerify(),
26242624
"huaweicloud_cdn_domain_rule": cdn.ResourceDomainRule(),
26252625
"huaweicloud_cdn_domain_template": cdn.ResourceDomainTemplate(),
2626+
"huaweicloud_cdn_domain_template_apply": cdn.ResourceDomainTemplateApply(),
26262627
"huaweicloud_cdn_rule_engine_rule": cdn.ResourceRuleEngineRule(),
26272628
"huaweicloud_cdn_statistic_configuration": cdn.ResourceStatisticConfiguration(),
26282629

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package cdn
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccDomainTemplateApply_basic(t *testing.T) {
13+
var (
14+
rName = "huaweicloud_cdn_domain_template_apply.test"
15+
)
16+
17+
// Avoid CheckDestroy, because there is nothing in the resource destroy method.
18+
// lintignore:AT001
19+
resource.ParallelTest(t, resource.TestCase{
20+
PreCheck: func() {
21+
acceptance.TestAccPreCheck(t)
22+
acceptance.TestAccPreCheckCdnDomainName(t)
23+
},
24+
ProviderFactories: acceptance.TestAccProviderFactories,
25+
Steps: []resource.TestStep{
26+
{
27+
Config: testAccDomainTemplateApply_basic(),
28+
Check: resource.ComposeTestCheckFunc(
29+
resource.TestCheckResourceAttrSet(rName, "id"),
30+
resource.TestCheckResourceAttrSet(rName, "template_id"),
31+
resource.TestCheckResourceAttrSet(rName, "resources"),
32+
),
33+
},
34+
},
35+
})
36+
}
37+
38+
func testAccDomainTemplateApply_basic() string {
39+
return fmt.Sprintf(`
40+
resource "huaweicloud_cdn_domain_template" "test" {
41+
name = "%[1]s"
42+
description = "Created by terraform for template apply test"
43+
configs = jsonencode({
44+
"cache_rules": [
45+
{
46+
"force_cache": "on",
47+
"follow_origin": "off",
48+
"match_type": "all",
49+
"priority": 1,
50+
"stale_while_revalidate": "off",
51+
"ttl": 20,
52+
"ttl_unit": "d",
53+
"url_parameter_type": "full_url",
54+
"url_parameter_value": ""
55+
}
56+
],
57+
"origin_follow302_status": "off",
58+
"compress": {
59+
"type": "gzip",
60+
"status": "on",
61+
"file_type": ".js,.html,.css"
62+
},
63+
"ip_filter": {
64+
"type": "white",
65+
"value": "1.1.1.1"
66+
}
67+
})
68+
}
69+
70+
resource "huaweicloud_cdn_domain_template_apply" "test" {
71+
template_id = huaweicloud_cdn_domain_template.test.id
72+
resources = "%[2]s"
73+
}
74+
`, acceptance.RandomAccResourceNameWithDash(), acceptance.HW_CDN_DOMAIN_NAME)
75+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

Comments
 (0)