Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions internal/services/ipam/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,19 @@ func NewAPIWithRegionAndID(m any, id string) (*ipam.API, scw.Region, string, err
return ipamAPI, region, ID, err
}

func diffSuppressFuncStandaloneIPandCIDR(_, oldValue, newValue string, _ *schema.ResourceData) bool {
oldIP, oldNet, errOld := net.ParseCIDR(oldValue)
if errOld != nil {
oldIP = net.ParseIP(oldValue)
}

newIP, newNet, errNew := net.ParseCIDR(newValue)
if errNew != nil {
newIP = net.ParseIP(newValue)
}

if oldIP != nil && newIP != nil && oldIP.Equal(newIP) {
return true
}
func DiffSuppressFuncStandaloneIPandCIDR(_, oldValue, newValue string, _ *schema.ResourceData) bool {
parseIPOrCIDR := func(s string) net.IP {
if ip, _, err := net.ParseCIDR(s); err == nil {
return ip
}

if oldNet != nil && newIP != nil && oldNet.Contains(newIP) {
return true
return net.ParseIP(s)
}

if newNet != nil && oldIP != nil && newNet.Contains(oldIP) {
return true
}
oldIP := parseIPOrCIDR(oldValue)
newIP := parseIPOrCIDR(newValue)

return false
return oldIP != nil && newIP != nil && oldIP.Equal(newIP)
}

type GetResourcePrivateIPsOptions struct {
Expand Down
86 changes: 86 additions & 0 deletions internal/services/ipam/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package ipam_test

import (
"testing"

"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
)

func TestDiffSuppress_IPAMIP(t *testing.T) {
tests := []struct {
name string
oldValue string
newValue string
want bool
}{
{
name: "IP == IP",
oldValue: "172.16.32.7",
newValue: "172.16.32.7",
want: true,
},
{
name: "IP == IP/CIDR same host",
oldValue: "172.16.32.7/22",
newValue: "172.16.32.7",
want: true,
},
{
name: "IP/CIDR == IP same host (reversed)",
oldValue: "172.16.32.7",
newValue: "172.16.32.7/22",
want: true,
},
{
name: "Different host within same CIDR is NOT suppressed",
oldValue: "172.16.32.7/22",
newValue: "172.16.32.8",
want: false,
},
{
name: "Different host (plain IPs) is NOT suppressed",
oldValue: "172.16.32.7",
newValue: "172.16.32.8",
want: false,
},
{
name: "Equal but with /32 single-host CIDR",
oldValue: "10.0.0.1/32",
newValue: "10.0.0.1",
want: true,
},
{
name: "Broader CIDR vs network address should NOT suppress",
oldValue: "10.0.0.1/24",
newValue: "10.0.0.0",
want: false,
},
{
name: "Invalid old value -> not suppressed",
oldValue: "not-an-ip",
newValue: "10.0.0.1",
want: false,
},
{
name: "Invalid new value -> not suppressed",
oldValue: "10.0.0.1",
newValue: "bad/32",
want: false,
},
{
name: "Both invalid -> not suppressed",
oldValue: "nope",
newValue: "nope2",
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ipam.DiffSuppressFuncStandaloneIPandCIDR("", tt.oldValue, tt.newValue, nil)
if got != tt.want {
t.Fatalf("diffSuppress(%q, %q) = %v, want %v", tt.oldValue, tt.newValue, got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/services/ipam/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func ResourceIP() *schema.Resource {
ForceNew: true,
Description: "Request a specific IP in the requested source pool",
ValidateFunc: validation.IsIPAddress,
DiffSuppressFunc: diffSuppressFuncStandaloneIPandCIDR,
DiffSuppressFunc: DiffSuppressFuncStandaloneIPandCIDR,
},
"source": {
Type: schema.TypeList,
Expand Down
Loading