Skip to content

Commit 02ba18a

Browse files
committed
add cdn datasource
1 parent 4bdbc4a commit 02ba18a

File tree

10 files changed

+640
-8
lines changed

10 files changed

+640
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
## 1.37.0 (Unreleased)
2+
3+
FEATURES:
4+
5+
* **New Data Source**: `tencentcloud_cdn_domains`
6+
27
## 1.36.1 (June 12, 2020)
38

49
ENHANCEMENTS:

examples/tencentcloud-cdn/main.tf

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
resource "tencentcloud_cdn_domain" "foo" {
2-
domain = "xxxx.com"
2+
domain = "xxxx.com"
33
service_type = "web"
4-
area = "mainland"
4+
area = "mainland"
55

66
origin {
7-
origin_type = "ip"
8-
origin_list = ["127.0.0.1"]
7+
origin_type = "ip"
8+
origin_list = ["127.0.0.1"]
99
origin_pull_protocol = "follow"
1010
}
1111

1212
https_config {
13-
https_switch = "off"
14-
http2_switch = "off"
13+
https_switch = "off"
14+
http2_switch = "off"
1515
ocsp_stapling_switch = "off"
16-
spdy_switch = "off"
17-
verify_client = "off"
16+
spdy_switch = "off"
17+
verify_client = "off"
1818
}
1919

2020
tags = {
2121
hello = "world"
2222
}
23+
}
24+
25+
data "tencentcloud_cdn_domains" "cdn_domain" {
26+
domain = tencentcloud_cdn_domain.foo.domain
27+
service_type = tencentcloud_cdn_domain.foo.service_type
2328
}

