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
|
|
@ -332,18 +332,9 @@ func mapDataSourceFields(ctx context.Context, imageResp *iaas.Image, model *Data
|
|||
}
|
||||
|
||||
// Map labels
|
||||
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags))
|
||||
}
|
||||
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)
|
||||
labels, err := iaasUtils.MapLabels(ctx, imageResp.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
model.ImageId = types.StringValue(imageId)
|
||||
|
|
|
|||
|
|
@ -670,18 +670,9 @@ func mapFields(ctx context.Context, imageResp *iaas.Image, model *Model) error {
|
|||
}
|
||||
|
||||
// Map labels
|
||||
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags))
|
||||
}
|
||||
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)
|
||||
labels, err := iaasUtils.MapLabels(ctx, imageResp.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
model.ImageId = types.StringValue(imageId)
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import (
|
|||
|
||||
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/resource"
|
||||
"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.Fingerprint = types.StringPointerValue(keyPairResp.Fingerprint)
|
||||
|
||||
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
|
||||
var err error
|
||||
model.Labels, err = iaasUtils.MapLabels(ctx, keyPairResp.Labels, model.Labels)
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"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/types"
|
||||
"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),
|
||||
)
|
||||
|
||||
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
|
||||
}
|
||||
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)
|
||||
labels, err := iaasUtils.MapLabels(ctx, networkResp.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// IPv4
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
||||
"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/resource"
|
||||
"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),
|
||||
)
|
||||
|
||||
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
|
||||
}
|
||||
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)
|
||||
labels, err := iaasUtils.MapLabels(ctx, networkResp.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// IPv4
|
||||
|
|
|
|||
|
|
@ -23,12 +23,12 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"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/wait"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
|
||||
"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"
|
||||
)
|
||||
|
||||
|
|
@ -489,12 +489,12 @@ func mapFields(ctx context.Context, networkAreaResp *iaas.NetworkArea, networkAr
|
|||
model.DefaultNameservers = types.ListNull(types.StringType)
|
||||
} else {
|
||||
respDefaultNameservers := *networkAreaResp.Ipv4.DefaultNameservers
|
||||
modelDefaultNameservers, err := internalUtils.ListValuetoStringSlice(model.DefaultNameservers)
|
||||
modelDefaultNameservers, err := utils.ListValuetoStringSlice(model.DefaultNameservers)
|
||||
if err != nil {
|
||||
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)
|
||||
if diags.HasError() {
|
||||
|
|
@ -509,18 +509,9 @@ func mapFields(ctx context.Context, networkAreaResp *iaas.NetworkArea, networkAr
|
|||
return fmt.Errorf("mapping network ranges: %w", err)
|
||||
}
|
||||
|
||||
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
|
||||
}
|
||||
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)
|
||||
labels, err := iaasUtils.MapLabels(ctx, networkAreaResp.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
model.NetworkAreaId = types.StringValue(networkAreaId)
|
||||
|
|
@ -567,7 +558,7 @@ func mapNetworkRanges(ctx context.Context, networkAreaRangesList *[]iaas.Network
|
|||
apiNetworkRangePrefixes = append(apiNetworkRangePrefixes, *n.Prefix)
|
||||
}
|
||||
|
||||
reconciledRangePrefixes := internalUtils.ReconcileStringSlices(modelNetworkRangePrefixes, apiNetworkRangePrefixes)
|
||||
reconciledRangePrefixes := utils.ReconcileStringSlices(modelNetworkRangePrefixes, apiNetworkRangePrefixes)
|
||||
|
||||
networkRangesList := []attr.Value{}
|
||||
for i, prefix := range reconciledRangePrefixes {
|
||||
|
|
@ -748,7 +739,7 @@ func updateNetworkRanges(ctx context.Context, organizationId, networkAreaId stri
|
|||
payload := iaas.CreateNetworkAreaRangePayload{
|
||||
Ipv4: &[]iaas.NetworkRange{
|
||||
{
|
||||
Prefix: utils.Ptr(prefix),
|
||||
Prefix: sdkUtils.Ptr(prefix),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import (
|
|||
|
||||
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/resource"
|
||||
"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),
|
||||
)
|
||||
|
||||
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
|
||||
}
|
||||
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)
|
||||
labels, err := iaasUtils.MapLabels(ctx, networkAreaRoute.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
model.NetworkAreaRouteId = types.StringValue(networkAreaRouteId)
|
||||
|
|
|
|||
|
|
@ -484,18 +484,9 @@ func mapFields(ctx context.Context, networkInterfaceResp *iaas.NIC, model *Model
|
|||
model.SecurityGroupIds = securityGroupsTF
|
||||
}
|
||||
|
||||
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
|
||||
}
|
||||
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)
|
||||
labels, err := iaasUtils.MapLabels(ctx, networkInterfaceResp.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
networkInterfaceName := types.StringNull()
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import (
|
|||
|
||||
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/resource"
|
||||
"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),
|
||||
)
|
||||
|
||||
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
|
||||
}
|
||||
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)
|
||||
labels, err := iaasUtils.MapLabels(ctx, publicIpResp.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
model.PublicIpId = types.StringValue(publicIpId)
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import (
|
|||
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/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"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),
|
||||
)
|
||||
|
||||
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
|
||||
}
|
||||
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)
|
||||
labels, err := iaasUtils.MapLabels(ctx, securityGroupResp.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
model.SecurityGroupId = types.StringValue(securityGroupId)
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"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/types"
|
||||
"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),
|
||||
)
|
||||
|
||||
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags))
|
||||
}
|
||||
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)
|
||||
labels, err := iaasUtils.MapLabels(ctx, serverResp.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var createdAt basetypes.StringValue
|
||||
if serverResp.CreatedAt != nil {
|
||||
createdAtValue := *serverResp.CreatedAt
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
||||
"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/resource"
|
||||
"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),
|
||||
)
|
||||
|
||||
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags))
|
||||
}
|
||||
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)
|
||||
labels, err := iaasUtils.MapLabels(ctx, serverResp.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var createdAt basetypes.StringValue
|
||||
if serverResp.CreatedAt != nil {
|
||||
createdAtValue := *serverResp.CreatedAt
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,10 @@ import (
|
|||
"reflect"
|
||||
"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"
|
||||
sdkClients "github.com/stackitcloud/stackit-sdk-go/core/clients"
|
||||
"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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -521,18 +521,9 @@ func mapFields(ctx context.Context, volumeResp *iaas.Volume, model *Model) error
|
|||
strings.Join(idParts, core.Separator),
|
||||
)
|
||||
|
||||
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
|
||||
}
|
||||
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)
|
||||
labels, err := iaasUtils.MapLabels(ctx, volumeResp.Labels, model.Labels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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),
|
||||
"id": types.StringPointerValue(volumeResp.Source.Id),
|
||||
}
|
||||
var diags diag.Diagnostics
|
||||
sourceObject, diags = types.ObjectValue(sourceTypes, sourceValues)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("creating source: %w", core.DiagsToError(diags))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue