diff --git a/mmv1/third_party/terraform/fwtransport/framework_config.go.erb b/mmv1/third_party/terraform/fwtransport/framework_config.go.erb index 8c510cbe155c..e8ddac51fe08 100644 --- a/mmv1/third_party/terraform/fwtransport/framework_config.go.erb +++ b/mmv1/third_party/terraform/fwtransport/framework_config.go.erb @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "os" + "regexp" "strconv" "time" @@ -19,6 +20,7 @@ import ( "github.com/hashicorp/go-cleanhttp" "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging" @@ -96,7 +98,7 @@ func (p *FrameworkProviderConfig) LoadAndValidateFramework(ctx context.Context, p.Context = ctx p.BillingProject = data.BillingProject p.Project = data.Project - p.Region = data.Region + p.Region = GetRegionFromRegionSelfLink(data.Region) p.Scopes = data.Scopes p.Zone = data.Zone p.UserProjectOverride = data.UserProjectOverride @@ -700,3 +702,16 @@ func GetBatchingConfig(ctx context.Context, data types.List, diags *diag.Diagnos return bc } + +func GetRegionFromRegionSelfLink(selfLink basetypes.StringValue) basetypes.StringValue { + re := regexp.MustCompile("/compute/[a-zA-Z0-9]*/projects/[a-zA-Z0-9-]*/regions/([a-zA-Z0-9-]*)") + value := selfLink.String() + switch { + case re.MatchString(value): + if res := re.FindStringSubmatch(value); len(res) == 2 && res[1] != "" { + region := res[1] + return types.StringValue(region) + } + } + return selfLink +} diff --git a/mmv1/third_party/terraform/fwtransport/framework_config_test.go.erb b/mmv1/third_party/terraform/fwtransport/framework_config_test.go.erb index 6a273183d4ad..e430390dc814 100644 --- a/mmv1/third_party/terraform/fwtransport/framework_config_test.go.erb +++ b/mmv1/third_party/terraform/fwtransport/framework_config_test.go.erb @@ -522,15 +522,13 @@ func TestFrameworkProvider_LoadAndValidateFramework_region(t *testing.T) { ExpectedDataModelValue: types.StringValue("region-from-config"), ExpectedConfigStructValue: types.StringValue("region-from-config"), }, - // This test currently fails - PF code doesn't behave like SDK code - // TODO(SarahFrench) - address https://github.com/hashicorp/terraform-provider-google/issues/15714 - // "region values can be supplied as a self link": { - // ConfigValues: fwmodels.ProviderModel{ - // Region: types.StringValue("https://www.googleapis.com/compute/v1/projects/my-project/regions/us-central1"), - // }, - // ExpectedDataModelValue: types.StringValue("https://www.googleapis.com/compute/v1/projects/my-project/regions/us-central1"), - // ExpectedConfigStructValue: types.StringValue("us-central1"), - // }, + "region values can be supplied as a self link": { + ConfigValues: fwmodels.ProviderModel{ + Region: types.StringValue("https://www.googleapis.com/compute/v1/projects/my-project/regions/us-central1"), + }, + ExpectedDataModelValue: types.StringValue("https://www.googleapis.com/compute/v1/projects/my-project/regions/us-central1"), + ExpectedConfigStructValue: types.StringValue("us-central1"), + }, "region value can be set by environment variable: GOOGLE_REGION is used": { ConfigValues: fwmodels.ProviderModel{ Region: types.StringNull(), @@ -1713,3 +1711,34 @@ func TestFrameworkProvider_LoadAndValidateFramework_batching(t *testing.T) { }) } } + +func TestGetRegionFromRegionSelfLink(t *testing.T) { + cases := map[string]struct { + Input basetypes.StringValue + ExpectedOutput basetypes.StringValue + }{ + "A short region name is returned unchanged": { + Input: types.StringValue("us-central1"), + ExpectedOutput: types.StringValue("us-central1"), + }, + "A selflink is shortened to a region name": { + Input: types.StringValue("https://www.googleapis.com/compute/v1/projects/my-project/regions/us-central1"), + ExpectedOutput: types.StringValue("us-central1"), + }, + "Logic is specific to region selflinks; zone selflinks are not shortened": { + Input: types.StringValue("https://www.googleapis.com/compute/v1/projects/my-project/zones/asia-east1-a"), + ExpectedOutput: types.StringValue("https://www.googleapis.com/compute/v1/projects/my-project/zones/asia-east1-a"), + }, + } + + for tn, tc := range cases { + t.Run(tn, func(t *testing.T) { + + region := fwtransport.GetRegionFromRegionSelfLink(tc.Input) + + if region != tc.ExpectedOutput { + t.Fatalf("want %s, got %s", region, tc.ExpectedOutput) + } + }) + } +} diff --git a/mmv1/third_party/terraform/transport/config_test.go b/mmv1/third_party/terraform/transport/config_test.go index 849b902672b4..90d458bb5a31 100644 --- a/mmv1/third_party/terraform/transport/config_test.go +++ b/mmv1/third_party/terraform/transport/config_test.go @@ -700,3 +700,34 @@ func TestRemoveBasePathVersion(t *testing.T) { } } } + +func TestGetRegionFromRegionSelfLink(t *testing.T) { + cases := map[string]struct { + Input string + ExpectedOutput string + }{ + "A short region name is returned unchanged": { + Input: "us-central1", + ExpectedOutput: "us-central1", + }, + "A selflink is shortened to a region name": { + Input: "https://www.googleapis.com/compute/v1/projects/my-project/regions/us-central1", + ExpectedOutput: "us-central1", + }, + "Logic is specific to region selflinks; zone selflinks are not shortened": { + Input: "https://www.googleapis.com/compute/v1/projects/my-project/zones/asia-east1-a", + ExpectedOutput: "https://www.googleapis.com/compute/v1/projects/my-project/zones/asia-east1-a", + }, + } + + for tn, tc := range cases { + t.Run(tn, func(t *testing.T) { + + region := transport_tpg.GetRegionFromRegionSelfLink(tc.Input) + + if region != tc.ExpectedOutput { + t.Fatalf("want %s, got %s", region, tc.ExpectedOutput) + } + }) + } +}