Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds validation to cloudflare_record to ensure TTL is not set while proxied is true #127

Merged
merged 6 commits into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cloudflare/resource_cloudflare_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ func resourceCloudflareRecordCreate(d *schema.ResourceData, meta interface{}) er
}

if ttl, ok := d.GetOk("ttl"); ok {
if ttl.(int) != 1 && newRecord.Proxied {
return fmt.Errorf("error validating record %s: ttl cannot be set when `proxied` is true", newRecord.Name)
mrparkers marked this conversation as resolved.
Show resolved Hide resolved
}

newRecord.TTL = ttl.(int)
}

Expand Down Expand Up @@ -437,6 +441,10 @@ func resourceCloudflareRecordUpdate(d *schema.ResourceData, meta interface{}) er
}

if ttl, ok := d.GetOk("ttl"); ok {
if ttl.(int) != 1 && updateRecord.Proxied {
return fmt.Errorf("error validating record %s: ttl cannot be set when `proxied` is true", updateRecord.Name)
mrparkers marked this conversation as resolved.
Show resolved Hide resolved
}

updateRecord.TTL = ttl.(int)
}

Expand Down
56 changes: 56 additions & 0 deletions cloudflare/resource_cloudflare_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,49 @@ func TestAccCloudflareRecord_CreateAfterManualDestroy(t *testing.T) {
})
}

func TestAccCloudflareRecord_TtlValidation(t *testing.T) {
t.Parallel()
domain := os.Getenv("CLOUDFLARE_DOMAIN")
recordName := "tf-acctest-ttl-validation"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudflareRecordDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareRecordConfigTtlValidation(domain, recordName),
ExpectError: regexp.MustCompile(fmt.Sprintf("error validating record %s: ttl cannot be set when `proxied` is true", recordName)),
mrparkers marked this conversation as resolved.
Show resolved Hide resolved
},
},
})
}

func TestAccCloudflareRecord_TtlValidationUpdate(t *testing.T) {
t.Parallel()
domain := os.Getenv("CLOUDFLARE_DOMAIN")
recordName := "tf-acctest-ttl-validation"
name := "cloudflare_record.foobar"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudflareRecordDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareRecordConfigProxied(domain, recordName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflareRecordExists(name, &cloudflare.DNSRecord{}),
),
},
{
Config: testAccCheckCloudflareRecordConfigTtlValidation(domain, recordName),
ExpectError: regexp.MustCompile(fmt.Sprintf("error validating record %s: ttl cannot be set when `proxied` is true", recordName)),
mrparkers marked this conversation as resolved.
Show resolved Hide resolved
},
},
})
}

func testAccCheckCloudflareRecordRecreated(before, after *cloudflare.DNSRecord) resource.TestCheckFunc {
return func(s *terraform.State) error {
if before.ID == after.ID {
Expand Down Expand Up @@ -550,3 +593,16 @@ resource "cloudflare_record" "foobar" {
ttl = 3600
}`, zone, name)
}

func testAccCheckCloudflareRecordConfigTtlValidation(zone, name string) string {
return fmt.Sprintf(`
resource "cloudflare_record" "foobar" {
domain = "%[1]s"

name = "%[2]s"
value = "%[1]s"
type = "CNAME"
proxied = true
ttl = 3600
}`, zone, name)
}