Skip to content
Draft
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
3 changes: 3 additions & 0 deletions mmv1/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ type Resource struct {
// The compiler to generate the downstream files, for example "terraformgoogleconversion-codegen".
Compiler string `yaml:"-"`

// If true it will generate all flatteners in a flatten{{resourceName}} method instead of generating all flatteners in Read()
FlattenAllInMethod bool `yaml:"flatten_all_in_method,omitempty"`

// The API "resource type kind" used for this resource e.g., "Function".
// If this is not set, then :name is used instead, which is strongly
// preferred wherever possible. Its main purpose is for supporting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ nested_query:
- resources
is_list_of_ids: true
modify_by_patch: true
flatten_all_in_method: true
custom_code:
encoder: 'templates/terraform/encoders/access_context_manager_service_perimeter_resource.go.tmpl'
custom_import: 'templates/terraform/custom_import/access_context_manager_service_perimeter_resource.go.tmpl'
Expand Down
1 change: 1 addition & 0 deletions mmv1/products/bigquery/Dataset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ timeouts:
delete_minutes: 20
include_in_tgc_next: true
tgc_include_handwritten_tests: true
flatten_all_in_method: true
custom_code:
constants: 'templates/terraform/constants/bigquery_dataset.go.tmpl'
pre_read: 'templates/terraform/pre_read/bigquery_dataset.go.tmpl'
Expand Down
1 change: 1 addition & 0 deletions mmv1/products/compute/Reservation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async:
result:
resource_inside_response: false
collection_url_key: 'items'
flatten_all_in_method: true
custom_code:
constants: 'templates/terraform/constants/reservation.go.tmpl'
update_encoder: 'templates/terraform/update_encoder/reservation.go.tmpl'
Expand Down
1 change: 1 addition & 0 deletions mmv1/products/kms/CryptoKey.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ self_link: '{{key_ring}}/cryptoKeys/{{name}}'
create_url: '{{key_ring}}/cryptoKeys?cryptoKeyId={{name}}&skipInitialVersionCreation={{skip_initial_version_creation}}'
update_verb: 'PATCH'
update_mask: true
flatten_all_in_method: true
import_format:
- '{{key_ring}}/cryptoKeys/{{name}}'
include_in_tgc_next: true
Expand Down
1 change: 1 addition & 0 deletions mmv1/products/secretmanager/Secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ base_url: 'projects/{{project}}/secrets'
self_link: 'projects/{{project}}/secrets/{{secret_id}}'
create_url: 'projects/{{project}}/secrets?secretId={{secret_id}}'
update_verb: 'PATCH'
flatten_all_in_method: true
update_mask: true
import_format:
- 'projects/{{project}}/secrets/{{secret_id}}'
Expand Down
1 change: 1 addition & 0 deletions mmv1/products/sql/Database.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async:
result:
resource_inside_response: false
collection_url_key: 'items'
flatten_all_in_method: true
custom_code:
pre_delete: 'templates/terraform/pre_delete/sql_database_deletion_policy.tmpl'
pre_read: 'templates/terraform/pre_read/sql_database_activation_policy.tmpl'
Expand Down
77 changes: 74 additions & 3 deletions mmv1/templates/terraform/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ func resource{{ $.ResourceName -}}Read(d *schema.ResourceData, meta interface{})
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("{{ $.ResourceName }} %q", d.Id()))
{{- end}}
}
{{- if $.CustomCode.PostRead }}
{{- if and $.CustomCode.PostRead (not $.FlattenAllInMethod)}}
{{ customTemplate $ $.CustomCode.PostRead false -}}
{{- end }}

Expand All @@ -648,7 +648,7 @@ func resource{{ $.ResourceName -}}Read(d *schema.ResourceData, meta interface{})

{{- end}}

