Skip to content

Commit 75c78bb

Browse files
committed
Handle decoders when setting computed fields in post-create
1 parent 6499220 commit 75c78bb

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

mmv1/products/bigqueryanalyticshub/ListingSubscription.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ immutable: true
3030
import_format:
3131
- 'projects/{{project}}/locations/{{location}}/subscriptions/{{subscription_id}}'
3232
custom_code:
33+
decoder: 'templates/terraform/decoders/bigqueryanalyticshub_listing_subscription.go.tmpl'
3334
pre_read: 'templates/terraform/pre_read/bigqueryanalyticshub_listing_subscription.tmpl'
3435
pre_delete: 'templates/terraform/pre_read/bigqueryanalyticshub_listing_subscription.tmpl'
3536
post_import: templates/terraform/post_import/analytics_hub_subscription.go.tmpl
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// The response from Create nests the resource inside a "subscription" key.
2+
if s, ok := res["subscription"]; ok {
3+
return s.(map[string]interface{}), nil
4+
}
5+
return res, nil

mmv1/templates/terraform/resource.go.tmpl

+13-1
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,25 @@ func resource{{ $.ResourceName -}}Create(d *schema.ResourceData, meta interface{
291291
{{- /* Set output-only resource properties from create API response (as long as Create doesn't use an async operation) */}}
292292
{{- /* This is necessary so that the ID is built correctly. */}}
293293
{{- if or (not $.GetAsync) (not (and ($.GetAsync.IsA "OpAsync") ($.GetAsync.Allow "Create"))) }}
294+
{{- $renderedIdFromName := "false" }}
295+
{{- $renderedDecoder := "false" }}
294296
{{- range $prop := $.GettableProperties }}
295297
{{- if $.InIdFormat $prop }}
296-
{{- if eq $prop.CustomFlatten "templates/terraform/custom_flatten/id_from_name.tmpl" }}
298+
{{- if and ($.CustomCode.Decoder) (eq $renderedDecoder "false") }}
299+
res, err = resource{{ $.ResourceName -}}Decoder(d, meta, res)
300+
if err != nil {
301+
return nil, err
302+
}
303+
if res == nil {
304+
return nil, tpgresource.Fake404("decoded", "{{ $.ResourceName }}")
305+
}
306+
{{- end }}
307+
{{- if and (eq $prop.CustomFlatten "templates/terraform/custom_flatten/id_from_name.tmpl") (eq $renderedIdFromName "false") }}
297308
// Setting `name` field so that `id_from_name` flattener will work properly.
298309
if err := d.Set("name", flatten{{ if $.NestedQuery -}}Nested{{end}}{{ $.ResourceName -}}Name(res["name"], d, config)); err != nil {
299310
return fmt.Errorf(`Error setting computed identity field "name": %s`, err)
300311
}
312+
{{- $renderedIdFromName = "true" }}
301313
{{- end }}
302314
{{- if and $prop.Output (not $prop.IgnoreRead) }}
303315
if err := d.Set("{{ underscore $prop.Name -}}", flatten{{ if $.NestedQuery -}}Nested{{end}}{{ $.ResourceName -}}{{ camelize $prop.Name "upper" -}}(res["{{ $prop.ApiName -}}"], d, config)); err != nil {

0 commit comments

Comments
 (0)