chore(iaas): extract mapping of labels into util func (#867)

This commit is contained in:
Ruben Hönle 2025-06-05 13:46:33 +02:00 committed by GitHub
parent ad24ebe52d
commit 281d31f615
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 156 additions and 171 deletions

View file

@ -332,18 +332,9 @@ func mapDataSourceFields(ctx context.Context, imageResp *iaas.Image, model *Data
} }
// Map labels // Map labels
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{}) labels, err := iaasUtils.MapLabels(ctx, imageResp.Labels, model.Labels)
if diags.HasError() { if err != nil {
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags)) return err
}
if imageResp.Labels != nil && len(*imageResp.Labels) != 0 {
var diags diag.Diagnostics
labels, diags = types.MapValueFrom(ctx, types.StringType, *imageResp.Labels)
if diags.HasError() {
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags))
}
} else if model.Labels.IsNull() {
labels = types.MapNull(types.StringType)
} }
model.ImageId = types.StringValue(imageId) model.ImageId = types.StringValue(imageId)

View file

@ -670,18 +670,9 @@ func mapFields(ctx context.Context, imageResp *iaas.Image, model *Model) error {
} }
// Map labels // Map labels
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{}) labels, err := iaasUtils.MapLabels(ctx, imageResp.Labels, model.Labels)
if diags.HasError() { if err != nil {
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags)) return err
}
if imageResp.Labels != nil && len(*imageResp.Labels) != 0 {
var diags diag.Diagnostics
labels, diags = types.MapValueFrom(ctx, types.StringType, *imageResp.Labels)
if diags.HasError() {
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags))
}
} else if model.Labels.IsNull() {
labels = types.MapNull(types.StringType)
} }
model.ImageId = types.StringValue(imageId) model.ImageId = types.StringValue(imageId)

View file

@ -8,7 +8,6 @@ import (
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils" iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema"
@ -326,20 +325,11 @@ func mapFields(ctx context.Context, keyPairResp *iaas.Keypair, model *Model) err
model.PublicKey = types.StringPointerValue(keyPairResp.PublicKey) model.PublicKey = types.StringPointerValue(keyPairResp.PublicKey)
model.Fingerprint = types.StringPointerValue(keyPairResp.Fingerprint) model.Fingerprint = types.StringPointerValue(keyPairResp.Fingerprint)
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{}) var err error
if diags.HasError() { model.Labels, err = iaasUtils.MapLabels(ctx, keyPairResp.Labels, model.Labels)
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags)) if err != nil {
return err
} }
if keyPairResp.Labels != nil && len(*keyPairResp.Labels) != 0 {
var diags diag.Diagnostics
labels, diags = types.MapValueFrom(ctx, types.StringType, *keyPairResp.Labels)
if diags.HasError() {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
}
} else if model.Labels.IsNull() {
labels = types.MapNull(types.StringType)
}
model.Labels = labels
return nil return nil
} }

View file

@ -13,7 +13,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-log/tflog"
@ -254,18 +253,9 @@ func mapDataSourceFields(ctx context.Context, networkResp *iaas.Network, model *
strings.Join(idParts, core.Separator), strings.Join(idParts, core.Separator),
) )
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{}) labels, err := iaasUtils.MapLabels(ctx, networkResp.Labels, model.Labels)
if diags.HasError() { if err != nil {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags)) return err
}
if networkResp.Labels != nil && len(*networkResp.Labels) != 0 {
var diags diag.Diagnostics
labels, diags = types.MapValueFrom(ctx, types.StringType, *networkResp.Labels)
if diags.HasError() {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
}
} else if model.Labels.IsNull() {
labels = types.MapNull(types.StringType)
} }
// IPv4 // IPv4

View file

@ -12,7 +12,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator" "github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema"
@ -512,18 +511,9 @@ func mapFields(ctx context.Context, networkResp *iaas.Network, model *Model) err
strings.Join(idParts, core.Separator), strings.Join(idParts, core.Separator),
) )
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{}) labels, err := iaasUtils.MapLabels(ctx, networkResp.Labels, model.Labels)
if diags.HasError() { if err != nil {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags)) return err
}
if networkResp.Labels != nil && len(*networkResp.Labels) != 0 {
var diags diag.Diagnostics
labels, diags = types.MapValueFrom(ctx, types.StringType, *networkResp.Labels)
if diags.HasError() {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
}
} else if model.Labels.IsNull() {
labels = types.MapNull(types.StringType)
} }
// IPv4 // IPv4

View file

