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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue