Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add post_read custom_code #12703

Merged
merged 4 commits into from
Jan 7, 2025
Merged
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
4 changes: 4 additions & 0 deletions mmv1/api/resource/custom_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ type CustomCode struct {
// in the Read function.
PreRead string `yaml:"pre_read"`

// This code is run after Read calls happen. It's placed in the
// Read function and also after the nested_query read call.
PostRead string `yaml:"post_read"`

// This code is run before the Update call happens. It's placed
// in the Update function, just after the encoder call, before
// the Update call. Just like the encoder, it is only used if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ nested_query:
modify_by_patch: true
custom_code:
custom_import: 'templates/terraform/custom_import/access_context_manager_service_perimeter_resource.go.tmpl'
post_read: 'templates/terraform/post_read/access_context_manager_service_perimeter_resource.go.tmpl'
exclude_tgc: true
# Skipping the sweeper due to the non-standard base_url and because this is fine-grained under ServicePerimeter
exclude_sweeper: true
Expand Down
3 changes: 3 additions & 0 deletions mmv1/templates/terraform/nested_query.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ func resource{{ $.ResourceName }}ListForPatch(d *schema.ResourceData, meta inter
return nil, err
}

{{- if $.CustomCode.PostRead }}
{{ $.CustomTemplate $.CustomCode.PostRead false -}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the CustomTemplate function is from here: https://github.com/GoogleCloudPlatform/magic-modules/blob/main/mmv1/api/resource.go#L1510

the false will toggle off the option for appending a new line - a feature we really only cared about for an older migration and should not be relevant either way for you

the - tells the go template to delete any whitespace after the statement. Getting an example working with this should show how that ends up looking

{{- end }}
var v interface{}
var ok bool
{{- range $i, $k := $.NestedQuery.Keys }}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// post_read template for access_context_manager_service_perimeter_resource

// this is a placeholder for now but in the future we can use this to access
// the etag from the read response
3 changes: 3 additions & 0 deletions mmv1/templates/terraform/resource.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ 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 }}
{{ $.CustomTemplate $.CustomCode.PostRead false -}}
{{- end }}

{{if $.NestedQuery -}}
res, err = flattenNested{{ $.ResourceName -}}(d, meta, res)
Expand Down
Loading