Skip to content

Commit c1ada31

Browse files
authored
fix: Fix network interface handling of allowed addresses and security… (#579)
* fix: Fix network interface handling of allowed addresses and security fields * fix: Simplify toCreatePayload
1 parent f1a6179 commit c1ada31

2 files changed

Lines changed: 88 additions & 7 deletions

File tree

stackit/internal/services/iaas/networkinterface/resource.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,16 @@ func mapFields(ctx context.Context, networkInterfaceResp *iaas.NIC, model *Model
469469
respAllowedAddresses := []string{}
470470
var diags diag.Diagnostics
471471
if networkInterfaceResp.AllowedAddresses == nil {
472-
model.AllowedAddresses = types.ListNull(types.StringType)
472+
// If we send an empty list, the API will send null in the response
473+
// We should handle this case and set the value to an empty list
474+
if !model.AllowedAddresses.IsNull() {
475+
model.AllowedAddresses, diags = types.ListValueFrom(ctx, types.StringType, []string{})
476+
if diags.HasError() {
477+
return fmt.Errorf("map network interface allowed addresses: %w", core.DiagsToError(diags))
478+
}
479+
} else {
480+
model.AllowedAddresses = types.ListNull(types.StringType)
481+
}
473482
} else {
474483
for _, n := range *networkInterfaceResp.AllowedAddresses {
475484
respAllowedAddresses = append(respAllowedAddresses, *n.String)
@@ -553,19 +562,20 @@ func toCreatePayload(ctx context.Context, model *Model) (*iaas.CreateNICPayload,
553562
}
554563
}
555564

556-
allowedAddressesPayload := []iaas.AllowedAddressesInner{}
557-
565+
allowedAddressesPayload := &[]iaas.AllowedAddressesInner{}
558566
if !(model.AllowedAddresses.IsNull() || model.AllowedAddresses.IsUnknown()) {
559567
for _, allowedAddressModel := range model.AllowedAddresses.Elements() {
560568
allowedAddressString, ok := allowedAddressModel.(types.String)
561569
if !ok {
562570
return nil, fmt.Errorf("type assertion failed")
563571
}
564572

565-
allowedAddressesPayload = append(allowedAddressesPayload, iaas.AllowedAddressesInner{
573+
*allowedAddressesPayload = append(*allowedAddressesPayload, iaas.AllowedAddressesInner{
566574
String: conversion.StringValueToPointer(allowedAddressString),
567575
})
568576
}
577+
} else {
578+
allowedAddressesPayload = nil
569579
}
570580

571581
if !model.Labels.IsNull() && !model.Labels.IsUnknown() {
@@ -577,14 +587,15 @@ func toCreatePayload(ctx context.Context, model *Model) (*iaas.CreateNICPayload,
577587
}
578588

579589
return &iaas.CreateNICPayload{
580-
AllowedAddresses: &allowedAddressesPayload,
590+
AllowedAddresses: allowedAddressesPayload,
581591
SecurityGroups: &modelSecurityGroups,
582592
Labels: labelPayload,
583593
Name: conversion.StringValueToPointer(model.Name),
584594
Device: conversion.StringValueToPointer(model.Device),
585595
Ipv4: conversion.StringValueToPointer(model.IPv4),
586596
Mac: conversion.StringValueToPointer(model.Mac),
587597
Type: conversion.StringValueToPointer(model.Type),
598+
NicSecurity: conversion.BoolValueToPointer(model.Security),
588599
}, nil
589600
}
590601

@@ -604,8 +615,7 @@ func toUpdatePayload(ctx context.Context, model *Model, currentLabels types.Map)
604615
modelSecurityGroups = append(modelSecurityGroups, securityGroupString.ValueString())
605616
}
606617

607-
allowedAddressesPayload := []iaas.AllowedAddressesInner{}
608-
618+
allowedAddressesPayload := []iaas.AllowedAddressesInner{} // Even if null in the model, we need to send an empty list to the API since it's a PATCH endpoint
609619
if !(model.AllowedAddresses.IsNull() || model.AllowedAddresses.IsUnknown()) {
610620
for _, allowedAddressModel := range model.AllowedAddresses.Elements() {
611621
allowedAddressString, ok := allowedAddressModel.(types.String)
@@ -632,5 +642,6 @@ func toUpdatePayload(ctx context.Context, model *Model, currentLabels types.Map)
632642
SecurityGroups: &modelSecurityGroups,
633643
Labels: labelPayload,
634644
Name: conversion.StringValueToPointer(model.Name),
645+
NicSecurity: conversion.BoolValueToPointer(model.Security),
635646
}, nil
636647
}

stackit/internal/services/iaas/networkinterface/resource_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,30 @@ func TestMapFields(t *testing.T) {
130130
},
131131
true,
132132
},
133+
{
134+
"empty_list_allowed_addresses",
135+
Model{
136+
ProjectId: types.StringValue("pid"),
137+
NetworkId: types.StringValue("nid"),
138+
NetworkInterfaceId: types.StringValue("nicid"),
139+
AllowedAddresses: types.ListValueMust(types.StringType, []attr.Value{}),
140+
},
141+
&iaas.NIC{
142+
Id: utils.Ptr("nicid"),
143+
AllowedAddresses: nil,
144+
},
145+
Model{
146+
Id: types.StringValue("pid,nid,nicid"),
147+
ProjectId: types.StringValue("pid"),
148+
NetworkId: types.StringValue("nid"),
149+
NetworkInterfaceId: types.StringValue("nicid"),
150+
Name: types.StringNull(),
151+
SecurityGroupIds: types.ListNull(types.StringType),
152+
AllowedAddresses: types.ListValueMust(types.StringType, []attr.Value{}),
153+
Labels: types.MapNull(types.StringType),
154+
},
155+
true,
156+
},
133157
{
134158
"response_nil_fail",
135159
Model{},
@@ -184,6 +208,7 @@ func TestToCreatePayload(t *testing.T) {
184208
AllowedAddresses: types.ListValueMust(types.StringType, []attr.Value{
185209
types.StringValue("aa1"),
186210
}),
211+
Security: types.BoolValue(true),
187212
},
188213
&iaas.CreateNICPayload{
189214
Name: utils.Ptr("name"),
@@ -196,6 +221,28 @@ func TestToCreatePayload(t *testing.T) {
196221
String: utils.Ptr("aa1"),
197222
},
198223
},
224+
NicSecurity: utils.Ptr(true),
225+
},
226+
true,
227+
},
228+
{
229+
"empty_allowed_addresses",
230+
&Model{
231+
Name: types.StringValue("name"),
232+
SecurityGroupIds: types.ListValueMust(types.StringType, []attr.Value{
233+
types.StringValue("sg1"),
234+
types.StringValue("sg2"),
235+
}),
236+
237+
AllowedAddresses: types.ListNull(types.StringType),
238+
},
239+
&iaas.CreateNICPayload{
240+
Name: utils.Ptr("name"),
241+
SecurityGroups: &[]string{
242+
"sg1",
243+
"sg2",
244+
},
245+
AllowedAddresses: nil,
199246
},
200247
true,
201248
},
@@ -237,6 +284,7 @@ func TestToUpdatePayload(t *testing.T) {
237284
AllowedAddresses: types.ListValueMust(types.StringType, []attr.Value{
238285
types.StringValue("aa1"),
239286
}),
287+
Security: types.BoolValue(true),
240288
},
241289
&iaas.UpdateNICPayload{
242290
Name: utils.Ptr("name"),
@@ -249,6 +297,28 @@ func TestToUpdatePayload(t *testing.T) {
249297
String: utils.Ptr("aa1"),
250298
},
251299
},
300+
NicSecurity: utils.Ptr(true),
301+
},
302+
true,
303+
},
304+
{
305+
"empty_allowed_addresses",
306+
&Model{
307+
Name: types.StringValue("name"),
308+
SecurityGroupIds: types.ListValueMust(types.StringType, []attr.Value{
309+
types.StringValue("sg1"),
310+
types.StringValue("sg2"),
311+
}),
312+
313+
AllowedAddresses: types.ListNull(types.StringType),
314+
},
315+
&iaas.UpdateNICPayload{
316+
Name: utils.Ptr("name"),
317+
SecurityGroups: &[]string{
318+
"sg1",
319+
"sg2",
320+
},
321+
AllowedAddresses: utils.Ptr([]iaas.AllowedAddressesInner{}),
252322
},
253323
true,
254324
},

0 commit comments

Comments
 (0)