66 "strings"
77
88 "github.com/hashicorp/go-cty/cty"
9+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
910 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1011 "github.com/scaleway/scaleway-sdk-go/scw"
1112 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
@@ -18,12 +19,25 @@ type terraformResourceData interface {
1819 GetOk (string ) (any , bool )
1920 Get (string ) any
2021 Id () string
22+ GetRawConfig () cty.Value
23+ GetRawConfigAt (valPath cty.Path ) (cty.Value , diag.Diagnostics )
2124}
2225
26+ var (
27+ _ terraformResourceData = (* schema .ResourceData )(nil )
28+ _ terraformResourceData = (* schema .ResourceDiff )(nil )
29+ )
30+
2331// ExtractZone will try to guess the zone from the following:
2432// - zone field of the resource data
2533// - default zone from config
2634func 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+ }
40+ }
2741 rawZone , exist := d .GetOk ("zone" )
2842 if exist {
2943 return scw .ParseZone (rawZone .(string ))
@@ -41,6 +55,12 @@ func ExtractZone(d terraformResourceData, m any) (scw.Zone, error) {
4155// - region field of the resource data
4256// - default region from config
4357func ExtractRegion (d terraformResourceData , m any ) (scw.Region , error ) {
58+ rawConfig , err := d .GetRawConfigAt (cty .GetAttrPath ("region" ))
59+ if err == nil && rawConfig .IsKnown () && ! rawConfig .IsNull () {
60+ if rawConfig .AsString () != "" {
61+ return scw .ParseRegion (rawConfig .AsString ())
62+ }
63+ }
4464 rawRegion , exist := d .GetOk ("region" )
4565 if exist {
4666 return scw .ParseRegion (rawRegion .(string ))
@@ -59,6 +79,12 @@ func ExtractRegion(d terraformResourceData, m any) (scw.Region, error) {
5979// - default region given in argument
6080// - default region from config
6181func ExtractRegionWithDefault (d terraformResourceData , m any , defaultRegion scw.Region ) (scw.Region , error ) {
82+ rawConfig , err := d .GetRawConfigAt (cty .GetAttrPath ("region" ))
83+ if err == nil && rawConfig .IsKnown () && ! rawConfig .IsNull () {
84+ if rawConfig .AsString () != "" {
85+ return scw .ParseRegion (rawConfig .AsString ())
86+ }
87+ }
6288 rawRegion , exist := d .GetOk ("region" )
6389 if exist {
6490 return scw .ParseRegion (rawRegion .(string ))
0 commit comments