Skip to content

Commit 1cc1205

Browse files
committed
add IsEmpty utils function and remove cmp.Equal
1 parent 6b1b207 commit 1cc1205

File tree

4 files changed

+131
-15
lines changed

4 files changed

+131
-15
lines changed

stackit/internal/services/ske/cluster/resource.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
skeUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/ske/utils"
1414
stringplanmodifierUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/planmodifiers/stringplanmodifier"
1515

16-
"github.com/google/go-cmp/cmp"
1716
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
1817
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1918
"github.com/hashicorp/terraform-plugin-framework/attr"
@@ -1676,8 +1675,7 @@ func mapNetwork(cl *ske.Cluster, m *Model) error {
16761675
// If the network field is not provided, the SKE API returns an empty object.
16771676
// If we parse that object into the terraform model, it will produce an inconsistent result after apply error
16781677

1679-
emptyNetwork := &ske.Network{}
1680-
if cmp.Equal(cl.Network, emptyNetwork, utils.CmpIgnoreAdditionalProperties()) && m.Network.IsNull() {
1678+
if skeUtils.IsEmptyNetwork(cl.Network) && m.Network.IsNull() {
16811679
if m.Network.Attributes() == nil {
16821680
m.Network = types.ObjectNull(networkTypes)
16831681
}
@@ -1833,8 +1831,7 @@ func mapExtensions(ctx context.Context, cl *ske.Cluster, m *Model) error {
18331831
}
18341832
disabledExtensions := aclDisabled && observabilityDisabled && dnsDisabled
18351833

1836-
emptyExtensions := &ske.Extension{}
1837-
if cmp.Equal(cl.Extensions, emptyExtensions, utils.CmpIgnoreAdditionalProperties()) && (disabledExtensions || m.Extensions.IsNull()) {
1834+
if skeUtils.IsEmptyExtension(cl.Extensions) && (disabledExtensions || m.Extensions.IsNull()) {
18381835
if m.Extensions.Attributes() == nil {
18391836
m.Extensions = types.ObjectNull(extensionsTypes)
18401837
}

stackit/internal/services/ske/utils/util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,17 @@ func ConfigureClient(ctx context.Context, providerData *core.ProviderData, diags
2828

2929
return apiClient
3030
}
31+
32+
func IsEmptyNetwork(network *ske.Network) bool {
33+
if !network.HasId() && !network.HasControlPlane() {
34+
return true
35+
}
36+
return false
37+
}
38+
39+
func IsEmptyExtension(extension *ske.Extension) bool {
40+
if !extension.HasDns() && !extension.HasAcl() && !extension.HasObservability() {
41+
return true
42+
}
43+
return false
44+
}

stackit/internal/services/ske/utils/util_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,118 @@ func TestConfigureClient(t *testing.T) {
9292
})
9393
}
9494
}
95+
96+
func TestIsEmptyNetwork(t *testing.T) {
97+
tests := []struct {
98+
name string
99+
input *ske.Network
100+
want bool
101+
}{
102+
{
103+
name: "nil network",
104+
input: nil,
105+
want: true,
106+
},
107+
{
108+
name: "empty",
109+
input: &ske.Network{},
110+
want: true,
111+
},
112+
{
113+
name: "only AdditionalProperties are set",
114+
input: &ske.Network{
115+
AdditionalProperties: map[string]interface{}{
116+
"foo": "bar",
117+
},
118+
},
119+
want: true,
120+
},
121+
{
122+
name: "id set",
123+
input: &ske.Network{
124+
Id: new("network-id"),
125+
},
126+
want: false,
127+
},
128+
{
129+
name: "control plane set",
130+
input: &ske.Network{
131+
ControlPlane: &ske.V2ControlPlaneNetwork{},
132+
},
133+
want: false,
134+
},
135+
{
136+
name: "id and control plane set",
137+
input: &ske.Network{
138+
Id: new("network-id"),
139+
ControlPlane: &ske.V2ControlPlaneNetwork{
140+
AccessScope: ske.ACCESSSCOPE_SNA.Ptr(),
141+
},
142+
},
143+
want: false,
144+
},
145+
}
146+
for _, tt := range tests {
147+
t.Run(tt.name, func(t *testing.T) {
148+
if got := IsEmptyNetwork(tt.input); got != tt.want {
149+
t.Errorf("IsEmptyNetwork() = %v, want %v", got, tt.want)
150+
}
151+
})
152+
}
153+
}
154+
155+
func TestIsEmptyExtension(t *testing.T) {
156+
tests := []struct {
157+
name string
158+
input *ske.Extension
159+
want bool
160+
}{
161+
{
162+
name: "nil extension",
163+
input: nil,
164+
want: true,
165+
},
166+
{
167+
name: "empty",
168+
input: &ske.Extension{},
169+
want: true,
170+
},
171+
{
172+
name: "only AdditionalProperties are set",
173+
input: &ske.Extension{
174+
AdditionalProperties: map[string]interface{}{
175+
"foo": "bar",
176+
},
177+
},
178+
want: true,
179+
},
180+
{
181+
name: "acl set",
182+
input: &ske.Extension{
183+
Acl: ske.NewACL([]string{"1.1.1.0/24"}, true),
184+
},
185+
want: false,
186+
},
187+
{
188+
name: "observability set",
189+
input: &ske.Extension{
190+
Observability: ske.NewObservability(true, "instance-id"),
191+
},
192+
want: false,
193+
},
194+
{
195+
name: "dns set",
196+
input: &ske.Extension{
197+
Dns: ske.NewDNS(true),
198+
},
199+
want: false,
200+
},
201+
}
202+
for _, tt := range tests {
203+
t.Run(tt.name, func(t *testing.T) {
204+
if got := IsEmptyExtension(tt.input); got != tt.want {
205+
t.Errorf("IsEmptyExtension() = %v, want %v", got, tt.want)
206+
}
207+
})
208+
}
209+
}

stackit/internal/utils/utils.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"strings"
1010

11-
"github.com/google/go-cmp/cmp"
1211
"github.com/hashicorp/terraform-plugin-framework/attr"
1312
"github.com/hashicorp/terraform-plugin-framework/diag"
1413
"github.com/hashicorp/terraform-plugin-framework/path"
@@ -215,12 +214,3 @@ func PrettyApiErr(ctx context.Context, diags *diag.Diagnostics, err error) strin
215214
}
216215
return err.Error()
217216
}
218-
219-
func CmpIgnoreAdditionalProperties() cmp.Option {
220-
return cmp.FilterPath(
221-
func(path cmp.Path) bool {
222-
return path.Last().String() == ".AdditionalProperties"
223-
},
224-
cmp.Ignore(),
225-
)
226-
}

0 commit comments

Comments
 (0)