Add a DiffSupress for ipv6 shortening (#1551)

This commit is contained in:
Vincent Roseberry 2018-05-29 14:43:39 -07:00 committed by GitHub
parent a598390697
commit e4c9b2d4b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/dns/v1"
"net"
)
func resourceDnsRecordSet() *schema.Resource {
@ -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, `"`))
@ -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)
}

View File

@ -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()