Skip to content

Commit

Permalink
resource/cloudflare_zone: support setting type
Browse files Browse the repository at this point in the history
Updates the `cloudflare_zone` resource to support setting the zone type
when creating and updating the resource.

Fixes #1277

Co-Authored-By: Eric Herot <[email protected]>
  • Loading branch information
jacobbednarz and eherot committed Nov 5, 2021
1 parent 2874a0b commit 1a31b61
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
17 changes: 16 additions & 1 deletion cloudflare/resource_cloudflare_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ func resourceCloudflareZoneCreate(d *schema.ResourceData, meta interface{}) erro
}
}

if ztype, ok := d.GetOk("type"); ok {
_, err := client.ZoneSetType(context.Background(), zone.ID, ztype.(string))
if err != nil {
return fmt.Errorf("error setting type on zone ID %q: %s", zone.ID, err)
}
}

return resourceCloudflareZoneRead(d, meta)
}

Expand Down Expand Up @@ -203,7 +210,15 @@ func resourceCloudflareZoneUpdate(d *schema.ResourceData, meta interface{}) erro
_, err := client.ZoneSetPaused(context.Background(), zoneID, paused.(bool))

if err != nil {
return fmt.Errorf("error updating zone_id %q: %s", zoneID, err)
return fmt.Errorf("error setting paused for zone ID %q: %s", zoneID, err)
}
}

if ztype, ok := d.GetOkExists("type"); ok && d.HasChange("type") {
_, err := client.ZoneSetType(context.Background(), zoneID, ztype.(string))

if err != nil {
return fmt.Errorf("error setting type for on zone ID %q: %s", zoneID, err)
}
}

Expand Down
34 changes: 28 additions & 6 deletions cloudflare/resource_cloudflare_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cloudflare

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccCloudflareZoneBasic(t *testing.T) {
func TestAccCloudflareZone_Basic(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_zone." + rnd

Expand All @@ -29,7 +30,7 @@ func TestAccCloudflareZoneBasic(t *testing.T) {
})
}

func TestAccCloudflareZoneBasicWithJumpStartEnabled(t *testing.T) {
func TestAccCloudflareZone_BasicWithJumpStartEnabled(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_zone." + rnd

Expand All @@ -52,7 +53,7 @@ func TestAccCloudflareZoneBasicWithJumpStartEnabled(t *testing.T) {
})
}

func TestAccCloudflareZoneWithPlan(t *testing.T) {
func TestAccCloudflareZone_WithPlan(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_zone." + rnd

Expand All @@ -74,7 +75,7 @@ func TestAccCloudflareZoneWithPlan(t *testing.T) {
})
}

func TestAccCloudflareZonePartialSetup(t *testing.T) {
func TestAccCloudflareZone_PartialSetup(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_zone." + rnd

Expand All @@ -95,7 +96,7 @@ func TestAccCloudflareZonePartialSetup(t *testing.T) {
})
}

func TestAccCloudflareZoneFullSetup(t *testing.T) {
func TestAccCloudflareZone_FullSetup(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_zone." + rnd

Expand Down Expand Up @@ -194,7 +195,7 @@ func TestAccZonePerformsUnicodeComparison(t *testing.T) {
})
}

func TestAccCloudflareZoneWithEnterprisePlan(t *testing.T) {
func TestAccCloudflareZone_WithEnterprisePlan(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_zone." + rnd

Expand Down Expand Up @@ -285,6 +286,27 @@ func TestPlanIDFallsBackToEmptyIfUnknown(t *testing.T) {
}
}

func TestAccCloudflareZone_SetType(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_zone." + rnd
zoneName := os.Getenv("CLOUDFLARE_DOMAIN")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testZoneConfigWithExplicitFullSetup(rnd, fmt.Sprintf("%s.%s", rnd, zoneName), "true", "false", "enterprise"),
},
{
Config: testZoneConfigWithPartialSetup(rnd, fmt.Sprintf("%s.%s", rnd, zoneName), "true", "false", "enterprise"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "type", "partial"),
),
},
},
})
}

func testZoneConfigWithPartialSetup(resourceID, zoneName, paused, jumpStart, plan string) string {
return fmt.Sprintf(`
resource "cloudflare_zone" "%[1]s" {
Expand Down

0 comments on commit 1a31b61

Please sign in to comment.