Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions docs/resources/cdn_domain_template_apply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
subcategory: "Content Delivery Network (CDN)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_cdn_domain_template_apply"
description: |-
Manages a CDN domain template apply resource within HuaweiCloud.
---

# huaweicloud_cdn_domain_template_apply

Manages a CDN domain template apply resource within HuaweiCloud.

-> This resource is a one-time action resource for applying CDN domain template to domains. Deleting this resource will
not clear the corresponding request record, but will only remove the resource information from the tfstate file.

## Example Usage

```hcl
variable "template_id" {}
variable "apply_domain_names" {
type = list(string)
}

resource "huaweicloud_cdn_domain_template_apply" "test" {
template_id = var.template_id
resources = join(",", var.apply_domain_names)
}
```

## Argument Reference

The following arguments are supported:

* `template_id` - (Required, String) Specifies the ID of the domain template to apply.

* `resources` - (Required, String) Specifies the list of domain names to apply the template.
Multiple domain names are separated by commas (,).
A maximum of 50 domains can be applied in a single operation.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The resource ID.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2623,6 +2623,7 @@ func Provider() *schema.Provider {
"huaweicloud_cdn_domain_owner_verify": cdn.ResourceDomainOwnerVerify(),
"huaweicloud_cdn_domain_rule": cdn.ResourceDomainRule(),
"huaweicloud_cdn_domain_template": cdn.ResourceDomainTemplate(),
"huaweicloud_cdn_domain_template_apply": cdn.ResourceDomainTemplateApply(),
"huaweicloud_cdn_rule_engine_rule": cdn.ResourceRuleEngineRule(),
"huaweicloud_cdn_statistic_configuration": cdn.ResourceStatisticConfiguration(),

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cdn

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccDomainTemplateApply_basic(t *testing.T) {
var (
rName = "huaweicloud_cdn_domain_template_apply.test"
)

// Avoid CheckDestroy, because there is nothing in the resource destroy method.
// lintignore:AT001
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckCdnDomainName(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDomainTemplateApply_basic(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(rName, "id"),
resource.TestCheckResourceAttrSet(rName, "template_id"),
resource.TestCheckResourceAttrSet(rName, "resources"),
),
},
},
})
}

func testAccDomainTemplateApply_basic() string {
return fmt.Sprintf(`
resource "huaweicloud_cdn_domain_template" "test" {
name = "%[1]s"
description = "Created by terraform for template apply test"
configs = jsonencode({
"cache_rules": [
{
"force_cache": "on",
"follow_origin": "off",
"match_type": "all",
"priority": 1,
"stale_while_revalidate": "off",
"ttl": 20,
"ttl_unit": "d",
"url_parameter_type": "full_url",
"url_parameter_value": ""
}
],
"origin_follow302_status": "off",
"compress": {
"type": "gzip",
"status": "on",
"file_type": ".js,.html,.css"
},
"ip_filter": {
"type": "white",
"value": "1.1.1.1"
}
})
}

resource "huaweicloud_cdn_domain_template_apply" "test" {
template_id = huaweicloud_cdn_domain_template.test.id
resources = "%[2]s"
}
`, acceptance.RandomAccResourceNameWithDash(), acceptance.HW_CDN_DOMAIN_NAME)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package cdn

import (
"context"
"log"
"strings"

"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/chnsz/golangsdk"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

// @API CDN POST /v1.0/cdn/configuration/templates/{tml_id}/apply
func ResourceDomainTemplateApply() *schema.Resource {
return &schema.Resource{
CreateContext: resourceDomainTemplateApplyCreate,
ReadContext: resourceDomainTemplateApplyRead,
UpdateContext: resourceDomainTemplateApplyUpdate,
DeleteContext: resourceDomainTemplateApplyDelete,

Schema: map[string]*schema.Schema{
// Required parameters.
"template_id": {
Type: schema.TypeString,
Required: true,
Description: `The ID of the domain template.`,
},
"resources": {
Type: schema.TypeString,
Required: true,
Description: `The list of domain names to apply the template. Multiple domain names are separated by commas.`,
},
},
}
}

func applyDomainTemplate(client *golangsdk.ServiceClient, templateId, resources string) error {
httpUrl := "v1.0/cdn/configuration/templates/{tml_id}/apply"
applyPath := client.Endpoint + httpUrl
applyPath = strings.ReplaceAll(applyPath, "{tml_id}", templateId)

applyOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{
"Content-Type": "application/json",
},
JSONBody: map[string]interface{}{
"resources": resources,
},
}

requestResp, err := client.Request("POST", applyPath, &applyOpt)
if err != nil {
return err
}

respBody, err := utils.FlattenResponse(requestResp)
if err != nil {
return err
}
if utils.PathSearch("status", respBody, nil) == "success" {
return nil
}

jobDetails := utils.PathSearch("detail", respBody, make([]interface{}, 0)).([]interface{})
if len(jobDetails) < 1 {
log.Printf("[ERROR] Unable to find the job details (with field `detail`) in the API response: %+v", respBody)
return nil
}

for _, jobDetail := range jobDetails {
if utils.PathSearch("status", jobDetail, nil) == "success" {
continue
}
log.Printf("[ERROR] The job (about domain `%v`) is applied failed, the error message is: %v",
utils.PathSearch("domain_name", jobDetail, ""), utils.PathSearch("error_msg", jobDetail, ""))
}
return nil
}

func resourceDomainTemplateApplyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
client, err := cfg.NewServiceClient("cdn", "")
if err != nil {
return diag.Errorf("error creating CDN client: %s", err)
}

err = applyDomainTemplate(client, d.Get("template_id").(string), d.Get("resources").(string))
if err != nil {
return diag.Errorf("error applying CDN domain template: %s", err)
}

randomUUID, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate ID: %s", err)
}
d.SetId(randomUUID)

return resourceDomainTemplateApplyRead(ctx, d, meta)
}

func resourceDomainTemplateApplyRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
}

func resourceDomainTemplateApplyUpdate(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
}

func resourceDomainTemplateApplyDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
errorMsg := `This resource is a one-time action resource used to apply CDN domain template. Deleting this resource
will not clear the corresponding request record, but will only remove the resource information from the tf state
file.`
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: errorMsg,
},
}
}
Loading