From 02ba18a972dbd79a4f9b2cfbaac6734acc3b97eb Mon Sep 17 00:00:00 2001 From: crab Date: Mon, 22 Jun 2020 17:56:17 +0800 Subject: [PATCH 1/7] add cdn datasource --- CHANGELOG.md | 5 + examples/tencentcloud-cdn/main.tf | 21 +- tencentcloud/common.go | 28 ++ tencentcloud/data_source_tc_cdn_domains.go | 292 ++++++++++++++++++ .../data_source_tc_cdn_domains_test.go | 130 ++++++++ tencentcloud/extension_cdn.go | 8 + tencentcloud/provider.go | 1 + tencentcloud/service_tencentcloud_cdn.go | 35 +++ website/docs/d/cdn_domain.html.markdown | 64 ++++ website/docs/d/cdn_domains.html.markdown | 64 ++++ 10 files changed, 640 insertions(+), 8 deletions(-) create mode 100644 tencentcloud/data_source_tc_cdn_domains.go create mode 100644 tencentcloud/data_source_tc_cdn_domains_test.go create mode 100644 website/docs/d/cdn_domain.html.markdown create mode 100644 website/docs/d/cdn_domains.html.markdown diff --git a/CHANGELOG.md b/CHANGELOG.md index c06ddf50de..aab51f1d14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,9 @@ ## 1.37.0 (Unreleased) + +FEATURES: + +* **New Data Source**: `tencentcloud_cdn_domains` + ## 1.36.1 (June 12, 2020) ENHANCEMENTS: diff --git a/examples/tencentcloud-cdn/main.tf b/examples/tencentcloud-cdn/main.tf index ebe24392be..6242f34e36 100644 --- a/examples/tencentcloud-cdn/main.tf +++ b/examples/tencentcloud-cdn/main.tf @@ -1,23 +1,28 @@ resource "tencentcloud_cdn_domain" "foo" { - domain = "xxxx.com" + domain = "xxxx.com" service_type = "web" - area = "mainland" + area = "mainland" origin { - origin_type = "ip" - origin_list = ["127.0.0.1"] + origin_type = "ip" + origin_list = ["127.0.0.1"] origin_pull_protocol = "follow" } https_config { - https_switch = "off" - http2_switch = "off" + https_switch = "off" + http2_switch = "off" ocsp_stapling_switch = "off" - spdy_switch = "off" - verify_client = "off" + spdy_switch = "off" + verify_client = "off" } tags = { hello = "world" } +} + +data "tencentcloud_cdn_domains" "cdn_domain" { + domain = tencentcloud_cdn_domain.foo.domain + service_type = tencentcloud_cdn_domain.foo.service_type } \ No newline at end of file diff --git a/tencentcloud/common.go b/tencentcloud/common.go index db21aea52c..6dae2d3439 100644 --- a/tencentcloud/common.go +++ b/tencentcloud/common.go @@ -240,3 +240,31 @@ func IsContains(array interface{}, value interface{}) bool { return reflect.DeepEqual(array, value) } } + +func UnderlineToHump(underline string) (humpValue string) { + if len(underline) == 0 { + return "" + } + + splitResult := strings.Split(underline, "_") + if len(splitResult) == 0 { + return "" + } + + if len(splitResult) == 1 { + return underline + } + + for flag, v := range splitResult { + if len(v) == 0 { + continue + } + if flag == 0 { + humpValue += v + continue + } + humpValue += strings.ToUpper(v[:1]) + v[1:] + } + + return +} diff --git a/tencentcloud/data_source_tc_cdn_domains.go b/tencentcloud/data_source_tc_cdn_domains.go new file mode 100644 index 0000000000..c6f9ce69c1 --- /dev/null +++ b/tencentcloud/data_source_tc_cdn_domains.go @@ -0,0 +1,292 @@ +/* +Use this data source to query the detail information of CDN domain. + +Example Usage + +```hcl +data "tencentcloud_cdn_domains" "foo" { + domain = "xxxx.com" + service_type = "web" + full_url_cache = false + origin_pull_protocol = "follow" + status = "online" + https_switch = "on" +} +``` +*/ +package tencentcloud + +import ( + "context" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + cdn "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn/v20180606" +) + +func dataSourceTencentCloudCdnDomains() *schema.Resource { + return &schema.Resource{ + Read: dataSourceTencentCloudCdnDomainsRead, + Schema: map[string]*schema.Schema{ + "domain": { + Type: schema.TypeString, + Optional: true, + Description: "Name of the acceleration domain.", + }, + "service_type": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validateAllowedStringValue(CDN_SERVICE_TYPE), + Description: "Service type of Acceleration domain name. The available value include `web`, `download` and `media`.", + }, + "full_url_cache": { + Type: schema.TypeBool, + Optional: true, + Description: "Whether to enable full-path cache.", + }, + "origin_pull_protocol": { + Type: schema.TypeBool, + Optional: true, + ValidateFunc: validateAllowedStringValue(CDN_ORIGIN_PULL_PROTOCOL), + Description: "Origin-pull protocol configuration. The available value include `http`, `https` and `follow`.", + }, + "https_switch": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validateAllowedStringValue(CDN_HTTPS_SWITCH), + Description: "HTTPS configuration. The available value include `on`, `off` and `processing`.", + }, + "result_output_file": { + Type: schema.TypeString, + Optional: true, + Description: "Used to save results.", + }, + "cdn_domain_list": { + Type: schema.TypeList, + Computed: true, + Description: "Information list of cdn domain.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "domain": { + Type: schema.TypeString, + Computed: true, + Description: "Name of the acceleration domain.", + }, + "service_type": { + Type: schema.TypeString, + Computed: true, + Description: "Service type of Acceleration domain name.", + }, + "area": { + Type: schema.TypeString, + Computed: true, + Description: "Domain name acceleration region.", + }, + "project_id": { + Type: schema.TypeString, + Computed: true, + Description: "The project CDN belongs to.", + }, + "full_url_cache": { + Type: schema.TypeBool, + Computed: true, + Description: "Whether to enable full-path cache.", + }, + "origin": { + Type: schema.TypeList, + Computed: true, + Description: "Origin server configuration. It's a list and consist of at most one item.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "origin_type": { + Type: schema.TypeString, + Computed: true, + Description: "Master origin server type.", + }, + "origin_list": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Description: "Master origin server list.", + }, + "backup_origin_type": { + Type: schema.TypeString, + Computed: true, + Description: "Backup origin server type.", + }, + "backup_origin_list": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Description: "Backup origin server list.", + }, + "backup_server_name": { + Type: schema.TypeString, + Computed: true, + Description: "Host header used when accessing the backup origin server. If left empty, the ServerName of master origin server will be used by default.", + }, + "server_name": { + Type: schema.TypeString, + Computed: true, + Description: "Host header used when accessing the master origin server. If left empty, the acceleration domain name will be used by default.", + }, + "cos_private_access": { + Type: schema.TypeString, + Computed: true, + Description: "When OriginType is COS, you can specify if access to private buckets is allowed.", + }, + "origin_pull_protocol": { + Type: schema.TypeString, + Computed: true, + Description: "Origin-pull protocol configuration.", + }, + }, + }, + }, + "https_config": { + Type: schema.TypeList, + Computed: true, + Description: "HTTPS acceleration configuration. It's a list and consist of at most one item.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "https_switch": { + Type: schema.TypeString, + Computed: true, + Description: "HTTPS configuration switch.", + }, + "http2_switch": { + Type: schema.TypeString, + Computed: true, + Description: "HTTP2 configuration switch.", + }, + "ocsp_stapling_switch": { + Type: schema.TypeString, + Computed: true, + Description: "OCSP configuration switch.", + }, + "spdy_switch": { + Type: schema.TypeString, + Computed: true, + Description: "Spdy configuration switch.", + }, + "verify_client": { + Type: schema.TypeString, + Computed: true, + Description: "Client certificate authentication feature.", + }, + }, + }, + }, + "tags": { + Type: schema.TypeMap, + Computed: true, + Description: "Tags of cdn domain.", + }, + }, + }, + }, + }, + } +} + +func dataSourceTencentCloudCdnDomainsRead(d *schema.ResourceData, meta interface{}) error { + defer logElapsed("data_source.tencentcloud_cdn_domain.read")() + + logId := getLogId(contextNil) + ctx := context.WithValue(context.TODO(), logIdKey, logId) + + client := meta.(*TencentCloudClient).apiV3Conn + region := client.Region + cdnService := CdnService{client: client} + tagService := TagService{client: client} + + var domainFilterMap = make(map[string]interface{}, 5) + + if v, ok := d.GetOk(CDN_DATASOURCE_NAME_DOMAIN); ok { + domainFilterMap[CDN_DATASOURCE_NAME_DOMAIN] = v.(string) + } + if v, ok := d.GetOk("service_type"); ok { + domainFilterMap["service_type"] = v.(string) + } + if v, ok := d.GetOk("status"); ok { + domainFilterMap["status"] = v.(string) + } + if v, ok := d.GetOk("https_switch"); ok { + domainFilterMap["https_switch"] = v.(string) + } + if v, ok := d.GetOk("full_url_cache"); ok { + var value string + if v.(bool) { + value = "on" + } else { + value = "off" + } + + domainFilterMap["full_url_cache"] = value + } + + if len(domainFilterMap) == 0 { + return nil + } + + var domainConfig *cdn.DetailDomain + var errRet error + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { + domainConfig, errRet = cdnService.DescribeDomainsConfigByFilters(ctx, domainFilterMap) + if errRet != nil { + return retryError(errRet, InternalError) + } + return nil + }) + + if err != nil { + log.Printf("[CRITAL]%s describeDomainsConfigByFilters fail, reason:%s\n ", logId, err.Error()) + return err + } + if domainConfig == nil { + return nil + } + + domain := domainFilterMap[CDN_DATASOURCE_NAME_DOMAIN] + _ = d.Set(CDN_DATASOURCE_NAME_DOMAIN, domain) + _ = d.Set("service_type", domainConfig.ServiceType) + _ = d.Set("project_id", domainConfig.ProjectId) + _ = d.Set("area", domainConfig.Area) + _ = d.Set("status", domainConfig.Status) + + if *domainConfig.CacheKey.FullUrlCache == CDN_SWITCH_OFF { + _ = d.Set("full_url_cache", false) + } else { + _ = d.Set("full_url_cache", true) + } + + origins := make([]map[string]interface{}, 0, 1) + origin := make(map[string]interface{}, 8) + origin["origin_type"] = domainConfig.Origin.OriginType + origin["origin_list"] = domainConfig.Origin.Origins + origin["server_name"] = domainConfig.Origin.ServerName + origin["cos_private_access"] = domainConfig.Origin.CosPrivateAccess + origin["origin_pull_protocol"] = domainConfig.Origin.OriginPullProtocol + origin["backup_origin_type"] = domainConfig.Origin.BackupOriginType + origin["backup_origin_list"] = domainConfig.Origin.BackupOrigins + origin["backup_server_name"] = domainConfig.Origin.BackupServerName + origins = append(origins, origin) + _ = d.Set("origin", origins) + + httpsConfig := make(map[string]interface{}, 7) + httpsConfig["https_switch"] = domainConfig.Https.Switch + httpsConfig["http2_switch"] = domainConfig.Https.Http2 + httpsConfig["ocsp_stapling_switch"] = domainConfig.Https.OcspStapling + httpsConfig["spdy_switch"] = domainConfig.Https.Spdy + httpsConfig["verify_client"] = domainConfig.Https.VerifyClient + _ = d.Set("https_config", httpsConfig) + + tags, errRet := tagService.DescribeResourceTags(ctx, CDN_SERVICE_NAME, CDN_RESOURCE_NAME_DOMAIN, region, domain.(string)) + if errRet != nil { + return errRet + } + _ = d.Set("tags", tags) + + return nil +} diff --git a/tencentcloud/data_source_tc_cdn_domains_test.go b/tencentcloud/data_source_tc_cdn_domains_test.go new file mode 100644 index 0000000000..e3c06d2565 --- /dev/null +++ b/tencentcloud/data_source_tc_cdn_domains_test.go @@ -0,0 +1,130 @@ +package tencentcloud + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +func TestAccTencentCloudCdnDomains(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCdnDomainDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCdnDomainInfo, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "domain", "test.zhaoshaona.com"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "service_type", "web"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "area", "mainland"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "full_url_cache", "false"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "origin.0.origin_type", "ip"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "origin.0.origin_list.#", "1"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "origin.0.server_name", "test.zhaoshaona.com"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "origin.0.origin_pull_protocol", "follow"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.https_switch", "on"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.http2_switch", "on"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.ocsp_stapling_switch", "on"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.spdy_switch", "on"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.verify_client", "off"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.message", "test"), + resource.TestCheckResourceAttrSet("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.deploy_time"), + resource.TestCheckResourceAttrSet("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.expire_time"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "tags.test", "world"), + ), + }, + }, + }) +} + +const testAccCdnDomainInfo = ` +resource "tencentcloud_cdn_domain" "foo" { + domain = "test.zhaoshaona.com" + service_type = "web" + area = "mainland" + full_url_cache = false + + origin { + origin_type = "ip" + origin_list = ["172.199.199.140"] + server_name = "test.zhaoshaona.com" + origin_pull_protocol = "follow" + } + + https_config { + https_switch = "on" + http2_switch = "on" + ocsp_stapling_switch = "on" + spdy_switch = "on" + verify_client = "off" + + server_certificate_config { + certificate_content = < 0 { + domainConfig = response.Response.Domains[0] + } + return +} diff --git a/website/docs/d/cdn_domain.html.markdown b/website/docs/d/cdn_domain.html.markdown new file mode 100644 index 0000000000..181b61ee2c --- /dev/null +++ b/website/docs/d/cdn_domain.html.markdown @@ -0,0 +1,64 @@ +--- +layout: "tencentcloud" +page_title: "TencentCloud: tencentcloud_cdn_domain" +sidebar_current: "docs-tencentcloud-datasource-cdn_domain" +description: |- + Use this data source to query the detail information of CDN domain. +--- + +# tencentcloud_cdn_domain + +Use this data source to query the detail information of CDN domain. + +## Example Usage + +```hcl +data "tencentcloud_cdn_domains" "foo" { + domain = "xxxx.com" + service_type = "web" + full_url_cache = false + origin_pull_protocol = "follow" + status = "online" + https_switch = "on" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `domain` - (Optional) Name of the acceleration domain. +* `full_url_cache` - (Optional) Whether to enable full-path cache. +* `https_switch` - (Optional) HTTPS configuration. The available value include `on`, `off` and `processing`. +* `origin_pull_protocol` - (Optional) Origin-pull protocol configuration. The available value include `http`, `https` and `follow`. +* `result_output_file` - (Optional) Used to save results. +* `service_type` - (Optional) Service type of Acceleration domain name. The available value include `web`, `download` and `media`. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `cdn_domain_list` - Information list of cdn domain. + * `area` - Domain name acceleration region. + * `domain` - Name of the acceleration domain. + * `full_url_cache` - Whether to enable full-path cache. + * `https_config` - HTTPS acceleration configuration. It's a list and consist of at most one item. + * `http2_switch` - HTTP2 configuration switch. + * `https_switch` - HTTPS configuration switch. + * `ocsp_stapling_switch` - OCSP configuration switch. + * `spdy_switch` - Spdy configuration switch. + * `verify_client` - Client certificate authentication feature. + * `origin` - Origin server configuration. It's a list and consist of at most one item. + * `backup_origin_list` - Backup origin server list. + * `backup_origin_type` - Backup origin server type. + * `backup_server_name` - Host header used when accessing the backup origin server. If left empty, the ServerName of master origin server will be used by default. + * `cos_private_access` - When OriginType is COS, you can specify if access to private buckets is allowed. + * `origin_list` - Master origin server list. + * `origin_pull_protocol` - Origin-pull protocol configuration. + * `origin_type` - Master origin server type. + * `server_name` - Host header used when accessing the master origin server. If left empty, the acceleration domain name will be used by default. + * `project_id` - The project CDN belongs to. + * `service_type` - Service type of Acceleration domain name. + * `tags` - Tags of cdn domain. + + diff --git a/website/docs/d/cdn_domains.html.markdown b/website/docs/d/cdn_domains.html.markdown new file mode 100644 index 0000000000..0285efdca4 --- /dev/null +++ b/website/docs/d/cdn_domains.html.markdown @@ -0,0 +1,64 @@ +--- +layout: "tencentcloud" +page_title: "TencentCloud: tencentcloud_cdn_domains" +sidebar_current: "docs-tencentcloud-datasource-cdn_domains" +description: |- + Use this data source to query the detail information of CDN domain. +--- + +# tencentcloud_cdn_domains + +Use this data source to query the detail information of CDN domain. + +## Example Usage + +```hcl +data "tencentcloud_cdn_domains" "foo" { + domain = "xxxx.com" + service_type = "web" + full_url_cache = false + origin_pull_protocol = "follow" + status = "online" + https_switch = "on" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `domain` - (Optional) Name of the acceleration domain. +* `full_url_cache` - (Optional) Whether to enable full-path cache. +* `https_switch` - (Optional) HTTPS configuration. The available value include `on`, `off` and `processing`. +* `origin_pull_protocol` - (Optional) Origin-pull protocol configuration. The available value include `http`, `https` and `follow`. +* `result_output_file` - (Optional) Used to save results. +* `service_type` - (Optional) Service type of Acceleration domain name. The available value include `web`, `download` and `media`. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `cdn_domain_list` - Information list of cdn domain. + * `area` - Domain name acceleration region. + * `domain` - Name of the acceleration domain. + * `full_url_cache` - Whether to enable full-path cache. + * `https_config` - HTTPS acceleration configuration. It's a list and consist of at most one item. + * `http2_switch` - HTTP2 configuration switch. + * `https_switch` - HTTPS configuration switch. + * `ocsp_stapling_switch` - OCSP configuration switch. + * `spdy_switch` - Spdy configuration switch. + * `verify_client` - Client certificate authentication feature. + * `origin` - Origin server configuration. It's a list and consist of at most one item. + * `backup_origin_list` - Backup origin server list. + * `backup_origin_type` - Backup origin server type. + * `backup_server_name` - Host header used when accessing the backup origin server. If left empty, the ServerName of master origin server will be used by default. + * `cos_private_access` - When OriginType is COS, you can specify if access to private buckets is allowed. + * `origin_list` - Master origin server list. + * `origin_pull_protocol` - Origin-pull protocol configuration. + * `origin_type` - Master origin server type. + * `server_name` - Host header used when accessing the master origin server. If left empty, the acceleration domain name will be used by default. + * `project_id` - The project CDN belongs to. + * `service_type` - Service type of Acceleration domain name. + * `tags` - Tags of cdn domain. + + From a15ab6c315d44e521cb6db346160b8d0cf53946a Mon Sep 17 00:00:00 2001 From: crab Date: Tue, 23 Jun 2020 11:27:57 +0800 Subject: [PATCH 2/7] fix cdn datasource logic --- tencentcloud/common.go | 28 ---- tencentcloud/data_source_tc_cdn_domains.go | 127 ++++++++++-------- .../data_source_tc_cdn_domains_test.go | 6 + tencentcloud/extension_cdn.go | 2 - tencentcloud/provider.go | 3 + tencentcloud/service_tencentcloud_cdn.go | 24 +++- website/docs/d/cdn_domains.html.markdown | 15 +-- website/tencentcloud.erb | 9 +- 8 files changed, 115 insertions(+), 99 deletions(-) diff --git a/tencentcloud/common.go b/tencentcloud/common.go index 6dae2d3439..db21aea52c 100644 --- a/tencentcloud/common.go +++ b/tencentcloud/common.go @@ -240,31 +240,3 @@ func IsContains(array interface{}, value interface{}) bool { return reflect.DeepEqual(array, value) } } - -func UnderlineToHump(underline string) (humpValue string) { - if len(underline) == 0 { - return "" - } - - splitResult := strings.Split(underline, "_") - if len(splitResult) == 0 { - return "" - } - - if len(splitResult) == 1 { - return underline - } - - for flag, v := range splitResult { - if len(v) == 0 { - continue - } - if flag == 0 { - humpValue += v - continue - } - humpValue += strings.ToUpper(v[:1]) + v[1:] - } - - return -} diff --git a/tencentcloud/data_source_tc_cdn_domains.go b/tencentcloud/data_source_tc_cdn_domains.go index c6f9ce69c1..3783b2c77b 100644 --- a/tencentcloud/data_source_tc_cdn_domains.go +++ b/tencentcloud/data_source_tc_cdn_domains.go @@ -9,7 +9,6 @@ data "tencentcloud_cdn_domains" "foo" { service_type = "web" full_url_cache = false origin_pull_protocol = "follow" - status = "online" https_switch = "on" } ``` @@ -32,13 +31,13 @@ func dataSourceTencentCloudCdnDomains() *schema.Resource { "domain": { Type: schema.TypeString, Optional: true, - Description: "Name of the acceleration domain.", + Description: "Acceleration domain name.", }, "service_type": { Type: schema.TypeString, Optional: true, ValidateFunc: validateAllowedStringValue(CDN_SERVICE_TYPE), - Description: "Service type of Acceleration domain name. The available value include `web`, `download` and `media`.", + Description: "Service type of acceleration domain name. The available value include `web`, `download` and `media`.", }, "full_url_cache": { Type: schema.TypeBool, @@ -46,7 +45,7 @@ func dataSourceTencentCloudCdnDomains() *schema.Resource { Description: "Whether to enable full-path cache.", }, "origin_pull_protocol": { - Type: schema.TypeBool, + Type: schema.TypeString, Optional: true, ValidateFunc: validateAllowedStringValue(CDN_ORIGIN_PULL_PROTOCOL), Description: "Origin-pull protocol configuration. The available value include `http`, `https` and `follow`.", @@ -62,29 +61,29 @@ func dataSourceTencentCloudCdnDomains() *schema.Resource { Optional: true, Description: "Used to save results.", }, - "cdn_domain_list": { + "domain_list": { Type: schema.TypeList, Computed: true, - Description: "Information list of cdn domain.", + Description: "An information list of cdn domain. Each element contains the following attributes:", Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "domain": { Type: schema.TypeString, Computed: true, - Description: "Name of the acceleration domain.", + Description: "Acceleration domain name.", }, "service_type": { Type: schema.TypeString, Computed: true, - Description: "Service type of Acceleration domain name.", + Description: "Service type of acceleration domain name.", }, "area": { Type: schema.TypeString, Computed: true, - Description: "Domain name acceleration region.", + Description: "Acceleration region.", }, "project_id": { - Type: schema.TypeString, + Type: schema.TypeInt, Computed: true, Description: "The project CDN belongs to.", }, @@ -96,7 +95,7 @@ func dataSourceTencentCloudCdnDomains() *schema.Resource { "origin": { Type: schema.TypeList, Computed: true, - Description: "Origin server configuration. It's a list and consist of at most one item.", + Description: "Origin server configuration.", Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "origin_type": { @@ -203,18 +202,18 @@ func dataSourceTencentCloudCdnDomainsRead(d *schema.ResourceData, meta interface var domainFilterMap = make(map[string]interface{}, 5) - if v, ok := d.GetOk(CDN_DATASOURCE_NAME_DOMAIN); ok { - domainFilterMap[CDN_DATASOURCE_NAME_DOMAIN] = v.(string) + if v, ok := d.GetOk("domain"); ok { + domainFilterMap["domain"] = v.(string) } if v, ok := d.GetOk("service_type"); ok { domainFilterMap["service_type"] = v.(string) } - if v, ok := d.GetOk("status"); ok { - domainFilterMap["status"] = v.(string) - } if v, ok := d.GetOk("https_switch"); ok { domainFilterMap["https_switch"] = v.(string) } + if v, ok := d.GetOk("origin_pull_protocol"); ok { + domainFilterMap["origin_pull_protocol"] = v.(string) + } if v, ok := d.GetOk("full_url_cache"); ok { var value string if v.(bool) { @@ -226,14 +225,10 @@ func dataSourceTencentCloudCdnDomainsRead(d *schema.ResourceData, meta interface domainFilterMap["full_url_cache"] = value } - if len(domainFilterMap) == 0 { - return nil - } - - var domainConfig *cdn.DetailDomain + var domainConfigs []*cdn.DetailDomain var errRet error err := resource.Retry(readRetryTimeout, func() *resource.RetryError { - domainConfig, errRet = cdnService.DescribeDomainsConfigByFilters(ctx, domainFilterMap) + domainConfigs, errRet = cdnService.DescribeDomainsConfigByFilters(ctx, domainFilterMap) if errRet != nil { return retryError(errRet, InternalError) } @@ -244,49 +239,67 @@ func dataSourceTencentCloudCdnDomainsRead(d *schema.ResourceData, meta interface log.Printf("[CRITAL]%s describeDomainsConfigByFilters fail, reason:%s\n ", logId, err.Error()) return err } - if domainConfig == nil { + if domainConfigs == nil { return nil } - domain := domainFilterMap[CDN_DATASOURCE_NAME_DOMAIN] - _ = d.Set(CDN_DATASOURCE_NAME_DOMAIN, domain) - _ = d.Set("service_type", domainConfig.ServiceType) - _ = d.Set("project_id", domainConfig.ProjectId) - _ = d.Set("area", domainConfig.Area) - _ = d.Set("status", domainConfig.Status) + cdnDomainList := make([]map[string]interface{}, 0, len(domainConfigs)) + for _, detailDomain := range domainConfigs { + var fullUrlCache bool + if detailDomain.CacheKey!=nil && *detailDomain.CacheKey.FullUrlCache == CDN_SWITCH_ON { + fullUrlCache = true + } - if *domainConfig.CacheKey.FullUrlCache == CDN_SWITCH_OFF { - _ = d.Set("full_url_cache", false) - } else { - _ = d.Set("full_url_cache", true) - } + origins := make([]map[string]interface{}, 0, 1) + origin := make(map[string]interface{}, 8) + origin["origin_type"] = detailDomain.Origin.OriginType + origin["origin_list"] = detailDomain.Origin.Origins + origin["backup_origin_type"] = detailDomain.Origin.BackupOriginType + origin["backup_origin_list"] = detailDomain.Origin.BackupOrigins + origin["backup_server_name"] = detailDomain.Origin.BackupServerName + origin["server_name"] = detailDomain.Origin.ServerName + origin["cos_private_access"] = detailDomain.Origin.CosPrivateAccess + origin["origin_pull_protocol"] = detailDomain.Origin.OriginPullProtocol + origins = append(origins, origin) - origins := make([]map[string]interface{}, 0, 1) - origin := make(map[string]interface{}, 8) - origin["origin_type"] = domainConfig.Origin.OriginType - origin["origin_list"] = domainConfig.Origin.Origins - origin["server_name"] = domainConfig.Origin.ServerName - origin["cos_private_access"] = domainConfig.Origin.CosPrivateAccess - origin["origin_pull_protocol"] = domainConfig.Origin.OriginPullProtocol - origin["backup_origin_type"] = domainConfig.Origin.BackupOriginType - origin["backup_origin_list"] = domainConfig.Origin.BackupOrigins - origin["backup_server_name"] = domainConfig.Origin.BackupServerName - origins = append(origins, origin) - _ = d.Set("origin", origins) + httpsconfigs := make([]map[string]interface{}, 0, 1) + httpsConfig := make(map[string]interface{}, 7) + httpsConfig["https_switch"] = detailDomain.Https.Switch + httpsConfig["http2_switch"] = detailDomain.Https.Http2 + httpsConfig["ocsp_stapling_switch"] = detailDomain.Https.OcspStapling + httpsConfig["spdy_switch"] = detailDomain.Https.Spdy + httpsConfig["verify_client"] = detailDomain.Https.VerifyClient + httpsconfigs = append(httpsconfigs, httpsConfig) - httpsConfig := make(map[string]interface{}, 7) - httpsConfig["https_switch"] = domainConfig.Https.Switch - httpsConfig["http2_switch"] = domainConfig.Https.Http2 - httpsConfig["ocsp_stapling_switch"] = domainConfig.Https.OcspStapling - httpsConfig["spdy_switch"] = domainConfig.Https.Spdy - httpsConfig["verify_client"] = domainConfig.Https.VerifyClient - _ = d.Set("https_config", httpsConfig) + tags, errRet := tagService.DescribeResourceTags(ctx, CDN_SERVICE_NAME, CDN_RESOURCE_NAME_DOMAIN, region, *detailDomain.Domain) + if errRet != nil { + return errRet + } - tags, errRet := tagService.DescribeResourceTags(ctx, CDN_SERVICE_NAME, CDN_RESOURCE_NAME_DOMAIN, region, domain.(string)) - if errRet != nil { - return errRet + mapping := map[string]interface{}{ + "domain": detailDomain.Domain, + "service_type": detailDomain.ServiceType, + "area": detailDomain.Area, + "project_id": detailDomain.ProjectId, + "full_url_cache": fullUrlCache, + "origin": origins, + "https_config": httpsconfigs, + "tags": tags, + } + + cdnDomainList = append(cdnDomainList, mapping) } - _ = d.Set("tags", tags) + err = d.Set("domain_list", cdnDomainList) + if err != nil { + log.Printf("[CRITAL]%s provider set cdn domain list fail, reason:%s\n ", logId, err.Error()) + return err + } + output, ok := d.GetOk("result_output_file") + if ok && output.(string) != "" { + if err := writeToFile(output.(string), cdnDomainList); err != nil { + return err + } + } return nil } diff --git a/tencentcloud/data_source_tc_cdn_domains_test.go b/tencentcloud/data_source_tc_cdn_domains_test.go index e3c06d2565..dd8931bfe3 100644 --- a/tencentcloud/data_source_tc_cdn_domains_test.go +++ b/tencentcloud/data_source_tc_cdn_domains_test.go @@ -34,6 +34,12 @@ func TestAccTencentCloudCdnDomains(t *testing.T) { resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "tags.test", "world"), ), }, + { + ResourceName: "tencentcloud_cdn_domain.foo", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"https_config"}, + }, }, }) } diff --git a/tencentcloud/extension_cdn.go b/tencentcloud/extension_cdn.go index 37810623ef..25d89f38b5 100644 --- a/tencentcloud/extension_cdn.go +++ b/tencentcloud/extension_cdn.go @@ -31,8 +31,6 @@ const ( CDN_HOST_NOT_FOUND = "ResourceNotFound.CdnHostNotExists" CDN_DOMAIN_CONFIG_ERROE = "FailedOperation.CdnConfigError" - - CDN_DATASOURCE_NAME_DOMAIN = "domain" ) var CDN_SERVICE_TYPE = []string{ diff --git a/tencentcloud/provider.go b/tencentcloud/provider.go index b6fda37f67..860ada32d2 100644 --- a/tencentcloud/provider.go +++ b/tencentcloud/provider.go @@ -71,6 +71,9 @@ Auto Scaling(AS) tencentcloud_as_notification CDN + Data Source + tencentcloud_cdn_domains + Resource tencentcloud_cdn_domain diff --git a/tencentcloud/service_tencentcloud_cdn.go b/tencentcloud/service_tencentcloud_cdn.go index 66cc418fe8..4dc33dca3b 100644 --- a/tencentcloud/service_tencentcloud_cdn.go +++ b/tencentcloud/service_tencentcloud_cdn.go @@ -3,6 +3,7 @@ package tencentcloud import ( "context" "log" + "strings" cdn "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn/v20180606" "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" @@ -89,7 +90,7 @@ func (me *CdnService) StartDomain(ctx context.Context, domain string) error { return nil } -func (me *CdnService) DescribeDomainsConfigByFilters(ctx context.Context, filterMap map[string]interface{}) (domainConfig *cdn.DetailDomain, errRet error) { +func (me *CdnService) DescribeDomainsConfigByFilters(ctx context.Context, filterMap map[string]interface{}) (domainConfig []*cdn.DetailDomain, errRet error) { logId := getLogId(ctx) request := cdn.NewDescribeDomainsConfigRequest() request.Filters = make([]*cdn.DomainFilter, 0, len(filterMap)) @@ -98,7 +99,7 @@ func (me *CdnService) DescribeDomainsConfigByFilters(ctx context.Context, filter value := v.(string) filter := &cdn.DomainFilter{ - Name: helper.String(UnderlineToHump(k)), + Name: helper.String(underlineToHump([]byte(k))), Value: []*string{&value}, } request.Filters = append(request.Filters, filter) @@ -119,7 +120,24 @@ func (me *CdnService) DescribeDomainsConfigByFilters(ctx context.Context, filter } if len(response.Response.Domains) > 0 { - domainConfig = response.Response.Domains[0] + domainConfig = response.Response.Domains } return } + +func underlineToHump(underline []byte) (humpValue string) { + lenUnderLine := len(underline) + for i := 0; i < lenUnderLine; i++ { + if string(underline[i]) == "_" { + if i+1 < lenUnderLine { + humpValue += strings.ToUpper(string(underline[i+1])) + i++ + } + continue + } + + humpValue += string(underline[i]) + } + + return +} diff --git a/website/docs/d/cdn_domains.html.markdown b/website/docs/d/cdn_domains.html.markdown index 0285efdca4..de95d3b7d2 100644 --- a/website/docs/d/cdn_domains.html.markdown +++ b/website/docs/d/cdn_domains.html.markdown @@ -18,7 +18,6 @@ data "tencentcloud_cdn_domains" "foo" { service_type = "web" full_url_cache = false origin_pull_protocol = "follow" - status = "online" https_switch = "on" } ``` @@ -27,20 +26,20 @@ data "tencentcloud_cdn_domains" "foo" { The following arguments are supported: -* `domain` - (Optional) Name of the acceleration domain. +* `domain` - (Optional) Acceleration domain name. * `full_url_cache` - (Optional) Whether to enable full-path cache. * `https_switch` - (Optional) HTTPS configuration. The available value include `on`, `off` and `processing`. * `origin_pull_protocol` - (Optional) Origin-pull protocol configuration. The available value include `http`, `https` and `follow`. * `result_output_file` - (Optional) Used to save results. -* `service_type` - (Optional) Service type of Acceleration domain name. The available value include `web`, `download` and `media`. +* `service_type` - (Optional) Service type of acceleration domain name. The available value include `web`, `download` and `media`. ## Attributes Reference In addition to all arguments above, the following attributes are exported: -* `cdn_domain_list` - Information list of cdn domain. - * `area` - Domain name acceleration region. - * `domain` - Name of the acceleration domain. +* `domain_list` - An information list of cdn domain. Each element contains the following attributes: + * `area` - Acceleration region. + * `domain` - Acceleration domain name. * `full_url_cache` - Whether to enable full-path cache. * `https_config` - HTTPS acceleration configuration. It's a list and consist of at most one item. * `http2_switch` - HTTP2 configuration switch. @@ -48,7 +47,7 @@ In addition to all arguments above, the following attributes are exported: * `ocsp_stapling_switch` - OCSP configuration switch. * `spdy_switch` - Spdy configuration switch. * `verify_client` - Client certificate authentication feature. - * `origin` - Origin server configuration. It's a list and consist of at most one item. + * `origin` - Origin server configuration. * `backup_origin_list` - Backup origin server list. * `backup_origin_type` - Backup origin server type. * `backup_server_name` - Host header used when accessing the backup origin server. If left empty, the ServerName of master origin server will be used by default. @@ -58,7 +57,7 @@ In addition to all arguments above, the following attributes are exported: * `origin_type` - Master origin server type. * `server_name` - Host header used when accessing the master origin server. If left empty, the acceleration domain name will be used by default. * `project_id` - The project CDN belongs to. - * `service_type` - Service type of Acceleration domain name. + * `service_type` - Service type of acceleration domain name. * `tags` - Tags of cdn domain. diff --git a/website/tencentcloud.erb b/website/tencentcloud.erb index f7880fa149..65c8643e75 100644 --- a/website/tencentcloud.erb +++ b/website/tencentcloud.erb @@ -126,7 +126,14 @@
  • CDN