Skip to content

Commit 4fa9a2f

Browse files
committed
fix(ipam): only suppress diffs when IP host part is identical
1 parent a4d6f1c commit 4fa9a2f

File tree

2 files changed

+92
-20
lines changed

2 files changed

+92
-20
lines changed

internal/services/ipam/helpers.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,17 @@ func NewAPIWithRegionAndID(m any, id string) (*ipam.API, scw.Region, string, err
4444
}
4545

4646
func diffSuppressFuncStandaloneIPandCIDR(_, oldValue, newValue string, _ *schema.ResourceData) bool {
47-
oldIP, oldNet, errOld := net.ParseCIDR(oldValue)
48-
if errOld != nil {
49-
oldIP = net.ParseIP(oldValue)
50-
}
51-
52-
newIP, newNet, errNew := net.ParseCIDR(newValue)
53-
if errNew != nil {
54-
newIP = net.ParseIP(newValue)
55-
}
56-
57-
if oldIP != nil && newIP != nil && oldIP.Equal(newIP) {
58-
return true
59-
}
60-
61-
if oldNet != nil && newIP != nil && oldNet.Contains(newIP) {
62-
return true
47+
parseIPOrCIDR := func(s string) net.IP {
48+
if ip, _, err := net.ParseCIDR(s); err == nil {
49+
return ip
50+
}
51+
return net.ParseIP(s)
6352
}
6453

65-
if newNet != nil && oldIP != nil && newNet.Contains(oldIP) {
66-
return true
67-
}
54+
oldIP := parseIPOrCIDR(oldValue)
55+
newIP := parseIPOrCIDR(newValue)
6856

69-
return false
57+
return oldIP != nil && newIP != nil && oldIP.Equal(newIP)
7058
}
7159

7260
type GetResourcePrivateIPsOptions struct {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package ipam
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestDiffSuppress_IPAMIP(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
oldValue string
11+
newValue string
12+
want bool
13+
}{
14+
{
15+
name: "IP == IP",
16+
oldValue: "172.16.32.7",
17+
newValue: "172.16.32.7",
18+
want: true,
19+
},
20+
{
21+
name: "IP == IP/CIDR same host",
22+
oldValue: "172.16.32.7/22",
23+
newValue: "172.16.32.7",
24+
want: true,
25+
},
26+
{
27+
name: "IP/CIDR == IP same host (reversed)",
28+
oldValue: "172.16.32.7",
29+
newValue: "172.16.32.7/22",
30+
want: true,
31+
},
32+
{
33+
name: "Different host within same CIDR is NOT suppressed",
34+
oldValue: "172.16.32.7/22",
35+
newValue: "172.16.32.8",
36+
want: false,
37+
},
38+
{
39+
name: "Different host (plain IPs) is NOT suppressed",
40+
oldValue: "172.16.32.7",
41+
newValue: "172.16.32.8",
42+
want: false,
43+
},
44+
{
45+
name: "Equal but with /32 single-host CIDR",
46+
oldValue: "10.0.0.1/32",
47+
newValue: "10.0.0.1",
48+
want: true,
49+
},
50+
{
51+
name: "Broader CIDR vs network address should NOT suppress",
52+
oldValue: "10.0.0.1/24",
53+
newValue: "10.0.0.0",
54+
want: false,
55+
},
56+
{
57+
name: "Invalid old value -> not suppressed",
58+
oldValue: "not-an-ip",
59+
newValue: "10.0.0.1",
60+
want: false,
61+
},
62+
{
63+
name: "Invalid new value -> not suppressed",
64+
oldValue: "10.0.0.1",
65+
newValue: "bad/32",
66+
want: false,
67+
},
68+
{
69+
name: "Both invalid -> not suppressed",
70+
oldValue: "nope",
71+
newValue: "nope2",
72+
want: false,
73+
},
74+
}
75+
76+
for _, tt := range tests {
77+
t.Run(tt.name, func(t *testing.T) {
78+
got := diffSuppressFuncStandaloneIPandCIDR("", tt.oldValue, tt.newValue, nil)
79+
if got != tt.want {
80+
t.Fatalf("diffSuppress(%q, %q) = %v, want %v", tt.oldValue, tt.newValue, got, tt.want)
81+
}
82+
})
83+
}
84+
}

0 commit comments

Comments
 (0)