@ -23,12 +23,12 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/core/utils" sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils"
"github.com/stackitcloud/stackit-sdk-go/services/iaas" "github.com/stackitcloud/stackit-sdk-go/services/iaas"
"github.com/stackitcloud/stackit-sdk-go/services/iaas/wait" "github.com/stackitcloud/stackit-sdk-go/services/iaas/wait"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
internalUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
) )
@ -489,12 +489,12 @@ func mapFields(ctx context.Context, networkAreaResp *iaas.NetworkArea, networkAr
model.DefaultNameservers = types.ListNull(types.StringType) model.DefaultNameservers = types.ListNull(types.StringType)
} else { } else {
respDefaultNameservers := *networkAreaResp.Ipv4.DefaultNameservers respDefaultNameservers := *networkAreaResp.Ipv4.DefaultNameservers
modelDefaultNameservers, err := internalUtils.ListValuetoStringSlice(model.DefaultNameservers) modelDefaultNameservers, err := utils.ListValuetoStringSlice(model.DefaultNameservers)
if err != nil { if err != nil {
return fmt.Errorf("get current network area default nameservers from model: %w", err) return fmt.Errorf("get current network area default nameservers from model: %w", err)
} }
reconciledDefaultNameservers := internalUtils.ReconcileStringSlices(modelDefaultNameservers, respDefaultNameservers) reconciledDefaultNameservers := utils.ReconcileStringSlices(modelDefaultNameservers, respDefaultNameservers)
defaultNameserversTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledDefaultNameservers) defaultNameserversTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledDefaultNameservers)
if diags.HasError() { if diags.HasError() {
@ -509,18 +509,9 @@ func mapFields(ctx context.Context, networkAreaResp *iaas.NetworkArea, networkAr
return fmt.Errorf("mapping network ranges: %w", err) return fmt.Errorf("mapping network ranges: %w", err)
} }
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{}) labels, err := iaasUtils.MapLabels(ctx, networkAreaResp.Labels, model.Labels)
if diags.HasError() { if err != nil {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags)) return err
}
if networkAreaResp.Labels != nil && len(*networkAreaResp.Labels) != 0 {
var diags diag.Diagnostics
labels, diags = types.MapValueFrom(ctx, types.StringType, *networkAreaResp.Labels)
if diags.HasError() {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
}
} else if model.Labels.IsNull() {
labels = types.MapNull(types.StringType)
} }
model.NetworkAreaId = types.StringValue(networkAreaId) model.NetworkAreaId = types.StringValue(networkAreaId)
@ -567,7 +558,7 @@ func mapNetworkRanges(ctx context.Context, networkAreaRangesList *[]iaas.Network
apiNetworkRangePrefixes = append(apiNetworkRangePrefixes, *n.Prefix) apiNetworkRangePrefixes = append(apiNetworkRangePrefixes, *n.Prefix)
} }
reconciledRangePrefixes := internalUtils.ReconcileStringSlices(modelNetworkRangePrefixes, apiNetworkRangePrefixes) reconciledRangePrefixes := utils.ReconcileStringSlices(modelNetworkRangePrefixes, apiNetworkRangePrefixes)
networkRangesList := []attr.Value{} networkRangesList := []attr.Value{}
for i, prefix := range reconciledRangePrefixes { for i, prefix := range reconciledRangePrefixes {
@ -748,7 +739,7 @@ func updateNetworkRanges(ctx context.Context, organizationId, networkAreaId stri
payload := iaas.CreateNetworkAreaRangePayload{ payload := iaas.CreateNetworkAreaRangePayload{
Ipv4: &[]iaas.NetworkRange{ Ipv4: &[]iaas.NetworkRange{
{ {
Prefix: utils.Ptr(prefix), Prefix: sdkUtils.Ptr(prefix),
}, },
}, },
} }

View file

@ -8,7 +8,6 @@ import (
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils" iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema"
@ -380,18 +379,9 @@ func mapFields(ctx context.Context, networkAreaRoute *iaas.Route, model *Model)
strings.Join(idParts, core.Separator), strings.Join(idParts, core.Separator),
) )
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{}) labels, err := iaasUtils.MapLabels(ctx, networkAreaRoute.Labels, model.Labels)
if diags.HasError() { if err != nil {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags)) return err
}
if networkAreaRoute.Labels != nil && len(*networkAreaRoute.Labels) != 0 {
var diags diag.Diagnostics
labels, diags = types.MapValueFrom(ctx, types.StringType, *networkAreaRoute.Labels)
if diags.HasError() {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
}
} else if model.Labels.IsNull() {
labels = types.MapNull(types.StringType)
} }
model.NetworkAreaRouteId = types.StringValue(networkAreaRouteId) model.NetworkAreaRouteId = types.StringValue(networkAreaRouteId)

