Skip to content

Commit

Permalink
Resolved dns_record_set clearing rrdatas (#4725) (#3207)
Browse files Browse the repository at this point in the history
* Updated func

* fixed misc

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Apr 29, 2021
1 parent f05b3c0 commit cb3fed5
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .changelog/4725.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
dns: fixed empty `rrdatas` list on `google_dns_record_set` for AAAA records
```
2 changes: 1 addition & 1 deletion google-beta/resource_dataflow_flex_template_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
compute "google.golang.org/api/compute/v1"
"google.golang.org/api/compute/v1"
)

func TestAccDataflowFlexTemplateJob_basic(t *testing.T) {
Expand Down
32 changes: 26 additions & 6 deletions google-beta/resource_dns_record_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"
"net"
"strings"
"testing"

Expand All @@ -11,23 +12,42 @@ import (

func TestIpv6AddressDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
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",
Old: []string{"2a03:b0c0:1:e0::29b:8001"},
New: []string{"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",
Old: []string{"2a03:b0c0:1:e00::29b:8001"},
New: []string{"2a03:b0c0:0001:00e0:0000:0000:029b:8001"},
ShouldSuppress: false,
},
"increase address should not suppress diff": {
Old: []string{""},
New: []string{"2a03:b0c0:0001:00e0:0000:0000:029b:8001"},
ShouldSuppress: false,
},
"decrease address should not suppress diff": {
Old: []string{"2a03:b0c0:1:e00::29b:8001"},
New: []string{""},
ShouldSuppress: false,
},
"switch address positions should suppress diff": {
Old: []string{"2a03:b0c0:1:e00::28b:8001", "2a03:b0c0:1:e0::29b:8001"},
New: []string{"2a03:b0c0:1:e0::29b:8001", "2a03:b0c0:1:e00::28b:8001"},
ShouldSuppress: true,
},
}

parseFunc := func(x string) string {
return net.ParseIP(x).String()
}

for tn, tc := range cases {
shouldSuppress := ipv6AddressforDnsDiffSuppress("", tc.Old, tc.New, nil)
shouldSuppress := rrdatasListDiffSuppress(tc.Old, tc.New, parseFunc, nil)
if shouldSuppress != tc.ShouldSuppress {
t.Errorf("%s: expected %t", tn, tc.ShouldSuppress)
}
Expand Down
60 changes: 46 additions & 14 deletions google-beta/resource_dns_resource_dns_record_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,55 @@ import (
)

func rrdatasDnsDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
if d.Get("type") == "AAAA" {
return ipv6AddressforDnsDiffSuppress(k, old, new, d)
}
if d.Get("type") == "MX" {
return (strings.ToLower(old) == strings.ToLower(new))
}
if d.Get("type") == "TXT" {
return strings.ToLower(strings.Trim(old, `"`)) == strings.ToLower(strings.Trim(new, `"`))
o, n := d.GetChange("rrdatas")
if o == nil || n == nil {
return false
}

oList := convertStringArr(o.([]interface{}))
nList := convertStringArr(n.([]interface{}))

parseFunc := func(record string) string {
switch d.Get("type") {
case "AAAA":
// parse ipv6 to a key from one list
return net.ParseIP(record).String()
case "MX":
return strings.ToLower(record)
case "TXT":
return strings.ToLower(strings.Trim(record, `"`))
default:
return record
}
}
return false
return rrdatasListDiffSuppress(oList, nList, parseFunc, d)
}

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

return oldIp.Equal(newIp)
// suppress on a list when 1) its items have dups that need to be ignored
// and 2) string comparison on the items may need a special parse function
// example of usage can be found ../../../third_party/terraform/tests/resource_dns_record_set_test.go.erb
func rrdatasListDiffSuppress(oldList, newList []string, fun func(x string) string, _ *schema.ResourceData) bool {
// compare two lists of unordered records
diff := make(map[string]bool, len(oldList))
for _, oldRecord := range oldList {
// set all new IPs to true
diff[fun(oldRecord)] = true
}
for _, newRecord := range newList {
// set matched IPs to false otherwise can't suppress
if diff[fun(newRecord)] {
diff[fun(newRecord)] = false
} else {
return false
}
}
// can't suppress if unmatched records are found
for _, element := range diff {
if element {
return false
}
}
return true
}

func resourceDNSResourceDnsRecordSet() *schema.Resource {
Expand Down

0 comments on commit cb3fed5

Please sign in to comment.