Skip to content

Commit 56294dd

Browse files
committed
Factorize function
1 parent de93bdf commit 56294dd

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

internal/meta/extractors.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,23 @@ var (
2828
_ terraformResourceData = (*schema.ResourceDiff)(nil)
2929
)
3030

31+
func ExtractRawConfigString(d terraformResourceData, field string) (string, diag.Diagnostics) {
32+
rawConfig, diags := d.GetRawConfigAt(cty.GetAttrPath(field))
33+
if diags == nil && rawConfig.IsKnown() && !rawConfig.IsNull() {
34+
if rawConfig.AsString() != "" {
35+
return rawConfig.AsString(), nil
36+
}
37+
}
38+
return "", diags
39+
}
40+
3141
// ExtractZone will try to guess the zone from the following:
3242
// - zone field of the resource data
3343
// - default zone from config
3444
func ExtractZone(d terraformResourceData, m any) (scw.Zone, error) {
35-
rawConfig, err := d.GetRawConfigAt(cty.GetAttrPath("zone"))
36-
if err == nil && rawConfig.IsKnown() && !rawConfig.IsNull() {
37-
if rawConfig.AsString() != "" {
38-
return scw.ParseZone(rawConfig.AsString())
39-
}
45+
rawConfigZone, diags := ExtractRawConfigString(d, "zone")
46+
if diags == nil && rawConfigZone != "" {
47+
return scw.ParseZone(rawConfigZone)
4048
}
4149

4250
rawZone, exist := d.GetOk("zone")
@@ -56,11 +64,9 @@ func ExtractZone(d terraformResourceData, m any) (scw.Zone, error) {
5664
// - region field of the resource data
5765
// - default region from config
5866
func ExtractRegion(d terraformResourceData, m any) (scw.Region, error) {
59-
rawConfig, err := d.GetRawConfigAt(cty.GetAttrPath("region"))
60-
if err == nil && rawConfig.IsKnown() && !rawConfig.IsNull() {
61-
if rawConfig.AsString() != "" {
62-
return scw.ParseRegion(rawConfig.AsString())
63-
}
67+
rawConfigZone, diags := ExtractRawConfigString(d, "region")
68+
if diags == nil && rawConfigZone != "" {
69+
return scw.ParseRegion(rawConfigZone)
6470
}
6571

6672
rawRegion, exist := d.GetOk("region")
@@ -81,11 +87,9 @@ func ExtractRegion(d terraformResourceData, m any) (scw.Region, error) {
8187
// - default region given in argument
8288
// - default region from config
8389
func ExtractRegionWithDefault(d terraformResourceData, m any, defaultRegion scw.Region) (scw.Region, error) {
84-
rawConfig, err := d.GetRawConfigAt(cty.GetAttrPath("region"))
85-
if err == nil && rawConfig.IsKnown() && !rawConfig.IsNull() {
86-
if rawConfig.AsString() != "" {
87-
return scw.ParseRegion(rawConfig.AsString())
88-
}
90+
rawConfigZone, diags := ExtractRawConfigString(d, "region")
91+
if diags == nil && rawConfigZone != "" {
92+
return scw.ParseRegion(rawConfigZone)
8993
}
9094

9195
rawRegion, exist := d.GetOk("region")

internal/services/ipam/ips_data_source.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/scaleway/scaleway-sdk-go/scw"
1111
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1212
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
13+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
1314
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
1415
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1516
)
@@ -167,11 +168,9 @@ func DataSourceIPAMIPsRead(ctx context.Context, d *schema.ResourceData, m any) d
167168
OrganizationID: types.ExpandStringPtr(d.Get("organization_id")),
168169
}
169170

170-
rawConfig, diagError := d.GetRawConfigAt(cty.GetAttrPath("zonal"))
171-
if diagError == nil && rawConfig.IsKnown() && !rawConfig.IsNull() {
172-
if rawConfig.AsString() != "" {
173-
req.Zonal = types.ExpandStringPtr(rawConfig.AsString())
174-
}
171+
rawConfigZonal, diags := meta.ExtractRawConfigString(d, "zonal")
172+
if diags == nil && rawConfigZonal != "" {
173+
req.Zonal = types.ExpandStringPtr(rawConfigZonal)
175174
}
176175

177176
attached, attachedExists := d.GetOk("attached")

internal/services/k8s/pool.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"time"
77

8-
"github.com/hashicorp/go-cty/cty"
98
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
109
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1110
"github.com/scaleway/scaleway-sdk-go/api/k8s/v1"
@@ -15,6 +14,7 @@ import (
1514
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
1615
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1716
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
17+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
1818
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
1919
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
2020
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
@@ -287,18 +287,14 @@ func ResourceK8SPoolCreate(ctx context.Context, d *schema.ResourceData, m any) d
287287
PublicIPDisabled: d.Get("public_ip_disabled").(bool),
288288
}
289289

290-
rawConfig, diags := d.GetRawConfigAt(cty.GetAttrPath("region"))
291-
if diags == nil && rawConfig.IsKnown() && !rawConfig.IsNull() {
292-
if rawConfig.AsString() != "" {
293-
req.Region = scw.Region(rawConfig.AsString())
294-
}
290+
rawConfigRegion, diags := meta.ExtractRawConfigString(d, "region")
291+
if diags == nil && rawConfigRegion != "" {
292+
req.Region = scw.Region(rawConfigRegion)
295293
}
296294

297-
rawConfig, diags = d.GetRawConfigAt(cty.GetAttrPath("zone"))
298-
if diags == nil && rawConfig.IsKnown() && !rawConfig.IsNull() {
299-
if rawConfig.AsString() != "" {
300-
req.Zone = scw.Zone(rawConfig.AsString())
301-
}
295+
rawConfigZone, diags := meta.ExtractRawConfigString(d, "zone")
296+
if diags == nil && rawConfigZone != "" {
297+
req.Region = scw.Region(rawConfigRegion)
302298
}
303299

304300
if placementGroupID, ok := d.GetOk("placement_group_id"); ok {

0 commit comments

Comments
 (0)