Skip to content

Commit 7b3f17f

Browse files
authored
Made generated base paths get calculated with a BaseUrl function instead of inline (GoogleCloudPlatform#17245)
1 parent d12fee4 commit 7b3f17f

8 files changed

Lines changed: 171 additions & 45 deletions

File tree

mmv1/provider/terraform_tgc_next.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,20 +241,21 @@ func (tgc TerraformGoogleConversionNext) CopyCommonFiles(outputFolder string, ge
241241

242242
resourceConverters := map[string]string{
243243
// common
244+
"pkg/transport/base_url.go": "third_party/terraform/transport/base_url.go",
244245
"pkg/transport/batcher.go": "third_party/terraform/transport/batcher.go",
246+
"pkg/transport/bigtable_client_factory.go": "third_party/terraform/transport/bigtable_client_factory.go",
247+
"pkg/transport/error_retry_predicates.go": "third_party/terraform/transport/error_retry_predicates.go",
248+
"pkg/transport/header_transport.go": "third_party/terraform/transport/header_transport.go",
249+
"pkg/transport/mtls_util.go": "third_party/terraform/transport/mtls_util.go",
245250
"pkg/transport/retry_transport.go": "third_party/terraform/transport/retry_transport.go",
246251
"pkg/transport/retry_utils.go": "third_party/terraform/transport/retry_utils.go",
247-
"pkg/transport/header_transport.go": "third_party/terraform/transport/header_transport.go",
248-
"pkg/transport/error_retry_predicates.go": "third_party/terraform/transport/error_retry_predicates.go",
249-
"pkg/transport/bigtable_client_factory.go": "third_party/terraform/transport/bigtable_client_factory.go",
250252
"pkg/transport/transport.go": "third_party/terraform/transport/transport.go",
251253
"pkg/tpgresource/utils.go": "third_party/terraform/tpgresource/utils.go",
252254
"pkg/tpgresource/self_link_helpers.go": "third_party/terraform/tpgresource/self_link_helpers.go",
253255
"pkg/tpgresource/hashcode.go": "third_party/terraform/tpgresource/hashcode.go",
254256
"pkg/tpgresource/regional_utils.go": "third_party/terraform/tpgresource/regional_utils.go",
255257
"pkg/tpgresource/field_helpers.go": "third_party/terraform/tpgresource/field_helpers.go",
256258
"pkg/tpgresource/service_scope.go": "third_party/terraform/tpgresource/service_scope.go",
257-
"pkg/provider/mtls_util.go": "third_party/terraform/provider/mtls_util.go",
258259
"pkg/verify/validation.go": "third_party/terraform/verify/validation.go",
259260
"pkg/verify/path_or_contents.go": "third_party/terraform/verify/path_or_contents.go",
260261
"pkg/version/version.go": "third_party/terraform/version/version.go",

mmv1/third_party/terraform/provider/provider.go.tmpl

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -329,34 +329,27 @@ func ProviderConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr
329329
})
330330
}
331331

