Skip to content

Commit a15ab6c

Browse files
committed
fix cdn datasource logic
1 parent 02ba18a commit a15ab6c

File tree

8 files changed

+115
-99
lines changed

8 files changed

+115
-99
lines changed

tencentcloud/common.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -240,31 +240,3 @@ func IsContains(array interface{}, value interface{}) bool {
240240
return reflect.DeepEqual(array, value)
241241
}
242242
}
243-
244-
func UnderlineToHump(underline string) (humpValue string) {
245-
if len(underline) == 0 {
246-
return ""
247-
}
248-
249-
splitResult := strings.Split(underline, "_")
250-
if len(splitResult) == 0 {
251-
return ""
252-
}
253-
254-
if len(splitResult) == 1 {
255-
return underline
256-
}
257-
258-
for flag, v := range splitResult {
259-
if len(v) == 0 {
260-
continue
261-
}
262-
if flag == 0 {
263-
humpValue += v
264-
continue
265-
}
266-
humpValue += strings.ToUpper(v[:1]) + v[1:]
267-
}
268-
269-
return
270-
}

tencentcloud/data_source_tc_cdn_domains.go

Lines changed: 70 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ data "tencentcloud_cdn_domains" "foo" {
99
service_type = "web"
1010
full_url_cache = false
1111
origin_pull_protocol = "follow"
12-
status = "online"
1312
https_switch = "on"
1413
}
1514
```
@@ -32,21 +31,21 @@ func dataSourceTencentCloudCdnDomains() *schema.Resource {
3231
"domain": {
3332
Type: schema.TypeString,
3433
Optional: true,
35-
Description: "Name of the acceleration domain.",
34+
Description: "Acceleration domain name.",
3635
},
3736
"service_type": {
3837
Type: schema.TypeString,
3938
Optional: true,
4039
ValidateFunc: validateAllowedStringValue(CDN_SERVICE_TYPE),
41-
Description: "Service type of Acceleration domain name. The available value include `web`, `download` and `media`.",
40+
Description: "Service type of acceleration domain name. The available value include `web`, `download` and `media`.",
4241
},
4342
"full_url_cache": {
4443
Type: schema.TypeBool,
4544
Optional: true,
4645
Description: "Whether to enable full-path cache.",
4746
},
4847
"origin_pull_protocol": {
49-
Type: schema.TypeBool,
48+
Type: schema.TypeString,
5049
Optional: true,
5150
ValidateFunc: validateAllowedStringValue(CDN_ORIGIN_PULL_PROTOCOL),
5251
Description: "Origin-pull protocol configuration. The available value include `http`, `https` and `follow`.",
@@ -62,29 +61,29 @@ func dataSourceTencentCloudCdnDomains() *schema.Resource {
6261
Optional: true,
6362
Description: "Used to save results.",
6463
},
65-
"cdn_domain_list": {
64+
"domain_list": {
6665
Type: schema.TypeList,
6766
Computed: true,
68-
Description: "Information list of cdn domain.",
67+
Description: "An information list of cdn domain. Each element contains the following attributes:",
6968
Elem: &schema.Resource{
7069
Schema: map[string]*schema.Schema{
7170
"domain": {
7271
Type: schema.TypeString,
7372
Computed: true,
74-
Description: "Name of the acceleration domain.",
73+
Description: "Acceleration domain name.",
7574
},
7675
"service_type": {
7776
Type: schema.TypeString,
7877
Computed: true,
79-
Description: "Service type of Acceleration domain name.",
78+
Description: "Service type of acceleration domain name.",
8079
},
8180
"area": {
8281
Type: schema.TypeString,
8382
Computed: true,
84-
Description: "Domain name acceleration region.",
83+
Description: "Acceleration region.",
8584
},
8685
"project_id": {
87-
Type: schema.TypeString,
86+
Type: schema.TypeInt,
8887
Computed: true,
8988
Description: "The project CDN belongs to.",
9089
},
@@ -96,7 +95,7 @@ func dataSourceTencentCloudCdnDomains() *schema.Resource {
9695
"origin": {
9796
Type: schema.TypeList,
9897
Computed: true,
99-
Description: "Origin server configuration. It's a list and consist of at most one item.",
98+
Description: "Origin server configuration.",
10099
Elem: &schema.Resource{
101100
Schema: map[string]*schema.Schema{
102101
"origin_type": {
@@ -203,18 +202,18 @@ func dataSourceTencentCloudCdnDomainsRead(d *schema.ResourceData, meta interface
203202

204203
var domainFilterMap = make(map[string]interface{}, 5)
205204

206-
if v, ok := d.GetOk(CDN_DATASOURCE_NAME_DOMAIN); ok {
207-
domainFilterMap[CDN_DATASOURCE_NAME_DOMAIN] = v.(string)
205+
if v, ok := d.GetOk("domain"); ok {
206+
domainFilterMap["domain"] = v.(string)
208207
}
209208
if v, ok := d.GetOk("service_type"); ok {
210209
domainFilterMap["service_type"] = v.(string)
211210
}
212-
if v, ok := d.GetOk("status"); ok {
213-
domainFilterMap["status"] = v.(string)
214-
}
215211
if v, ok := d.GetOk("https_switch"); ok {
216212
domainFilterMap["https_switch"] = v.(string)
217213
}
214+
if v, ok := d.GetOk("origin_pull_protocol"); ok {
215+
domainFilterMap["origin_pull_protocol"] = v.(string)
216+
}
218217
if v, ok := d.GetOk("full_url_cache"); ok {
219218
var value string
220219
if v.(bool) {
@@ -226,14 +225,10 @@ func dataSourceTencentCloudCdnDomainsRead(d *schema.ResourceData, meta interface
226225
domainFilterMap["full_url_cache"] = value
227226
}
228227

229-
if len(domainFilterMap) == 0 {
230-
return nil
231-
}
232-
233-
var domainConfig *cdn.DetailDomain
228+
var domainConfigs []*cdn.DetailDomain
234229
var errRet error
235230
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
236-
domainConfig, errRet = cdnService.DescribeDomainsConfigByFilters(ctx, domainFilterMap)
231+
domainConfigs, errRet = cdnService.DescribeDomainsConfigByFilters(ctx, domainFilterMap)
237232
if errRet != nil {
238233
return retryError(errRet, InternalError)
239234
}
@@ -244,49 +239,67 @@ func dataSourceTencentCloudCdnDomainsRead(d *schema.ResourceData, meta interface
244239
log.Printf("[CRITAL]%s describeDomainsConfigByFilters fail, reason:%s\n ", logId, err.Error())
245240
return err
246241
}
247-
if domainConfig == nil {
242+
if domainConfigs == nil {
248243
return nil
249244
}
250245

251-
domain := domainFilterMap[CDN_DATASOURCE_NAME_DOMAIN]
252-
_ = d.Set(CDN_DATASOURCE_NAME_DOMAIN, domain)
253-
_ = d.Set("service_type", domainConfig.ServiceType)
254-
_ = d.Set("project_id", domainConfig.ProjectId)
255-
_ = d.Set("area", domainConfig.Area)
256-
_ = d.Set("status", domainConfig.Status)
246+
cdnDomainList := make([]map[string]interface{}, 0, len(domainConfigs))
247+
for _, detailDomain := range domainConfigs {
248+
var fullUrlCache bool
249+
if detailDomain.CacheKey!=nil && *detailDomain.CacheKey.FullUrlCache == CDN_SWITCH_ON {
250+
fullUrlCache = true
251+
}
257252

258-
if *domainConfig.CacheKey.FullUrlCache == CDN_SWITCH_OFF {
259-
_ = d.Set("full_url_cache", false)
260-
} else {
261-
_ = d.Set("full_url_cache", true)
262-
}
253+
origins := make([]map[string]interface{}, 0, 1)
254+
origin := make(map[string]interface{}, 8)
255+
origin["origin_type"] = detailDomain.Origin.OriginType
256+
origin["origin_list"] = detailDomain.Origin.Origins
257+
origin["backup_origin_type"] = detailDomain.Origin.BackupOriginType
258+
origin["backup_origin_list"] = detailDomain.Origin.BackupOrigins
259+
origin["backup_server_name"] = detailDomain.Origin.BackupServerName
260+
origin["server_name"] = detailDomain.Origin.ServerName
261+
origin["cos_private_access"] = detailDomain.Origin.CosPrivateAccess
262+
origin["origin_pull_protocol"] = detailDomain.Origin.OriginPullProtocol
263+
origins = append(origins, origin)
263264

264-
origins := make([]map[string]interface{}, 0, 1)
265-
origin := make(map[string]interface{}, 8)
266-
origin["origin_type"] = domainConfig.Origin.OriginType
267-
origin["origin_list"] = domainConfig.Origin.Origins
268-
origin["server_name"] = domainConfig.Origin.ServerName
269-
origin["cos_private_access"] = domainConfig.Origin.CosPrivateAccess
270-
origin["origin_pull_protocol"] = domainConfig.Origin.OriginPullProtocol
271-
origin["backup_origin_type"] = domainConfig.Origin.BackupOriginType
272-
origin["backup_origin_list"] = domainConfig.Origin.BackupOrigins
273-
origin["backup_server_name"] = domainConfig.Origin.BackupServerName
274-
origins = append(origins, origin)
275-
_ = d.Set("origin", origins)
265+
httpsconfigs := make([]map[string]interface{}, 0, 1)
266+
httpsConfig := make(map[string]interface{}, 7)
267+
httpsConfig["https_switch"] = detailDomain.Https.Switch
268+
httpsConfig["http2_switch"] = detailDomain.Https.Http2
269+
httpsConfig["ocsp_stapling_switch"] = detailDomain.Https.OcspStapling
270+
httpsConfig["spdy_switch"] = detailDomain.Https.Spdy
271+
httpsConfig["verify_client"] = detailDomain.Https.VerifyClient
272+
httpsconfigs = append(httpsconfigs, httpsConfig)
276273

277-
httpsConfig := make(map[string]interface{}, 7)
278-
httpsConfig["https_switch"] = domainConfig.Https.Switch
279-
httpsConfig["http2_switch"] = domainConfig.Https.Http2
280-
httpsConfig["ocsp_stapling_switch"] = domainConfig.Https.OcspStapling
281-
httpsConfig["spdy_switch"] = domainConfig.Https.Spdy
282-
httpsConfig["verify_client"] = domainConfig.Https.VerifyClient
283-
_ = d.Set("https_config", httpsConfig)
274+
tags, errRet := tagService.DescribeResourceTags(ctx, CDN_SERVICE_NAME, CDN_RESOURCE_NAME_DOMAIN, region, *detailDomain.Domain)
275+
if errRet != nil {
276+
return errRet
277+
}
284278

285-
tags, errRet := tagService.DescribeResourceTags(ctx, CDN_SERVICE_NAME, CDN_RESOURCE_NAME_DOMAIN, region, domain.(string))
286-
if errRet != nil {
287-
return errRet
279+
mapping := map[string]interface{}{
280+
"domain": detailDomain.Domain,
281+
"service_type": detailDomain.ServiceType,
282+
"area": detailDomain.Area,
283+
"project_id": detailDomain.ProjectId,
284+
"full_url_cache": fullUrlCache,
285+
"origin": origins,
286+
"https_config": httpsconfigs,
287+
"tags": tags,
288+
}
289+
290+
cdnDomainList = append(cdnDomainList, mapping)
288291
}
289-
_ = d.Set("tags", tags)
290292

293+
err = d.Set("domain_list", cdnDomainList)
294+
if err != nil {
295+
log.Printf("[CRITAL]%s provider set cdn domain list fail, reason:%s\n ", logId, err.Error())
296+
return err
297+
}
298+
output, ok := d.GetOk("result_output_file")
299+
if ok && output.(string) != "" {
300+
if err := writeToFile(output.(string), cdnDomainList); err != nil {
301+
return err
302+
}
303+
}
291304
return nil
292305
}

tencentcloud/data_source_tc_cdn_domains_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ func TestAccTencentCloudCdnDomains(t *testing.T) {
3434
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "tags.test", "world"),
3535
),
3636
},
37+
{
38+
ResourceName: "tencentcloud_cdn_domain.foo",
39+
ImportState: true,
40+
ImportStateVerify: true,
41+
ImportStateVerifyIgnore: []string{"https_config"},
42+
},
3743
},
3844
})
3945
}

tencentcloud/extension_cdn.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ const (
3131

3232
CDN_HOST_NOT_FOUND = "ResourceNotFound.CdnHostNotExists"
3333
CDN_DOMAIN_CONFIG_ERROE = "FailedOperation.CdnConfigError"
34-
35-
CDN_DATASOURCE_NAME_DOMAIN = "domain"
3634
)
3735

3836
var CDN_SERVICE_TYPE = []string{

tencentcloud/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ Auto Scaling(AS)
7171
tencentcloud_as_notification
7272
7373
CDN
74+
Data Source
75+
tencentcloud_cdn_domains
76+
7477
Resource
7578
tencentcloud_cdn_domain
7679

tencentcloud/service_tencentcloud_cdn.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tencentcloud
33
import (
44
"context"
55
"log"
6+
"strings"
67

78
cdn "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn/v20180606"
89
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
@@ -89,7 +90,7 @@ func (me *CdnService) StartDomain(ctx context.Context, domain string) error {
8990
return nil
9091
}
9192

92-
func (me *CdnService) DescribeDomainsConfigByFilters(ctx context.Context, filterMap map[string]interface{}) (domainConfig *cdn.DetailDomain, errRet error) {
93+
func (me *CdnService) DescribeDomainsConfigByFilters(ctx context.Context, filterMap map[string]interface{}) (domainConfig []*cdn.DetailDomain, errRet error) {
9394
logId := getLogId(ctx)
9495
request := cdn.NewDescribeDomainsConfigRequest()
9596
request.Filters = make([]*cdn.DomainFilter, 0, len(filterMap))
@@ -98,7 +99,7 @@ func (me *CdnService) DescribeDomainsConfigByFilters(ctx context.Context, filter
9899
value := v.(string)
99100

100101
filter := &cdn.DomainFilter{
101-
Name: helper.String(UnderlineToHump(k)),
102+
Name: helper.String(underlineToHump([]byte(k))),
102103
Value: []*string{&value},
103104
}
104105
request.Filters = append(request.Filters, filter)
@@ -119,7 +120,24 @@ func (me *CdnService) DescribeDomainsConfigByFilters(ctx context.Context, filter
119120
}
120121

121122
if len(response.Response.Domains) > 0 {
122-
domainConfig = response.Response.Domains[0]
123+
domainConfig = response.Response.Domains
123124
}
124125
return
125126
}
127+
128+
func underlineToHump(underline []byte) (humpValue string) {
129+
lenUnderLine := len(underline)
130+
for i := 0; i < lenUnderLine; i++ {
131+
if string(underline[i]) == "_" {
132+
if i+1 < lenUnderLine {
133+
humpValue += strings.ToUpper(string(underline[i+1]))
134+
i++
135+
}
136+
continue
137+
}
138+
139+
humpValue += string(underline[i])
140+
}
141+
142+
return
143+
}

website/docs/d/cdn_domains.html.markdown

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ data "tencentcloud_cdn_domains" "foo" {
1818
service_type = "web"
1919
full_url_cache = false
2020
origin_pull_protocol = "follow"
21-
status = "online"
2221
https_switch = "on"
2322
}
2423
```
@@ -27,28 +26,28 @@ data "tencentcloud_cdn_domains" "foo" {
2726

2827
The following arguments are supported:
2928

30-
* `domain` - (Optional) Name of the acceleration domain.
29+
* `domain` - (Optional) Acceleration domain name.
3130
* `full_url_cache` - (Optional) Whether to enable full-path cache.
3231
* `https_switch` - (Optional) HTTPS configuration. The available value include `on`, `off` and `processing`.
3332
* `origin_pull_protocol` - (Optional) Origin-pull protocol configuration. The available value include `http`, `https` and `follow`.
3433
* `result_output_file` - (Optional) Used to save results.
35-
* `service_type` - (Optional) Service type of Acceleration domain name. The available value include `web`, `download` and `media`.
34+
* `service_type` - (Optional) Service type of acceleration domain name. The available value include `web`, `download` and `media`.
3635

3736
## Attributes Reference
3837

3938
In addition to all arguments above, the following attributes are exported:
4039

41-
* `cdn_domain_list` - Information list of cdn domain.
42-
* `area` - Domain name acceleration region.
43-
* `domain` - Name of the acceleration domain.
40+
* `domain_list` - An information list of cdn domain. Each element contains the following attributes:
41+
* `area` - Acceleration region.
42+
* `domain` - Acceleration domain name.
4443
* `full_url_cache` - Whether to enable full-path cache.
4544
* `https_config` - HTTPS acceleration configuration. It's a list and consist of at most one item.
4645
* `http2_switch` - HTTP2 configuration switch.
4746
* `https_switch` - HTTPS configuration switch.
4847
* `ocsp_stapling_switch` - OCSP configuration switch.
4948
* `spdy_switch` - Spdy configuration switch.
5049
* `verify_client` - Client certificate authentication feature.
51-
* `origin` - Origin server configuration. It's a list and consist of at most one item.
50+
* `origin` - Origin server configuration.
5251
* `backup_origin_list` - Backup origin server list.
5352
* `backup_origin_type` - Backup origin server type.
5453
* `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:
5857
* `origin_type` - Master origin server type.
5958
* `server_name` - Host header used when accessing the master origin server. If left empty, the acceleration domain name will be used by default.
6059
* `project_id` - The project CDN belongs to.
61-
* `service_type` - Service type of Acceleration domain name.
60+
* `service_type` - Service type of acceleration domain name.
6261
* `tags` - Tags of cdn domain.
6362

6463

0 commit comments

Comments
 (0)