Skip to content

Commit

Permalink
allow creating custom_hostname records without ssl
Browse files Browse the repository at this point in the history
  • Loading branch information
nickysemenza committed Jul 8, 2021
1 parent 4b4712b commit ab7e5aa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
39 changes: 21 additions & 18 deletions cloudflare/resource_cloudflare_custom_hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand All @@ -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
}
30 changes: 30 additions & 0 deletions cloudflare/resource_cloudflare_custom_hostname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit ab7e5aa

Please sign in to comment.