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..1df3ade59c 100644 --- a/tencentcloud/resource_tc_cdn_domain.go +++ b/tencentcloud/resource_tc_cdn_domain.go @@ -22,6 +22,12 @@ resource "tencentcloud_cdn_domain" "foo" { ocsp_stapling_switch = "off" spdy_switch = "off" verify_client = "off" + + force_redirect { + switch = "on" + redirect_type = "http" + redirect_status_code = 302 + } } tags = { @@ -304,6 +310,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, + Default: CDN_ORIGIN_PULL_PROTOCOL_HTTP, + ValidateFunc: validateAllowedStringValue(CDN_FORCE_REDIRECT_TYPE), + 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, + 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`. Default value is `302`.", + }, + }, + }, + }, }, }, }, @@ -435,6 +475,23 @@ func resourceTencentCloudCdnDomainCreate(d *schema.ResourceData, meta interface{ } } } + if v, ok := config["force_redirect"]; ok { + 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 +599,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 +654,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 +790,23 @@ func resourceTencentCloudCdnDomainUpdate(d *schema.ResourceData, meta interface{ } } } + if v, ok := config["force_redirect"]; ok { + 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 + } + } } } diff --git a/tencentcloud/resource_tc_cdn_domain_test.go b/tencentcloud/resource_tc_cdn_domain_test.go index 70739e2338..a1ae851a76 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", "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"), + ), + }, + { + 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", "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"), ), }, { @@ -140,24 +184,30 @@ 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" + + force_redirect { + switch = "on" + redirect_type = "https" + redirect_status_code = 302 + } server_certificate_config { certificate_content = <