Skip to content

Commit e23fbd2

Browse files
author
ttomzhou
committed
fix cdn support https force redirect
1 parent eb56eef commit e23fbd2

File tree

5 files changed

+249
-12
lines changed

5 files changed

+249
-12
lines changed

examples/tencentcloud-cdn/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ resource "tencentcloud_cdn_domain" "foo" {
1515
ocsp_stapling_switch = "off"
1616
spdy_switch = "off"
1717
verify_client = "off"
18+
19+
force_redirect {
20+
switch = "on"
21+
}
1822
}
1923

2024
tags = {

tencentcloud/extension_cdn.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ var CDN_ORIGIN_PULL_PROTOCOL = []string{
5858
CDN_ORIGIN_PULL_PROTOCOL_FOLLOW,
5959
}
6060

61+
var CDN_FORCE_REDIRECT_TYPE = []string{
62+
CDN_ORIGIN_PULL_PROTOCOL_HTTP,
63+
CDN_ORIGIN_PULL_PROTOCOL_HTTPS,
64+
}
65+
6166
var CDN_AREA = []string{
6267
CDN_AREA_MAINLAND,
6368
CDN_AREA_OVERSEAS,

tencentcloud/resource_tc_cdn_domain.go

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ resource "tencentcloud_cdn_domain" "foo" {
2222
ocsp_stapling_switch = "off"
2323
spdy_switch = "off"
2424
verify_client = "off"
25+
26+
force_redirect {
27+
switch = "on"
28+
}
2529
}
2630
2731
tags = {
@@ -304,6 +308,40 @@ func resourceTencentCloudCdnDomain() *schema.Resource {
304308
},
305309
},
306310
},
311+
"force_redirect": {
312+
Type: schema.TypeList,
313+
Optional: true,
314+
Computed: true,
315+
MaxItems: 1,
316+
Description: "Access protocol mandatory jump configuration. It's a list and consist of at most one item.",
317+
Elem: &schema.Resource{
318+
Schema: map[string]*schema.Schema{
319+
"switch": {
320+
Type: schema.TypeString,
321+
Optional: true,
322+
Default: CDN_SWITCH_OFF,
323+
ValidateFunc: validateAllowedStringValue(CDN_SWITCH),
324+
Description: "Access forced jump configuration switch. Valid values are `on` and `off`. Default value is `off`.",
325+
},
326+
"redirect_type": {
327+
Type: schema.TypeString,
328+
Optional: true,
329+
Computed: true,
330+
ValidateFunc: validateAllowedStringValue(CDN_FORCE_REDIRECT_TYPE),
331+
Description: "Access forced jump type. Valid values are `http` and `https`. " +
332+
"When `switch` setting `off`, this property does not need to be set or set to `http`.",
333+
},
334+
"redirect_status_code": {
335+
Type: schema.TypeInt,
336+
Optional: true,
337+
Computed: true,
338+
ValidateFunc: validateAllowedIntValue([]int{301, 302}),
339+
Description: "Access forced jump code. Valid values are `301` and `302`. " +
340+
"When `switch` setting `off`, this property does not need to be set or set to `302`.",
341+
},
342+
},
343+
},
344+
},
307345
},
308346
},
309347
},
@@ -435,6 +473,23 @@ func resourceTencentCloudCdnDomainCreate(d *schema.ResourceData, meta interface{
435473
}
436474
}
437475
}
476+
if v := config["force_redirect"]; len(v.([]interface{})) > 0 {
477+
forceRedirect := v.([]interface{})
478+
if len(forceRedirect) > 0 {
479+
var redirect cdn.ForceRedirect
480+
redirectMap := forceRedirect[0].(map[string]interface{})
481+
if sw := redirectMap["switch"]; sw.(string) != "" {
482+
redirect.Switch = helper.String(sw.(string))
483+
}
484+
if rt := redirectMap["redirect_type"]; rt.(string) != "" {
485+
redirect.RedirectType = helper.String(rt.(string))
486+
}
487+
if rsc := redirectMap["redirect_status_code"]; rsc.(int) != 0 {
488+
redirect.RedirectStatusCode = helper.Int64(int64(rsc.(int)))
489+
}
490+
request.ForceRedirect = &redirect
491+
}
492+
}
438493
}
439494
}
440495

@@ -542,7 +597,7 @@ func resourceTencentCloudCdnDomainRead(d *schema.ResourceData, meta interface{})
542597
_ = d.Set("origin", origins)
543598