tencentcloud/common.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,31 @@ 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+
}
Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
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+
status = "online"
13+
https_switch = "on"
14+
}
15+
```
16+
*/
17+
package tencentcloud
18+
19+
import (
20+
"context"
21+
"log"
22+
23+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
24+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
25+
cdn "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn/v20180606"
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: "Name of the acceleration domain.",
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.TypeBool,
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+
"result_output_file": {
61+
Type: schema.TypeString,
62+
Optional: true,
63+
Description: "Used to save results.",
64+
},
65+
"cdn_domain_list": {
66+
Type: schema.TypeList,
67+
Computed: true,
68+
Description: "Information list of cdn domain.",
69+
Elem: &schema.Resource{
70+
Schema: map[string]*schema.Schema{
71+
"domain": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
Description: "Name of the acceleration domain.",
75+
},
76+
"service_type": {
77+
Type: schema.TypeString,
78+
Computed: true,
79+
Description: "Service type of Acceleration domain name.",
80+
},
81+
"area": {
82+
Type: schema.TypeString,
83+
Computed: true,
84+
Description: "Domain name acceleration region.",
85+
},
86+
"project_id": {
87+
Type: schema.TypeString,
88+
Computed: true,
89+
Description: "The project CDN belongs to.",
90+
},
91+
"full_url_cache": {
92+
Type: schema.TypeBool,
93+
Computed: true,
94+
Description: "Whether to enable full-path cache.",
95+
},
96+
"origin": {
97+
Type: schema.TypeList,
98+
Computed: true,
99+
Description: "Origin server configuration. It's a list and consist of at most one item.",
100+
Elem: &schema.Resource{
101+
Schema: map[string]*schema.Schema{
102+
"origin_type": {
103+
Type: schema.TypeString,
104+
Computed: true,
105+
Description: "Master origin server type.",
106+
},
107+
"origin_list": {
108+
Type: schema.TypeList,
109+
Computed: true,
110+
Elem: &schema.Schema{Type: schema.TypeString},
111+
Description: "Master origin server list.",
112+
},
113+
"backup_origin_type": {
114+
Type: schema.TypeString,
115+
Computed: true,
116+
Description: "Backup origin server type.",
117+
},
118+
"backup_origin_list": {
119+
Type: schema.TypeList,
120+
Computed: true,
121+
Elem: &schema.Schema{Type: schema.TypeString},
122+
Description: "Backup origin server list.",
123+
},
124+
"backup_server_name": {
125+
Type: schema.TypeString,
126+
Computed: true,
127+
Description: "Host header used when accessing the backup origin server. If left empty, the ServerName of master origin server will be used by default.",
128+
},
129+
"server_name": {
130+
Type: schema.TypeString,
131+
Computed: true,
132+
Description: "Host header used when accessing the master origin server. If left empty, the acceleration domain name will be used by default.",
133+
},
134+
"cos_private_access": {
135+
Type: schema.TypeString,
136+
Computed: true,
137+
Description: "When OriginType is COS, you can specify if access to private buckets is allowed.",
138+
},
139+
"origin_pull_protocol": {
140+
Type: schema.TypeString,
141+
Computed: true,
142+
Description: "Origin-pull protocol configuration.",
143+
},
144+
},
145+
},
146+
},
147+
"https_config": {
148+
Type: schema.TypeList,
149+
Computed: true,
150+
Description: "HTTPS acceleration configuration. It's a list and consist of at most one item.",
151+
Elem: &schema.Resource{
152+
Schema: map[string]*schema.Schema{
153+
"https_switch": {
154+
Type: schema.TypeString,
155+
Computed: true,
156+
Description: "HTTPS configuration switch.",
157+
},
158+
"http2_switch": {
159+
Type: schema.TypeString,
160+
Computed: true,
161+
Description: "HTTP2 configuration switch.",
162+
},
163+
"ocsp_stapling_switch": {
164+
Type: schema.TypeString,
165+
Computed: true,
166+
Description: "OCSP configuration switch.",
167+
},
168+
"spdy_switch": {
169+
Type: schema.TypeString,
170+
Computed: true,
171+
Description: "Spdy configuration switch.",
172+
},
173+
"verify_client": {
174+
Type: schema.TypeString,
175+
Computed: true,
176+
Description: "Client certificate authentication feature.",
177+
},
178+
},
179+
},
180+
},
181+
"tags": {
182+
Type: schema.TypeMap,
183+
Computed: true,
184+
Description: "Tags of cdn domain.",
185+
},
186+
},
187+
},
188+
},
189+
},
190+
}
191+
}
192+
193+
func dataSourceTencentCloudCdnDomainsRead(d *schema.ResourceData, meta interface{}) error {
194+
defer logElapsed("data_source.tencentcloud_cdn_domain.read")()
195+
196+
logId := getLogId(contextNil)
197+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
198+
199+
client := meta.(*TencentCloudClient).apiV3Conn
200+
region := client.Region
201+
cdnService := CdnService{client: client}
202+
tagService := TagService{client: client}
203+
204+
var domainFilterMap = make(map[string]interface{}, 5)
205+
206+
if v, ok := d.GetOk(CDN_DATASOURCE_NAME_DOMAIN); ok {
207+
domainFilterMap[CDN_DATASOURCE_NAME_DOMAIN] = v.(string)
208+
}
209+
if v, ok := d.GetOk("service_type"); ok {
210+
domainFilterMap["service_type"] = v.(string)
211+
}
212+
if v, ok := d.GetOk("status"); ok {
213+
domainFilterMap["status"] = v.(string)
214+
}
215+
if v, ok := d.GetOk("https_switch"); ok {
216+
domainFilterMap["https_switch"] = v.(string)
217+
}
218+
if v, ok := d.GetOk("full_url_cache"); ok {
219+
var value string
220+
if v.(bool) {
221+
value = "on"
222+
} else {
223+
value = "off"
224+
}
225+
226+
domainFilterMap["full_url_cache"] = value
227+
}
228+
229+
if len(domainFilterMap) == 0 {
230+
return nil
231+
}
232+
233+
var domainConfig *cdn.DetailDomain
234+
var errRet error
235+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
236+
domainConfig, errRet = cdnService.DescribeDomainsConfigByFilters(ctx, domainFilterMap)
237+
if errRet != nil {
238+
return retryError(errRet, InternalError)
239+
}
240+
return nil
241+
})
242+
243+
if err != nil {
244+
log.Printf("[CRITAL]%s describeDomainsConfigByFilters fail, reason:%s\n ", logId, err.Error())
245+
return err
246+
}
247+
if domainConfig == nil {
248+
return nil
249+
}
250+
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)
257+
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+
}
263+
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)
276+
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)
284+
285+
tags, errRet := tagService.DescribeResourceTags(ctx, CDN_SERVICE_NAME, CDN_RESOURCE_NAME_DOMAIN, region, domain.(string))
286+
if errRet != nil {
287+
return errRet
288+
}
289+
_ = d.Set("tags", tags)
290+
291+
return nil
292+
}

0 commit comments

Comments
 (0)