Skip to content

Commit 7eab017

Browse files
Handle *BasePath replacement with BaseUrl func (#17385)
[upstream:17308d1635ed53dff7bccafc031eda5080924662] Signed-off-by: Modular Magician <[email protected]>
1 parent d06d83e commit 7eab017

5 files changed

Lines changed: 38 additions & 10 deletions

File tree

google-beta/fwresource/field_helpers.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/hashicorp/terraform-plugin-framework/diag"
2626
"github.com/hashicorp/terraform-plugin-framework/types"
2727
"github.com/hashicorp/terraform-plugin-testing/terraform"
28+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/registry"
2829
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
2930
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
3031
)
@@ -93,7 +94,7 @@ func ParseProjectFieldValueFramework(resourceType, fieldValue, projectSchemaFiel
9394

9495
// This function isn't a test of transport.go; instead, it is used as an alternative
9596
// to ReplaceVars inside tests.
96-
func ReplaceVarsForFrameworkTest(prov *transport_tpg.Config, rs *terraform.ResourceState, linkTmpl string) (string, error) {
97+
func ReplaceVarsForFrameworkTest(config *transport_tpg.Config, rs *terraform.ResourceState, linkTmpl string) (string, error) {
9798
re := regexp.MustCompile("{{([[:word:]]+)}}")
9899
var project, region, zone string
99100

@@ -125,8 +126,13 @@ func ReplaceVarsForFrameworkTest(prov *transport_tpg.Config, rs *terraform.Resou
125126
return v
126127
}
127128

128-
// Attempt to draw values from the provider
129-
if f := reflect.Indirect(reflect.ValueOf(prov)).FieldByName(m); f.IsValid() {
129+
// Draw base path values from the provider config. For other config fields, fall back to reflection.
130+
if pName, found := strings.CutSuffix(m, "BasePath"); found {
131+
// the field will look like ComputeBasePath, but the product name will be like compute (just lowercase, no underscores)
132+
p := registry.GetProduct(strings.ToLower(pName))
133+
return transport_tpg.BaseUrl(p, config)
134+
}
135+
if f := reflect.Indirect(reflect.ValueOf(config)).FieldByName(m); f.IsValid() {
130136
return f.String()
131137
}
132138

google-beta/fwtransport/framework_utils.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737

3838
"github.com/hashicorp/terraform-provider-google-beta/google-beta/fwmodels"
3939
"github.com/hashicorp/terraform-provider-google-beta/google-beta/fwresource"
40+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/registry"
4041
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
4142
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
4243
"google.golang.org/api/googleapi"
@@ -372,7 +373,12 @@ func BuildReplacementFunc(ctx context.Context, re *regexp.Regexp, req interface{
372373

373374
// terraform-google-conversion doesn't provide a provider config in tests.
374375
if config != nil {
375-
// Attempt to draw values from the provider config if it's present.
376+
// Draw base path values from the provider config. For other config fields, fall back to reflection.
377+
if pName, found := strings.CutSuffix(m, "BasePath"); found {
378+
// the field will look like ComputeBasePath, but the product name will be like compute (just lowercase, no underscores)
379+
p := registry.GetProduct(strings.ToLower(pName))
380+
return transport_tpg.BaseUrl(p, config)
381+
}
376382
if f := reflect.Indirect(reflect.ValueOf(config)).FieldByName(m); f.IsValid() {
377383
return f.String()
378384
}

google-beta/tpgresource/resource_test_utils.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/hashicorp/go-cty/cty"
2828
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2929
"github.com/hashicorp/terraform-plugin-testing/terraform"
30+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/registry"
3031
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
3132
)
3233

@@ -196,7 +197,12 @@ func ReplaceVarsForTest(config *transport_tpg.Config, rs *terraform.ResourceStat
196197
return v
197198
}
198199

199-
// Attempt to draw values from the provider config
200+
// Draw base path values from the provider config. For other config fields, fall back to reflection.
201+
if pName, found := strings.CutSuffix(m, "BasePath"); found {
202+
// the field will look like ComputeBasePath, but the product name will be like compute (just lowercase, no underscores)
203+
p := registry.GetProduct(strings.ToLower(pName))
204+
return transport_tpg.BaseUrl(p, config)
205+
}
200206
if f := reflect.Indirect(reflect.ValueOf(config)).FieldByName(m); f.IsValid() {
201207
return f.String()
202208
}

google-beta/tpgresource/utils.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"strings"
3434
"time"
3535

36+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/registry"
3637
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
3738

3839
"github.com/hashicorp/errwrap"
@@ -825,7 +826,12 @@ func BuildReplacementFunc(re *regexp.Regexp, d TerraformResourceData, config *tr
825826

826827
// terraform-google-conversion doesn't provide a provider config in tests.
827828
if config != nil {
828-
// Attempt to draw values from the provider config if it's present.
829+
// Draw base path values from the provider config. For other config fields, fall back to reflection.
830+
if pName, found := strings.CutSuffix(m, "BasePath"); found {
831+
// the field will look like ComputeBasePath, but the product name will be like compute (just lowercase, no underscores)
832+
p := registry.GetProduct(strings.ToLower(pName))
833+
return transport_tpg.BaseUrl(p, config)
834+
}
829835
if f := reflect.Indirect(reflect.ValueOf(config)).FieldByName(m); f.IsValid() {
830836
return f.String()
831837
}

google-beta/tpgresource/utils_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/hashicorp/errwrap"
2626
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2727

28+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/cloudrun"
2829
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
2930
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
3031

@@ -33,6 +34,8 @@ import (
3334
"google.golang.org/grpc/status"
3435
)
3536

37+
var _ = cloudrun.Product
38+
3639
var fictionalSchema = map[string]*schema.Schema{
3740
"location": {
3841
Type: schema.TypeString,
@@ -1150,11 +1153,12 @@ func TestReplaceVars(t *testing.T) {
11501153
"base path recursive replacement": {
11511154
Template: "{{CloudRunBasePath}}namespaces/{{project}}/services",
11521155
Config: &transport_tpg.Config{
1153-
Project: "default-project",
1154-
Region: "default-region",
1155-
CloudRunBasePath: "https://{{region}}-run.googleapis.com/",
1156+
Project: "default-project",
1157+
},
1158+
SchemaValues: map[string]interface{}{
1159+
"location": "whatever",
11561160
},
1157-
Expected: "https://default-region-run.googleapis.com/namespaces/default-project/services",
1161+
Expected: "https://whatever-run.googleapis.com/namespaces/default-project/services",
11581162
},
11591163
}
11601164

0 commit comments

Comments
 (0)