544599
httpsConfigs := make([]map[string]interface{}, 0, 1)
545-
httpsConfig := make(map[string]interface{}, 7)
600+
httpsConfig := make(map[string]interface{}, 8)
546601
httpsConfig["https_switch"] = domainConfig.Https.Switch
547602
httpsConfig["http2_switch"] = domainConfig.Https.Http2
548603
httpsConfig["ocsp_stapling_switch"] = domainConfig.Https.OcspStapling
@@ -597,6 +652,15 @@ func resourceTencentCloudCdnDomainRead(d *schema.ResourceData, meta interface{})
597652
clientCertConfigs = append(clientCertConfigs, clientCertConfig)
598653
httpsConfig["client_certificate_config"] = clientCertConfigs
599654
}
655+
if domainConfig.ForceRedirect != nil {
656+
httpsConfig["force_redirect"] = []map[string]interface{}{
657+
{
658+
"switch": domainConfig.ForceRedirect.Switch,
659+
"redirect_type": domainConfig.ForceRedirect.RedirectType,
660+
"redirect_status_code": domainConfig.ForceRedirect.RedirectStatusCode,
661+
},
662+
}
663+
}
600664
httpsConfigs = append(httpsConfigs, httpsConfig)
601665
_ = d.Set("https_config", httpsConfigs)
602666

@@ -724,6 +788,23 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
724788
}
725789
}
726790
}
791+
if v := config["force_redirect"]; len(v.([]interface{})) > 0 {
792+
forceRedirect := v.([]interface{})
793+
if len(forceRedirect) > 0 {
794+
var redirect cdn.ForceRedirect
795+
redirectMap := forceRedirect[0].(map[string]interface{})
796+
if sw := redirectMap["switch"]; sw.(string) != "" {
797+
redirect.Switch = helper.String(sw.(string))
798+
}
799+
if rt := redirectMap["redirect_type"]; rt.(string) != "" {
800+
redirect.RedirectType = helper.String(rt.(string))
801+
}
802+
if rsc := redirectMap["redirect_status_code"]; rsc.(int) != 0 {
803+
redirect.RedirectStatusCode = helper.Int64(int64(rsc.(int)))
804+
}
805+
request.ForceRedirect = &redirect
806+
}
807+
}
727808
}
728809
}
729810

@@ -746,7 +827,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{
746827
d.SetPartial(attr)
747828
}
748829

