chore(iaas): extract mapping of labels into util func (#867)
This commit is contained in:
parent
ad24ebe52d
commit
281d31f615
15 changed files with 156 additions and 171 deletions
|
|
@ -4,6 +4,9 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
|
||||
|
|
@ -29,3 +32,22 @@ func ConfigureClient(ctx context.Context, providerData *core.ProviderData, diags
|
|||
|
||||
return apiClient
|
||||
}
|
||||
|
||||
func MapLabels(ctx context.Context, responseLabels *map[string]interface{}, currentLabels types.Map) (basetypes.MapValue, error) { //nolint:gocritic // Linter wants to have a non-pointer type for the map, but this would mean a nil check has to be done before every usage of this func.
|
||||
labelsTF, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
|
||||
if diags.HasError() {
|
||||
return labelsTF, fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags))
|
||||
}
|
||||
|
||||
if responseLabels != nil && len(*responseLabels) != 0 {
|
||||
var diags diag.Diagnostics
|
||||
labelsTF, diags = types.MapValueFrom(ctx, types.StringType, *responseLabels)
|
||||
if diags.HasError() {
|
||||
return labelsTF, fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags))
|
||||
}
|
||||
} else if currentLabels.IsNull() {
|
||||
labelsTF = types.MapNull(types.StringType)
|
||||
}
|
||||
|
||||
return labelsTF, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue