|
| 1 | +package cdn |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | + |
| 10 | + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccDataDomainTemplateApplyRecords_basic(t *testing.T) { |
| 14 | + var ( |
| 15 | + name = acceptance.RandomAccResourceNameWithDash() |
| 16 | + |
| 17 | + allRecords = "data.huaweicloud_cdn_domain_template_apply_records.all" |
| 18 | + dc = acceptance.InitDataSourceCheck(allRecords) |
| 19 | + |
| 20 | + byTemplateId = "data.huaweicloud_cdn_domain_template_apply_records.filter_by_template_id" |
| 21 | + dcByTemplateId = acceptance.InitDataSourceCheck(byTemplateId) |
| 22 | + |
| 23 | + byTemplateName = "data.huaweicloud_cdn_domain_template_apply_records.filter_by_template_name" |
| 24 | + dcByTemplateName = acceptance.InitDataSourceCheck(byTemplateName) |
| 25 | + |
| 26 | + byOperatorId = "data.huaweicloud_cdn_domain_template_apply_records.filter_by_operator_id" |
| 27 | + dcByOperatorId = acceptance.InitDataSourceCheck(byOperatorId) |
| 28 | + ) |
| 29 | + |
| 30 | + resource.ParallelTest(t, resource.TestCase{ |
| 31 | + PreCheck: func() { |
| 32 | + acceptance.TestAccPreCheck(t) |
| 33 | + acceptance.TestAccPreCheckCdnDomainName(t) |
| 34 | + }, |
| 35 | + ProviderFactories: acceptance.TestAccProviderFactories, |
| 36 | + Steps: []resource.TestStep{ |
| 37 | + { |
| 38 | + Config: testAccDataDomainTemplateApplyRecords_basic(name), |
| 39 | + Check: resource.ComposeTestCheckFunc( |
| 40 | + dc.CheckResourceExists(), |
| 41 | + resource.TestMatchResourceAttr(allRecords, "records.#", regexp.MustCompile("^[1-9]([0-9]+)?$")), |
| 42 | + dcByTemplateId.CheckResourceExists(), |
| 43 | + resource.TestCheckOutput("is_template_id_filter_useful", "true"), |
| 44 | + resource.TestCheckResourceAttrSet(byTemplateId, "records.0.operator_id"), |
| 45 | + resource.TestCheckResourceAttrSet(byTemplateId, "records.0.status"), |
| 46 | + resource.TestCheckResourceAttrSet(byTemplateId, "records.0.template_id"), |
| 47 | + resource.TestCheckResourceAttrSet(byTemplateId, "records.0.template_name"), |
| 48 | + resource.TestCheckResourceAttrSet(byTemplateId, "records.0.description"), |
| 49 | + resource.TestMatchResourceAttr(byTemplateId, "records.0.apply_time", |
| 50 | + regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)), |
| 51 | + resource.TestCheckResourceAttrSet(byTemplateId, "records.0.type"), |
| 52 | + resource.TestCheckResourceAttrSet(byTemplateId, "records.0.account_id"), |
| 53 | + resource.TestCheckResourceAttrSet(byTemplateId, "records.0.resources.#"), |
| 54 | + resource.TestCheckResourceAttrSet(byTemplateId, "records.0.resources.0.status"), |
| 55 | + resource.TestCheckResourceAttrSet(byTemplateId, "records.0.resources.0.domain_name"), |
| 56 | + resource.TestCheckResourceAttrSet(byTemplateId, "records.0.configs"), |
| 57 | + dcByTemplateName.CheckResourceExists(), |
| 58 | + resource.TestCheckOutput("is_template_name_filter_useful", "true"), |
| 59 | + dcByOperatorId.CheckResourceExists(), |
| 60 | + resource.TestCheckOutput("is_operator_id_filter_useful", "true"), |
| 61 | + ), |
| 62 | + }, |
| 63 | + }, |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +func testAccDataDomainTemplateApplyRecords_basic(name string) string { |
| 68 | + return fmt.Sprintf(` |
| 69 | +resource "huaweicloud_cdn_domain_template" "test" { |
| 70 | + name = "%[1]s" |
| 71 | + description = "Created by terraform for template apply records test" |
| 72 | + configs = jsonencode({ |
| 73 | + "cache_rules": [ |
| 74 | + { |
| 75 | + "force_cache": "on", |
| 76 | + "follow_origin": "off", |
| 77 | + "match_type": "all", |
| 78 | + "priority": 1, |
| 79 | + "stale_while_revalidate": "off", |
| 80 | + "ttl": 20, |
| 81 | + "ttl_unit": "d", |
| 82 | + "url_parameter_type": "full_url", |
| 83 | + "url_parameter_value": "" |
| 84 | + } |
| 85 | + ], |
| 86 | + "origin_follow302_status": "off", |
| 87 | + "compress": { |
| 88 | + "type": "gzip", |
| 89 | + "status": "on", |
| 90 | + "file_type": ".js,.html,.css" |
| 91 | + } |
| 92 | + }) |
| 93 | +} |
| 94 | +
|
| 95 | +resource "huaweicloud_cdn_domain_template_apply" "test" { |
| 96 | + template_id = huaweicloud_cdn_domain_template.test.id |
| 97 | + resources = "%[2]s" |
| 98 | +} |
| 99 | +
|
| 100 | +data "huaweicloud_cdn_domain_template_apply_records" "all" { |
| 101 | + depends_on = [ |
| 102 | + huaweicloud_cdn_domain_template_apply.test |
| 103 | + ] |
| 104 | +} |
| 105 | +
|
| 106 | +locals { |
| 107 | + template_id = huaweicloud_cdn_domain_template.test.id |
| 108 | +} |
| 109 | +
|
| 110 | +data "huaweicloud_cdn_domain_template_apply_records" "filter_by_template_id" { |
| 111 | + depends_on = [ |
| 112 | + huaweicloud_cdn_domain_template_apply.test |
| 113 | + ] |
| 114 | +
|
| 115 | + template_id = local.template_id |
| 116 | +} |
| 117 | +
|
| 118 | +locals { |
| 119 | + template_id_filter_result = [for v in data.huaweicloud_cdn_domain_template_apply_records.filter_by_template_id.records[*].template_id : |
| 120 | + v == local.template_id] |
| 121 | +} |
| 122 | +
|
| 123 | +output "is_template_id_filter_useful" { |
| 124 | + value = length(local.template_id_filter_result) > 0 && alltrue(local.template_id_filter_result) |
| 125 | +} |
| 126 | +
|
| 127 | +locals { |
| 128 | + template_name = huaweicloud_cdn_domain_template.test.name |
| 129 | +} |
| 130 | +
|
| 131 | +data "huaweicloud_cdn_domain_template_apply_records" "filter_by_template_name" { |
| 132 | + depends_on = [ |
| 133 | + huaweicloud_cdn_domain_template_apply.test |
| 134 | + ] |
| 135 | +
|
| 136 | + template_name = local.template_name |
| 137 | +} |
| 138 | +
|
| 139 | +locals { |
| 140 | + template_name_filter_result = [for v in data.huaweicloud_cdn_domain_template_apply_records.filter_by_template_name.records[*].template_name : |
| 141 | + v == local.template_name] |
| 142 | +} |
| 143 | +
|
| 144 | +output "is_template_name_filter_useful" { |
| 145 | + value = length(local.template_name_filter_result) > 0 && alltrue(local.template_name_filter_result) |
| 146 | +} |
| 147 | +
|
| 148 | +locals { |
| 149 | + operator_id = try(data.huaweicloud_cdn_domain_template_apply_records.all.records[0].operator_id, "NOT_FOUND") |
| 150 | +} |
| 151 | +
|
| 152 | +data "huaweicloud_cdn_domain_template_apply_records" "filter_by_operator_id" { |
| 153 | + operator_id = local.operator_id |
| 154 | +} |
| 155 | +
|
| 156 | +locals { |
| 157 | + operator_id_filter_result = [for v in data.huaweicloud_cdn_domain_template_apply_records.filter_by_operator_id.records[*].operator_id : |
| 158 | + v == local.operator_id] |
| 159 | +} |
| 160 | +
|
| 161 | +output "is_operator_id_filter_useful" { |
| 162 | + value = length(local.operator_id_filter_result) > 0 && alltrue(local.operator_id_filter_result) |
| 163 | +} |
| 164 | +`, name, acceptance.HW_CDN_DOMAIN_NAME) |
| 165 | +} |
0 commit comments