View file

@ -484,18 +484,9 @@ func mapFields(ctx context.Context, networkInterfaceResp *iaas.NIC, model *Model
model.SecurityGroupIds = securityGroupsTF model.SecurityGroupIds = securityGroupsTF
} }
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{}) labels, err := iaasUtils.MapLabels(ctx, networkInterfaceResp.Labels, model.Labels)
if diags.HasError() { if err != nil {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags)) return err
}
if networkInterfaceResp.Labels != nil && len(*networkInterfaceResp.Labels) != 0 {
var diags diag.Diagnostics
labels, diags = types.MapValueFrom(ctx, types.StringType, *networkInterfaceResp.Labels)
if diags.HasError() {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
}
} else if model.Labels.IsNull() {
labels = types.MapNull(types.StringType)
} }
networkInterfaceName := types.StringNull() networkInterfaceName := types.StringNull()

View file

@ -8,7 +8,6 @@ import (
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils" iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema"
@ -339,18 +338,9 @@ func mapFields(ctx context.Context, publicIpResp *iaas.PublicIp, model *Model) e
strings.Join(idParts, core.Separator), strings.Join(idParts, core.Separator),
) )
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{}) labels, err := iaasUtils.MapLabels(ctx, publicIpResp.Labels, model.Labels)
if diags.HasError() { if err != nil {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags)) return err
}
if publicIpResp.Labels != nil && len(*publicIpResp.Labels) != 0 {
var diags diag.Diagnostics
labels, diags = types.MapValueFrom(ctx, types.StringType, *publicIpResp.Labels)
if diags.HasError() {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
}
} else if model.Labels.IsNull() {
labels = types.MapNull(types.StringType)
} }
model.PublicIpId = types.StringValue(publicIpId) model.PublicIpId = types.StringValue(publicIpId)

View file

@ -10,7 +10,6 @@ import (
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils" iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema"
@ -358,18 +357,9 @@ func mapFields(ctx context.Context, securityGroupResp *iaas.SecurityGroup, model
strings.Join(idParts, core.Separator), strings.Join(idParts, core.Separator),
) )
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{}) labels, err := iaasUtils.MapLabels(ctx, securityGroupResp.Labels, model.Labels)
if diags.HasError() { if err != nil {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags)) return err
}
if securityGroupResp.Labels != nil && len(*securityGroupResp.Labels) != 0 {
var diags diag.Diagnostics
labels, diags = types.MapValueFrom(ctx, types.StringType, *securityGroupResp.Labels)
if diags.HasError() {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
}
} else if model.Labels.IsNull() {
labels = types.MapNull(types.StringType)
} }
model.SecurityGroupId = types.StringValue(securityGroupId) model.SecurityGroupId = types.StringValue(securityGroupId)

View file

