-
Notifications
You must be signed in to change notification settings - Fork 2.3k
MMv1: Add list-resource Generation
#17514
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
Changes from 1 commit
4533c2a
63b0154
711b9b7
c2e4616
3d1797b
f146f4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -245,6 +245,8 @@ type Resource struct { | |||||||||||||||||||||||||||||||
| // EXPERIMENTAL: If true, resource should be autogenerated as a data source | ||||||||||||||||||||||||||||||||
| Datasource *resource.Datasource `yaml:"datasource_experimental,omitempty"` | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| GenerateListResource bool `yaml:"generate_list_resource,omitempty"` | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // If true, skip sweeper generation for this resource | ||||||||||||||||||||||||||||||||
| ExcludeSweeper bool `yaml:"exclude_sweeper,omitempty"` | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
@@ -715,6 +717,25 @@ func (r Resource) IdentityProperties() []*Type { | |||||||||||||||||||||||||||||||
| return props | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| func (r Resource) ListScopeProperties() []*Type { | ||||||||||||||||||||||||||||||||
| scope := r.ExtractIdentifiers(r.CollectionUrl()) | ||||||||||||||||||||||||||||||||
| return google.Select(r.IdentityProperties(), func(p *Type) bool { | ||||||||||||||||||||||||||||||||
| return slices.Contains(scope, google.Underscore(p.Name)) | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| func (r Resource) ListResultDisplayNameKeyStrings() []string { | ||||||||||||||||||||||||||||||||
| var keys []string | ||||||||||||||||||||||||||||||||
| if slices.ContainsFunc(r.RootProperties(), func(p *Type) bool { return p.Name == "display_name" }) { | ||||||||||||||||||||||||||||||||
| keys = append(keys, "display_name") | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| markers := regexp.MustCompile(`\{\{(\w+)\}\}`).FindAllStringSubmatch(r.IdFormat, -1) | ||||||||||||||||||||||||||||||||
| if len(markers) > 0 { | ||||||||||||||||||||||||||||||||
| keys = append(keys, markers[len(markers)-1][1]) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| return keys | ||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be length checking
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think a length check is necessary since we do that within the Capture function. in generated resources the capture method takes the value returned from `ListResultDisplayNameKeyStrings1 magic-modules/mmv1/third_party/terraform/acctest/test_utils.go.tmpl Lines 102 to 116 in f42abb2
|
||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| func (r Resource) SensitiveProps() []*Type { | ||||||||||||||||||||||||||||||||
| props := r.AllNestedProperties(r.RootProperties()) | ||||||||||||||||||||||||||||||||
| return google.Select(props, func(p *Type) bool { | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| {{/* The license inside this block applies to this file | ||
| Copyright 2024 Google LLC. All Rights Reserved. | ||
|
BBBmau marked this conversation as resolved.
Outdated
|
||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. */ -}} | ||
| // Copyright IBM Corp. 2014, 2026 | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| {{$.CodeHeader TemplatePath}} | ||
|
|
||
| package {{ lower $.ProductMetadata.Name }} | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/diag" | ||
| "github.com/hashicorp/terraform-plugin-framework/list" | ||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
|
|
||
| "{{ $.ImportPath }}/registry" | ||
| "{{ $.ImportPath }}/tpgresource" | ||
| transport_tpg "{{ $.ImportPath }}/transport" | ||
| ) | ||
|
|
||
| func init() { | ||
| registry.FrameworkListResource{ | ||
| Name: "{{ $.TerraformName }}", | ||
| ProductName: "{{ $.ProductMetadata.Name }}", | ||
| Func: New{{ $.ResourceName -}}ListResource, | ||
| }.Register() | ||
| } | ||
|
|
||
| var _ list.ListResource = &{{ $.ResourceName -}}ListResource{} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated to your PR, but why is the official name a list RESOURCE In my mind resources vs data sources were separated by the CUD parts of CRUD isn't this more of a list data source? :)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good question, it's been tricky navigating naming since i do agree we aren't technically creating a resource but more calling existing resources (which exactly matches what data source is) List Source probably would've been better? Reviewed the list resources docs and mentions the following
|
||
|
|
||
| type {{ $.ResourceName -}}ListResource struct { | ||
| tpgresource.ListResourceMetadata | ||
| } | ||
|
|
||
| func New{{ $.ResourceName -}}ListResource() list.ListResource { | ||
| listR := &{{ $.ResourceName -}}ListResource{} | ||
| listR.TypeName = "{{ $.TerraformName }}" | ||
| listR.SDKv2Resource = Resource{{ $.ResourceName -}}() | ||
| listR.ListConfigFields = []tpgresource.ListConfigField{ | ||
| {{- range $scope := $.ListScopeProperties }} | ||
| {Name: "{{ underscore $scope.Name }}", Kind: tpgresource.ListConfigKindString, Optional: {{ if $scope.Required }}false{{ else }}true{{ end }}}, | ||
| {{- end }} | ||
| } | ||
| return listR | ||
| } | ||
|
|
||
| // {{ $.ResourceName }}ListModel matches ListResourceMetadata.ListConfigFields (tfsdk names and types). | ||
| type {{ $.ResourceName -}}ListModel struct { | ||
| {{- range $scope := $.ListScopeProperties }} | ||
| {{ $scope.TitlelizeProperty }} types.String `tfsdk:"{{ underscore $scope.Name }}"` | ||
| {{- end }} | ||
| } | ||
|
|
||
| func (listR *{{ $.ResourceName -}}ListResource) List(ctx context.Context, listReq list.ListRequest, stream *list.ListResultsStream) { | ||
| var data {{ $.ResourceName -}}ListModel | ||
| diags := listReq.Config.Get(ctx, &data) | ||
| if diags.HasError() { | ||
| stream.Results = list.ListResultsStreamDiagnostics(diags) | ||
| return | ||
| } | ||
| if listR.Client == nil { | ||
| diags = append(diags, diag.NewErrorDiagnostic( | ||
| "Provider not configured", | ||
| "The Google provider client is not available; ensure the provider is configured (e.g. credentials and default project).", | ||
| )) | ||
| stream.Results = list.ListResultsStreamDiagnostics(diags) | ||
| return | ||
| } | ||
|
|
||
| {{- range $scope := $.ListScopeProperties }} | ||
| {{- if or (eq $scope.Name "project") (eq $scope.Name "region") (eq $scope.Name "zone") (eq $scope.Name "location") }} | ||
| {{ $scope.CamelizeProperty }} := listR.Get{{ $scope.TitlelizeProperty }}(data.{{ $scope.TitlelizeProperty }}) | ||
| {{- else }} | ||
| {{ $scope.CamelizeProperty }} := data.{{ $scope.TitlelizeProperty }}.ValueString() | ||
| {{- end }} | ||
| {{- end }} | ||
|
|
||
| stream.Results = func(push func(list.ListResult) bool) { | ||
| err := List{{ $.ResourceName }}s( | ||
| listR.Client, | ||
| {{- range $scope := $.ListScopeProperties }} | ||
| {{ $scope.CamelizeProperty }}, | ||
| {{- end }} | ||
| func(rd *schema.ResourceData) error { | ||
| result := listReq.NewListResult(ctx) | ||
|
|
||
| if err := listR.SetResult(ctx, listReq.IncludeResource, &result, rd{{- range $k := $.ListResultDisplayNameKeyStrings }}, "{{ $k }}"{{- end }}); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !push(result) { | ||
| return errors.New("stream closed") | ||
| } | ||
| return nil | ||
| }, | ||
| ) | ||
| if err != nil { | ||
| diags.AddError("API Error", err.Error()) | ||
| result := listReq.NewListResult(ctx) | ||
| result.Diagnostics = diags | ||
| push(result) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| {{ template "listResourceMethod" $ }} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| {{- /* Copyright 2024 Google LLC. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. */ -}} | ||
| --- | ||
| {{$.MarkdownHeader TemplatePath}} | ||
| subcategory: "{{$.ProductMetadata.DisplayName}}" | ||
| description: |- | ||
| List {{$.ProductMetadata.DisplayName}} {{lower $.Name}} resources in a project for use with terraform query | ||
| and .tfquery.hcl files. | ||
| --- | ||
|
|
||
| # {{$.TerraformName}} (list) | ||
|
|
||
| Lists [`{{$.TerraformName}}`](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/{{$.TerraformName}}) resources for use with [`terraform query`](https://developer.hashicorp.com/terraform/cli/commands/query) and **`.tfquery.hcl`** files. | ||
|
|
||
| For how list resources work in this provider, file layout, Terraform version requirements, and shared `list` block arguments, refer to the guide [Use list resources with terraform query (Google Cloud provider)](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/using_list_resources_with_terraform_query). | ||
|
|
||
| ## Example | ||
|
|
||
| ```hcl | ||
| list "{{$.TerraformName}}" "all" { | ||
| provider = {{ if eq $.MinVersion "beta" }}google-beta{{ else }}google{{ end }} | ||
|
|
||
| config { | ||
| {{- range $scope := $.ListScopeProperties }} | ||
| {{ underscore $scope.Name }} = {{ if $scope.Required }}"..."{{ else }}"..." # Optional{{ end }} | ||
| {{- end }} | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Run `terraform query` from the directory that contains the `.tfquery.hcl` file. | ||
|
|
||
| ## Configuration (`config` block) | ||
|
|
||
| {{- range $scope := $.ListScopeProperties }} | ||
| * `{{ underscore $scope.Name }}` - ({{ if $scope.Required }}Required{{ else }}Optional{{ end }}){{ if $scope.Description }} {{ $scope.Description }}{{ end }} | ||
| {{- end }} | ||
|
|
||
| ## Results | ||
|
|
||
| By default each result includes **resource identity** for [`{{$.TerraformName}}`](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/{{$.TerraformName}}) (see [Resource identity](https://developer.hashicorp.com/terraform/language/resources/identities)). | ||
|
|
||
| With `include_resource = true` on the `list` block, results also include the full resource-style attributes documented for the managed [`{{$.TerraformName}}` resource](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/{{$.TerraformName}}#attributes-reference). |
Uh oh!
There was an error while loading. Please reload this page.