Skip to content

Commit

Permalink
fix(lb):ignore changes on /32 subnets on ACLs (#2442)
Browse files Browse the repository at this point in the history
  • Loading branch information
yfodil authored Mar 11, 2024
1 parent 443bc4e commit be334e8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
44 changes: 28 additions & 16 deletions scaleway/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,30 +767,42 @@ func diffSuppressFuncLocality(_, oldValue, newValue string, _ *schema.ResourceDa
// diffSuppressFuncOrderDiff suppresses diffs for TypeList attributes when the only change is the order of elements.
// https://github.com/hashicorp/terraform-plugin-sdk/issues/477#issuecomment-1238807249
func diffSuppressFuncOrderDiff(k, _, _ string, d *schema.ResourceData) bool {
// Extract the base key path to the list attribute, ignoring the index and value parts
baseKey := extractBaseKey(k)
oldList, newList := getStringListsFromState(baseKey, d)

return compareStringListsIgnoringOrder(oldList, newList)
}

func extractBaseKey(k string) string {
lastDotIndex := strings.LastIndex(k, ".")
baseKey := k
if lastDotIndex != -1 {
baseKey = k[:lastDotIndex]
return k[:lastDotIndex]
}

oldList, newList := d.GetChange(baseKey)
if oldList == nil || newList == nil {
return false
}
return k
}

oldListSlice, newListSlice := oldList.([]interface{}), newList.([]interface{})
if len(oldListSlice) != len(newListSlice) {
return false // Different lengths means there's definitely a change
}
func getStringListsFromState(key string, d *schema.ResourceData) ([]string, []string) {
oldList, newList := d.GetChange(key)

oldListStr, newListStr := make([]string, len(oldListSlice)), make([]string, len(newListSlice))
for i, oldItem := range oldListSlice {
oldListStr[i] = fmt.Sprint(oldItem)
oldListStr := make([]string, len(oldList.([]interface{})))
newListStr := make([]string, len(newList.([]interface{})))

for i, v := range oldList.([]interface{}) {
oldListStr[i] = fmt.Sprint(v)
}
for i, v := range newList.([]interface{}) {
newListStr[i] = fmt.Sprint(v)
}
for j, newItem := range newListSlice {
newListStr[j] = fmt.Sprint(newItem)

return oldListStr, newListStr
}

func compareStringListsIgnoringOrder(oldListStr, newListStr []string) bool {
if len(oldListStr) != len(newListStr) {
return false // different lengths means there's definitely a change
}

sort.Strings(oldListStr)
sort.Strings(newListStr)

Expand Down
26 changes: 26 additions & 0 deletions scaleway/helpers_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,3 +625,29 @@ func lbPrivateNetworkSetHash(v interface{}) int {

return StringHashcode(buf.String())
}

func diffSuppressFunc32SubnetMask(k, _, _ string, d *schema.ResourceData) bool {
baseKey := extractBaseKey(k)
oldList, newList := getStringListsFromState(baseKey, d)

oldList = normalizeIPSubnetList(oldList)
newList = normalizeIPSubnetList(newList)

return compareStringListsIgnoringOrder(oldList, newList)
}

func normalizeIPSubnetList(list []string) []string {
normalized := make([]string, len(list))
for i, ip := range list {
normalized[i] = normalizeIPSubnet(ip)
}

return normalized
}

func normalizeIPSubnet(ip string) string {
if strings.HasSuffix(ip, "/32") {
return strings.TrimSuffix(ip, "/32")
}
return ip
}
5 changes: 3 additions & 2 deletions scaleway/resource_lb_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ func resourceScalewayLbACL() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "A list of IPs or CIDR v4/v6 addresses of the client of the session to match",
Optional: true,
Description: "A list of IPs or CIDR v4/v6 addresses of the client of the session to match",
DiffSuppressFunc: diffSuppressFunc32SubnetMask,
},
"http_filter": {
Type: schema.TypeString,
Expand Down
5 changes: 3 additions & 2 deletions scaleway/resource_lb_frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ func resourceScalewayLbFrontend() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "A list of IPs or CIDR v4/v6 addresses of the client of the session to match",
Optional: true,
Description: "A list of IPs or CIDR v4/v6 addresses of the client of the session to match",
DiffSuppressFunc: diffSuppressFunc32SubnetMask,
},
"http_filter": {
Type: schema.TypeString,
Expand Down

0 comments on commit be334e8

Please sign in to comment.