Skip to content

Commit be334e8

Browse files
authored
fix(lb):ignore changes on /32 subnets on ACLs (#2442)
1 parent 443bc4e commit be334e8

File tree

4 files changed

+60
-20
lines changed

4 files changed

+60
-20
lines changed

scaleway/helpers.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -767,30 +767,42 @@ func diffSuppressFuncLocality(_, oldValue, newValue string, _ *schema.ResourceDa
767767
// diffSuppressFuncOrderDiff suppresses diffs for TypeList attributes when the only change is the order of elements.
768768
// https://github.com/hashicorp/terraform-plugin-sdk/issues/477#issuecomment-1238807249
769769
func diffSuppressFuncOrderDiff(k, _, _ string, d *schema.ResourceData) bool {
770-
// Extract the base key path to the list attribute, ignoring the index and value parts
770+
baseKey := extractBaseKey(k)
771+
oldList, newList := getStringListsFromState(baseKey, d)
772+
773+
return compareStringListsIgnoringOrder(oldList, newList)
774+
}
775+
776+
func extractBaseKey(k string) string {
771777
lastDotIndex := strings.LastIndex(k, ".")
772-
baseKey := k
773778
if lastDotIndex != -1 {
774-
baseKey = k[:lastDotIndex]
779+
return k[:lastDotIndex]
775780
}
776781

777-
oldList, newList := d.GetChange(baseKey)
778-
if oldList == nil || newList == nil {
779-
return false
780-
}
782+
return k
783+
}
781784

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

787-
oldListStr, newListStr := make([]string, len(oldListSlice)), make([]string, len(newListSlice))
788-
for i, oldItem := range oldListSlice {
789-
oldListStr[i] = fmt.Sprint(oldItem)
788+
oldListStr := make([]string, len(oldList.([]interface{})))
789+
newListStr := make([]string, len(newList.([]interface{})))
790+
791+
for i, v := range oldList.([]interface{}) {
792+
oldListStr[i] = fmt.Sprint(v)
793+
}
794+
for i, v := range newList.([]interface{}) {
795+
newListStr[i] = fmt.Sprint(v)
790796
}
791-
for j, newItem := range newListSlice {
792-
newListStr[j] = fmt.Sprint(newItem)
797+
798+
return oldListStr, newListStr
799+
}
800+
801+
func compareStringListsIgnoringOrder(oldListStr, newListStr []string) bool {
802+
if len(oldListStr) != len(newListStr) {
803+
return false // different lengths means there's definitely a change
793804
}
805+
794806
sort.Strings(oldListStr)
795807
sort.Strings(newListStr)
796808

scaleway/helpers_lb.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,3 +625,29 @@ func lbPrivateNetworkSetHash(v interface{}) int {
625625

626626
return StringHashcode(buf.String())
627627
}
628+
629+
func diffSuppressFunc32SubnetMask(k, _, _ string, d *schema.ResourceData) bool {
630+
baseKey := extractBaseKey(k)
631+
oldList, newList := getStringListsFromState(baseKey, d)
632+
633+
oldList = normalizeIPSubnetList(oldList)
634+
newList = normalizeIPSubnetList(newList)
635+
636+
return compareStringListsIgnoringOrder(oldList, newList)
637+
}
638+
639+
func normalizeIPSubnetList(list []string) []string {
640+
normalized := make([]string, len(list))
641+
for i, ip := range list {
642+
normalized[i] = normalizeIPSubnet(ip)
643+
}
644+
645+
return normalized
646+
}
647+
648+
func normalizeIPSubnet(ip string) string {
649+
if strings.HasSuffix(ip, "/32") {
650+
return strings.TrimSuffix(ip, "/32")
651+
}
652+
return ip
653+
}

scaleway/resource_lb_acl.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ func resourceScalewayLbACL() *schema.Resource {
110110
Elem: &schema.Schema{
111111
Type: schema.TypeString,
112112
},
113-
Optional: true,
114-
Description: "A list of IPs or CIDR v4/v6 addresses of the client of the session to match",
113+
Optional: true,
114+
Description: "A list of IPs or CIDR v4/v6 addresses of the client of the session to match",
115+
DiffSuppressFunc: diffSuppressFunc32SubnetMask,
115116
},
116117
"http_filter": {
117118
Type: schema.TypeString,

scaleway/resource_lb_frontend.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ func resourceScalewayLbFrontend() *schema.Resource {
161161
Elem: &schema.Schema{
162162
Type: schema.TypeString,
163163
},
164-
Optional: true,
165-
Description: "A list of IPs or CIDR v4/v6 addresses of the client of the session to match",
164+
Optional: true,
165+
Description: "A list of IPs or CIDR v4/v6 addresses of the client of the session to match",
166+
DiffSuppressFunc: diffSuppressFunc32SubnetMask,
166167
},
167168
"http_filter": {
168169
Type: schema.TypeString,

0 commit comments

Comments
 (0)