{{if $.CustomCode.Decoder -}}
{{if and $.CustomCode.Decoder (not $.FlattenAllInMethod)}}
res, err = resource{{ $.ResourceName -}}Decoder(d, meta, res)
if err != nil {
return err
Expand Down Expand Up @@ -699,6 +699,12 @@ func resource{{ $.ResourceName -}}Read(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error reading {{ $.Name -}}: %s", err)
}
{{- end}}
{{ if $.FlattenAllInMethod }}
err = flatten{{ $.ResourceName -}}(d, meta, res, config, {{- if $.HasProject }}project{{ else }}""{{ end }}, userAgent)
if err != nil {
return err
}
{{ else }}
{{ range $prop := $.GettableProperties }}
{{if $prop.FlattenObject -}}
// Terraform must set the top level schema field, but since this object contains collapsed properties
Expand Down Expand Up @@ -727,7 +733,7 @@ func resource{{ $.ResourceName -}}Read(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error reading {{ $.Name -}}: %s", err)
}
{{- end}}

{{ end }}
return nil
{{ end -}}
}
Expand Down Expand Up @@ -1317,3 +1323,68 @@ func resource{{ $.ResourceName -}}PostCreateSetComputedFields(d *schema.Resource
return nil
}
{{- end }}

{{ if $.FlattenAllInMethod }}
func flatten{{ $.ResourceName -}}(d *schema.ResourceData, meta interface{}, res map[string]interface{}, config *transport_tpg.Config, project string, userAgent string) error {
{{- if and $.CustomCode.PostRead }}
{{ customTemplate $ $.CustomCode.PostRead false -}}
{{- end }}
{{if $.NestedQuery -}}
res, err := flattenNested{{ $.ResourceName -}}(d, meta, res)
if err != nil {
return err
}

if res == nil {
// Object isn't there any more - remove it from the state.
log.Printf("[DEBUG] Removing {{ $.ResourceName }} because it couldn't be matched.")
d.SetId("")
return nil
}

{{- end}}

{{if and $.CustomCode.Decoder (not $.FlattenAllInMethod)}}
res, err = resource{{ $.ResourceName -}}Decoder(d, meta, res)
if err != nil {
return err
}

if res == nil {
// Decoding the object has resulted in it being gone. It may be marked deleted
log.Printf("[DEBUG] Removing {{ $.ResourceName }} because it no longer exists.")
d.SetId("")
return nil
}
{{ end}}
{{ range $prop := $.GettableProperties }}
{{if $prop.FlattenObject -}}
// Terraform must set the top level schema field, but since this object contains collapsed properties
// it's difficult to know what the top level should be. Instead we just loop over the map returned from flatten.
if flattenedProp := flatten{{ if $.NestedQuery -}}Nested{{end}}{{ $.ResourceName -}}{{ camelize $prop.Name "upper" -}}(res["{{ $prop.ApiName -}}"], d, config); flattenedProp != nil {
if gerr, ok := flattenedProp.(*googleapi.Error); ok {
return fmt.Errorf("Error reading {{ $.Name -}}: %s", gerr)
}
casted := flattenedProp.([]interface{})[0]
if casted != nil {
for k, v := range casted.(map[string]interface{}) {
if err := d.Set(k, v); err != nil {
return fmt.Errorf("Error setting %s: %s", k, err)
}
}
}
}
{{- else -}}
if err := d.Set("{{ underscore $prop.Name -}}", flatten{{ if $.NestedQuery -}}Nested{{end}}{{ $.ResourceName -}}{{ camelize $prop.Name "upper" -}}(res["{{ $prop.ApiName -}}"], d, config)); err != nil {
return fmt.Errorf("Error reading {{ $.Name -}}: %s", err)
}
{{- end}}
{{- end}}
{{if $.HasSelfLink -}}
if err := d.Set("self_link", tpgresource.ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
return fmt.Errorf("Error reading {{ $.Name -}}: %s", err)
}
{{- end }}
return nil
}
{{- end -}}
Loading