diff --git a/CHANGELOG.md b/CHANGELOG.md index 34354c4d78..fca1085b67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,9 @@ ## 1.38.0 (Unreleased) + +FEATURES: + +* **New Data Source**: `tencentcloud_cdn_domains` + ## 1.37.0 (June 23, 2020) FEATURES: 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/data_source_tc_cdn_domains.go b/tencentcloud/data_source_tc_cdn_domains.go new file mode 100644 index 0000000000..f3e7dfffa0 --- /dev/null +++ b/tencentcloud/data_source_tc_cdn_domains.go @@ -0,0 +1,345 @@ +/* +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" + 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" + "github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper" +) + +func dataSourceTencentCloudCdnDomains() *schema.Resource { + return &schema.Resource{ + Read: dataSourceTencentCloudCdnDomainsRead, + Schema: map[string]*schema.Schema{ + "domain": { + Type: schema.TypeString, + Optional: true, + 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`.", + }, + "full_url_cache": { + Type: schema.TypeBool, + Optional: true, + Description: "Whether to enable full-path cache.", + }, + "origin_pull_protocol": { + Type: schema.TypeString, + 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`.", + }, + "offset": { + Type: schema.TypeInt, + Optional: true, + Default: 10, + ValidateFunc: validateIntegerInRange(1, 10000), + Description: "Record offset. Default is 10.", + }, + "result_output_file": { + Type: schema.TypeString, + Optional: true, + Description: "Used to save results.", + }, + "domain_list": { + Type: schema.TypeList, + Computed: true, + Description: "An information list of cdn domain. Each element contains the following attributes:", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + Description: "Domain name ID.", + }, + "domain": { + Type: schema.TypeString, + Computed: true, + Description: "Acceleration domain name.", + }, + "cname": { + Type: schema.TypeString, + Computed: true, + Description: "CNAME address of domain name.", + }, + "status": { + Type: schema.TypeString, + Computed: true, + Description: "Acceleration service status.", + }, + "create_time": { + Type: schema.TypeString, + Computed: true, + Description: "Domain name creation time.", + }, + "update_time": { + Type: schema.TypeString, + Computed: true, + Description: "Last modified time of domain name.", + }, + "service_type": { + Type: schema.TypeString, + Computed: true, + Description: "Service type of acceleration domain name.", + }, + "area": { + Type: schema.TypeString, + Computed: true, + Description: "Acceleration region.", + }, + "project_id": { + Type: schema.TypeInt, + 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.", + 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} + + offset, _ := d.Get("offset").(int) + var domainFilterMap = make(map[string]interface{}, 5) + if v, ok := d.GetOk("domain"); ok { + domainFilterMap["domain"] = v.(string) + } + if v, ok := d.GetOk("service_type"); ok { + domainFilterMap["serviceType"] = v.(string) + } + if v, ok := d.GetOk("https_switch"); ok { + domainFilterMap["httpsSwitch"] = v.(string) + } + if v, ok := d.GetOk("origin_pull_protocol"); ok { + domainFilterMap["originPullProtocol"] = v.(string) + } + if v, ok := d.GetOkExists("full_url_cache"); ok { + var value string + if v.(bool) { + value = "on" + } else { + value = "off" + } + + domainFilterMap["fullUrlCache"] = value + } + + var domainConfigs []*cdn.DetailDomain + var errRet error + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { + domainConfigs, errRet = cdnService.DescribeDomainsConfigByFilters(ctx, domainFilterMap, int64(offset)) + 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 + } + + cdnDomainList := make([]map[string]interface{}, 0, len(domainConfigs)) + ids := make([]string, 0, len(domainConfigs)) + for _, detailDomain := range domainConfigs { + var fullUrlCache bool + if detailDomain.CacheKey != nil && *detailDomain.CacheKey.FullUrlCache == CDN_SWITCH_ON { + fullUrlCache = 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) + + httpsconfigs := make([]map[string]interface{}, 0, 1) + if detailDomain.Https != nil { + 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) + } + + tags, errRet := tagService.DescribeResourceTags(ctx, CDN_SERVICE_NAME, CDN_RESOURCE_NAME_DOMAIN, region, *detailDomain.Domain) + if errRet != nil { + return errRet + } + + mapping := map[string]interface{}{ + "id": detailDomain.ResourceId, + "domain": detailDomain.Domain, + "cname": detailDomain.Cname, + "status": detailDomain.Status, + "create_time": detailDomain.CreateTime, + "update_time": detailDomain.UpdateTime, + "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) + ids = append(ids, *detailDomain.ResourceId) + } + + d.SetId(helper.DataResourceIdsHash(ids)) + 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 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 = append(domainConfig, response.Response.Domains...) + } + + if leftNumber > 0 && totalCount-offset > 0 { + goto needMoreDomains + } + return +} diff --git a/website/docs/d/cdn_domains.html.markdown b/website/docs/d/cdn_domains.html.markdown new file mode 100644 index 0000000000..ded1a83906 --- /dev/null +++ b/website/docs/d/cdn_domains.html.markdown @@ -0,0 +1,69 @@ +--- +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" + https_switch = "on" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `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`. +* `offset` - (Optional) Record offset. Default is 10. +* `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: + +* `domain_list` - An information list of cdn domain. Each element contains the following attributes: + * `area` - Acceleration region. + * `cname` - CNAME address of domain name. + * `create_time` - Domain name creation time. + * `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. + * `https_switch` - HTTPS configuration switch. + * `ocsp_stapling_switch` - OCSP configuration switch. + * `spdy_switch` - Spdy configuration switch. + * `verify_client` - Client certificate authentication feature. + * `id` - Domain name ID. + * `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. + * `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. + * `status` - Acceleration service status. + * `tags` - Tags of cdn domain. + * `update_time` - Last modified time of domain name. + + diff --git a/website/tencentcloud.erb b/website/tencentcloud.erb index 9c47d53e96..b6af3371a0 100644 --- a/website/tencentcloud.erb +++ b/website/tencentcloud.erb @@ -126,7 +126,14 @@
  • CDN