fix: state drift of "stackit_server" (#679)
* fix: State drift of "stackit_server" when using "stackit_server_network_interface_attach" * fix: tests * add acceptance tests for stackit_server_network_interface_attach
This commit is contained in:
parent
3e8dcc542b
commit
b7f56d1685
3 changed files with 172 additions and 22 deletions
|
|
@ -888,12 +888,39 @@ func mapFields(ctx context.Context, serverResp *iaas.Server, model *Model) error
|
|||
for _, nic := range *serverResp.Nics {
|
||||
respNics = append(respNics, *nic.NicId)
|
||||
}
|
||||
nicTF, diags := types.ListValueFrom(ctx, types.StringType, respNics)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("failed to map networkInterfaces: %w", core.DiagsToError(diags))
|
||||
|
||||
var modelNics []string
|
||||
for _, modelNic := range model.NetworkInterfaces.Elements() {
|
||||
modelNicString, ok := modelNic.(types.String)
|
||||
if !ok {
|
||||
return fmt.Errorf("type assertion for network interfaces failed")
|
||||
}
|
||||
modelNics = append(modelNics, modelNicString.ValueString())
|
||||
}
|
||||
|
||||
model.NetworkInterfaces = nicTF
|
||||
var filteredNics []string
|
||||
for _, modelNic := range modelNics {
|
||||
for _, nic := range respNics {
|
||||
if nic == modelNic {
|
||||
filteredNics = append(filteredNics, nic)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sorts the filteredNics based on the modelNics order
|
||||
resultNics := utils.ReconcileStringSlices(modelNics, filteredNics)
|
||||
|
||||
if len(resultNics) != 0 {
|
||||
nicTF, diags := types.ListValueFrom(ctx, types.StringType, resultNics)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("failed to map networkInterfaces: %w", core.DiagsToError(diags))
|
||||
}
|
||||
|
||||
model.NetworkInterfaces = nicTF
|
||||
} else {
|
||||
model.NetworkInterfaces = types.ListNull(types.StringType)
|
||||
}
|
||||
} else {
|
||||
model.NetworkInterfaces = types.ListNull(types.StringType)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -98,16 +98,13 @@ func TestMapFields(t *testing.T) {
|
|||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
ImageId: types.StringValue("image_id"),
|
||||
NetworkInterfaces: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("nic1"),
|
||||
types.StringValue("nic2"),
|
||||
}),
|
||||
KeypairName: types.StringValue("keypair_name"),
|
||||
AffinityGroup: types.StringValue("group_id"),
|
||||
CreatedAt: types.StringValue(testTimestampValue),
|
||||
UpdatedAt: types.StringValue(testTimestampValue),
|
||||
LaunchedAt: types.StringValue(testTimestampValue),
|
||||
ImageId: types.StringValue("image_id"),
|
||||
NetworkInterfaces: types.ListNull(types.StringType),
|
||||
KeypairName: types.StringValue("keypair_name"),
|
||||
AffinityGroup: types.StringValue("group_id"),
|
||||
CreatedAt: types.StringValue(testTimestampValue),
|
||||
UpdatedAt: types.StringValue(testTimestampValue),
|
||||
LaunchedAt: types.StringValue(testTimestampValue),
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue