Skip to content

Commit

Permalink
Fix ordering issue on regions meta fields by hashing set on name only
Browse files Browse the repository at this point in the history
  • Loading branch information
fformica committed Nov 29, 2024
1 parent b0e7170 commit cede372
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
19 changes: 13 additions & 6 deletions ns1/resource_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
12 changes: 11 additions & 1 deletion ns1/resource_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit cede372

Please sign in to comment.