Skip to content

Commit

Permalink
Merge pull request #3315 from josip-stanic/feat/set-vanityNameServers…
Browse files Browse the repository at this point in the history
…-in-zone-resource

resource/resource_cloudflare_zone: add support for `vanity_name_servers`
  • Loading branch information
jacobbednarz authored Jun 3, 2024
2 parents acbf361 + 5f72f34 commit 9b86d4c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/3315.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/resource_cloudflare_zone: add support for 'vanity_name_servers'
```
2 changes: 1 addition & 1 deletion docs/resources/zone.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ resource "cloudflare_zone" "example" {
- `paused` (Boolean) Whether this zone is paused (traffic bypasses Cloudflare). Defaults to `false`.
- `plan` (String) The name of the commercial plan to apply to the zone. Available values: `free`, `lite`, `pro`, `pro_plus`, `business`, `enterprise`, `partners_free`, `partners_pro`, `partners_business`, `partners_enterprise`.
- `type` (String) A full zone implies that DNS is hosted with Cloudflare. A partial zone is typically a partner-hosted zone or a CNAME setup. Available values: `full`, `partial`, `secondary`. Defaults to `full`.
- `vanity_name_servers` (List of String) List of Vanity Nameservers (if set).

### Read-Only

- `id` (String) The ID of this resource.
- `meta` (Map of Boolean)
- `name_servers` (List of String) Cloudflare-assigned name servers. This is only populated for zones that use Cloudflare DNS.
- `status` (String) Status of the zone. Available values: `active`, `pending`, `initializing`, `moved`, `deleted`, `deactivated`.
- `vanity_name_servers` (List of String) List of Vanity Nameservers (if set).
- `verification_key` (String) Contains the TXT record value to validate domain ownership. This is only populated for zones of type `partial`.

## Import
Expand Down
18 changes: 18 additions & 0 deletions internal/sdkv2provider/resource_cloudflare_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ func resourceCloudflareZoneCreate(ctx context.Context, d *schema.ResourceData, m
}
}

vNS := []string{}
if vanityNS, ok := d.GetOk("vanity_name_servers"); ok {
vNS = expandInterfaceToStringList(vanityNS.([]interface{}))
_, err := client.ZoneSetVanityNS(ctx, zone.ID, vNS)
if err != nil {
return diag.FromErr(fmt.Errorf("error setting vanity_name_servers on zone ID %q: %w", zone.ID, err))
}
}

return resourceCloudflareZoneRead(ctx, d, meta)
}

Expand Down Expand Up @@ -221,6 +230,15 @@ func resourceCloudflareZoneUpdate(ctx context.Context, d *schema.ResourceData, m
}
}

vNS := []string{}
if vanityNS, ok := d.GetOkExists("vanity_name_servers"); ok && d.HasChange("vanity_name_servers") {
vNS = expandInterfaceToStringList(vanityNS.([]interface{}))
_, err := client.ZoneSetVanityNS(ctx, zone.ID, vNS)
if err != nil {
return diag.FromErr(fmt.Errorf("error setting vanity_name_servers on zone ID %q: %w", zone.ID, err))
}
}

// In the cases where the zone isn't completely setup yet, we need to
// check the `status` field and should it be pending, use the `LegacyID`
// from `zone.PlanPending` instead to account for paid plans.
Expand Down
63 changes: 63 additions & 0 deletions internal/sdkv2provider/resource_cloudflare_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,31 @@ func TestAccCloudflareZone_WithEnterprisePlan(t *testing.T) {
})
}

func TestAccCloudflareZone_WithEnterprisePlanVanityNameServers(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_zone." + rnd
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
zoneName := os.Getenv("CLOUDFLARE_DOMAIN")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testZoneConfigWithTypeVanityNameServersSetup(rnd, fmt.Sprintf("%s.%s", rnd, zoneName), "false", "false", "enterprise", accountID, "full"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "zone", fmt.Sprintf("%s.%s", rnd, zoneName)),
resource.TestCheckResourceAttr(name, "paused", "false"),
resource.TestCheckResourceAttr(name, "name_servers.#", "2"),
resource.TestCheckResourceAttr(name, "plan", planIDEnterprise),
resource.TestCheckResourceAttr(name, "type", "full"),
resource.TestCheckResourceAttr(name, "vanity_name_servers.#", "2"),
),
},
},
})
}

func TestAccCloudflareZone_Secondary(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_zone." + rnd
Expand All @@ -250,6 +275,31 @@ func TestAccCloudflareZone_Secondary(t *testing.T) {
})
}

func TestAccCloudflareZone_SecondaryWithVanityNameServers(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_zone." + rnd
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
zoneName := os.Getenv("CLOUDFLARE_DOMAIN")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testZoneConfigWithTypeVanityNameServersSetup(rnd, fmt.Sprintf("%s.%s", rnd, zoneName), "true", "false", "enterprise", accountID, "secondary"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "zone", fmt.Sprintf("%s.%s", rnd, zoneName)),
resource.TestCheckResourceAttr(name, "paused", "true"),
resource.TestCheckResourceAttr(name, "name_servers.#", "2"),
resource.TestCheckResourceAttr(name, "plan", planIDEnterprise),
resource.TestCheckResourceAttr(name, "type", "secondary"),
resource.TestCheckResourceAttr(name, "vanity_name_servers.#", "2"),
),
},
},
})
}

func testZoneConfig(resourceID, zoneName, paused, jumpStart, accountID string) string {
return fmt.Sprintf(`
resource "cloudflare_zone" "%[1]s" {
Expand Down Expand Up @@ -303,3 +353,16 @@ func testZoneConfigWithTypeSetup(resourceID, zoneName, paused, jumpStart, plan,
type = "%[7]s"
}`, resourceID, zoneName, paused, jumpStart, plan, accountID, zoneType)
}

func testZoneConfigWithTypeVanityNameServersSetup(resourceID, zoneName, paused, jumpStart, plan, accountID, zoneType string) string {
return fmt.Sprintf(`
resource "cloudflare_zone" "%[1]s" {
account_id = "%[6]s"
zone = "%[2]s"
paused = %[3]s
jump_start = %[4]s
plan = "%[5]s"
type = "%[7]s"
vanity_name_servers = ["ns1.%[2]s", "ns2.%[2]s"]
}`, resourceID, zoneName, paused, jumpStart, plan, accountID, zoneType)
}
1 change: 1 addition & 0 deletions internal/sdkv2provider/schema_cloudflare_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func resourceCloudflareZoneSchema() map[string]*schema.Schema {
},
"vanity_name_servers": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Expand Down

0 comments on commit 9b86d4c

Please sign in to comment.