Skip to content

Commit

Permalink
Merge pull request #3888 from alexluong/suppress-record-ipv6
Browse files Browse the repository at this point in the history
suppress matching ipv6 dns record
  • Loading branch information
jacobbednarz authored Sep 3, 2024
2 parents c0931fc + d76c534 commit 26eebcf
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/3888.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/cloudflare_record: Suppress matching ipv6 dns record
```
20 changes: 20 additions & 0 deletions internal/sdkv2provider/resource_cloudflare_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net"
"strings"
"time"

Expand Down Expand Up @@ -501,3 +502,22 @@ func suppressTrailingDots(k, old, new string, d *schema.ResourceData) bool {

return strings.TrimSuffix(old, ".") == newTrimmed
}

func suppressMatchingIpv6(old, new string) bool {
oldIpv6 := net.ParseIP(old)
if oldIpv6 == nil || oldIpv6.To16() == nil {
return false
}
newIpv6 := net.ParseIP(new)
if newIpv6 == nil || newIpv6.To16() == nil {
return false
}
return oldIpv6.Equal(newIpv6)
}

func suppressContent(k, old, new string, d *schema.ResourceData) bool {
if suppressMatchingIpv6(old, new) {
return true
}
return suppressTrailingDots(k, old, new, d)
}
63 changes: 63 additions & 0 deletions internal/sdkv2provider/resource_cloudflare_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -672,6 +673,35 @@ func TestAccCloudflareRecord_ClearTags(t *testing.T) {
})
}

func TestAccCloudflareRecord_CompareIPv6(t *testing.T) {
t.Parallel()
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
rnd := generateRandomResourceName()
name := fmt.Sprintf("cloudflare_record.%s", rnd)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckCloudflareRecordDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareRecordConfigIPv6(zoneID, rnd, rnd, "2001:4860:4860:0:0:0:0:8888"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttr(name, "type", "AAAA"),
resource.TestCheckResourceAttr(name, "content", "2001:4860:4860::8888"),
),
},
{
Config: testAccCheckCloudflareRecordConfigIPv6(zoneID, rnd, rnd, "2001:4860:4860::8888"),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{plancheck.ExpectEmptyPlan()},
},
},
},
})
}

func TestSuppressTrailingDots(t *testing.T) {
t.Parallel()

Expand All @@ -697,6 +727,28 @@ func TestSuppressTrailingDots(t *testing.T) {
}
}

func TestIPv6Comparison(t *testing.T) {
t.Parallel()

cases := []struct {
old string
new string
}{
{"2001:db8:3333:4444:5555:6666:7777:8888", "2001:db8:3333:4444:5555:6666:7777:8888"},
{"1050:0000:0000:0000:0005:0600:300c:326b", "1050:0:0:0:5:0600:300c:326b"},
{"ff06:0:0:0:0:0:0:c3", "ff06::c3"},
{"0:0:0:0:0:0:0:0", "::"},
{"2001:db8::", "2001:db8:0:0:0:0:0:0"},
{"::1234:5678", "0:0:0:0:0:0:1234:5678"},
{"2001:db8:0:0:0:0:1234:5678", "2001:db8::1234:5678"},
}

for _, c := range cases {
got := suppressContent("", c.old, c.new, nil)
assert.Equal(t, true, got)
}
}

func testAccCheckCloudflareRecordRecreated(before, after *cloudflare.DNSRecord) resource.TestCheckFunc {
return func(s *terraform.State) error {
if before.ID == after.ID {
Expand Down Expand Up @@ -1076,3 +1128,14 @@ func testAccCheckCloudflareRecordDNSKEY(zoneID, name string) string {
}
`, zoneID, name)
}

func testAccCheckCloudflareRecordConfigIPv6(zoneID, name, rnd, content string) string {
return fmt.Sprintf(`
resource "cloudflare_record" "%[3]s" {
zone_id = "%[1]s"
name = "%[2]s"
content = "%[4]s"
type = "AAAA"
ttl = 3600
}`, zoneID, name, rnd, content)
}
2 changes: 1 addition & 1 deletion internal/sdkv2provider/schema_cloudflare_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func resourceCloudflareRecordSchema() map[string]*schema.Schema {
Optional: true,
Computed: true,
ExactlyOneOf: []string{"data", "content", "value"},
DiffSuppressFunc: suppressTrailingDots,
DiffSuppressFunc: suppressContent,
Description: "The content of the record.",
},

Expand Down

0 comments on commit 26eebcf

Please sign in to comment.