From b015113f030d7032f4365009a00e0f78544fd3ca Mon Sep 17 00:00:00 2001 From: Ryan Oaks Date: Wed, 13 May 2026 09:53:56 -0400 Subject: [PATCH] Generate docs for datasources --- mmv1/api/resource.go | 20 ++--- mmv1/provider/template_data.go | 10 +++ mmv1/provider/terraform.go | 18 +++-- .../terraform/datasource.html.markdown.tmpl | 78 +++++++++++++++++++ .../docs/d/cloud_run_service.html.markdown | 37 --------- .../website/docs/d/iap_client.html.markdown | 34 -------- ...l_folder_intelligence_config.html.markdown | 32 -------- ...nization_intelligence_config.html.markdown | 32 -------- ..._project_intelligence_config.html.markdown | 32 -------- .../d/vmwareengine_datastore_html.markdown | 32 -------- 10 files changed, 107 insertions(+), 218 deletions(-) create mode 100644 mmv1/templates/terraform/datasource.html.markdown.tmpl delete mode 100644 mmv1/third_party/terraform/website/docs/d/cloud_run_service.html.markdown delete mode 100644 mmv1/third_party/terraform/website/docs/d/iap_client.html.markdown delete mode 100644 mmv1/third_party/terraform/website/docs/d/storage_control_folder_intelligence_config.html.markdown delete mode 100644 mmv1/third_party/terraform/website/docs/d/storage_control_organization_intelligence_config.html.markdown delete mode 100644 mmv1/third_party/terraform/website/docs/d/storage_control_project_intelligence_config.html.markdown delete mode 100644 mmv1/third_party/terraform/website/docs/d/vmwareengine_datastore_html.markdown diff --git a/mmv1/api/resource.go b/mmv1/api/resource.go index bffb7033da2b..3cdf8ede991d 100644 --- a/mmv1/api/resource.go +++ b/mmv1/api/resource.go @@ -2494,14 +2494,10 @@ func (r Resource) ShouldDatasourceSetAnnotations() bool { // that should be marked as "Required". func (r Resource) DatasourceRequiredFields() []string { requiredFields := []string{} - uriParts := strings.Split(r.IdFormat, "/") - for _, part := range uriParts { - if strings.HasPrefix(part, "{{") && strings.HasSuffix(part, "}}") { - field := strings.TrimSuffix(strings.TrimPrefix(part, "{{"), "}}") - if field != "region" && field != "project" && field != "zone" { - requiredFields = append(requiredFields, field) - } + for _, field := range r.ExtractIdentifiers(r.IdFormat) { + if field != "region" && field != "project" && field != "zone" { + requiredFields = append(requiredFields, field) } } return requiredFields @@ -2511,14 +2507,10 @@ func (r Resource) DatasourceRequiredFields() []string { // that should be marked as "Optional". func (r Resource) DatasourceOptionalFields() []string { optionalFields := []string{} - uriParts := strings.Split(r.IdFormat, "/") - for _, part := range uriParts { - if strings.HasPrefix(part, "{{") && strings.HasSuffix(part, "}}") { - field := strings.TrimSuffix(strings.TrimPrefix(part, "{{"), "}}") - if field == "region" || field == "project" || field == "zone" { - optionalFields = append(optionalFields, field) - } + for _, field := range r.ExtractIdentifiers(r.IdFormat) { + if field == "region" || field == "project" || field == "zone" { + optionalFields = append(optionalFields, field) } } return optionalFields diff --git a/mmv1/provider/template_data.go b/mmv1/provider/template_data.go index 55183ca1d1fe..2dae5646d37d 100644 --- a/mmv1/provider/template_data.go +++ b/mmv1/provider/template_data.go @@ -122,6 +122,16 @@ func (td *TemplateData) GenerateDocumentationFile(filePath string, resource api. td.GenerateFile(filePath, templatePath, resource, false, templates...) } +func (td *TemplateData) GenerateDataSourceDocumentationFile(filePath string, resource api.Resource) { + templatePath := "templates/terraform/datasource.html.markdown.tmpl" + templates := []string{ + templatePath, + "templates/terraform/property_documentation.html.markdown.tmpl", + "templates/terraform/nested_property_documentation.html.markdown.tmpl", + } + td.GenerateFile(filePath, templatePath, resource, false, templates...) +} + func (td *TemplateData) GenerateTestFileLegacy(filePath string, resource api.Resource) { templatePath := "templates/terraform/examples/base_configs/test_file.go.tmpl" templates := []string{ diff --git a/mmv1/provider/terraform.go b/mmv1/provider/terraform.go index 7731668970f1..b773d0f85ad0 100644 --- a/mmv1/provider/terraform.go +++ b/mmv1/provider/terraform.go @@ -104,12 +104,12 @@ func (t *Terraform) GenerateObject(object api.Resource, outputFolder, productPat if !object.IsExcluded() { log.Printf("Generating %s resource", object.Name) t.GenerateResource(object, *templateData, outputFolder, generateCode, generateDocs) + t.GenerateSingularDataSource(object, *templateData, outputFolder, generateCode, generateDocs) if generateCode { // log.Printf("Generating %s tests", object.Name) t.GenerateResourceTests(object, *templateData, outputFolder) t.GenerateResourceSweeper(object, *templateData, outputFolder) - t.GenerateSingularDataSource(object, *templateData, outputFolder) t.GenerateSingularDataSourceTests(object, *templateData, outputFolder) // log.Printf("Generating %s metadata", object.Name) t.GenerateResourceMetadata(object, *templateData, outputFolder) @@ -273,14 +273,22 @@ func (t *Terraform) GenerateResourceSweeperFile(object api.Resource, targetFileP templateData.GenerateSweeperFile(targetFilePath, object) } -func (t *Terraform) GenerateSingularDataSource(object api.Resource, templateData TemplateData, outputFolder string) { +func (t *Terraform) GenerateSingularDataSource(object api.Resource, templateData TemplateData, outputFolder string, generateCode, generateDocs bool) { if !object.ShouldGenerateSingularDataSource() { return } - targetFolder := t.makeFolder(outputFolder, t.FolderName(), "services", t.Product.ApiName) - targetFilePath := path.Join(targetFolder, fmt.Sprintf("data_source_%s.go", t.ResourceGoFilename(object))) - templateData.GenerateDataSourceFile(targetFilePath, object) + if generateCode { + targetFolder := t.makeFolder(outputFolder, t.FolderName(), "services", t.Product.ApiName) + targetFilePath := path.Join(targetFolder, fmt.Sprintf("data_source_%s.go", t.ResourceGoFilename(object))) + templateData.GenerateDataSourceFile(targetFilePath, object) + } + + if generateDocs { + targetFolder := t.makeFolder(outputFolder, "website", "docs", "d") + targetFilePath := path.Join(targetFolder, fmt.Sprintf("%s.html.markdown", t.FullResourceName(object))) + templateData.GenerateDataSourceDocumentationFile(targetFilePath, object) + } } func (t *Terraform) GenerateSingularDataSourceTestsLegacy(object api.Resource, templateData TemplateData, outputFolder string) { diff --git a/mmv1/templates/terraform/datasource.html.markdown.tmpl b/mmv1/templates/terraform/datasource.html.markdown.tmpl new file mode 100644 index 000000000000..f15e39da25bf --- /dev/null +++ b/mmv1/templates/terraform/datasource.html.markdown.tmpl @@ -0,0 +1,78 @@ +{{- /* Copyright 2026 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: |- + Get information about a {{$.ProductMetadata.DisplayName}} {{$.Name}}. +--- + +# {{$.TerraformName}} + +Get information about a {{$.ProductMetadata.DisplayName}} {{$.Name}}. +{{ if eq $.MinVersion "beta"}} +~> **Warning:** This datasource is in beta, and should be used with the terraform-provider-google-beta provider. +See [Provider Versions](../guides/provider_versions.html.markdown) for more details on beta resources. +{{- end }} +{{ if and $.References.Api (index $.References.Guides "Official Documentation") }} +For more information see the [official documentation]({{index $.References.Guides "Official Documentation"}}) and +the [API]({{$.References.Api}}). +{{- end }} + +## Example Usage + +```hcl +{{- if $.Samples }} +{{- $sample := index $.Samples 0 }} +data "{{$.TerraformName}}" "default" { +{{- if eq $.MinVersionObj.Name "beta" }} + provider = google-beta +{{- end }} +{{- range $fieldName := $.DatasourceRequiredFields }} + {{ $fieldName }} = {{ $sample.ResourceType $.TerraformName }}.{{ $sample.PrimaryResourceId }}.{{ $fieldName }} +{{- end }} +} +{{- end }} +``` + +## Argument Reference + +The following arguments are supported: + +{{ "" }} +{{ "" }} +{{- range $p := $.AllUserProperties }} + {{- range $field := $.DatasourceRequiredFields }} + {{- if eq (underscore $p.Name) (underscore $field) }} + {{- trimTemplate "property_documentation.html.markdown.tmpl" $p -}} + {{- end }} + {{- end }} +{{- end }} +{{ "" }} +{{- range $p := $.AllUserProperties }} + {{- range $field := $.DatasourceOptionalFields }} + {{- if eq (underscore $p.Name) (underscore $field) }} + {{- trimTemplate "property_documentation.html.markdown.tmpl" $p -}} + {{- end }} + {{- end }} +{{- end }} +{{- if or (contains $.BaseUrl "{{project}}") (contains $.CreateUrl "{{project}}")}} +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. +{{ "" }} +{{- end }} + +## Attributes Reference + +See [{{$.TerraformName}}](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/{{replaceAll $.TerraformName "google_" ""}}#argument-reference) resource for details of the available attributes. diff --git a/mmv1/third_party/terraform/website/docs/d/cloud_run_service.html.markdown b/mmv1/third_party/terraform/website/docs/d/cloud_run_service.html.markdown deleted file mode 100644 index dbd57f2ec63a..000000000000 --- a/mmv1/third_party/terraform/website/docs/d/cloud_run_service.html.markdown +++ /dev/null @@ -1,37 +0,0 @@ ---- -subcategory: "Cloud Run" -description: |- - Get information about a Google Cloud Run Service. ---- - -# google_cloud_run_service - -Get information about a Google Cloud Run Service. For more information see -the [official documentation](https://cloud.google.com/run/docs/) -and [API](https://cloud.google.com/run/docs/apis). - -## Example Usage - -```hcl -data "google_cloud_run_service" "run-service" { - name = "my-service" - location = "us-central1" -} -``` - -## Argument Reference - -The following arguments are supported: - -* `name` - (Required) The name of the Cloud Run Service. - -* `location` - (Required) The location of the cloud run instance. eg us-central1 - -- - - - -* `project` - (Optional) The project in which the resource belongs. If it - is not provided, the provider project is used. - -## Attributes Reference - -See [google_cloud_run_service](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_run_service#argument-reference) resource for details of the available attributes. diff --git a/mmv1/third_party/terraform/website/docs/d/iap_client.html.markdown b/mmv1/third_party/terraform/website/docs/d/iap_client.html.markdown deleted file mode 100644 index 4fbd593b894b..000000000000 --- a/mmv1/third_party/terraform/website/docs/d/iap_client.html.markdown +++ /dev/null @@ -1,34 +0,0 @@ ---- -subcategory: "Identity-Aware Proxy" -description: |- - Contains the data that describes an Identity Aware Proxy owned client. ---- -# google_iap_client - -Get info about a Google Cloud IAP Client. - -## Example Usage - -```tf -data "google_project" "project" { - project_id = "foobar" -} - -data "google_iap_client" "project_client" { - brand = "projects/${data.google_project.project.number}/brands/[BRAND_NUMBER]" - client_id = FOO.apps.googleusercontent.com -} - -``` - -## Argument Reference - -The following arguments are supported: - -* `brand` - (Required) The name of the brand. - -* `client_id` - (Required) The client_id of the brand. - -## Attributes Reference - -See [google_iap_client](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/iap_client) resource for details of the available attributes. diff --git a/mmv1/third_party/terraform/website/docs/d/storage_control_folder_intelligence_config.html.markdown b/mmv1/third_party/terraform/website/docs/d/storage_control_folder_intelligence_config.html.markdown deleted file mode 100644 index 5ad88b38d6f2..000000000000 --- a/mmv1/third_party/terraform/website/docs/d/storage_control_folder_intelligence_config.html.markdown +++ /dev/null @@ -1,32 +0,0 @@ ---- -subcategory: "Cloud Storage Control" -description: |- - Gets a Folder Storage Intelligence config. ---- - -# google_storage_control_folder_intelligence_config - -Use this data source to get information about a Folder Storage Intelligence config resource. -See [the official documentation](https://cloud.google.com/storage/docs/storage-intelligence/overview#resource) -and -[API](https://cloud.google.com/storage/docs/json_api/v1/intelligenceConfig). - - -## Example Usage - -```hcl -data "google_storage_control_folder_intelligence_config" "sample-config" { - name = "123456789" -} -``` - -## Argument Reference - -The following arguments are supported: - -* `name` - (Required) The number of GCP folder. - - -## Attributes Reference - -See [google_storage_control_folder_intelligence_config](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_control_folder_intelligence_config#argument-reference) resource for details of the available attributes. diff --git a/mmv1/third_party/terraform/website/docs/d/storage_control_organization_intelligence_config.html.markdown b/mmv1/third_party/terraform/website/docs/d/storage_control_organization_intelligence_config.html.markdown deleted file mode 100644 index 2fb52fa2d2af..000000000000 --- a/mmv1/third_party/terraform/website/docs/d/storage_control_organization_intelligence_config.html.markdown +++ /dev/null @@ -1,32 +0,0 @@ ---- -subcategory: "Cloud Storage Control" -description: |- - Gets a Organization Storage Intelligence config. ---- - -# google_storage_control_organization_intelligence_config - -Use this data source to get information about a Organization Storage Intelligence config resource. -See [the official documentation](https://cloud.google.com/storage/docs/storage-intelligence/overview#resource) -and -[API](https://cloud.google.com/storage/docs/json_api/v1/intelligenceConfig). - - -## Example Usage - -```hcl -data "google_storage_control_organization_intelligence_config" "sample-config" { - name = "123456789" -} -``` - -## Argument Reference - -The following arguments are supported: - -* `name` - (Required) The id of GCP organization. - - -## Attributes Reference - -See [google_storage_control_organization_intelligence_config](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_control_organization_intelligence_config#argument-reference) resource for details of the available attributes. diff --git a/mmv1/third_party/terraform/website/docs/d/storage_control_project_intelligence_config.html.markdown b/mmv1/third_party/terraform/website/docs/d/storage_control_project_intelligence_config.html.markdown deleted file mode 100644 index c687cdea7e10..000000000000 --- a/mmv1/third_party/terraform/website/docs/d/storage_control_project_intelligence_config.html.markdown +++ /dev/null @@ -1,32 +0,0 @@ ---- -subcategory: "Cloud Storage Control" -description: |- - Gets a Project Storage Intelligence config. ---- - -# google_storage_control_project_intelligence_config - -Use this data source to get information about a Project Storage Intelligence config resource. -See [the official documentation](https://cloud.google.com/storage/docs/storage-intelligence/overview#resource) -and -[API](https://cloud.google.com/storage/docs/json_api/v1/intelligenceConfig). - - -## Example Usage - -```hcl -data "google_storage_control_project_intelligence_config" "sample-config" { - name = "my-project" -} -``` - -## Argument Reference - -The following arguments are supported: - -* `name` - (Required) The name or number of the GCP project. - - -## Attributes Reference - -See [google_storage_control_project_intelligence_config](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_control_project_intelligence_config#argument-reference) resource for details of the available attributes. diff --git a/mmv1/third_party/terraform/website/docs/d/vmwareengine_datastore_html.markdown b/mmv1/third_party/terraform/website/docs/d/vmwareengine_datastore_html.markdown deleted file mode 100644 index bc5f2cd1b422..000000000000 --- a/mmv1/third_party/terraform/website/docs/d/vmwareengine_datastore_html.markdown +++ /dev/null @@ -1,32 +0,0 @@ ---- -subcategory: "Cloud VMware Engine" -description: |- - Get information about a datastore. ---- - -# google\_vmwareengine\_datastore - -Use this data source to get details about a datastore resource. - -To get more information about datastore, see: -* [API documentation](https://docs.cloud.google.com/vmware-engine/docs/vmware-ecosystem/concepts-nfs-datastores-overview) - -## Example Usage - -```hcl -data "google_vmwareengine_datastore" "test_ds" { - name = "example-ds" - location = "us-west2" -} -``` - -## Argument Reference - -The following arguments are supported: - -* `name` - (Required) Name of the resource. -* `location` - (Required) either regional or zonal location of the resource. - -## Attributes Reference - -See [google_vmwareengine_datastore](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/vmwareengine_datastore#attributes-reference) resource for details of all the available attributes.