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
This commit is contained in:
parent
f1a6179ccf
commit
c1ada319ce
2 changed files with 88 additions and 7 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue