|
| 1 | +/* |
| 2 | +Use this data source to query the detail information of CDN domain. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cdn_domains" "foo" { |
| 8 | + domain = "xxxx.com" |
| 9 | + service_type = "web" |
| 10 | + full_url_cache = false |
| 11 | + origin_pull_protocol = "follow" |
| 12 | + https_switch = "on" |
| 13 | +} |
| 14 | +``` |
| 15 | +*/ |
| 16 | +package tencentcloud |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "log" |
| 21 | + |
| 22 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 23 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 24 | + cdn "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn/v20180606" |
| 25 | + "github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 26 | +) |
| 27 | + |
| 28 | +func dataSourceTencentCloudCdnDomains() *schema.Resource { |
| 29 | + return &schema.Resource{ |
| 30 | + Read: dataSourceTencentCloudCdnDomainsRead, |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "domain": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Optional: true, |
| 35 | + Description: "Acceleration domain name.", |
| 36 | + }, |
| 37 | + "service_type": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Optional: true, |
| 40 | + ValidateFunc: validateAllowedStringValue(CDN_SERVICE_TYPE), |
| 41 | + Description: "Service type of acceleration domain name. The available value include `web`, `download` and `media`.", |
| 42 | + }, |
| 43 | + "full_url_cache": { |
| 44 | + Type: schema.TypeBool, |
| 45 | + Optional: true, |
| 46 | + Description: "Whether to enable full-path cache.", |
| 47 | + }, |
| 48 | + "origin_pull_protocol": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Optional: true, |
| 51 | + ValidateFunc: validateAllowedStringValue(CDN_ORIGIN_PULL_PROTOCOL), |
| 52 | + Description: "Origin-pull protocol configuration. The available value include `http`, `https` and `follow`.", |
| 53 | + }, |
| 54 | + "https_switch": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Optional: true, |
| 57 | + ValidateFunc: validateAllowedStringValue(CDN_HTTPS_SWITCH), |
| 58 | + Description: "HTTPS configuration. The available value include `on`, `off` and `processing`.", |
| 59 | + }, |
| 60 | + "offset": { |
| 61 | + Type: schema.TypeInt, |
| 62 | + Optional: true, |
| 63 | + Default: 10, |
| 64 | + ValidateFunc: validateIntegerInRange(1, 10000), |
| 65 | + Description: "Record offset. Default is 10.", |
| 66 | + }, |
| 67 | + "result_output_file": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Optional: true, |
| 70 | + Description: "Used to save results.", |
| 71 | + }, |
| 72 | + "domain_list": { |
| 73 | + Type: schema.TypeList, |
| 74 | + Computed: true, |
| 75 | + Description: "An information list of cdn domain. Each element contains the following attributes:", |
| 76 | + Elem: &schema.Resource{ |
| 77 | + Schema: map[string]*schema.Schema{ |
| 78 | + "id": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + Description: "Domain name ID.", |
| 82 | + }, |
| 83 | + "domain": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Computed: true, |
| 86 | + Description: "Acceleration domain name.", |
| 87 | + }, |
| 88 | + "cname": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Computed: true, |
| 91 | + Description: "CNAME address of domain name.", |
| 92 | + }, |
| 93 | + "status": { |
| 94 | + Type: schema.TypeString, |
| 95 | + Computed: true, |
| 96 | + Description: "Acceleration service status.", |
| 97 | + }, |
| 98 | + "create_time": { |
| 99 | + Type: schema.TypeString, |
| 100 | + Computed: true, |
| 101 | + Description: "Domain name creation time.", |
| 102 | + }, |
| 103 | + "update_time": { |
| 104 | + Type: schema.TypeString, |
| 105 | + Computed: true, |
| 106 | + Description: "Last modified time of domain name.", |
| 107 | + }, |
| 108 | + "service_type": { |
| 109 | + Type: schema.TypeString, |
| 110 | + Computed: true, |
| 111 | + Description: "Service type of acceleration domain name.", |
| 112 | + }, |
| 113 | + "area": { |
| 114 | + Type: schema.TypeString, |
| 115 | + Computed: true, |
| 116 | + Description: "Acceleration region.", |
| 117 | + }, |
| 118 | + "project_id": { |
| 119 | + Type: schema.TypeInt, |
| 120 | + Computed: true, |
| 121 | + Description: "The project CDN belongs to.", |
| 122 | + }, |
| 123 | + "full_url_cache": { |
| 124 | + Type: schema.TypeBool, |
| 125 | + Computed: true, |
| 126 | + Description: "Whether to enable full-path cache.", |
| 127 | + }, |
| 128 | + "origin": { |
| 129 | + Type: schema.TypeList, |
| 130 | + Computed: true, |
| 131 | + Description: "Origin server configuration.", |
| 132 | + Elem: &schema.Resource{ |
| 133 | + Schema: map[string]*schema.Schema{ |
| 134 | + "origin_type": { |
| 135 | + Type: schema.TypeString, |
| 136 | + Computed: true, |
| 137 | + Description: "Master origin server type.", |
| 138 | + }, |
| 139 | + "origin_list": { |
| 140 | + Type: schema.TypeList, |
| 141 | + Computed: true, |
| 142 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 143 | + Description: "Master origin server list.", |
| 144 | + }, |
| 145 | + "backup_origin_type": { |
| 146 | + Type: schema.TypeString, |
| 147 | + Computed: true, |
| 148 | + Description: "Backup origin server type.", |
| 149 | + }, |
| 150 | + "backup_origin_list": { |
| 151 | + Type: schema.TypeList, |
| 152 | + Computed: true, |
| 153 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 154 | + Description: "Backup origin server list.", |
| 155 | + }, |
| 156 | + "backup_server_name": { |
| 157 | + Type: schema.TypeString, |
| 158 | + Computed: true, |
| 159 | + Description: "Host header used when accessing the backup origin server. If left empty, the ServerName of master origin server will be used by default.", |
| 160 | + }, |
| 161 | + "server_name": { |
| 162 | + Type: schema.TypeString, |
| 163 | + Computed: true, |
| 164 | + Description: "Host header used when accessing the master origin server. If left empty, the acceleration domain name will be used by default.", |
| 165 | + }, |
| 166 | + "cos_private_access": { |
| 167 | + Type: schema.TypeString, |
| 168 | + Computed: true, |
| 169 | + Description: "When OriginType is COS, you can specify if access to private buckets is allowed.", |
| 170 | + }, |
| 171 | + "origin_pull_protocol": { |
| 172 | + Type: schema.TypeString, |
| 173 | + Computed: true, |
| 174 | + Description: "Origin-pull protocol configuration.", |
| 175 | + }, |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + "https_config": { |
| 180 | + Type: schema.TypeList, |
| 181 | + Computed: true, |
| 182 | + Description: "HTTPS acceleration configuration. It's a list and consist of at most one item.", |
| 183 | + Elem: &schema.Resource{ |
| 184 | + Schema: map[string]*schema.Schema{ |
| 185 | + "https_switch": { |
| 186 | + Type: schema.TypeString, |
| 187 | + Computed: true, |
| 188 | + Description: "HTTPS configuration switch.", |
| 189 | + }, |
| 190 | + "http2_switch": { |
| 191 | + Type: schema.TypeString, |
| 192 | + Computed: true, |
| 193 | + Description: "HTTP2 configuration switch.", |
| 194 | + }, |
| 195 | + "ocsp_stapling_switch": { |
| 196 | + Type: schema.TypeString, |
| 197 | + Computed: true, |
| 198 | + Description: "OCSP configuration switch.", |
| 199 | + }, |
| 200 | + "spdy_switch": { |
| 201 | + Type: schema.TypeString, |
| 202 | + Computed: true, |
| 203 | + Description: "Spdy configuration switch.", |
| 204 | + }, |
| 205 | + "verify_client": { |
| 206 | + Type: schema.TypeString, |
| 207 | + Computed: true, |
| 208 | + Description: "Client certificate authentication feature.", |
| 209 | + }, |
| 210 | + }, |
| 211 | + }, |
| 212 | + }, |
| 213 | + "tags": { |
| 214 | + Type: schema.TypeMap, |
| 215 | + Computed: true, |
| 216 | + Description: "Tags of cdn domain.", |
| 217 | + }, |
| 218 | + }, |
| 219 | + }, |
| 220 | + }, |
| 221 | + }, |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +func dataSourceTencentCloudCdnDomainsRead(d *schema.ResourceData, meta interface{}) error { |
| 226 | + defer logElapsed("data_source.tencentcloud_cdn_domain.read")() |
| 227 | + |
| 228 | + logId := getLogId(contextNil) |
| 229 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 230 | + |
| 231 | + client := meta.(*TencentCloudClient).apiV3Conn |
| 232 | + region := client.Region |
| 233 | + cdnService := CdnService{client: client} |
| 234 | + tagService := TagService{client: client} |
| 235 | + |
| 236 | + offset, _ := d.Get("offset").(int) |
| 237 | + var domainFilterMap = make(map[string]interface{}, 5) |
| 238 | + if v, ok := d.GetOk("domain"); ok { |
| 239 | + domainFilterMap["domain"] = v.(string) |
| 240 | + } |
| 241 | + if v, ok := d.GetOk("service_type"); ok { |
| 242 | + domainFilterMap["serviceType"] = v.(string) |
| 243 | + } |
| 244 | + if v, ok := d.GetOk("https_switch"); ok { |
| 245 | + domainFilterMap["httpsSwitch"] = v.(string) |
| 246 | + } |
| 247 | + if v, ok := d.GetOk("origin_pull_protocol"); ok { |
| 248 | + domainFilterMap["originPullProtocol"] = v.(string) |
| 249 | + } |
| 250 | + if v, ok := d.GetOkExists("full_url_cache"); ok { |
| 251 | + var value string |
| 252 | + if v.(bool) { |
| 253 | + value = "on" |
| 254 | + } else { |
| 255 | + value = "off" |
| 256 | + } |
| 257 | + |
| 258 | + domainFilterMap["fullUrlCache"] = value |
| 259 | + } |
| 260 | + |
| 261 | + var domainConfigs []*cdn.DetailDomain |
| 262 | + var errRet error |
| 263 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 264 | + domainConfigs, errRet = cdnService.DescribeDomainsConfigByFilters(ctx, domainFilterMap, int64(offset)) |
| 265 | + if errRet != nil { |
| 266 | + return retryError(errRet, InternalError) |
| 267 | + } |
| 268 | + return nil |
| 269 | + }) |
| 270 | + |
| 271 | + if err != nil { |
| 272 | + log.Printf("[CRITAL]%s describeDomainsConfigByFilters fail, reason:%s\n ", logId, err.Error()) |
| 273 | + return err |
| 274 | + } |
| 275 | + |
| 276 | + cdnDomainList := make([]map[string]interface{}, 0, len(domainConfigs)) |
| 277 | + ids := make([]string, 0, len(domainConfigs)) |
| 278 | + for _, detailDomain := range domainConfigs { |
| 279 | + var fullUrlCache bool |
| 280 | + if detailDomain.CacheKey != nil && *detailDomain.CacheKey.FullUrlCache == CDN_SWITCH_ON { |
| 281 | + fullUrlCache = true |
| 282 | + } |
| 283 | + |
| 284 | + origins := make([]map[string]interface{}, 0, 1) |
| 285 | + origin := make(map[string]interface{}, 8) |
| 286 | + origin["origin_type"] = detailDomain.Origin.OriginType |
| 287 | + origin["origin_list"] = detailDomain.Origin.Origins |
| 288 | + origin["backup_origin_type"] = detailDomain.Origin.BackupOriginType |
| 289 | + origin["backup_origin_list"] = detailDomain.Origin.BackupOrigins |
| 290 | + origin["backup_server_name"] = detailDomain.Origin.BackupServerName |
| 291 | + origin["server_name"] = detailDomain.Origin.ServerName |
| 292 | + origin["cos_private_access"] = detailDomain.Origin.CosPrivateAccess |
| 293 | + origin["origin_pull_protocol"] = detailDomain.Origin.OriginPullProtocol |
| 294 | + origins = append(origins, origin) |
| 295 | + |
| 296 | + httpsconfigs := make([]map[string]interface{}, 0, 1) |
| 297 | + if detailDomain.Https != nil { |
| 298 | + httpsConfig := make(map[string]interface{}, 7) |
| 299 | + httpsConfig["https_switch"] = detailDomain.Https.Switch |
| 300 | + httpsConfig["http2_switch"] = detailDomain.Https.Http2 |
| 301 | + httpsConfig["ocsp_stapling_switch"] = detailDomain.Https.OcspStapling |
| 302 | + httpsConfig["spdy_switch"] = detailDomain.Https.Spdy |
| 303 | + httpsConfig["verify_client"] = detailDomain.Https.VerifyClient |
| 304 | + httpsconfigs = append(httpsconfigs, httpsConfig) |
| 305 | + } |
| 306 | + |
| 307 | + tags, errRet := tagService.DescribeResourceTags(ctx, CDN_SERVICE_NAME, CDN_RESOURCE_NAME_DOMAIN, region, *detailDomain.Domain) |
| 308 | + if errRet != nil { |
| 309 | + return errRet |
| 310 | + } |
| 311 | + |
| 312 | + mapping := map[string]interface{}{ |
| 313 | + "id": detailDomain.ResourceId, |
| 314 | + "domain": detailDomain.Domain, |
| 315 | + "cname": detailDomain.Cname, |
| 316 | + "status": detailDomain.Status, |
| 317 | + "create_time": detailDomain.CreateTime, |
| 318 | + "update_time": detailDomain.UpdateTime, |
| 319 | + "service_type": detailDomain.ServiceType, |
| 320 | + "area": detailDomain.Area, |
| 321 | + "project_id": detailDomain.ProjectId, |
| 322 | + "full_url_cache": fullUrlCache, |
| 323 | + "origin": origins, |
| 324 | + "https_config": httpsconfigs, |
| 325 | + "tags": tags, |
| 326 | + } |
| 327 | + |
| 328 | + cdnDomainList = append(cdnDomainList, mapping) |
| 329 | + ids = append(ids, *detailDomain.ResourceId) |
| 330 | + } |
| 331 | + |
| 332 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 333 | + err = d.Set("domain_list", cdnDomainList) |
| 334 | + if err != nil { |
| 335 | + log.Printf("[CRITAL]%s provider set cdn domain list fail, reason:%s\n ", logId, err.Error()) |
| 336 | + return err |
| 337 | + } |
| 338 | + output, ok := d.GetOk("result_output_file") |
| 339 | + if ok && output.(string) != "" { |
| 340 | + if err := writeToFile(output.(string), cdnDomainList); err != nil { |
| 341 | + return err |
| 342 | + } |
| 343 | + } |
| 344 | + return nil |
| 345 | +} |
0 commit comments