Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mmv1/api/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ type Product struct {

// ImportPath contains the prefix used for importing packages in generated files.
ImportPath string `yaml:"-"`

RepEnabled bool `yaml:"rep_enabled,omitempty"`
}

func (p *Product) UnmarshalYAML(value *yaml.Node) error {
Expand Down
3 changes: 2 additions & 1 deletion mmv1/products/alloydb/product.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ name: 'Alloydb'
display_name: 'AlloyDB'
versions:
- name: 'beta'
base_url: 'https://alloydb.googleapis.com/v1beta/'
base_url: 'https://alloydb.{{location}}.rep.googleapis.com/v1beta/'
- name: 'ga'
base_url: 'https://alloydb.googleapis.com/v1/'
scopes:
- 'https://www.googleapis.com/auth/cloud-identity'
rep_enabled: true
21 changes: 16 additions & 5 deletions mmv1/templates/terraform/operation.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type {{ $.ProductMetadata.Name }}OperationWaiter struct {
{{- if $.IncludeProjectForOperation }}
Project string
{{- end }}
{{- if $.ProductMetadata.RepEnabled }}
Location string
{{- end }}
{{- if $.ProductMetadata.OperationRetry }}
retryCount int
{{- end }}
Expand All @@ -60,8 +63,13 @@ func (w *{{ $.ProductMetadata.Name }}OperationWaiter) QueryOp() (interface{}, er
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}
{{- if $.ProductMetadata.RepEnabled }}
url := fmt.Sprintf("%s{{ replaceAll $.GetAsync.Operation.BaseUrl "{{op_id}}" "%s" }}", w.Config.{{ $.ProductMetadata.Name }}BasePath, w.CommonOperationWaiter.Op.Name)
url = strings.ReplaceAll(url, "{{`{{location}}`}}", w.Location)
{{ else }}
// Returns the proper get.
url := fmt.Sprintf("%s{{ replaceAll $.GetAsync.Operation.BaseUrl "{{op_id}}" "%s" }}", w.Config.{{ $.ProductMetadata.Name }}BasePath, w.CommonOperationWaiter.Op.Name)
{{- end }}

return transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: w.Config,
Expand All @@ -87,12 +95,15 @@ func (w *{{ $.ProductMetadata.Name }}OperationWaiter) IsRetryable(err error) boo
{{- end }}


func create{{ $.ProductMetadata.Name }}Waiter(config *transport_tpg.Config, op map[string]interface{}, {{- if $.IncludeProjectForOperation }} project, {{- end }} activity, userAgent string) (*{{ $.ProductMetadata.Name }}OperationWaiter, error) {
func create{{ $.ProductMetadata.Name }}Waiter(config *transport_tpg.Config, op map[string]interface{}, {{- if $.IncludeProjectForOperation }} project,{{- end }}{{- if $.ProductMetadata.RepEnabled }} location, {{- end }} activity, userAgent string) (*{{ $.ProductMetadata.Name }}OperationWaiter, error) {
w := &{{ $.ProductMetadata.Name }}OperationWaiter{
Config: config,
UserAgent: userAgent,
{{- if $.IncludeProjectForOperation }}
Project: project,
{{- end }}
{{- if $.ProductMetadata.RepEnabled }}
Location: location,
{{- end }}
}
if err := w.CommonOperationWaiter.SetOp(op); err != nil {
Expand All @@ -107,8 +118,8 @@ func create{{ $.ProductMetadata.Name }}Waiter(config *transport_tpg.Config, op m
*/}}

// nolint: deadcode,unused {{/* TODO rewrite: remove the comment */}}
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 {
w, err := create{{ $.ProductMetadata.Name }}Waiter(config, op, {{- if $.IncludeProjectForOperation }} project, {{ end }} activity, userAgent)
func {{ camelize $.ProductMetadata.Name "upper" }}OperationWaitTimeWithResponse(config *transport_tpg.Config, op map[string]interface{}, response *map[string]interface{},{{- if $.IncludeProjectForOperation }} project,{{- end }}{{- if $.ProductMetadata.RepEnabled }} location,{{- end }} activity, userAgent string, timeout time.Duration) error {
w, err := create{{ $.ProductMetadata.Name }}Waiter(config, op, {{- if $.IncludeProjectForOperation }} project, {{- end }}{{- if $.ProductMetadata.RepEnabled }} location, {{ end }} activity, userAgent)
if err != nil {
return err
}
Expand All @@ -122,12 +133,12 @@ func {{ camelize $.ProductMetadata.Name "upper" }}OperationWaitTimeWithResponse(
return json.Unmarshal(rawResponse, response)
}

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 {
func {{ camelize $.ProductMetadata.Name "upper" }}OperationWaitTime(config *transport_tpg.Config, op map[string]interface{}, {{- if $.IncludeProjectForOperation }} project,{{- end }}{{- if $.ProductMetadata.RepEnabled }} location,{{- end }} activity, userAgent string, timeout time.Duration) error {
if val, ok := op["name"]; !ok || val == "" {
// This was a synchronous call - there is no operation to wait for.
return nil
}
w, err := create{{ $.ProductMetadata.Name }}Waiter(config, op, {{- if $.IncludeProjectForOperation }} project, {{ end }} activity, userAgent)
w, err := create{{ $.ProductMetadata.Name }}Waiter(config, op, {{- if $.IncludeProjectForOperation }} project, {{- end }}{{- if $.ProductMetadata.RepEnabled }} location, {{ end }} activity, userAgent)
if err != nil {
// If w is nil, the op was synchronous.
return err
Expand Down
5 changes: 3 additions & 2 deletions mmv1/templates/terraform/pre_update/alloydb_cluster.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
location := tpgresource.LocationFromId(d.Id())
// Implementation for cluster upgrade
if d.HasChange("database_version") && !tpgresource.IsEmptyValue(reflect.ValueOf(d.Get("database_version"))) {
upgradeUrl := strings.Split(url, "?updateMask")[0] + ":upgrade"
Expand Down Expand Up @@ -42,7 +43,7 @@ if d.HasChange("database_version") && !tpgresource.IsEmptyValue(reflect.ValueOf(

if d.Get("skip_await_major_version_upgrade") == false {
err = AlloydbOperationWaitTime(
config, res, project, "Upgrading cluster", userAgent,
config, res, project, location, "Upgrading cluster", userAgent,
upgradeClusterTimeout)

if err != nil {
Expand Down Expand Up @@ -112,7 +113,7 @@ if d.HasChange("cluster_type") && d.Get("cluster_type") == "PRIMARY" {
}

err = AlloydbOperationWaitTime(
config, res, project, "Promoting Cluster", userAgent,
config, res, project, location, "Promoting Cluster", userAgent,
d.Timeout(schema.TimeoutCreate))

if err != nil {
Expand Down
84 changes: 79 additions & 5 deletions mmv1/templates/terraform/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,21 @@ func resource{{ $.ResourceName -}}Create(d *schema.ResourceData, meta interface{
defer transport_tpg.MutexStore.Unlock(lockName)
{{- end}}

{{- if $.ProductMetadata.RepEnabled }}
urlFormatted, err := tpgresource.ReplaceVars(d, config, "{{$.CreateUri}}")
if err != nil {
return err
}
loc := tpgresource.LocationFromId(urlFormatted)
basePath := tpgresource.RepQualifiedBasePath(config.{{$.ProductMetadata.Name}}BasePath, loc)

url, err := tpgresource.ReplaceVars{{if $.LegacyLongFormProject -}}ForId{{ end -}}(d, config, "{{$.CreateUri}}")
url = fmt.Sprintf("%s%s", basePath, url)
{{ else }}

url, err := tpgresource.ReplaceVars{{if $.LegacyLongFormProject -}}ForId{{ end -}}(d, config, "{{"{{"}}{{$.ProductMetadata.Name}}BasePath{{"}}"}}{{$.CreateUri}}")
{{- end }}

if err != nil {
return err
}
Expand Down Expand Up @@ -352,11 +366,15 @@ func resource{{ $.ResourceName -}}Create(d *schema.ResourceData, meta interface{
{{if and $.GetAsync ($.GetAsync.Allow "Create") -}}
{{ if ($.GetAsync.IsA "OpAsync") -}}
{{ if and $.GetAsync.Result.ResourceInsideResponse $.HasPostCreateComputedFields -}}
{{- if $.ProductMetadata.RepEnabled }}
// Derive location for use in REP endpoints
location := tpgresource.LocationFromId(d.Id())
{{- end}}
// Use the resource in the operation response to populate
// identity fields and d.Id() before read
var opRes map[string]interface{}
err = {{ $.ClientNamePascal -}}OperationWaitTimeWithResponse(
config, res, &opRes, {{if or $.HasProject $.GetAsync.IncludeProject -}} {{if $.LegacyLongFormProject -}}tpgresource.GetResourceNameFromSelfLink(project){{ else }}project{{ end }}, {{ end -}} "Creating {{ $.Name -}}", userAgent,
config, res, &opRes, {{if or $.HasProject $.GetAsync.IncludeProject -}} {{if $.LegacyLongFormProject -}}tpgresource.GetResourceNameFromSelfLink(project){{ else }}project{{ end }},{{if $.ProductMetadata.RepEnabled }} location,{{ end -}}{{ end -}} "Creating {{ $.Name -}}", userAgent,
d.Timeout(schema.TimeoutCreate))
if err != nil {
{{if $.CustomCode.PostCreateFailure -}}
Expand Down Expand Up @@ -430,8 +448,12 @@ func resource{{ $.ResourceName -}}Create(d *schema.ResourceData, meta interface{
d.SetId(id)

{{ else -}}
{{- if $.ProductMetadata.RepEnabled }}
// Derive location for use in REP endpoints
location := tpgresource.LocationFromId(d.Id())
{{- end}}
err = {{ $.ClientNamePascal -}}OperationWaitTime(
config, res, {{if or $.HasProject $.GetAsync.IncludeProject -}} {{if $.LegacyLongFormProject -}}tpgresource.GetResourceNameFromSelfLink(project){{ else }}project{{ end }}, {{ end -}} "Creating {{ $.Name -}}", userAgent,
config, res, {{if or $.HasProject $.GetAsync.IncludeProject -}} {{if $.LegacyLongFormProject -}}tpgresource.GetResourceNameFromSelfLink(project){{ else }}project{{ end }}, {{ end -}}{{if $.ProductMetadata.RepEnabled }} location,{{ end -}} "Creating {{ $.Name -}}", userAgent,
d.Timeout(schema.TimeoutCreate))

if err != nil {
Expand Down Expand Up @@ -574,7 +596,21 @@ func resource{{ $.ResourceName -}}Read(d *schema.ResourceData, meta interface{})
return err
}

{{- if $.ProductMetadata.RepEnabled }}
urlFormatted, err := tpgresource.ReplaceVars(d, config, "{{$.CreateUri}}")
if err != nil {
return err
}
loc := tpgresource.LocationFromId(urlFormatted)
basePath := tpgresource.RepQualifiedBasePath(config.{{$.ProductMetadata.Name}}BasePath, loc)

url, err := tpgresource.ReplaceVars{{if $.LegacyLongFormProject -}}ForId{{ end -}}(d, config, "{{$.SelfLinkUri}}{{$.ReadQueryParams}}")
url = fmt.Sprintf("%s%s", basePath, url)
{{ else }}

url, err := tpgresource.ReplaceVars{{if $.LegacyLongFormProject -}}ForId{{ end -}}(d, config, "{{"{{"}}{{$.ProductMetadata.Name}}BasePath{{"}}"}}{{$.SelfLinkUri}}{{$.ReadQueryParams}}")
{{- end }}

if err != nil {
return err
}
Expand Down Expand Up @@ -801,7 +837,21 @@ func resource{{ $.ResourceName -}}Update(d *schema.ResourceData, meta interface{
defer transport_tpg.MutexStore.Unlock(lockName)
{{- end}}


{{- if $.ProductMetadata.RepEnabled }}
urlFormatted, err := tpgresource.ReplaceVars(d, config, "{{$.CreateUri}}")
if err != nil {
return err
}
loc := tpgresource.LocationFromId(urlFormatted)
basePath := tpgresource.RepQualifiedBasePath(config.{{$.ProductMetadata.Name}}BasePath, loc)

url, err := tpgresource.ReplaceVars{{if $.LegacyLongFormProject -}}ForId{{ end -}}(d, config, "{{ $.UpdateUri }}")
url = fmt.Sprintf("%s%s", basePath, url)
{{ else }}

url, err := tpgresource.ReplaceVars{{if $.LegacyLongFormProject -}}ForId{{ end -}}(d, config, "{{"{{"}}{{$.ProductMetadata.Name}}BasePath{{"}}"}}{{ $.UpdateUri }}")
{{- end }}
if err != nil {
return err
}
Expand Down Expand Up @@ -863,8 +913,12 @@ if len(updateMask) > 0 {

{{ if and ($.GetAsync) ($.GetAsync.Allow "update") -}}
{{ if $.GetAsync.IsA "OpAsync" -}}
{{- if $.ProductMetadata.RepEnabled }}
// Derive location for use in REP endpoints
location := tpgresource.LocationFromId(d.Id())
{{- end}}
err = {{ $.ClientNamePascal -}}OperationWaitTime(
config, res, {{if or $.HasProject $.GetAsync.IncludeProject -}} {{if $.LegacyLongFormProject -}}tpgresource.GetResourceNameFromSelfLink(project){{ else }}project{{ end }}, {{ end -}} "Updating {{ $.Name -}}", userAgent,
config, res, {{if or $.HasProject $.GetAsync.IncludeProject -}} {{if $.LegacyLongFormProject -}}tpgresource.GetResourceNameFromSelfLink(project){{ else }}project{{ end }}, {{ end -}}{{if $.ProductMetadata.RepEnabled }} location,{{ end -}} "Updating {{ $.Name -}}", userAgent,
d.Timeout(schema.TimeoutUpdate))

if err != nil {
Expand Down Expand Up @@ -1022,8 +1076,12 @@ if d.HasChange("{{ join ($.PropertyNamesToStrings (index $CustomUpdateProps $gro

{{ if and ($.GetAsync) ($.GetAsync.Allow "update") -}}
{{ if $.GetAsync.IsA "OpAsync" -}}
{{- if $.ProductMetadata.RepEnabled }}
// Derive location for use in REP endpoints
location := tpgresource.LocationFromId(d.Id())
{{- end}}
err = {{ $.ClientNamePascal -}}OperationWaitTime(
config, res, {{if or $.HasProject $.GetAsync.IncludeProject -}} {{if $.LegacyLongFormProject -}}tpgresource.GetResourceNameFromSelfLink(project){{ else }}project{{ end }}, {{ end -}} "Updating {{ $.Name -}}", userAgent,
config, res, {{if or $.HasProject $.GetAsync.IncludeProject -}} {{if $.LegacyLongFormProject -}}tpgresource.GetResourceNameFromSelfLink(project){{ else }}project{{ end }}, {{ end -}}{{if $.ProductMetadata.RepEnabled }} location,{{ end -}} "Updating {{ $.Name -}}", userAgent,
d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
Expand Down Expand Up @@ -1100,8 +1158,20 @@ func resource{{ $.ResourceName }}Delete(d *schema.ResourceData, meta interface{}
transport_tpg.MutexStore.Lock(lockName)
defer transport_tpg.MutexStore.Unlock(lockName)
{{- end }}
{{- if $.ProductMetadata.RepEnabled }}
urlFormatted, err := tpgresource.ReplaceVars(d, config, "{{$.CreateUri}}")
if err != nil {
return err
}
loc := tpgresource.LocationFromId(urlFormatted)
basePath := tpgresource.RepQualifiedBasePath(config.{{$.ProductMetadata.Name}}BasePath, loc)

url, err := tpgresource.ReplaceVars{{if $.LegacyLongFormProject -}}ForId{{ end -}}(d, config, "{{$.DeleteUri}}")
url = fmt.Sprintf("%s%s", basePath, url)
{{ else }}

url, err := tpgresource.ReplaceVars{{if $.LegacyLongFormProject -}}ForId{{ end -}}(d, config, "{{"{{"}}{{$.ProductMetadata.Name}}BasePath{{"}}"}}{{$.DeleteUri}}")
{{- end }}
if err != nil {
return err
}
Expand Down Expand Up @@ -1167,8 +1237,12 @@ func resource{{ $.ResourceName }}Delete(d *schema.ResourceData, meta interface{}
{{- end }}
}
{{- else }}
{{- if $.ProductMetadata.RepEnabled }}
// Derive location for use in REP endpoints
location := tpgresource.LocationFromId(d.Id())
{{- end}}
err = {{ $.ClientNamePascal }}OperationWaitTime(
config, res, {{if or $.HasProject $.GetAsync.IncludeProject -}} {{if $.LegacyLongFormProject -}}tpgresource.GetResourceNameFromSelfLink(project){{ else }}project{{ end }}, {{ end -}} "Deleting {{ $.Name -}}", userAgent,
config, res, {{if or $.HasProject $.GetAsync.IncludeProject -}} {{if $.LegacyLongFormProject -}}tpgresource.GetResourceNameFromSelfLink(project){{ else }}project{{ end }}, {{ end -}}{{if $.ProductMetadata.RepEnabled }} location,{{ end -}} "Deleting {{ $.Name -}}", userAgent,
d.Timeout(schema.TimeoutDelete))

if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions mmv1/third_party/terraform/tpgresource/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1003,3 +1003,33 @@ func IpAddrSetHashFunc(v interface{}) int {
log.Printf("[DEBUG] computed hash value of %v from %v", Hashcode(ipnet.String()), ipnet.String())
return Hashcode(ipnet.String())
}

func RepQualifiedBasePath(basePath, location string) string {
return strings.ReplaceAll(basePath, "{{location}}", location)
}

func LocationFromId(id string) string {
re := regexp.MustCompile(`/locations/([^/]+)/`)

match := re.FindStringSubmatch(id)

if len(match) > 1 {
return match[1]
}
re = regexp.MustCompile(`/regions/([^/]+)/`)

match = re.FindStringSubmatch(id)

if len(match) > 1 {
return match[1]
}

re = regexp.MustCompile(`/zones/([^/]+)/`)

match = re.FindStringSubmatch(id)

if len(match) > 1 {
return GetRegionFromZone(match[1])
}
return ""
}
Loading