From e23fbd21ab4a5829229a05471ee8dbe397c3354d Mon Sep 17 00:00:00 2001 From: ttomzhou Date: Thu, 19 Nov 2020 10:00:29 +0800 Subject: [PATCH 1/3] fix cdn support https force redirect --- examples/tencentcloud-cdn/main.tf | 4 + tencentcloud/extension_cdn.go | 5 + tencentcloud/resource_tc_cdn_domain.go | 85 ++++++++++- tencentcloud/resource_tc_cdn_domain_test.go | 156 ++++++++++++++++++-- website/docs/r/cdn_domain.html.markdown | 11 ++ 5 files changed, 249 insertions(+), 12 deletions(-) diff --git a/examples/tencentcloud-cdn/main.tf b/examples/tencentcloud-cdn/main.tf index 6242f34e36..054dd03d60 100644 --- a/examples/tencentcloud-cdn/main.tf +++ b/examples/tencentcloud-cdn/main.tf @@ -15,6 +15,10 @@ resource "tencentcloud_cdn_domain" "foo" { ocsp_stapling_switch = "off" spdy_switch = "off" verify_client = "off" + + force_redirect { + switch = "on" + } } tags = { diff --git a/tencentcloud/extension_cdn.go b/tencentcloud/extension_cdn.go index 25d89f38b5..51e67fcd4b 100644 --- a/tencentcloud/extension_cdn.go +++ b/tencentcloud/extension_cdn.go @@ -58,6 +58,11 @@ var CDN_ORIGIN_PULL_PROTOCOL = []string{ CDN_ORIGIN_PULL_PROTOCOL_FOLLOW, } +var CDN_FORCE_REDIRECT_TYPE = []string{ + CDN_ORIGIN_PULL_PROTOCOL_HTTP, + CDN_ORIGIN_PULL_PROTOCOL_HTTPS, +} + var CDN_AREA = []string{ CDN_AREA_MAINLAND, CDN_AREA_OVERSEAS, diff --git a/tencentcloud/resource_tc_cdn_domain.go b/tencentcloud/resource_tc_cdn_domain.go index c1a1ad00bc..af78426736 100644 --- a/tencentcloud/resource_tc_cdn_domain.go +++ b/tencentcloud/resource_tc_cdn_domain.go @@ -22,6 +22,10 @@ resource "tencentcloud_cdn_domain" "foo" { ocsp_stapling_switch = "off" spdy_switch = "off" verify_client = "off" + + force_redirect { + switch = "on" + } } tags = { @@ -304,6 +308,40 @@ func resourceTencentCloudCdnDomain() *schema.Resource { }, }, }, + "force_redirect": { + Type: schema.TypeList, + Optional: true, + Computed: true, + MaxItems: 1, + Description: "Access protocol mandatory jump configuration. It's a list and consist of at most one item.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "switch": { + Type: schema.TypeString, + Optional: true, + Default: CDN_SWITCH_OFF, + ValidateFunc: validateAllowedStringValue(CDN_SWITCH), + Description: "Access forced jump configuration switch. Valid values are `on` and `off`. Default value is `off`.", + }, + "redirect_type": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ValidateFunc: validateAllowedStringValue(CDN_FORCE_REDIRECT_TYPE), + Description: "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`.", + }, + "redirect_status_code": { + Type: schema.TypeInt, + Optional: true, + Computed: true, + ValidateFunc: validateAllowedIntValue([]int{301, 302}), + Description: "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`.", + }, + }, + }, + }, }, }, }, @@ -435,6 +473,23 @@ func resourceTencentCloudCdnDomainCreate(d *schema.ResourceData, meta interface{ } } } + if v := config["force_redirect"]; len(v.([]interface{})) > 0 { + forceRedirect := v.([]interface{}) + if len(forceRedirect) > 0 { + var redirect cdn.ForceRedirect + redirectMap := forceRedirect[0].(map[string]interface{}) + if sw := redirectMap["switch"]; sw.(string) != "" { + redirect.Switch = helper.String(sw.(string)) + } + if rt := redirectMap["redirect_type"]; rt.(string) != "" { + redirect.RedirectType = helper.String(rt.(string)) + } + if rsc := redirectMap["redirect_status_code"]; rsc.(int) != 0 { + redirect.RedirectStatusCode = helper.Int64(int64(rsc.(int))) + } + request.ForceRedirect = &redirect + } + } } } @@ -542,7 +597,7 @@ func resourceTencentCloudCdnDomainRead(d *schema.ResourceData, meta interface{}) _ = d.Set("origin", origins) httpsConfigs := make([]map[string]interface{}, 0, 1) - httpsConfig := make(map[string]interface{}, 7) + httpsConfig := make(map[string]interface{}, 8) httpsConfig["https_switch"] = domainConfig.Https.Switch httpsConfig["http2_switch"] = domainConfig.Https.Http2 httpsConfig["ocsp_stapling_switch"] = domainConfig.Https.OcspStapling @@ -597,6 +652,15 @@ func resourceTencentCloudCdnDomainRead(d *schema.ResourceData, meta interface{}) clientCertConfigs = append(clientCertConfigs, clientCertConfig) httpsConfig["client_certificate_config"] = clientCertConfigs } + if domainConfig.ForceRedirect != nil { + httpsConfig["force_redirect"] = []map[string]interface{}{ + { + "switch": domainConfig.ForceRedirect.Switch, + "redirect_type": domainConfig.ForceRedirect.RedirectType, + "redirect_status_code": domainConfig.ForceRedirect.RedirectStatusCode, + }, + } + } httpsConfigs = append(httpsConfigs, httpsConfig) _ = d.Set("https_config", httpsConfigs) @@ -724,6 +788,23 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{ } } } + if v := config["force_redirect"]; len(v.([]interface{})) > 0 { + forceRedirect := v.([]interface{}) + if len(forceRedirect) > 0 { + var redirect cdn.ForceRedirect + redirectMap := forceRedirect[0].(map[string]interface{}) + if sw := redirectMap["switch"]; sw.(string) != "" { + redirect.Switch = helper.String(sw.(string)) + } + if rt := redirectMap["redirect_type"]; rt.(string) != "" { + redirect.RedirectType = helper.String(rt.(string)) + } + if rsc := redirectMap["redirect_status_code"]; rsc.(int) != 0 { + redirect.RedirectStatusCode = helper.Int64(int64(rsc.(int))) + } + request.ForceRedirect = &redirect + } + } } } @@ -746,7 +827,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{ d.SetPartial(attr) } - err = resource.Retry(2*readRetryTimeout, func() *resource.RetryError { + err = resource.Retry(5*readRetryTimeout, func() *resource.RetryError { domainConfig, err := cdnService.DescribeDomainsConfigByDomain(ctx, domain) if err != nil { return retryError(err, InternalError) diff --git a/tencentcloud/resource_tc_cdn_domain_test.go b/tencentcloud/resource_tc_cdn_domain_test.go index 70739e2338..7a0c11d0c1 100644 --- a/tencentcloud/resource_tc_cdn_domain_test.go +++ b/tencentcloud/resource_tc_cdn_domain_test.go @@ -28,6 +28,22 @@ func TestAccTencentCloudCdnDomain(t *testing.T) { resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "origin.0.origin_list.#", "1"), ), }, + { + ResourceName: "tencentcloud_cdn_domain.foo", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"https_config"}, + }, + }, + }) +} + +func TestAccTencentCloudCdnDomainWithHTTPs(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCdnDomainDestroy, + Steps: []resource.TestStep{ { Config: testAccCdnDomainFull, Check: resource.ComposeTestCheckFunc( @@ -48,6 +64,34 @@ func TestAccTencentCloudCdnDomain(t *testing.T) { resource.TestCheckResourceAttrSet("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.deploy_time"), resource.TestCheckResourceAttrSet("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.expire_time"), resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "tags.hello", "world"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.switch", "off"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.redirect_type", "http"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.redirect_status_code", "302"), + ), + }, + { + Config: testAccCdnDomainFullUpdate, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "domain", "test.zhaoshaona.com"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "service_type", "web"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "area", "mainland"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "full_url_cache", "false"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "origin.0.origin_type", "ip"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "origin.0.origin_list.#", "1"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "origin.0.server_name", "test.zhaoshaona.com"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "origin.0.origin_pull_protocol", "follow"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.https_switch", "on"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.http2_switch", "on"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.ocsp_stapling_switch", "on"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.spdy_switch", "on"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.verify_client", "off"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.message", "test"), + resource.TestCheckResourceAttrSet("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.deploy_time"), + resource.TestCheckResourceAttrSet("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.expire_time"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "tags.hello", "world"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.switch", "on"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.redirect_type", "https"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.redirect_status_code", "302"), ), }, { @@ -140,24 +184,24 @@ resource "tencentcloud_cdn_domain" "foo" { const testAccCdnDomainFull = ` resource "tencentcloud_cdn_domain" "foo" { - domain = "test.zhaoshaona.com" - service_type = "web" - area = "mainland" + domain = "test.zhaoshaona.com" + service_type = "web" + area = "mainland" full_url_cache = false origin { - origin_type = "ip" - origin_list = ["139.199.199.140"] - server_name = "test.zhaoshaona.com" + origin_type = "ip" + origin_list = ["139.199.199.140"] + server_name = "test.zhaoshaona.com" origin_pull_protocol = "follow" } https_config { - https_switch = "on" - http2_switch = "on" + https_switch = "on" + http2_switch = "on" ocsp_stapling_switch = "on" - spdy_switch = "on" - verify_client = "off" + spdy_switch = "on" + verify_client = "off" server_certificate_config { certificate_content = < Date: Thu, 19 Nov 2020 10:05:44 +0800 Subject: [PATCH 2/3] change read retry time --- tencentcloud/resource_tc_cdn_domain.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tencentcloud/resource_tc_cdn_domain.go b/tencentcloud/resource_tc_cdn_domain.go index af78426736..93eb4b3fe8 100644 --- a/tencentcloud/resource_tc_cdn_domain.go +++ b/tencentcloud/resource_tc_cdn_domain.go @@ -827,7 +827,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{ d.SetPartial(attr) } - err = resource.Retry(5*readRetryTimeout, func() *resource.RetryError { + err = resource.Retry(2*readRetryTimeout, func() *resource.RetryError { domainConfig, err := cdnService.DescribeDomainsConfigByDomain(ctx, domain) if err != nil { return retryError(err, InternalError) From aba30e164fbe46c0dea7192ee0e3e3073be6e590 Mon Sep 17 00:00:00 2001 From: ttomzhou Date: Fri, 20 Nov 2020 10:18:27 +0800 Subject: [PATCH 3/3] fix cdn force redirect --- tencentcloud/resource_tc_cdn_domain.go | 18 ++++++++++-------- tencentcloud/resource_tc_cdn_domain_test.go | 18 +++++++++++------- website/docs/r/cdn_domain.html.markdown | 8 +++++--- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/tencentcloud/resource_tc_cdn_domain.go b/tencentcloud/resource_tc_cdn_domain.go index 93eb4b3fe8..1df3ade59c 100644 --- a/tencentcloud/resource_tc_cdn_domain.go +++ b/tencentcloud/resource_tc_cdn_domain.go @@ -24,7 +24,9 @@ resource "tencentcloud_cdn_domain" "foo" { verify_client = "off" force_redirect { - switch = "on" + switch = "on" + redirect_type = "http" + redirect_status_code = 302 } } @@ -326,18 +328,18 @@ func resourceTencentCloudCdnDomain() *schema.Resource { "redirect_type": { Type: schema.TypeString, Optional: true, - Computed: true, + Default: CDN_ORIGIN_PULL_PROTOCOL_HTTP, ValidateFunc: validateAllowedStringValue(CDN_FORCE_REDIRECT_TYPE), - Description: "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`.", + Description: "Access forced jump type. Valid values are `http` and `https`. `http` means force http redirect, `https` means force http redirect. " + + "When `switch` setting `off`, this property does not need to be set or set to `http`. Default value is `http`.", }, "redirect_status_code": { Type: schema.TypeInt, Optional: true, - Computed: true, + Default: 302, ValidateFunc: validateAllowedIntValue([]int{301, 302}), Description: "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`.", + "When `switch` setting `off`, this property does not need to be set or set to `302`. Default value is `302`.", }, }, }, @@ -473,7 +475,7 @@ func resourceTencentCloudCdnDomainCreate(d *schema.ResourceData, meta interface{ } } } - if v := config["force_redirect"]; len(v.([]interface{})) > 0 { + if v, ok := config["force_redirect"]; ok { forceRedirect := v.([]interface{}) if len(forceRedirect) > 0 { var redirect cdn.ForceRedirect @@ -788,7 +790,7 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{ } } } - if v := config["force_redirect"]; len(v.([]interface{})) > 0 { + if v, ok := config["force_redirect"]; ok { forceRedirect := v.([]interface{}) if len(forceRedirect) > 0 { var redirect cdn.ForceRedirect diff --git a/tencentcloud/resource_tc_cdn_domain_test.go b/tencentcloud/resource_tc_cdn_domain_test.go index 7a0c11d0c1..a1ae851a76 100644 --- a/tencentcloud/resource_tc_cdn_domain_test.go +++ b/tencentcloud/resource_tc_cdn_domain_test.go @@ -64,8 +64,8 @@ func TestAccTencentCloudCdnDomainWithHTTPs(t *testing.T) { resource.TestCheckResourceAttrSet("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.deploy_time"), resource.TestCheckResourceAttrSet("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.expire_time"), resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "tags.hello", "world"), - resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.switch", "off"), - resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.redirect_type", "http"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.switch", "on"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.redirect_type", "https"), resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.redirect_status_code", "302"), ), }, @@ -89,8 +89,8 @@ func TestAccTencentCloudCdnDomainWithHTTPs(t *testing.T) { resource.TestCheckResourceAttrSet("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.deploy_time"), resource.TestCheckResourceAttrSet("tencentcloud_cdn_domain.foo", "https_config.0.server_certificate_config.0.expire_time"), resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "tags.hello", "world"), - resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.switch", "on"), - resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.redirect_type", "https"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.switch", "off"), + resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.redirect_type", "http"), resource.TestCheckResourceAttr("tencentcloud_cdn_domain.foo", "https_config.0.force_redirect.0.redirect_status_code", "302"), ), }, @@ -203,6 +203,12 @@ resource "tencentcloud_cdn_domain" "foo" { spdy_switch = "on" verify_client = "off" + force_redirect { + switch = "on" + redirect_type = "https" + redirect_status_code = 302 + } + server_certificate_config { certificate_content = <