Skip to content

Commit

Permalink
use RestoreFallbackDomainDefaults instead of custom handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz committed Jan 20, 2022
1 parent 2fb0985 commit 9a9842f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
19 changes: 1 addition & 18 deletions cloudflare/resource_cloudflare_fallback_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var default_domains = []string{
"intranet", "internal", "private", "localdomain", "domain", "lan", "home",
"host", "corp", "local", "localhost", "home.arpa", "invalid", "test",
}

func resourceCloudflareFallbackDomain() *schema.Resource {
return &schema.Resource{
Schema: resourceCloudflareFallbackDomainSchema(),
Expand Down Expand Up @@ -66,7 +61,7 @@ func resourceCloudflareFallbackDomainDelete(d *schema.ResourceData, meta interfa
client := meta.(*cloudflare.API)
accountID := d.Get("account_id").(string)

_, err := client.UpdateFallbackDomain(context.Background(), accountID, getDefaultDomains())
err := client.RestoreFallbackDomainDefaults(context.Background(), accountID)
if err != nil {
return err
}
Expand Down Expand Up @@ -121,15 +116,3 @@ func expandFallbackDomains(domains []interface{}) []cloudflare.FallbackDomain {

return domainList
}

func getDefaultDomains() []cloudflare.FallbackDomain {
domainList := make([]cloudflare.FallbackDomain, 0)

for _, domain := range default_domains {
domainList = append(domainList, cloudflare.FallbackDomain{
Suffix: domain,
})
}

return domainList
}
13 changes: 7 additions & 6 deletions cloudflare/resource_cloudflare_fallback_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/pkg/errors"
)

func TestAccCloudflareFallbackDomain(t *testing.T) {
Expand All @@ -33,22 +34,22 @@ func TestAccCloudflareFallbackDomain(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCloudflareFallbackDomain(rnd, accountID, "example domain", "example.com", "2.2.2.2"),
Config: testAccCloudflareFallbackDomain(rnd, accountID, "example domain", "example.com", "1.0.0.1"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "account_id", accountID),
resource.TestCheckResourceAttr(name, "domains.#", "1"),
resource.TestCheckResourceAttr(name, "domains.0.description", "example domain"),
resource.TestCheckResourceAttr(name, "domains.0.suffix", "example.com"),
resource.TestCheckResourceAttr(name, "domains.0.dns_server.0", "2.2.2.2"),
resource.TestCheckResourceAttr(name, "domains.0.dns_server.0", "1.0.0.1"),
),
},
{
Config: testAccCloudflareFallbackDomain(rnd, accountID, "second example domain", "example_two.com", "1.1.1.1"),
Config: testAccCloudflareFallbackDomain(rnd, accountID, "second example domain", "example.net", "1.1.1.1"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "account_id", accountID),
resource.TestCheckResourceAttr(name, "domains.#", "1"),
resource.TestCheckResourceAttr(name, "domains.0.description", "second example domain"),
resource.TestCheckResourceAttr(name, "domains.0.suffix", "example_two.com"),
resource.TestCheckResourceAttr(name, "domains.0.suffix", "example.net"),
resource.TestCheckResourceAttr(name, "domains.0.dns_server.0", "1.1.1.1"),
),
},
Expand Down Expand Up @@ -78,8 +79,8 @@ func testAccCheckCloudflareFallbackDomainDestroy(s *terraform.State) error {
}

result, _ := client.ListFallbackDomains(context.Background(), rs.Primary.ID)
if len(result) != len(default_domains) {
return fmt.Errorf("Deleted Fallback Domains resource has %d domains instead of expected %d default entries.", len(result), len(default_domains))
if len(result) == 0 {
return errors.New("deleted Fallback Domain resource has does not include default domains")
}
}

Expand Down
13 changes: 12 additions & 1 deletion website/docs/r/fallback_domain.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ Provides a Cloudflare Fallback Domain resource. Fallback domains are used to ign
## Example Usage

```hcl
# Use DNS servers 1.1.1.1 or 1.0.0.1 for example.com
resource "cloudflare_fallback_domain" "example" {
account_id = "1d5fdc9e88c8a8c4518b068cd94331fe"
domains {
suffix = "example.com"
description = "Example domain"
dns_server = ["1.1.1.1", "1.0.0.1"]
}
}
# Explicitly adding example.com to the default entries.
resource "cloudflare_fallback_domain" "example" {
account_id = "1d5fdc9e88c8a8c4518b068cd94331fe"
Expand All @@ -22,10 +32,11 @@ resource "cloudflare_fallback_domain" "example" {
suffix = domains.value
}
}
domains {
suffix = "example.com"
description = "Example domain"
dns_server = ["1.1.1.1", "2.2.2.2"]
dns_server = ["1.1.1.1", "1.0.0.1"]
}
}
```
Expand Down

0 comments on commit 9a9842f

Please sign in to comment.