Skip to content

Commit 8a076e0

Browse files
committed
2 parents 9e1bc3c + 9bf8e22 commit 8a076e0

28 files changed

Lines changed: 595 additions & 101 deletions

docs/content/convert/add-new-resource-tgc.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,15 @@ func ResourceGoogleProject() *schema.Resource {
7272
}
7373
}
7474
}
75-
```
76-
77-
You will also need to add an entry to [`tgc_next/provider/provider_mmv1_resources.go.tmpl`](https://github.com/GoogleCloudPlatform/magic-modules/blob/main/mmv1/templates/tgc_next/provider/provider_mmv1_resources.go.tmpl), which is used to generate [`pkg/provider/provider_mmv1_resources.go`](https://github.com/GoogleCloudPlatform/terraform-google-conversion/blob/main/pkg/provider/provider_mmv1_resources.go). Each entry in `provider_mmv1_resources.go.tmpl` maps a terraform resource name to a function that returns the resource schema - in this case:
7875

79-
```golang
80-
// ...
81-
"google_product_resource": product.ResourceName(),
82-
// ...
76+
func init() {
77+
registry.Schema{
78+
Name: "google_product_resource",
79+
ProductName: "product",
80+
Type: registry.SchemaTypeResource,
81+
Schema: ResourceGoogleProject(),
82+
}.Register()
83+
}
8384
```
8485

8586
##### Resource_tfplan2cai.go file

docs/content/develop/add-handwritten-datasource.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,6 @@ added to that resource. You can create a new datasource of this type as follows:
169169
- If there is `labels` field with type `KeyValueLabels` in the corresponding resource: After calling the resource read method, call the function `tpgresource.SetDataSourceLabels(d)` to make `labels` and `terraform_labels` have all of the labels on the resource.
170170
- If there is `annotations` field with type `KeyValueAnnotations` in the corresponding resource: After calling the resource read method, call the function `tpgresource.SetDataSourceAnnotations(d)` to make `annotations` have all of the annotations on the resource.
171171

172-
1. Add the datasource to `handwrittenDatasources` in [`magic-modules/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.tmpl`](https://github.com/GoogleCloudPlatform/magic-modules/blob/main/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.tmpl)
173-
```go
174-
var handwrittenDatasources = map[string]*schema.Resource{
175-
// ...
176-
"google_memorystore_instance": registry.DataSource("google_memorystore_instance"),
177-
"google_memcache_instance": registry.DataSource("google_memcache_instance"),
178-
"google_redis_instance": registry.DataSource("google_redis_instance"),
179-
// ...
180-
}
181-
```
182172
1. [Add documentation](#add-documentation)
183173

184174
For creating a datasource based off an existing resource you can [make use of the

docs/content/develop/add-iam-support.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ iam_policy:
9292
- If any of the added Go code is beta-only:
9393
- Change the file suffix to `.go.tmpl`
9494
- Wrap each beta-only code block (including any imports) in a separate version guard: `{{- if ne $.TargetVersionName "ga" -}}...{{- else }}...{{- end }}`
95-
4. Register the binding, member, and policy resources `handwrittenIAMResources` in [`magic-modules/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.tmpl`](https://github.com/GoogleCloudPlatform/magic-modules/blob/main/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.tmpl)
96-
- Add a version guard for any beta-only resources.
9795
{{< /tab >}}
9896
{{% /tabs %}}
9997

docs/content/develop/add-resource.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,10 @@ For more information about types of resources and the generation process overall
165165
- Remove the comments at the top of the file.
166166
- If any of the added Go code (including any imports) is beta-only, change the file suffix to `.go.tmpl` and wrap the beta-only code in a version guard: `{{- if ne $.TargetVersionName "ga" -}}...{{- else }}...{{- end }}`.
167167
- If the whole resource is beta-only, wrap everything except package declarations. Otherwise, individually wrap each logically-related block of code in a version guard (field, test, etc) rather than grouping adjacent version-guarded sections - it's easier to read and easier to modify as things move out of beta.
168-
5. Register the resource `handwrittenResources` in [`magic-modules/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.tmpl`](https://github.com/GoogleCloudPlatform/magic-modules/blob/main/mmv1/third_party/terraform/provider/provider_mmv1_resources.go.tmpl)
169-
- Add a version guard for any beta-only resources.
170-
6. Optional: Complete other handwritten tasks that require the MMv1 configuration file.
168+
5. Optional: Complete other handwritten tasks that require the MMv1 configuration file.
171169
- [Add resource tests]({{< ref "/test/test" >}})
172170
- [Add IAM support]({{<ref "/develop/add-iam-support" >}})
173-
7. Delete the MMv1 configuration file.
171+
6. Delete the MMv1 configuration file.
174172
{{< /tab >}}
175173
{{% /tabs %}}
176174

mmv1/api/product.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ type Product struct {
8080

8181
// ImportPath contains the prefix used for importing packages in generated files.
8282
ImportPath string `yaml:"-"`
83+
84+
// RepByDefault is if this product should default to REP endpoints if
85+
// available. Changing this requires REP to be supported in *ALL* regions
86+
RepByDefault bool `yaml:"rep_by_default,omitempty"`
8387
}
8488

8589
func (p *Product) UnmarshalYAML(value *yaml.Node) error {

mmv1/api/product/version.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Version struct {
3030
CaiLegacyBaseUrl string `yaml:"cai_legacy_base_url,omitempty"`
3131
BaseUrl string `yaml:"base_url"`
3232
Name string
33+
RepUrl string `yaml:"rep_url,omitempty"`
3334
}
3435

3536
func (v *Version) Validate(pName string) {
@@ -44,3 +45,9 @@ func (v *Version) Validate(pName string) {
4445
func (v *Version) CompareTo(other *Version) int {
4546
return slices.Index(ORDER, v.Name) - slices.Index(ORDER, other.Name)
4647
}
48+
49+
// Whether this version supports regionalized endpoints (REP). The default
50+
// of regional vs global is controlled at the product level
51+
func (v *Version) RepEnabled() bool {
52+
return v.RepUrl != ""
53+
}

mmv1/products/artifactregistry/product.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ display_name: 'Artifact Registry'
1717
versions:
1818
- name: 'beta'
1919
base_url: 'https://artifactregistry.googleapis.com/v1/'
20+
rep_url: 'https://artifactregistry.{{location}}.rep.googleapis.com/v1/'
2021
- name: 'ga'
2122
base_url: 'https://artifactregistry.googleapis.com/v1/'
23+
rep_url: 'https://artifactregistry.{{location}}.rep.googleapis.com/v1/'
2224
scopes:
2325
- 'https://www.googleapis.com/auth/cloud-platform'
2426
async:

mmv1/products/metastore/Service.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ examples:
101101
metastore_service_name: 'metastore-srv'
102102
network_name: 'my-network'
103103
subnet_name: 'my-subnetwork'
104-
exclude_test: true
105104
- name: 'dataproc_metastore_service_private_service_connect_custom_routes'
106105
primary_resource_id: 'default'
107106
min_version: 'beta'

mmv1/templates/terraform/examples/base_configs/test_file.go.tmpl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,26 @@ func testAccCheck{{ $.Res.ResourceName }}DestroyProducer(t *testing.T) func(s *t
152152
{{- else }}
153153

154154
config := acctest.GoogleProviderConfig(t)
155+
{{ if $.Res.ProductMetadata.Version.RepEnabled }}
155156

157+
urlFormatted, err := tpgresource.ReplaceVarsForTest(config, rs, "{{$.Res.SelfLinkUri}}")
158+
159+
if err != nil {
160+
return err
161+
}
162+
163+
loc := tpgresource.LocationFromId(urlFormatted)
164+
basePath, err := transport_tpg.ResourceBasePath(config.{{$.Res.ProductMetadata.Name}}BasePath, config.{{$.Res.ProductMetadata.Name}}RepBasePath, "{{$.Res.ProductMetadata.Name}}", config, loc)
165+
if err != nil {
166+
return err
167+
}
168+
url := fmt.Sprintf("%s%s", basePath, urlFormatted)
169+
{{- else }}
156170
url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{"{{"}}{{$.Res.ProductMetadata.Name}}{{"BasePath}}"}}{{$.Res.SelfLinkUri}}")
157171
if err != nil {
158172
return err
159173
}
174+
{{- end }}
160175

161176
billingProject := ""
162177

mmv1/templates/terraform/operation.go.tmpl

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ type {{ $.ProductMetadata.Name }}OperationWaiter struct {
5050
{{- if $.IncludeProjectForOperation }}
5151
Project string
5252
{{- end }}
53+
{{- if $.ProductMetadata.Version.RepEnabled }}
54+
Location string
55+
{{- end }}
5356
{{- if $.ProductMetadata.OperationRetry }}
5457
retryCount int
5558
{{- end }}
@@ -60,9 +63,16 @@ func (w *{{ $.ProductMetadata.Name }}OperationWaiter) QueryOp() (interface{}, er
6063
if w == nil {
6164
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
6265
}
66+
{{- if $.ProductMetadata.Version.RepEnabled }}
67+
basePath, err := transport_tpg.ResourceBasePath(w.Config.{{$.ProductMetadata.Name}}BasePath, w.Config.{{$.ProductMetadata.Name}}RepBasePath, "{{$.ProductMetadata.Name}}", w.Config, w.Location)
68+
if err != nil {
69+
return nil, err
70+
}
71+
url := fmt.Sprintf("%s{{ replaceAll $.GetAsync.Operation.BaseUrl "{{op_id}}" "%s" }}", basePath, w.CommonOperationWaiter.Op.Name)
72+
{{ else }}
6373
// Returns the proper get.
6474
url := fmt.Sprintf("%s{{ replaceAll $.GetAsync.Operation.BaseUrl "{{op_id}}" "%s" }}", w.Config.{{ $.ProductMetadata.Name }}BasePath, w.CommonOperationWaiter.Op.Name)
65-
75+
{{ end }}
6676
return transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
6777
Config: w.Config,
6878
Method: "GET",
@@ -87,12 +97,15 @@ func (w *{{ $.ProductMetadata.Name }}OperationWaiter) IsRetryable(err error) boo
8797
{{- end }}
8898

8999

90-
func create{{ $.ProductMetadata.Name }}Waiter(config *transport_tpg.Config, op map[string]interface{}, {{- if $.IncludeProjectForOperation }} project, {{- end }} activity, userAgent string) (*{{ $.ProductMetadata.Name }}OperationWaiter, error) {
100+
func create{{ $.ProductMetadata.Name }}Waiter(config *transport_tpg.Config, op map[string]interface{}, {{- if $.IncludeProjectForOperation }} project,{{- end }}{{- if $.ProductMetadata.Version.RepEnabled }} location, {{- end }} activity, userAgent string) (*{{ $.ProductMetadata.Name }}OperationWaiter, error) {
91101
w := &{{ $.ProductMetadata.Name }}OperationWaiter{
92102
Config: config,
93103
UserAgent: userAgent,
94104
{{- if $.IncludeProjectForOperation }}
95105
Project: project,
106+
{{- end }}
107+
{{- if $.ProductMetadata.Version.RepEnabled }}
108+
Location: location,
96109
{{- end }}
97110
}
98111
if err := w.CommonOperationWaiter.SetOp(op); err != nil {
@@ -107,8 +120,8 @@ func create{{ $.ProductMetadata.Name }}Waiter(config *transport_tpg.Config, op m
107120
*/}}
108121

109122
// nolint: deadcode,unused {{/* TODO rewrite: remove the comment */}}
110-
func {{ camelize $.ProductMetadata.Name "upper" }}OperationWaitTimeWithResponse(config *transport_tpg.Config, op map[string]interface{}, response *map[string]interface{},{{- if $.IncludeProjectForOperation }} project,{{- end }} activity, userAgent string, timeout time.Duration) error {
111-
w, err := create{{ $.ProductMetadata.Name }}Waiter(config, op, {{- if $.IncludeProjectForOperation }} project, {{ end }} activity, userAgent)
123+
func {{ camelize $.ProductMetadata.Name "upper" }}OperationWaitTimeWithResponse(config *transport_tpg.Config, op map[string]interface{}, response *map[string]interface{},{{- if $.IncludeProjectForOperation }} project,{{- end }}{{- if $.ProductMetadata.Version.RepEnabled }} location,{{- end }} activity, userAgent string, timeout time.Duration) error {
124+
w, err := create{{ $.ProductMetadata.Name }}Waiter(config, op, {{- if $.IncludeProjectForOperation }} project, {{- end }}{{- if $.ProductMetadata.Version.RepEnabled }} location, {{ end }} activity, userAgent)
112125
if err != nil {
113126
return err
114127
}
@@ -122,12 +135,12 @@ func {{ camelize $.ProductMetadata.Name "upper" }}OperationWaitTimeWithResponse(
122135
return json.Unmarshal(rawResponse, response)
123136
}
124137

125-
func {{ camelize $.ProductMetadata.Name "upper" }}OperationWaitTime(config *transport_tpg.Config, op map[string]interface{}, {{- if $.IncludeProjectForOperation }} project,{{- end }} activity, userAgent string, timeout time.Duration) error {
138+
func {{ camelize $.ProductMetadata.Name "upper" }}OperationWaitTime(config *transport_tpg.Config, op map[string]interface{}, {{- if $.IncludeProjectForOperation }} project,{{- end }}{{- if $.ProductMetadata.Version.RepEnabled }} location,{{- end }} activity, userAgent string, timeout time.Duration) error {
126139
if val, ok := op["name"]; !ok || val == "" {
127140
// This was a synchronous call - there is no operation to wait for.
128141
return nil
129142
}
130-
w, err := create{{ $.ProductMetadata.Name }}Waiter(config, op, {{- if $.IncludeProjectForOperation }} project, {{ end }} activity, userAgent)
143+
w, err := create{{ $.ProductMetadata.Name }}Waiter(config, op, {{- if $.IncludeProjectForOperation }} project, {{- end }}{{- if $.ProductMetadata.Version.RepEnabled }} location, {{ end }} activity, userAgent)
131144
if err != nil {
132145
// If w is nil, the op was synchronous.
133146
return err

0 commit comments

Comments
 (0)