@ -13,7 +13,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes" "github.com/hashicorp/terraform-plugin-framework/types/basetypes"
@ -248,19 +247,11 @@ func mapDataSourceFields(ctx context.Context, serverResp *iaas.Server, model *Da
strings.Join(idParts, core.Separator), strings.Join(idParts, core.Separator),
) )
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{}) labels, err := iaasUtils.MapLabels(ctx, serverResp.Labels, model.Labels)
if diags.HasError() { if err != nil {
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags)) return err
}
if serverResp.Labels != nil && len(*serverResp.Labels) != 0 {
var diags diag.Diagnostics
labels, diags = types.MapValueFrom(ctx, types.StringType, *serverResp.Labels)
if diags.HasError() {
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags))
}
} else if model.Labels.IsNull() {
labels = types.MapNull(types.StringType)
} }
var createdAt basetypes.StringValue var createdAt basetypes.StringValue
if serverResp.CreatedAt != nil { if serverResp.CreatedAt != nil {
createdAtValue := *serverResp.CreatedAt createdAtValue := *serverResp.CreatedAt

View file

@ -15,7 +15,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator" "github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema"
@ -830,19 +829,11 @@ func mapFields(ctx context.Context, serverResp *iaas.Server, model *Model) error
strings.Join(idParts, core.Separator), strings.Join(idParts, core.Separator),
) )
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{}) labels, err := iaasUtils.MapLabels(ctx, serverResp.Labels, model.Labels)
if diags.HasError() { if err != nil {
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags)) return err
}
if serverResp.Labels != nil && len(*serverResp.Labels) != 0 {
var diags diag.Diagnostics
labels, diags = types.MapValueFrom(ctx, types.StringType, *serverResp.Labels)
if diags.HasError() {
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags))
}
} else if model.Labels.IsNull() {
labels = types.MapNull(types.StringType)
} }
var createdAt basetypes.StringValue var createdAt basetypes.StringValue
if serverResp.CreatedAt != nil { if serverResp.CreatedAt != nil {
createdAtValue := *serverResp.CreatedAt createdAtValue := *serverResp.CreatedAt

View file

@ -4,6 +4,9 @@ import (
"context" "context"
"fmt" "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/hashicorp/terraform-plugin-framework/diag"
"github.com/stackitcloud/stackit-sdk-go/core/config" "github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/services/iaas" "github.com/stackitcloud/stackit-sdk-go/services/iaas"
@ -29,3 +32,22 @@ func ConfigureClient(ctx context.Context, providerData *core.ProviderData, diags
return apiClient 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
}

View file

@ -6,6 +6,10 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/diag"
sdkClients "github.com/stackitcloud/stackit-sdk-go/core/clients" sdkClients "github.com/stackitcloud/stackit-sdk-go/core/clients"
"github.com/stackitcloud/stackit-sdk-go/core/config" "github.com/stackitcloud/stackit-sdk-go/core/config"
@ -92,3 +96,84 @@ func TestConfigureClient(t *testing.T) {
}) })
} }
} }
func TestMapLabels(t *testing.T) {
type args struct {
responseLabels *map[string]interface{}
currentLabels types.Map
}
tests := []struct {
name string
args args
want basetypes.MapValue
wantErr bool
}{
{
name: "response labels is set",
args: args{
responseLabels: &map[string]interface{}{
"foo1": "bar1",
"foo2": "bar2",
},
currentLabels: types.MapUnknown(types.StringType),
},
wantErr: false,
want: types.MapValueMust(types.StringType, map[string]attr.Value{
"foo1": types.StringValue("bar1"),
"foo2": types.StringValue("bar2"),
}),
},
{
name: "response labels is set but empty",
args: args{
responseLabels: &map[string]interface{}{},
currentLabels: types.MapUnknown(types.StringType),
},
wantErr: false,
want: types.MapValueMust(types.StringType, map[string]attr.Value{}),
},
{
name: "response labels is nil and model labels is nil",
args: args{
responseLabels: nil,
currentLabels: types.MapNull(types.StringType),
},
wantErr: false,
want: types.MapNull(types.StringType),
},
{
name: "response labels is nil and model labels is set",
args: args{
responseLabels: nil,
currentLabels: types.MapValueMust(types.StringType, map[string]attr.Value{
"foo1": types.StringValue("bar1"),
"foo2": types.StringValue("bar2"),
}),
},
wantErr: false,
want: types.MapValueMust(types.StringType, map[string]attr.Value{}),
},
{
name: "response labels is nil and model labels is set but empty",
args: args{
responseLabels: nil,
currentLabels: types.MapValueMust(types.StringType, map[string]attr.Value{}),
},
wantErr: false,
want: types.MapValueMust(types.StringType, map[string]attr.Value{}),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
got, err := MapLabels(ctx, tt.args.responseLabels, tt.args.currentLabels)
if (err != nil) != tt.wantErr {
t.Errorf("MapLabels() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MapLabels() got = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -521,18 +521,9 @@ func mapFields(ctx context.Context, volumeResp *iaas.Volume, model *Model) error
strings.Join(idParts, core.Separator), strings.Join(idParts, core.Separator),
) )
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{}) labels, err := iaasUtils.MapLabels(ctx, volumeResp.Labels, model.Labels)
if diags.HasError() { if err != nil {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags)) return err
}
if volumeResp.Labels != nil && len(*volumeResp.Labels) != 0 {
var diags diag.Diagnostics
labels, diags = types.MapValueFrom(ctx, types.StringType, *volumeResp.Labels)
if diags.HasError() {
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
}
} else if model.Labels.IsNull() {
labels = types.MapNull(types.StringType)
} }
var sourceValues map[string]attr.Value var sourceValues map[string]attr.Value
@ -544,6 +535,7 @@ func mapFields(ctx context.Context, volumeResp *iaas.Volume, model *Model) error
"type": types.StringPointerValue(volumeResp.Source.Type), "type": types.StringPointerValue(volumeResp.Source.Type),
"id": types.StringPointerValue(volumeResp.Source.Id), "id": types.StringPointerValue(volumeResp.Source.Id),
} }
var diags diag.Diagnostics
sourceObject, diags = types.ObjectValue(sourceTypes, sourceValues) sourceObject, diags = types.ObjectValue(sourceTypes, sourceValues)
if diags.HasError() { if diags.HasError() {
return fmt.Errorf("creating source: %w", core.DiagsToError(diags)) return fmt.Errorf("creating source: %w", core.DiagsToError(diags))