Skip to content

Commit 1d8e064

Browse files
committed
2 parents 95d39e0 + bb5071c commit 1d8e064

4 files changed

Lines changed: 44 additions & 23 deletions

File tree

mmv1/templates/terraform/product.go.tmpl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@ import (
2323
"{{ $.ImportPath }}/registry"
2424
)
2525

26-
const ProductName = "{{ lower $.Name }}"
26+
var Product = registry.Product{
27+
Name: "{{ lower $.Name }}",
28+
BaseUrl: "{{ $.Version.BaseUrl }}",
29+
{{- if $.Version.RepUrl }}
30+
RepUrl: "{{ $.Version.RepUrl }}",
31+
{{- end }}
32+
CustomEndpointField: "{{ underscore $.Name }}_custom_endpoint",
33+
CustomEndpointEnvVar: "GOOGLE_{{ upper (underscore $.Name) }}_CUSTOM_ENDPOINT",
34+
}
2735

2836
func init() {
29-
registry.Product{
30-
Name: "{{ lower $.Name }}",
31-
BaseUrl: "{{ $.Version.BaseUrl }}",
32-
}.Register()
37+
Product.Register()
3338
}

mmv1/third_party/terraform/fwprovider/framework_provider.go.tmpl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/hashicorp/terraform-provider-google/google/fwvalidators"
2222
"github.com/hashicorp/terraform-provider-google/google/functions"
2323
"github.com/hashicorp/terraform-provider-google/google/fwmodels"
24+
"github.com/hashicorp/terraform-provider-google/google/registry"
2425
"github.com/hashicorp/terraform-provider-google/google/services/resourcemanager"
2526
"github.com/hashicorp/terraform-provider-google/google/services/apigee"
2627
"github.com/hashicorp/terraform-provider-google/google/services/secretmanager"
@@ -179,15 +180,6 @@ func (p *FrameworkProvider) Schema(_ context.Context, _ provider.SchemaRequest,
179180
"prefer_regional_endpoints": schema.BoolAttribute{
180181
Optional: true,
181182
},
182-
// Generated Products
183-
{{- range $product := $.Products }}
184-
"{{ underscore $product.Name }}_custom_endpoint": &schema.StringAttribute{
185-
Optional: true,
186-
Validators: []validator.String{
187-
transport_tpg.CustomEndpointValidator(),
188-
},
189-
},
190-
{{- end }}
191183

192184
// Handwritten Products / Versioned / Atypical Entries
193185
"cloud_billing_custom_endpoint": &schema.StringAttribute{
@@ -321,6 +313,14 @@ func (p *FrameworkProvider) Schema(_ context.Context, _ provider.SchemaRequest,
321313
},
322314
},
323315
}
316+
for _, p := range registry.ListProducts() {
317+
resp.Schema.Attributes[p.CustomEndpointField] = &schema.StringAttribute{
318+
Optional: true,
319+
Validators: []validator.String{
320+
transport_tpg.CustomEndpointValidator(),
321+
},
322+
}
323+
}
324324
}
325325

326326
// Configure prepares the metadata/'meta' required for data sources and resources to function.

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,6 @@ func Provider() *schema.Provider {
176176
Optional: true,
177177
},
178178

179-
// Generated Products
180-
{{- range $product := $.Products }}
181-
"{{ underscore $product.Name }}_custom_endpoint": {
182-
Type: schema.TypeString,
183-
Optional: true,
184-
ValidateFunc: transport_tpg.ValidateCustomEndpoint,
185-
},
186-
{{- end }}
187-
188179
// Handwritten Products / Versioned / Atypical Entries
189180
transport_tpg.CloudBillingCustomEndpointEntryKey: transport_tpg.CloudBillingCustomEndpointEntry,
190181
transport_tpg.DataflowCustomEndpointEntryKey: transport_tpg.DataflowCustomEndpointEntry,
@@ -224,6 +215,14 @@ func Provider() *schema.Provider {
224215
ResourcesMap: registry.ResourceMap(),
225216
}
226217

218+
for _, p := range registry.ListProducts() {
219+
provider.Schema[p.CustomEndpointField] = &schema.Schema{
220+
Type: schema.TypeString,
221+
Optional: true,
222+
ValidateFunc: transport_tpg.ValidateCustomEndpoint,
223+
}
224+
}
225+
227226
provider.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
228227
return ProviderConfigure(ctx, d, provider)
229228
}

mmv1/third_party/terraform/registry/registry.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package registry
22

33
import (
44
"log"
5+
"maps"
6+
"slices"
7+
"strings"
58
"sync"
69

710
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -14,6 +17,12 @@ type Product struct {
1417
Name string
1518
// BaseUrl is the base URL for API requests. It may contain Magic Modules templating directives.
1619
BaseUrl string
20+
// RepUrl is the base URL for regional API requests. It may contain Magic Modules templating directives.
21+
RepUrl string
22+
// CustomEndpointField is the name of the product's custom endpoint field in the provider schema.
23+
CustomEndpointField string
24+
// CustomEndpointEnvVar is the name of the product's custom endpoint environment variable.
25+
CustomEndpointEnvVar string
1726
}
1827

1928
// Register adds the product definition to the internal product registry.
@@ -26,6 +35,14 @@ func (p Product) Register() {
2635
products.m[p.Name] = p
2736
}
2837

38+
func ListProducts() []Product {
39+
l := slices.Collect(maps.Values(products.m))
40+
slices.SortFunc(l, func(a, b Product) int {
41+
return strings.Compare(a.Name, b.Name)
42+
})
43+
return l
44+
}
45+
2946
type registeredProducts struct {
3047
sync.RWMutex
3148
m map[string]Product

0 commit comments

Comments
 (0)