Skip to content

Commit

Permalink
Add a DiffSupress for ipv6 shortening (#1551)
Browse files Browse the repository at this point in the history
  • Loading branch information
rosbo authored May 29, 2018
1 parent a598390 commit e4c9b2d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions google/resource_dns_record_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/dns/v1"
"net"
)

func resourceDnsRecordSet() *schema.Resource {
Expand Down Expand Up @@ -38,6 +39,12 @@ func resourceDnsRecordSet() *schema.Resource {
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if d.Get("type") == "AAAA" {
return ipv6AddressDiffSuppress(k, old, new, d)
}
return false
},
},
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.ToLower(strings.Trim(old, `"`)) == strings.ToLower(strings.Trim(new, `"`))
Expand Down Expand Up @@ -320,3 +327,10 @@ func rrdata(
}
return data
}

func ipv6AddressDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
oldIp := net.ParseIP(old)
newIp := net.ParseIP(new)

return oldIp.Equal(newIp)
}
25 changes: 25 additions & 0 deletions google/resource_dns_record_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestIpv6AddressDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
ShouldSuppress bool
}{
"compact form should suppress diff": {
Old: "2a03:b0c0:1:e0::29b:8001",
New: "2a03:b0c0:0001:00e0:0000:0000:029b:8001",
ShouldSuppress: true,
},
"different address should not suppress diff": {
Old: "2a03:b0c0:1:e00::29b:8001",
New: "2a03:b0c0:0001:00e0:0000:0000:029b:8001",
ShouldSuppress: false,
},
}

for tn, tc := range cases {
shouldSuppress := ipv6AddressDiffSuppress("", tc.Old, tc.New, nil)
if shouldSuppress != tc.ShouldSuppress {
t.Errorf("%s: expected %t", tn, tc.ShouldSuppress)
}
}
}

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

Expand Down

0 comments on commit e4c9b2d

Please sign in to comment.