Skip to content

Commit 281d31f

Browse files
authored
chore(iaas): extract mapping of labels into util func (#867)
1 parent ad24ebe commit 281d31f

15 files changed

Lines changed: 156 additions & 171 deletions

File tree

stackit/internal/services/iaas/image/datasource.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -332,18 +332,9 @@ func mapDataSourceFields(ctx context.Context, imageResp *iaas.Image, model *Data
332332
}
333333

334334
// Map labels
335-
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
336-
if diags.HasError() {
337-
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags))
338-
}
339-
if imageResp.Labels != nil && len(*imageResp.Labels) != 0 {
340-
var diags diag.Diagnostics
341-
labels, diags = types.MapValueFrom(ctx, types.StringType, *imageResp.Labels)
342-
if diags.HasError() {
343-
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags))
344-
}
345-
} else if model.Labels.IsNull() {
346-
labels = types.MapNull(types.StringType)
335+
labels, err := iaasUtils.MapLabels(ctx, imageResp.Labels, model.Labels)
336+
if err != nil {
337+
return err
347338
}
348339

349340
model.ImageId = types.StringValue(imageId)

stackit/internal/services/iaas/image/resource.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -670,18 +670,9 @@ func mapFields(ctx context.Context, imageResp *iaas.Image, model *Model) error {
670670
}
671671

672672
// Map labels
673-
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
674-
if diags.HasError() {
675-
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags))
676-
}
677-
if imageResp.Labels != nil && len(*imageResp.Labels) != 0 {
678-
var diags diag.Diagnostics
679-
labels, diags = types.MapValueFrom(ctx, types.StringType, *imageResp.Labels)
680-
if diags.HasError() {
681-
return fmt.Errorf("convert labels to StringValue map: %w", core.DiagsToError(diags))
682-
}
683-
} else if model.Labels.IsNull() {
684-
labels = types.MapNull(types.StringType)
673+
labels, err := iaasUtils.MapLabels(ctx, imageResp.Labels, model.Labels)
674+
if err != nil {
675+
return err
685676
}
686677

687678
model.ImageId = types.StringValue(imageId)

stackit/internal/services/iaas/keypair/resource.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
1010

11-
"github.com/hashicorp/terraform-plugin-framework/diag"
1211
"github.com/hashicorp/terraform-plugin-framework/path"
1312
"github.com/hashicorp/terraform-plugin-framework/resource"
1413
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -326,20 +325,11 @@ func mapFields(ctx context.Context, keyPairResp *iaas.Keypair, model *Model) err
326325
model.PublicKey = types.StringPointerValue(keyPairResp.PublicKey)
327326
model.Fingerprint = types.StringPointerValue(keyPairResp.Fingerprint)
328327

329-
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
330-
if diags.HasError() {
331-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
332-
}
333-
if keyPairResp.Labels != nil && len(*keyPairResp.Labels) != 0 {
334-
var diags diag.Diagnostics
335-
labels, diags = types.MapValueFrom(ctx, types.StringType, *keyPairResp.Labels)
336-
if diags.HasError() {
337-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
338-
}
339-
} else if model.Labels.IsNull() {
340-
labels = types.MapNull(types.StringType)
328+
var err error
329+
model.Labels, err = iaasUtils.MapLabels(ctx, keyPairResp.Labels, model.Labels)
330+
if err != nil {
331+
return err
341332
}
342-
model.Labels = labels
343333

344334
return nil
345335
}

stackit/internal/services/iaas/network/datasource.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1414
"github.com/hashicorp/terraform-plugin-framework/datasource"
1515
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
16-
"github.com/hashicorp/terraform-plugin-framework/diag"
1716
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1817
"github.com/hashicorp/terraform-plugin-framework/types"
1918
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -254,18 +253,9 @@ func mapDataSourceFields(ctx context.Context, networkResp *iaas.Network, model *
254253
strings.Join(idParts, core.Separator),
255254
)
256255

257-
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
258-
if diags.HasError() {
259-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
260-
}
261-
if networkResp.Labels != nil && len(*networkResp.Labels) != 0 {
262-
var diags diag.Diagnostics
263-
labels, diags = types.MapValueFrom(ctx, types.StringType, *networkResp.Labels)
264-
if diags.HasError() {
265-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
266-
}
267-
} else if model.Labels.IsNull() {
268-
labels = types.MapNull(types.StringType)
256+
labels, err := iaasUtils.MapLabels(ctx, networkResp.Labels, model.Labels)
257+
if err != nil {
258+
return err
269259
}
270260

271261
// IPv4

stackit/internal/services/iaas/network/resource.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
1313
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1414
"github.com/hashicorp/terraform-plugin-framework/attr"
15-
"github.com/hashicorp/terraform-plugin-framework/diag"
1615
"github.com/hashicorp/terraform-plugin-framework/path"
1716
"github.com/hashicorp/terraform-plugin-framework/resource"
1817
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -512,18 +511,9 @@ func mapFields(ctx context.Context, networkResp *iaas.Network, model *Model) err
512511
strings.Join(idParts, core.Separator),
513512
)
514513

515-
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
516-
if diags.HasError() {
517-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
518-
}
519-
if networkResp.Labels != nil && len(*networkResp.Labels) != 0 {
520-
var diags diag.Diagnostics
521-
labels, diags = types.MapValueFrom(ctx, types.StringType, *networkResp.Labels)
522-
if diags.HasError() {
523-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
524-
}
525-
} else if model.Labels.IsNull() {
526-
labels = types.MapNull(types.StringType)
514+
labels, err := iaasUtils.MapLabels(ctx, networkResp.Labels, model.Labels)
515+
if err != nil {
516+
return err
527517
}
528518

529519
// IPv4

stackit/internal/services/iaas/networkarea/resource.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ import (
2323
"github.com/hashicorp/terraform-plugin-framework/types"
2424
"github.com/hashicorp/terraform-plugin-log/tflog"
2525
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
26-
"github.com/stackitcloud/stackit-sdk-go/core/utils"
26+
sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils"
2727
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
2828
"github.com/stackitcloud/stackit-sdk-go/services/iaas/wait"
2929
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
3030
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
31-
internalUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
31+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
3232
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
3333
)
3434

@@ -489,12 +489,12 @@ func mapFields(ctx context.Context, networkAreaResp *iaas.NetworkArea, networkAr
489489
model.DefaultNameservers = types.ListNull(types.StringType)
490490
} else {
491491
respDefaultNameservers := *networkAreaResp.Ipv4.DefaultNameservers
492-
modelDefaultNameservers, err := internalUtils.ListValuetoStringSlice(model.DefaultNameservers)
492+
modelDefaultNameservers, err := utils.ListValuetoStringSlice(model.DefaultNameservers)
493493
if err != nil {
494494
return fmt.Errorf("get current network area default nameservers from model: %w", err)
495495
}
496496

497-
reconciledDefaultNameservers := internalUtils.ReconcileStringSlices(modelDefaultNameservers, respDefaultNameservers)
497+
reconciledDefaultNameservers := utils.ReconcileStringSlices(modelDefaultNameservers, respDefaultNameservers)
498498

499499
defaultNameserversTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledDefaultNameservers)
500500
if diags.HasError() {
@@ -509,18 +509,9 @@ func mapFields(ctx context.Context, networkAreaResp *iaas.NetworkArea, networkAr
509509
return fmt.Errorf("mapping network ranges: %w", err)
510510
}
511511

512-
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
513-
if diags.HasError() {
514-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
515-
}
516-
if networkAreaResp.Labels != nil && len(*networkAreaResp.Labels) != 0 {
517-
var diags diag.Diagnostics
518-
labels, diags = types.MapValueFrom(ctx, types.StringType, *networkAreaResp.Labels)
519-
if diags.HasError() {
520-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
521-
}
522-
} else if model.Labels.IsNull() {
523-
labels = types.MapNull(types.StringType)
512+
labels, err := iaasUtils.MapLabels(ctx, networkAreaResp.Labels, model.Labels)
513+
if err != nil {
514+
return err
524515
}
525516

526517
model.NetworkAreaId = types.StringValue(networkAreaId)
@@ -567,7 +558,7 @@ func mapNetworkRanges(ctx context.Context, networkAreaRangesList *[]iaas.Network
567558
apiNetworkRangePrefixes = append(apiNetworkRangePrefixes, *n.Prefix)
568559
}
569560

570-
reconciledRangePrefixes := internalUtils.ReconcileStringSlices(modelNetworkRangePrefixes, apiNetworkRangePrefixes)
561+
reconciledRangePrefixes := utils.ReconcileStringSlices(modelNetworkRangePrefixes, apiNetworkRangePrefixes)
571562

572563
networkRangesList := []attr.Value{}
573564
for i, prefix := range reconciledRangePrefixes {
@@ -748,7 +739,7 @@ func updateNetworkRanges(ctx context.Context, organizationId, networkAreaId stri
748739
payload := iaas.CreateNetworkAreaRangePayload{
749740
Ipv4: &[]iaas.NetworkRange{
750741
{
751-
Prefix: utils.Ptr(prefix),
742+
Prefix: sdkUtils.Ptr(prefix),
752743
},
753744
},
754745
}

stackit/internal/services/iaas/networkarearoute/resource.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
1010

11-
"github.com/hashicorp/terraform-plugin-framework/diag"
1211
"github.com/hashicorp/terraform-plugin-framework/path"
1312
"github.com/hashicorp/terraform-plugin-framework/resource"
1413
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -380,18 +379,9 @@ func mapFields(ctx context.Context, networkAreaRoute *iaas.Route, model *Model)
380379
strings.Join(idParts, core.Separator),
381380
)
382381

383-
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
384-
if diags.HasError() {
385-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
386-
}
387-
if networkAreaRoute.Labels != nil && len(*networkAreaRoute.Labels) != 0 {
388-
var diags diag.Diagnostics
389-
labels, diags = types.MapValueFrom(ctx, types.StringType, *networkAreaRoute.Labels)
390-
if diags.HasError() {
391-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
392-
}
393-
} else if model.Labels.IsNull() {
394-
labels = types.MapNull(types.StringType)
382+
labels, err := iaasUtils.MapLabels(ctx, networkAreaRoute.Labels, model.Labels)
383+
if err != nil {
384+
return err
395385
}
396386

397387
model.NetworkAreaRouteId = types.StringValue(networkAreaRouteId)

stackit/internal/services/iaas/networkinterface/resource.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -484,18 +484,9 @@ func mapFields(ctx context.Context, networkInterfaceResp *iaas.NIC, model *Model
484484
model.SecurityGroupIds = securityGroupsTF
485485
}
486486

487-
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
488-
if diags.HasError() {
489-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
490-
}
491-
if networkInterfaceResp.Labels != nil && len(*networkInterfaceResp.Labels) != 0 {
492-
var diags diag.Diagnostics
493-
labels, diags = types.MapValueFrom(ctx, types.StringType, *networkInterfaceResp.Labels)
494-
if diags.HasError() {
495-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
496-
}
497-
} else if model.Labels.IsNull() {
498-
labels = types.MapNull(types.StringType)
487+
labels, err := iaasUtils.MapLabels(ctx, networkInterfaceResp.Labels, model.Labels)
488+
if err != nil {
489+
return err
499490
}
500491

501492
networkInterfaceName := types.StringNull()

stackit/internal/services/iaas/publicip/resource.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
1010

11-
"github.com/hashicorp/terraform-plugin-framework/diag"
1211
"github.com/hashicorp/terraform-plugin-framework/path"
1312
"github.com/hashicorp/terraform-plugin-framework/resource"
1413
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -339,18 +338,9 @@ func mapFields(ctx context.Context, publicIpResp *iaas.PublicIp, model *Model) e
339338
strings.Join(idParts, core.Separator),
340339
)
341340

342-
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
343-
if diags.HasError() {
344-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
345-
}
346-
if publicIpResp.Labels != nil && len(*publicIpResp.Labels) != 0 {
347-
var diags diag.Diagnostics
348-
labels, diags = types.MapValueFrom(ctx, types.StringType, *publicIpResp.Labels)
349-
if diags.HasError() {
350-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
351-
}
352-
} else if model.Labels.IsNull() {
353-
labels = types.MapNull(types.StringType)
341+
labels, err := iaasUtils.MapLabels(ctx, publicIpResp.Labels, model.Labels)
342+
if err != nil {
343+
return err
354344
}
355345

356346
model.PublicIpId = types.StringValue(publicIpId)

stackit/internal/services/iaas/securitygroup/resource.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
1111

1212
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
13-
"github.com/hashicorp/terraform-plugin-framework/diag"
1413
"github.com/hashicorp/terraform-plugin-framework/path"
1514
"github.com/hashicorp/terraform-plugin-framework/resource"
1615
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -358,18 +357,9 @@ func mapFields(ctx context.Context, securityGroupResp *iaas.SecurityGroup, model
358357
strings.Join(idParts, core.Separator),
359358
)
360359

361-
labels, diags := types.MapValueFrom(ctx, types.StringType, map[string]interface{}{})
362-
if diags.HasError() {
363-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
364-
}
365-
if securityGroupResp.Labels != nil && len(*securityGroupResp.Labels) != 0 {
366-
var diags diag.Diagnostics
367-
labels, diags = types.MapValueFrom(ctx, types.StringType, *securityGroupResp.Labels)
368-
if diags.HasError() {
369-
return fmt.Errorf("converting labels to StringValue map: %w", core.DiagsToError(diags))
370-
}
371-
} else if model.Labels.IsNull() {
372-
labels = types.MapNull(types.StringType)
360+
labels, err := iaasUtils.MapLabels(ctx, securityGroupResp.Labels, model.Labels)
361+
if err != nil {
362+
return err
373363
}
374364

375365
model.SecurityGroupId = types.StringValue(securityGroupId)

0 commit comments

Comments
 (0)