-
Notifications
You must be signed in to change notification settings - Fork 144
add cdn datasource #452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add cdn datasource #452
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
02ba18a
add cdn datasource
crab21 a15ab6c
fix cdn datasource logic
crab21 e00043a
remove check
crab21 598b1aa
Merge branch 'master' into cdn_data_source
crab21 98e15f3
remove unused cdn file
crab21 2ffd81a
Merge branch 'cdn_data_source' of https://github.com/crab21/terraform…
crab21 a558bc4
add cdn domain result fileds.
crab21 5ad5c7c
add domain id
crab21 380f257
remove redundancy code and add field
crab21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 { | ||
crab21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.