332-
// The mtls service client gives the type of endpoint (mtls/regular)
333-
// at client creation. Since we use a shared client for requests we must
334-
// rewrite the endpoints to be mtls endpoints for the scenario where
335-
// mtls is enabled.
336-
if isMtls() {
337-
// if mtls is enabled switch all default endpoints to use the mtls endpoint
338-
for key, bp := range transport_tpg.DefaultBasePaths {
339-
transport_tpg.DefaultBasePaths[key] = getMtlsEndpoint(bp)
332+
config.CustomEndpoints = map[string]string{}
333+
for _, p := range registry.ListProducts() {
334+
customEndpoint := ""
335+
if v := d.Get(p.CustomEndpointField).(string); v != "" {
336+
customEndpoint = v
337+
} else if v = transport_tpg.MultiEnvSearch([]string{p.CustomEndpointEnvVar}); v != "" {
338+
customEndpoint = v
339+
}
340+
if customEndpoint != "" {
341+
config.CustomEndpoints[p.CustomEndpointField] = customEndpoint
340342
}
341343
}
344+
345+
// Detect whether this is running in an mTLS context.
346+
config.IsMtls = transport_tpg.IsMtls()
342347

343348
// Set the universe domain to the configured value, if any
344349
if v, ok := d.GetOk("universe_domain"); ok {
345350
config.UniverseDomain = v.(string)
346351
}
347352

348-
// Replace hostname by the universe_domain field.
349-
if config.UniverseDomain != "" && config.UniverseDomain != "googleapis.com" {
350-
for key, basePath := range transport_tpg.DefaultBasePaths {
351-
transport_tpg.DefaultBasePaths[key] = strings.ReplaceAll(basePath, "googleapis.com", config.UniverseDomain)
352-
}
353-
}
354-
355-
err = transport_tpg.SetEndpointDefaults(d)
356-
if err != nil {
357-
return nil, diag.FromErr(err)
358-
}
359-
360353
// Given that impersonate_service_account is a secondary auth method, it has
361354
// no conflicts to worry about. We pull the env var in a DefaultFunc.
362355
if v, ok := d.GetOk("impersonate_service_account"); ok {
@@ -405,14 +398,36 @@ func ProviderConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr
405398
}
406399
config.BatchingConfig = batchCfg
407400

408-
// Generated products
401+
// Registered products
409402
{{- range $product := $.Products }}
410-
config.{{ $product.Name }}BasePath = d.Get("{{ underscore $product.Name }}_custom_endpoint").(string)
403+
config.{{ $product.Name }}BasePath = transport_tpg.BaseUrl(registry.GetProduct("{{ lower $product.Name }}"), &config)
411404
{{- if $product.Version.RepEnabled }}
412-
config.{{ $product.Name }}RepBasePath = "{{ $product.Version.RepUrl }}"
405+
config.{{ $product.Name }}RepBasePath = registry.GetProduct("{{ lower $product.Name }}").RepUrl
413406
{{- end -}}
414407
{{- end }}
415408

409+
// Legacy logic for non-registered products
410+
// The mtls service client gives the type of endpoint (mtls/regular)
411+
// at client creation. Since we use a shared client for requests we must
412+
// rewrite the endpoints to be mtls endpoints for the scenario where
413+
// mtls is enabled.
414+
if config.IsMtls {
415+
// if mtls is enabled switch all default endpoints to use the mtls endpoint
416+
for key, bp := range transport_tpg.DefaultBasePaths {
417+
transport_tpg.DefaultBasePaths[key] = transport_tpg.GetMtlsEndpoint(bp)
418+
}
419+
}
420+
// Replace hostname by the universe_domain field.
421+
if config.UniverseDomain != "" && config.UniverseDomain != "googleapis.com" {
422+
for key, basePath := range transport_tpg.DefaultBasePaths {
423+
transport_tpg.DefaultBasePaths[key] = strings.ReplaceAll(basePath, "googleapis.com", config.UniverseDomain)
424+
}
425+
}
426+
err = transport_tpg.SetEndpointDefaults(d)
427+
if err != nil {
428+
return nil, diag.FromErr(err)
429+
}
430+
416431
// Handwritten Products / Versioned / Atypical Entries
417432
config.DataflowBasePath = d.Get(transport_tpg.DataflowCustomEndpointEntryKey).(string)
418433
config.IamCredentialsBasePath = d.Get(transport_tpg.IamCredentialsCustomEndpointEntryKey).(string)

mmv1/third_party/terraform/registry/registry.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ func (p Product) Register() {
3535
products.m[p.Name] = p
3636
}
3737

38+
// Product returns the product information for the given product name. The function panics
39+
// if the requested product is not registered. This function is called during provider
40+
// intitialization when the absence of a product is an unrecoverable error.
41+
func GetProduct(name string) Product {
42+
products.RLock()
43+
defer products.RUnlock()
44+
p, ok := products.m[name]
45+
if !ok {
46+
log.Fatalf("No product %q registered", name)
47+
}
48+
return p
49+
}
50+
3851
func ListProducts() []Product {
3952
l := slices.Collect(maps.Values(products.m))
4053
slices.SortFunc(l, func(a, b Product) int {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package transport
2+
3+
import (
4+
"strings"
5+
6+
"github.com/hashicorp/terraform-provider-google/google/registry"
7+
)
8+
9+
// Returns the base URL for a product taking into account the following rules:
10+
// 1. If there is a custom endpoint set, return that immediately.
11+
// 2. Otherwise, start with the base URL for the product.
12+
// 2. If mTLS is active, make necessary adjustments.
13+
// 3. If universe_domain is active, make necessary adjustments.
14+
// 4. Return final URL.
15+
func BaseUrl(product registry.Product, config *Config) string {
16+
if v := config.CustomEndpoints[product.CustomEndpointField]; v != "" {
17+
return v
18+
}
19+
20+
path := product.BaseUrl
21+
if config.IsMtls {
22+
path = GetMtlsEndpoint(product.BaseUrl)
23+
}
24+
if config.UniverseDomain != "" && config.UniverseDomain != "googleapis.com" {
25+
path = strings.ReplaceAll(path, "googleapis.com", config.UniverseDomain)
26+
}
27+
return path
28+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package transport_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-provider-google/google/registry"
7+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
8+
)
9+
10+
func TestBaseUrl(t *testing.T) {
11+
computeProduct := registry.Product{
12+
Name: "compute",
13+
BaseUrl: "https://compute.googleapis.com/compute/beta/",
14+
CustomEndpointField: "compute_custom_endpoint",
15+
CustomEndpointEnvVar: "GOOGLE_COMPUTE_CUSTOM_ENDPOINT",
16+
}
17+
cases := []struct {
18+
name string
19+
product registry.Product
20+
config *transport_tpg.Config
21+
want string
22+
}{
23+
{
24+
name: "product BaseUrl",
25+
product: computeProduct,
26+
config: &transport_tpg.Config{},
27+
want: computeProduct.BaseUrl,
28+
},
29+
{
30+
name: "IsMtls",
31+
product: computeProduct,
32+
config: &transport_tpg.Config{
33+
IsMtls: true,
34+
},
35+
want: "https://compute.mtls.googleapis.com/compute/beta/",
36+
},
37+
{
38+
name: "UniverseDomain",
39+
product: computeProduct,
40+
config: &transport_tpg.Config{
41+
UniverseDomain: "fakedomain.test",
42+
},
43+
want: "https://compute.fakedomain.test/compute/beta/",
44+
},
45+
{
46+
name: "UniverseDomain and IsMtls",
47+
product: computeProduct,
48+
config: &transport_tpg.Config{
49+
UniverseDomain: "fakedomain.test",
50+
IsMtls: true,
51+
},
52+
want: "https://compute.mtls.fakedomain.test/compute/beta/",
53+
},
54+
{
55+
name: "CustomEndpoint",
56+
product: computeProduct,
57+
config: &transport_tpg.Config{
58+
CustomEndpoints: map[string]string{
59+
computeProduct.CustomEndpointField: "https://sandbox.compute.google.com/beta/",
60+
},
61+
UniverseDomain: "fakedomain.test",
62+
IsMtls: true,
63+
},
64+
want: "https://sandbox.compute.google.com/beta/",
65+
},
66+
}
67+
68+
for _, tc := range cases {
69+
t.Run(tc.name, func(t *testing.T) {
70+
baseUrl := transport_tpg.BaseUrl(tc.product, tc.config)
71+
72+
if baseUrl != tc.want {
73+
t.Fatalf("want %s, got %s", tc.want, baseUrl)
74+
}
75+
})
76+
}
77+
}

mmv1/third_party/terraform/transport/config.go.tmpl

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ type Config struct {
243243
Region string
244244
BillingProject string
245245
Zone string
246+
IsMtls bool
246247
UniverseDomain string
247248
Scopes []string
248249
BatchingConfig *BatchingConfig
@@ -263,6 +264,7 @@ type Config struct {
263264

264265
TokenSource oauth2.TokenSource
265266

267+
CustomEndpoints map[string]string
266268
{{ range $product := $.Products }}
267269
{{ $product.Name }}BasePath string
268270
{{- if ne $product.Version.RepUrl "" }}
@@ -406,16 +408,8 @@ func HandleSDKDefaults(d *schema.ResourceData) error {
406408
return nil
407409
}
408410

411+
// Legacy logic for non-registered products.
409412
func SetEndpointDefaults(d *schema.ResourceData) error {
410-
// Generated Products
411-
{{- range $product := $.Products }}
412-
if d.Get("{{ underscore $product.Name }}_custom_endpoint") == "" {
413-
d.Set("{{ underscore $product.Name }}_custom_endpoint", MultiEnvDefault([]string{
414-
"GOOGLE_{{ upper (underscore $product.Name) }}_CUSTOM_ENDPOINT",
415-
}, DefaultBasePaths[{{ $product.Name }}BasePathKey]))
416-
}
417-
{{- end }}
418-
419413
if d.Get(DataflowCustomEndpointEntryKey) == "" {
420414
d.Set(DataflowCustomEndpointEntryKey, MultiEnvDefault([]string{
421415
"GOOGLE_DATAFLOW_CUSTOM_ENDPOINT",

mmv1/third_party/terraform/provider/mtls_util.go renamed to mmv1/third_party/terraform/transport/mtls_util.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package provider
1+
package transport
22

33
import (
44
"context"
@@ -14,9 +14,9 @@ import (
1414
// the user is within mtls mode or not. They do return the mtls endpoint if
1515
// it is enabled during client creation so we will use this logic to determine
1616
// the mode the user is in and throw away the client they give us back.
17-
func isMtls() bool {
17+
func IsMtls() bool {
1818
regularEndpoint := "https://mockservice.googleapis.com/v1/"
19-
mtlsEndpoint := getMtlsEndpoint(regularEndpoint)
19+
mtlsEndpoint := GetMtlsEndpoint(regularEndpoint)
2020
_, endpoint, err := transport.NewHTTPClient(context.Background(),
2121
internaloption.WithDefaultEndpoint(regularEndpoint),
2222
internaloption.WithDefaultMTLSEndpoint(mtlsEndpoint),
@@ -28,7 +28,7 @@ func isMtls() bool {
2828
return isMtls
2929
}
3030

31-
func getMtlsEndpoint(baseEndpoint string) string {
31+
func GetMtlsEndpoint(baseEndpoint string) string {
3232
u, err := url.Parse(baseEndpoint)
3333
if err != nil {
3434
if strings.Contains(baseEndpoint, ".googleapis") {

mmv1/third_party/terraform/provider/mtls_util_test.go renamed to mmv1/third_party/terraform/transport/mtls_util_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
package provider
1+
package transport
22

33
import (
44
"strings"
55
"testing"
6-
7-
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
86
)
97

108
func TestUnitMtls_urlSwitching(t *testing.T) {
119
t.Parallel()
12-
for key, bp := range transport_tpg.DefaultBasePaths {
13-
url := getMtlsEndpoint(bp)
10+
for key, bp := range DefaultBasePaths {
11+
url := GetMtlsEndpoint(bp)
1412
if !strings.Contains(url, ".mtls.") {
1513
t.Errorf("%s: mtls conversion unsuccessful preconv - %s postconv - %s", key, bp, url)
1614
}

0 commit comments

Comments
 (0)