diff --git a/cloudflare/resource_cloudflare_custom_hostname.go b/cloudflare/resource_cloudflare_custom_hostname.go index d570aeb579..ffa1d180bb 100644 --- a/cloudflare/resource_cloudflare_custom_hostname.go +++ b/cloudflare/resource_cloudflare_custom_hostname.go @@ -41,7 +41,7 @@ func resourceCloudflareCustomHostname() *schema.Resource { }, "ssl": { Type: schema.TypeList, - Required: true, + Optional: true, Elem: &schema.Resource{ SchemaVersion: 1, Schema: map[string]*schema.Schema{ @@ -177,21 +177,21 @@ func resourceCloudflareCustomHostnameRead(d *schema.ResourceData, meta interface d.Set("hostname", customHostname.Hostname) d.Set("ssl.custom_origin_server", customHostname.CustomOriginServer) + if customHostname.SSL != nil { + d.Set("ssl.0.type", customHostname.SSL.Type) + d.Set("ssl.0.method", customHostname.SSL.Method) + d.Set("ssl.0.wildcard", customHostname.SSL.Wildcard) + d.Set("ssl.0.status", customHostname.SSL.Status) + d.Set("ssl.0.cname_target", customHostname.SSL.CnameTarget) + d.Set("ssl.0.cname_name", customHostname.SSL.CnameName) + d.Set("ssl.0.custom_certificate", customHostname.SSL.CustomCertificate) + d.Set("ssl.0.custom_key", customHostname.SSL.CustomKey) - d.Set("ssl.0.type", customHostname.SSL.Type) - d.Set("ssl.0.method", customHostname.SSL.Method) - d.Set("ssl.0.wildcard", customHostname.SSL.Wildcard) - d.Set("ssl.0.status", customHostname.SSL.Status) - d.Set("ssl.0.cname_target", customHostname.SSL.CnameTarget) - d.Set("ssl.0.cname_name", customHostname.SSL.CnameName) - d.Set("ssl.0.custom_certificate", customHostname.SSL.CustomCertificate) - d.Set("ssl.0.custom_key", customHostname.SSL.CustomKey) - - d.Set("ssl.0.settings.0.http2", customHostname.SSL.Settings.HTTP2) - d.Set("ssl.0.settings.0.tls13", customHostname.SSL.Settings.TLS13) - d.Set("ssl.0.settings.0.min_tls_version", customHostname.SSL.Settings.MinTLSVersion) - d.Set("ssl.0.settings.0.ciphers", flattenStringList(customHostname.SSL.Settings.Ciphers)) - + d.Set("ssl.0.settings.0.http2", customHostname.SSL.Settings.HTTP2) + d.Set("ssl.0.settings.0.tls13", customHostname.SSL.Settings.TLS13) + d.Set("ssl.0.settings.0.min_tls_version", customHostname.SSL.Settings.MinTLSVersion) + d.Set("ssl.0.settings.0.ciphers", flattenStringList(customHostname.SSL.Settings.Ciphers)) + } ownershipVerificationCfg := map[string]interface{}{} ownershipVerificationCfg["type"] = customHostname.OwnershipVerification.Type ownershipVerificationCfg["value"] = customHostname.OwnershipVerification.Value @@ -269,10 +269,12 @@ func resourceCloudflareCustomHostnameImport(d *schema.ResourceData, meta interfa // buildCustomHostname takes the existing schema and returns a // `cloudflare.CustomHostname`. func buildCustomHostname(d *schema.ResourceData) cloudflare.CustomHostname { - return cloudflare.CustomHostname{ + ch := cloudflare.CustomHostname{ Hostname: d.Get("hostname").(string), CustomOriginServer: d.Get("custom_origin_server").(string), - SSL: cloudflare.CustomHostnameSSL{ + } + if _, ok := d.GetOk("ssl"); ok { + ch.SSL = &cloudflare.CustomHostnameSSL{ Method: d.Get("ssl.0.method").(string), Type: d.Get("ssl.0.type").(string), Wildcard: &[]bool{d.Get("ssl.0.wildcard").(bool)}[0], @@ -286,6 +288,7 @@ func buildCustomHostname(d *schema.ResourceData) cloudflare.CustomHostname { MinTLSVersion: d.Get("ssl.0.settings.0.min_tls_version").(string), Ciphers: expandInterfaceToStringList(d.Get("ssl.0.settings.0.ciphers").([]interface{})), }, - }, + } } + return ch } diff --git a/cloudflare/resource_cloudflare_custom_hostname_test.go b/cloudflare/resource_cloudflare_custom_hostname_test.go index ae5a1b63b9..caa2bdcef4 100644 --- a/cloudflare/resource_cloudflare_custom_hostname_test.go +++ b/cloudflare/resource_cloudflare_custom_hostname_test.go @@ -246,6 +246,36 @@ resource "cloudflare_custom_hostname" "%[2]s" { `, zoneID, rnd, domain) } +func testAccCheckCloudflareCustomHostnameWithNoSSL(zoneID, rnd, domain string) string { + return fmt.Sprintf(` +resource "cloudflare_custom_hostname" "%[2]s" { + zone_id = "%[1]s" + hostname = "%[2]s.%[3]s" +} +`, zoneID, rnd, domain) +} +func TestAccCloudflareCustomHostnameWithNoSSL(t *testing.T) { + t.Parallel() + zoneID := os.Getenv("CLOUDFLARE_ZONE_ID") + domain := os.Getenv("CLOUDFLARE_DOMAIN") + rnd := generateRandomResourceName() + resourceName := "cloudflare_custom_hostname." + rnd + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckCloudflareCustomHostnameWithNoSSL(zoneID, rnd, domain), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "zone_id", zoneID), + resource.TestCheckResourceAttr(resourceName, "hostname", fmt.Sprintf("%s.%s", rnd, domain)), + resource.TestCheckNoResourceAttr(resourceName, "ssl"), + ), + }, + }, + }) +} + func TestAccCloudflareCustomHostname_UpdatingZoneForcesNewResource(t *testing.T) { t.Parallel() diff --git a/go.mod b/go.mod index 2ce70c71c3..3f9150c259 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/cloudflare/terraform-provider-cloudflare go 1.15 require ( - github.com/cloudflare/cloudflare-go v0.18.0 + github.com/cloudflare/cloudflare-go v0.19.0 github.com/hashicorp/go-cleanhttp v0.5.2 github.com/hashicorp/terraform-plugin-sdk v1.17.2 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index a5b417c508..96ae21338f 100644 --- a/go.sum +++ b/go.sum @@ -82,8 +82,8 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/cloudflare/cloudflare-go v0.18.0 h1:9q1yV4XuYqAZKsMUygRFH1rmmDq5rpaVXL+WWfeliao= -github.com/cloudflare/cloudflare-go v0.18.0/go.mod h1:sPWL/lIC6biLEdyGZwBQ1rGQKF1FhM7N60fuNiFdYTI= +github.com/cloudflare/cloudflare-go v0.19.0 h1:GXNEtlgl7gakuybTwxoD8tFrGFvZfghLfXZmitM/8DE= +github.com/cloudflare/cloudflare-go v0.19.0/go.mod h1:sPWL/lIC6biLEdyGZwBQ1rGQKF1FhM7N60fuNiFdYTI= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=