Skip to content

Commit e4c9b2d

Browse files
authored
Add a DiffSupress for ipv6 shortening (#1551)
1 parent a598390 commit e4c9b2d

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

google/resource_dns_record_set.go

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/hashicorp/terraform/helper/schema"
1010
"google.golang.org/api/dns/v1"
11+
"net"
1112
)
1213

1314
func resourceDnsRecordSet() *schema.Resource {
@@ -38,6 +39,12 @@ func resourceDnsRecordSet() *schema.Resource {
3839
Required: true,
3940
Elem: &schema.Schema{
4041
Type: schema.TypeString,
42+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
43+
if d.Get("type") == "AAAA" {
44+
return ipv6AddressDiffSuppress(k, old, new, d)
45+
}
46+
return false
47+
},
4148
},
4249
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
4350
return strings.ToLower(strings.Trim(old, `"`)) == strings.ToLower(strings.Trim(new, `"`))
@@ -320,3 +327,10 @@ func rrdata(
320327
}
321328
return data
322329
}
330+
331+
func ipv6AddressDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
332+
oldIp := net.ParseIP(old)
333+
newIp := net.ParseIP(new)
334+
335+
return oldIp.Equal(newIp)
336+
}

google/resource_dns_record_set_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,31 @@ import (
99
"github.com/hashicorp/terraform/terraform"
1010
)
1111

12+
func TestIpv6AddressDiffSuppress(t *testing.T) {
13+
cases := map[string]struct {
14+
Old, New string
15+
ShouldSuppress bool
16+
}{
17+
"compact form should suppress diff": {
18+
Old: "2a03:b0c0:1:e0::29b:8001",
19+
New: "2a03:b0c0:0001:00e0:0000:0000:029b:8001",
20+
ShouldSuppress: true,
21+
},
22+
"different address should not suppress diff": {
23+
Old: "2a03:b0c0:1:e00::29b:8001",
24+
New: "2a03:b0c0:0001:00e0:0000:0000:029b:8001",
25+
ShouldSuppress: false,
26+
},
27+
}
28+
29+
for tn, tc := range cases {
30+
shouldSuppress := ipv6AddressDiffSuppress("", tc.Old, tc.New, nil)
31+
if shouldSuppress != tc.ShouldSuppress {
32+
t.Errorf("%s: expected %t", tn, tc.ShouldSuppress)
33+
}
34+
}
35+
}
36+
1237
func TestAccDnsRecordSet_basic(t *testing.T) {
1338
t.Parallel()
1439

0 commit comments

Comments
 (0)