From cede37263e62dd15b4ab1583b542c25d7dd954e1 Mon Sep 17 00:00:00 2001 From: Ferdinando Formica Date: Fri, 29 Nov 2024 16:49:49 +0000 Subject: [PATCH] Fix ordering issue on regions meta fields by hashing set on name only --- ns1/resource_record.go | 19 +++++++++++++------ ns1/resource_record_test.go | 12 +++++++++++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/ns1/resource_record.go b/ns1/resource_record.go index 2376c78..af174cc 100644 --- a/ns1/resource_record.go +++ b/ns1/resource_record.go @@ -315,18 +315,14 @@ func recordToResourceData(d *schema.ResourceData, r *dns.Record) error { } } if len(r.Regions) > 0 { - regions := make([]map[string]interface{}, 0, len(r.Regions)) + regions := schema.NewSet(regionHash, nil) for name, region := range r.Regions { newRegion := make(map[string]interface{}) newRegion["name"] = name newRegion["meta"] = region.Meta.StringMap() - regions = append(regions, newRegion) + regions.Add(newRegion) } log.Printf("Setting regions %+v", regions) - err := d.Set("regions", regions) - if err != nil { - return fmt.Errorf("[DEBUG] Error setting regions for: %s, error: %#v", r.Domain, err) - } } return nil } @@ -801,3 +797,14 @@ func subdivisionConverter(s string) (map[string]interface{}, error) { return subdivisionsMap, nil } + +// regions are maps, but the names should be unique, so use the name as index +func regionHash(i interface{}) int { + if m, ok := i.(map[string]interface{}); ok { + if n, ok := m["name"].(string); ok { + return schema.HashString(n) + } + } + // if the name is missing + return 0 +} diff --git a/ns1/resource_record_test.go b/ns1/resource_record_test.go index 866d9cd..14103b9 100644 --- a/ns1/resource_record_test.go +++ b/ns1/resource_record_test.go @@ -539,6 +539,12 @@ func TestAccRecord_updatedWithRegions(t *testing.T) { ), ), }, + { + // Plan again to detect "loop" conditions + Config: testAccRecordUpdatedWithRegionWeight(rString), + PlanOnly: true, + ExpectNonEmptyPlan: false, + }, { Config: testAccRecordUpdatedWithRegions(rString), Check: resource.ComposeTestCheckFunc( @@ -1903,7 +1909,11 @@ resource "ns1_record" "it" { regions { name = "cal" meta = { - weight = 100 + // country = "CA,MX,RU,US" + // georegion = "AFRICA,EUROPE,US-CENTRAL,US-EAST,US-WEST" + country = "RU,CA,MX,US" + georegion = "EUROPE,US-CENTRAL,US-WEST,AFRICA,US-EAST" + weight = 100 } }