749-
err = resource.Retry(2*readRetryTimeout, func() *resource.RetryError {
830+
err = resource.Retry(5*readRetryTimeout, func() *resource.RetryError {
750831
domainConfig, err := cdnService.DescribeDomainsConfigByDomain(ctx, domain)
751832
if err != nil {
752833
return retryError(err, InternalError)

tencentcloud/resource_tc_cdn_domain_test.go

Lines changed: 146 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ func TestAccTencentCloudCdnDomain(t *testing.T) {
2828
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "origin.0.origin_list.#", "1"),
2929
),
3030
},
31+
{
32+
ResourceName: "tencentcloud_cdn_domain.foo",
33+
ImportState: true,
34+
ImportStateVerify: true,
35+
ImportStateVerifyIgnore: []string{"https_config"},
36+
},
37+
},
38+
})
39+
}
40+
41+
func TestAccTencentCloudCdnDomainWithHTTPs(t *testing.T) {
42+
resource.Test(t, resource.TestCase{
43+
PreCheck: func() { testAccPreCheck(t) },
44+
Providers: testAccProviders,
45+
CheckDestroy: testAccCheckCdnDomainDestroy,
46+
Steps: []resource.TestStep{
3147
{
3248
Config: testAccCdnDomainFull,
3349
Check: resource.ComposeTestCheckFunc(
@@ -48,6 +64,34 @@ func TestAccTencentCloudCdnDomain(t *testing.T) {
4864
resource.TestCheckResourceAttrSet("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.deploy_time"),
4965
resource.TestCheckResourceAttrSet("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.expire_time"),
5066
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "tags.hello", "world"),
67+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.switch", "off"),
68+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.redirect_type", "http"),
69+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.redirect_status_code", "302"),
70+
),
71+
},
72+
{
73+
Config: testAccCdnDomainFullUpdate,
74+
Check: resource.ComposeTestCheckFunc(
75+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "domain", "test.zhaoshaona.com"),
76+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "service_type", "web"),
77+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "area", "mainland"),
78+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "full_url_cache", "false"),
79+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "origin.0.origin_type", "ip"),
80+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "origin.0.origin_list.#", "1"),
81+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "origin.0.server_name", "test.zhaoshaona.com"),
82+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "origin.0.origin_pull_protocol", "follow"),
83+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.https_switch", "on"),
84+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.http2_switch", "on"),
85+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.ocsp_stapling_switch", "on"),
86+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.spdy_switch", "on"),
87+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.verify_client", "off"),
88+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.message", "test"),
89+
resource.TestCheckResourceAttrSet("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.deploy_time"),
90+
resource.TestCheckResourceAttrSet("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.expire_time"),
91+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "tags.hello", "world"),
92+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.switch", "on"),
93+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.redirect_type", "https"),
94+
resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.redirect_status_code", "302"),
5195
),
5296
},
5397
{
@@ -140,24 +184,24 @@ resource "tencentcloud_cdn_domain" "foo" {
140184

141185
const testAccCdnDomainFull = `
142186
resource "tencentcloud_cdn_domain" "foo" {
143-
domain = "test.zhaoshaona.com"
144-
service_type = "web"
145-
area = "mainland"
187+
domain = "test.zhaoshaona.com"
188+
service_type = "web"
189+
area = "mainland"
146190
full_url_cache = false
147191
148192
origin {
149-
origin_type = "ip"
150-
origin_list = ["139.199.199.140"]
151-
server_name = "test.zhaoshaona.com"
193+
origin_type = "ip"
194+
origin_list = ["139.199.199.140"]
195+
server_name = "test.zhaoshaona.com"
152196
origin_pull_protocol = "follow"
153197
}
154198
155199
https_config {
156-
https_switch = "on"
157-
http2_switch = "on"
200+
https_switch = "on"
201+
http2_switch = "on"
158202
ocsp_stapling_switch = "on"
159-
spdy_switch = "on"
160-
verify_client = "off"
203+
spdy_switch = "on"
204+
verify_client = "off"
161205
162206
server_certificate_config {
163207
certificate_content = <<EOT
@@ -216,7 +260,99 @@ KKcVGqvwVh2r8ocP7OnrQPVK9ZW7BcoYiqM2DjdKyl7AtQKnvWfPMai++oXKzo0y
216260
EOT
217261
message = "test"
218262
}
263+
}
264+
265+
tags = {
266+
hello = "world"
219267
}
268+
}
269+
`
270+
271+
const testAccCdnDomainFullUpdate = `
272+
resource "tencentcloud_cdn_domain" "foo" {
273+
domain = "test.zhaoshaona.com"
274+
service_type = "web"
275+
area = "mainland"
276+
full_url_cache = false
277+
278+
origin {
279+
origin_type = "ip"
280+
origin_list = ["139.199.199.140"]
281+
server_name = "test.zhaoshaona.com"
282+
origin_pull_protocol = "follow"
283+
}
284+
285+
https_config {
286+
https_switch = "on"
287+
http2_switch = "on"
288+
ocsp_stapling_switch = "on"
289+
spdy_switch = "on"
290+
verify_client = "off"
291+
292+
force_redirect {
293+
switch = "on"
294+
redirect_type = "https"
295+
redirect_status_code = 302
296+
}
297+
298+
server_certificate_config {
299+
certificate_content = <<EOT
300+
-----BEGIN CERTIFICATE-----
301+
MIIDuDCCAqACCQD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D5R
317+
nCa9qr9IO+FP9Ke4KKn0Ndx1sWJN3B6D8bUTnvFIkJoRsvsqNi24o2uKrUdcAYHL
318+
5BVtrVe8E55i0A5WosC8KWv4ZJxTacvuxVjfyroKzxsLwOQvCqBNSuZLg1HYUeG6
319+
XIj0/acmysb8S82Lxm39E82DbPdUO3Z0TlGL7umlAV947/6eGvPhszjnhBlxVo3p
320+
tmHdyqfHxWbkTW4bnO/Gu+Sll6a3n1uyQ/onXuXH3pBZoXLp3Jj+CV1+N6E=
321+
-----END CERTIFICATE-----
322+
EOT
323+
324+
private_key = <<EOT
325+
-----BEGIN RSA PRIVATE KEY-----
326+
MIIEpAIBAAKCAQEAoJ3ZtsRFi+wmlUP5W0zuho+BKhKdbVsoI3X6pFFwA37IDBj7
327+
pGGmWPpJVegCcy3I/FQ+YloCmvleJCx7X8oeunOOZ2a3VFaBfQhU8Nvgfy02v1M7
328+
L2y01TpxI1QYyNVeZFc5YheXCuTpUNufHcOshWnHDoF6kpKfYue2PaU9RSG+vdYb
329+
H9lR4lS4sz2Mt77y0HjfEZ10t7yuegXEJKPuPR9dUwLKyIdAuZmhdI6f+6aEkJK1
330+
9JI31+PoCv5guA59z4CuOuCqOTkeOSBo4g9+vP5JQitW6iLIZb+KMBH4MwBEDD1E
331+
NAn7r101/OWqIzOPEYmIg5mL3miArWJ0kdODEwIDAQABAoIBAQCW2uuLX9k6THkI
332+
pSlleWJm/A4C6Cz4cy/F7p+VCfA9OCzIQAbKI/VLiPisORdj+tLVPILDeWsNB75G
333+
F4lhNMObt8E+mRkDm6RPPS4ac0nt6ReMp63lIyLNSvDMj8Yfi1f2wn3hBesVjl8d
334+
VMmj+Q7m16zgkPgBBrmw+ZUPXU2oyUW4+0RvGYvuWnVUdtm/34PD1LC0NKBKaX9T
335+
MDHrSIns0WpQ7P4vNVQyHW7MGgEl81uzIitSWuT/k+zH6YxBlxd7d66vmhNoxz9c
336+
aeEf7DE3wAb4819UYWt0/ciMJwSLPkBOaTeAsktKUHVsrMLVELWcWqSIS+PYbSX8
337+
g3tY1DlxAoGBANSiDKNjfr1rZRtpZvxnVkssLY/586UaHs+dFfyFyd0unr/rAPf/
338+
GO/BIO0NbBdRb3XORMuiLQN3xf+qgKfoS0kXYglDMGKbEAC/5o6ZMV6E2E/aFrxh
339+
xmgKTZxCBVnOxlAy33UFs+qR8tpOnR4auAc0pNPA9QB4I7q17vGJRMyHAoGBAMFf
340+
7nF2aJ/k0Fcl53Cabs/FIaAwL/GBvok6Ny8wWLwiMZCtsGUUywnUdN/qbfr2GwC5
341+
g0w2iaxGqQPI+qw2qn0utAIfZ0Tz2VAH+P3aUTuG+M4XWHObHVXxBUqO61X9zgV2
342+
sXRXcbDOx3HgZeDCjk0otcGVJoC3zgzaaEZi5mQVAoGAQer+2gQ1PUm27XmOmL78
343+
bI+EjHbjhpKDbL95GnDrdKtIQZz8DuXBeEo6B+M6WDxBvpa0kyByrfmKo0jbW7JS
344+
7JTYKqDuthL2MhVLx3dMa83pNVAZ7kqtdIGFL+TzvbSxnBk5VxDuhtC6Jd1rLfMA
345+
jBNQ6eiOy5dzFCXkrnJspq8CgYAO4ISFsihmdMIakk31+cugrHfjzRFDMUopYJMy
346+
TDPndXH+wX4aqLjeLrw3JeAEOL7nFV6mlGOPH3iNU/8FFMeVDezHZQca5O/JGnPr
347+
g8pQHBg0MtOZQUvGet5/V/N/ECGzhegtHTUf9yic+DieTBmKkiE5nXHy4TE3B+6R
348+
y7YR6QKBgQDUoNAFOnMZB4BQMeCb/pQQnzNkNTG+Y02eMKjo5eZZDfyusqIui29l
349+
KKcVGqvwVh2r8ocP7OnrQPVK9ZW7BcoYiqM2DjdKyl7AtQKnvWfPMai++oXKzo0y
350+
8sg7m1Ic26sKO9W9t87cfZtFKcbKVcImLWucd9R7Ny4M4r6xlRKWpA==
351+
-----END RSA PRIVATE KEY-----
352+
EOT
353+
message = "test"
354+
}
355+
}
220356
221357
tags = {
222358
hello = "world"

website/docs/r/cdn_domain.html.markdown

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ resource "tencentcloud_cdn_domain" "foo" {
3232
ocsp_stapling_switch = "off"
3333
spdy_switch = "off"
3434
verify_client = "off"
35+
36+
force_redirect {
37+
switch = "on"
38+
}
3539
}
3640
3741
tags = {
@@ -91,10 +95,17 @@ The `client_certificate_config` object supports the following:
9195

9296
* `certificate_content` - (Required) Client Certificate PEM format, requires Base64 encoding.
9397

98+
The `force_redirect` object supports the following:
99+
100+
* `redirect_status_code` - (Optional) Access forced jump code. Valid values are `301` and `302`. When `switch` setting `off`, this property does not need to be set or set to `302`.
101+
* `redirect_type` - (Optional) Access forced jump type. Valid values are `http` and `https`. When `switch` setting `off`, this property does not need to be set or set to `http`.
102+
* `switch` - (Optional) Access forced jump configuration switch. Valid values are `on` and `off`. Default value is `off`.
103+
94104
The `https_config` object supports the following:
95105

96106
* `https_switch` - (Required) HTTPS configuration switch. Valid values are `on` and `off`.
97107
* `client_certificate_config` - (Optional) Client certificate configuration information.
108+
* `force_redirect` - (Optional) Access protocol mandatory jump configuration. It's a list and consist of at most one item.
98109
* `http2_switch` - (Optional) HTTP2 configuration switch. Valid values are `on` and `off`. and default value is `off`.
99110
* `ocsp_stapling_switch` - (Optional) OCSP configuration switch. Valid values are `on` and `off`. and default value is `off`.
100111
* `server_certificate_config` - (Optional) Server certificate configuration information.

0 commit comments

Comments
 (0)