Skip to content

Commit 2ea7325

Browse files
Make the PF provider configuration code process region selflinks in same way as the SDK provider (#9063) (#16100)
* Add test for `TestGetRegionFromRegionSelfLink` used in SDK provider * Add plugin framework equivalent for `TestGetRegionFromRegionSelfLink` * Update PF provider config so test case passes - behaviour now equivalent to SDK [upstream:ab3cf39d9609ee5e7e60cd6277132d0236fe0066] Signed-off-by: Modular Magician <[email protected]>
1 parent ff751a8 commit 2ea7325

File tree

4 files changed

+88
-10
lines changed

4 files changed

+88
-10
lines changed

.changelog/9063.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
provider: Fixed an issue where the plugin-framework implementation of the provider handled default region values that were self-links differently to the SDK implementation. This issue is not believed to have affected users because of downstream functions that turn self links into region names.
3+
```

google/fwtransport/framework_config.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net/http"
99
"os"
10+
"regexp"
1011
"strconv"
1112
"time"
1213

@@ -20,6 +21,7 @@ import (
2021
"github.com/hashicorp/go-cleanhttp"
2122
"github.com/hashicorp/terraform-plugin-framework/diag"
2223
"github.com/hashicorp/terraform-plugin-framework/types"
24+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
2325
"github.com/hashicorp/terraform-plugin-log/tflog"
2426
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
2527

@@ -287,7 +289,7 @@ func (p *FrameworkProviderConfig) LoadAndValidateFramework(ctx context.Context,
287289
p.Context = ctx
288290
p.BillingProject = data.BillingProject
289291
p.Project = data.Project
290-
p.Region = data.Region
292+
p.Region = GetRegionFromRegionSelfLink(data.Region)
291293
p.Scopes = data.Scopes
292294
p.Zone = data.Zone
293295
p.UserProjectOverride = data.UserProjectOverride
@@ -1654,3 +1656,16 @@ func GetBatchingConfig(ctx context.Context, data types.List, diags *diag.Diagnos
16541656

16551657
return bc
16561658
}
1659+
1660+
func GetRegionFromRegionSelfLink(selfLink basetypes.StringValue) basetypes.StringValue {
1661+
re := regexp.MustCompile("/compute/[a-zA-Z0-9]*/projects/[a-zA-Z0-9-]*/regions/([a-zA-Z0-9-]*)")
1662+
value := selfLink.String()
1663+
switch {
1664+
case re.MatchString(value):
1665+
if res := re.FindStringSubmatch(value); len(res) == 2 && res[1] != "" {
1666+
region := res[1]
1667+
return types.StringValue(region)
1668+
}
1669+
}
1670+
return selfLink
1671+
}

google/fwtransport/framework_config_test.go

+38-9
Original file line numberDiff line numberDiff line change
@@ -523,15 +523,13 @@ func TestFrameworkProvider_LoadAndValidateFramework_region(t *testing.T) {
523523
ExpectedDataModelValue: types.StringValue("region-from-config"),
524524
ExpectedConfigStructValue: types.StringValue("region-from-config"),
525525
},
526-
// This test currently fails - PF code doesn't behave like SDK code
527-
// TODO(SarahFrench) - address https://github.com/hashicorp/terraform-provider-google/issues/15714
528-
// "region values can be supplied as a self link": {
529-
// ConfigValues: fwmodels.ProviderModel{
530-
// Region: types.StringValue("https://www.googleapis.com/compute/v1/projects/my-project/regions/us-central1"),
531-
// },
532-
// ExpectedDataModelValue: types.StringValue("https://www.googleapis.com/compute/v1/projects/my-project/regions/us-central1"),
533-
// ExpectedConfigStructValue: types.StringValue("us-central1"),
534-
// },
526+
"region values can be supplied as a self link": {
527+
ConfigValues: fwmodels.ProviderModel{
528+
Region: types.StringValue("https://www.googleapis.com/compute/v1/projects/my-project/regions/us-central1"),
529+
},
530+
ExpectedDataModelValue: types.StringValue("https://www.googleapis.com/compute/v1/projects/my-project/regions/us-central1"),
531+
ExpectedConfigStructValue: types.StringValue("us-central1"),
532+
},
535533
"region value can be set by environment variable: GOOGLE_REGION is used": {
536534
ConfigValues: fwmodels.ProviderModel{
537535
Region: types.StringNull(),
@@ -1714,3 +1712,34 @@ func TestFrameworkProvider_LoadAndValidateFramework_batching(t *testing.T) {
17141712
})
17151713
}
17161714
}
1715+
1716+
func TestGetRegionFromRegionSelfLink(t *testing.T) {
1717+
cases := map[string]struct {
1718+
Input basetypes.StringValue
1719+
ExpectedOutput basetypes.StringValue
1720+
}{
1721+
"A short region name is returned unchanged": {
1722+
Input: types.StringValue("us-central1"),
1723+
ExpectedOutput: types.StringValue("us-central1"),
1724+
},
1725+
"A selflink is shortened to a region name": {
1726+
Input: types.StringValue("https://www.googleapis.com/compute/v1/projects/my-project/regions/us-central1"),
1727+
ExpectedOutput: types.StringValue("us-central1"),
1728+
},
1729+
"Logic is specific to region selflinks; zone selflinks are not shortened": {
1730+
Input: types.StringValue("https://www.googleapis.com/compute/v1/projects/my-project/zones/asia-east1-a"),
1731+
ExpectedOutput: types.StringValue("https://www.googleapis.com/compute/v1/projects/my-project/zones/asia-east1-a"),
1732+
},
1733+
}
1734+
1735+
for tn, tc := range cases {
1736+
t.Run(tn, func(t *testing.T) {
1737+
1738+
region := fwtransport.GetRegionFromRegionSelfLink(tc.Input)
1739+
1740+
if region != tc.ExpectedOutput {
1741+
t.Fatalf("want %s, got %s", region, tc.ExpectedOutput)
1742+
}
1743+
})
1744+
}
1745+
}

google/transport/config_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -702,3 +702,34 @@ func TestRemoveBasePathVersion(t *testing.T) {
702702
}
703703
}
704704
}
705+
706+
func TestGetRegionFromRegionSelfLink(t *testing.T) {
707+
cases := map[string]struct {
708+
Input string
709+
ExpectedOutput string
710+
}{
711+
"A short region name is returned unchanged": {
712+
Input: "us-central1",
713+
ExpectedOutput: "us-central1",
714+
},
715+
"A selflink is shortened to a region name": {
716+
Input: "https://www.googleapis.com/compute/v1/projects/my-project/regions/us-central1",
717+
ExpectedOutput: "us-central1",
718+
},
719+
"Logic is specific to region selflinks; zone selflinks are not shortened": {
720+
Input: "https://www.googleapis.com/compute/v1/projects/my-project/zones/asia-east1-a",
721+
ExpectedOutput: "https://www.googleapis.com/compute/v1/projects/my-project/zones/asia-east1-a",
722+
},
723+
}
724+
725+
for tn, tc := range cases {
726+
t.Run(tn, func(t *testing.T) {
727+
728+
region := transport_tpg.GetRegionFromRegionSelfLink(tc.Input)
729+
730+
if region != tc.ExpectedOutput {
731+
t.Fatalf("want %s, got %s", region, tc.ExpectedOutput)
732+
}
733+
})
734+
}
735+
}

0 commit comments

Comments
 (0)