From c1ada319ceb354b445061c0afca360f51e5d45e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Palet?= Date: Mon, 4 Nov 2024 13:27:24 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20Fix=20network=20interface=20handling=20o?= =?UTF-8?q?f=20allowed=20addresses=20and=20security=E2=80=A6=20(#579)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Fix network interface handling of allowed addresses and security fields * fix: Simplify toCreatePayload --- .../iaas/networkinterface/resource.go | 25 +++++-- .../iaas/networkinterface/resource_test.go | 70 +++++++++++++++++++ 2 files changed, 88 insertions(+), 7 deletions(-) diff --git a/stackit/internal/services/iaas/networkinterface/resource.go b/stackit/internal/services/iaas/networkinterface/resource.go index 6f3be178..c8f3ac57 100644 --- a/stackit/internal/services/iaas/networkinterface/resource.go +++ b/stackit/internal/services/iaas/networkinterface/resource.go @@ -469,7 +469,16 @@ func mapFields(ctx context.Context, networkInterfaceResp *iaas.NIC, model *Model respAllowedAddresses := []string{} var diags diag.Diagnostics if networkInterfaceResp.AllowedAddresses == nil { - model.AllowedAddresses = types.ListNull(types.StringType) + // If we send an empty list, the API will send null in the response + // We should handle this case and set the value to an empty list + if !model.AllowedAddresses.IsNull() { + model.AllowedAddresses, diags = types.ListValueFrom(ctx, types.StringType, []string{}) + if diags.HasError() { + return fmt.Errorf("map network interface allowed addresses: %w", core.DiagsToError(diags)) + } + } else { + model.AllowedAddresses = types.ListNull(types.StringType) + } } else { for _, n := range *networkInterfaceResp.AllowedAddresses { respAllowedAddresses = append(respAllowedAddresses, *n.String) @@ -553,8 +562,7 @@ func toCreatePayload(ctx context.Context, model *Model) (*iaas.CreateNICPayload, } } - allowedAddressesPayload := []iaas.AllowedAddressesInner{} - + allowedAddressesPayload := &[]iaas.AllowedAddressesInner{} if !(model.AllowedAddresses.IsNull() || model.AllowedAddresses.IsUnknown()) { for _, allowedAddressModel := range model.AllowedAddresses.Elements() { allowedAddressString, ok := allowedAddressModel.(types.String) @@ -562,10 +570,12 @@ func toCreatePayload(ctx context.Context, model *Model) (*iaas.CreateNICPayload, return nil, fmt.Errorf("type assertion failed") } - allowedAddressesPayload = append(allowedAddressesPayload, iaas.AllowedAddressesInner{ + *allowedAddressesPayload = append(*allowedAddressesPayload, iaas.AllowedAddressesInner{ String: conversion.StringValueToPointer(allowedAddressString), }) } + } else { + allowedAddressesPayload = nil } if !model.Labels.IsNull() && !model.Labels.IsUnknown() { @@ -577,7 +587,7 @@ func toCreatePayload(ctx context.Context, model *Model) (*iaas.CreateNICPayload, } return &iaas.CreateNICPayload{ - AllowedAddresses: &allowedAddressesPayload, + AllowedAddresses: allowedAddressesPayload, SecurityGroups: &modelSecurityGroups, Labels: labelPayload, Name: conversion.StringValueToPointer(model.Name), @@ -585,6 +595,7 @@ func toCreatePayload(ctx context.Context, model *Model) (*iaas.CreateNICPayload, Ipv4: conversion.StringValueToPointer(model.IPv4), Mac: conversion.StringValueToPointer(model.Mac), Type: conversion.StringValueToPointer(model.Type), + NicSecurity: conversion.BoolValueToPointer(model.Security), }, nil } @@ -604,8 +615,7 @@ func toUpdatePayload(ctx context.Context, model *Model, currentLabels types.Map) modelSecurityGroups = append(modelSecurityGroups, securityGroupString.ValueString()) } - allowedAddressesPayload := []iaas.AllowedAddressesInner{} - + 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 if !(model.AllowedAddresses.IsNull() || model.AllowedAddresses.IsUnknown()) { for _, allowedAddressModel := range model.AllowedAddresses.Elements() { allowedAddressString, ok := allowedAddressModel.(types.String) @@ -632,5 +642,6 @@ func toUpdatePayload(ctx context.Context, model *Model, currentLabels types.Map) SecurityGroups: &modelSecurityGroups, Labels: labelPayload, Name: conversion.StringValueToPointer(model.Name), + NicSecurity: conversion.BoolValueToPointer(model.Security), }, nil } diff --git a/stackit/internal/services/iaas/networkinterface/resource_test.go b/stackit/internal/services/iaas/networkinterface/resource_test.go index 0deee47a..15dc7cc6 100644 --- a/stackit/internal/services/iaas/networkinterface/resource_test.go +++ b/stackit/internal/services/iaas/networkinterface/resource_test.go @@ -130,6 +130,30 @@ func TestMapFields(t *testing.T) { }, true, }, + { + "empty_list_allowed_addresses", + Model{ + ProjectId: types.StringValue("pid"), + NetworkId: types.StringValue("nid"), + NetworkInterfaceId: types.StringValue("nicid"), + AllowedAddresses: types.ListValueMust(types.StringType, []attr.Value{}), + }, + &iaas.NIC{ + Id: utils.Ptr("nicid"), + AllowedAddresses: nil, + }, + Model{ + Id: types.StringValue("pid,nid,nicid"), + ProjectId: types.StringValue("pid"), + NetworkId: types.StringValue("nid"), + NetworkInterfaceId: types.StringValue("nicid"), + Name: types.StringNull(), + SecurityGroupIds: types.ListNull(types.StringType), + AllowedAddresses: types.ListValueMust(types.StringType, []attr.Value{}), + Labels: types.MapNull(types.StringType), + }, + true, + }, { "response_nil_fail", Model{}, @@ -184,6 +208,7 @@ func TestToCreatePayload(t *testing.T) { AllowedAddresses: types.ListValueMust(types.StringType, []attr.Value{ types.StringValue("aa1"), }), + Security: types.BoolValue(true), }, &iaas.CreateNICPayload{ Name: utils.Ptr("name"), @@ -196,6 +221,28 @@ func TestToCreatePayload(t *testing.T) { String: utils.Ptr("aa1"), }, }, + NicSecurity: utils.Ptr(true), + }, + true, + }, + { + "empty_allowed_addresses", + &Model{ + Name: types.StringValue("name"), + SecurityGroupIds: types.ListValueMust(types.StringType, []attr.Value{ + types.StringValue("sg1"), + types.StringValue("sg2"), + }), + + AllowedAddresses: types.ListNull(types.StringType), + }, + &iaas.CreateNICPayload{ + Name: utils.Ptr("name"), + SecurityGroups: &[]string{ + "sg1", + "sg2", + }, + AllowedAddresses: nil, }, true, }, @@ -237,6 +284,7 @@ func TestToUpdatePayload(t *testing.T) { AllowedAddresses: types.ListValueMust(types.StringType, []attr.Value{ types.StringValue("aa1"), }), + Security: types.BoolValue(true), }, &iaas.UpdateNICPayload{ Name: utils.Ptr("name"), @@ -249,6 +297,28 @@ func TestToUpdatePayload(t *testing.T) { String: utils.Ptr("aa1"), }, }, + NicSecurity: utils.Ptr(true), + }, + true, + }, + { + "empty_allowed_addresses", + &Model{ + Name: types.StringValue("name"), + SecurityGroupIds: types.ListValueMust(types.StringType, []attr.Value{ + types.StringValue("sg1"), + types.StringValue("sg2"), + }), + + AllowedAddresses: types.ListNull(types.StringType), + }, + &iaas.UpdateNICPayload{ + Name: utils.Ptr("name"), + SecurityGroups: &[]string{ + "sg1", + "sg2", + }, + AllowedAddresses: utils.Ptr([]iaas.AllowedAddressesInner{}), }, true, },