Skip to content

Commit

Permalink
Rewrite iam data source template with Go (GoogleCloudPlatform#10782)
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 authored and pcostell committed Jul 16, 2024
1 parent 56fb645 commit 0d984f4
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
8 changes: 8 additions & 0 deletions mmv1/provider/template_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ func (td *TemplateData) GenerateIamResourceDocumentationFile(filePath string, re
td.GenerateFile(filePath, templatePath, resource, false, templates...)
}

func (td *TemplateData) GenerateIamDatasourceDocumentationFile(filePath string, resource api.Resource) {
templatePath := "templates/terraform/datasource_iam.html.markdown.tmpl"
templates := []string{
templatePath,
}
td.GenerateFile(filePath, templatePath, resource, false, templates...)
}

func (td *TemplateData) GenerateIamPolicyTestFile(filePath string, resource api.Resource) {
}

Expand Down
4 changes: 2 additions & 2 deletions mmv1/provider/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ func (t *Terraform) GenerateIamDocumentation(object api.Resource, templateData T
if err := os.MkdirAll(datasourceDocFolder, os.ModePerm); err != nil {
log.Println(fmt.Errorf("error creating parent directory %v: %v", datasourceDocFolder, err))
}
// targetFilePath = path.Join(datasourceDocFolder, fmt.Sprintf("%s_iam.html.markdown", t.FullResourceName(object)))
// templateData.GenerateIamDatasourceDocumentationFile(targetFilePath, object)
targetFilePath = path.Join(datasourceDocFolder, fmt.Sprintf("%s_iam.html.markdown", t.FullResourceName(object)))
templateData.GenerateIamDatasourceDocumentationFile(targetFilePath, object)
}

func (t *Terraform) FolderName() string {
Expand Down
112 changes: 112 additions & 0 deletions mmv1/templates/terraform/datasource_iam.html.markdown.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{{/* The license inside this block applies to this file
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. */ -}}
{{- /* NOTE NOTE NOTE
The newlines in this file are *load bearing*. This file outputs
Markdown, which is extremely sensitive to newlines. You have got
to have a newline after every attribute and property, because
otherwise MD will think the next element is part of the previous
property's bullet point. You cannot have any double newlines in the
middle of a property or attribute, because MD will think that the
empty line ends the bullet point and the indentation will be off.
You must have a newline before and after all --- document indicators,
and you must have a newline before and after all - - - hlines.
You cannot have more than one blank line between properties.
The --- document indicator must be the first line of the file.
As long as you only use `build_property_documentation`, it all works
fine - but when you need to add custom docs (notes, etc), you need
to remember these things.

Know also that the `lines` function in heavy use in MagicModules will
strip exactly one trailing newline - unless that's what you've designed
your docstring for, it's easier to insert newlines where you need them
manually. That's why, in this file, we use `lines` on anything which
is generated from a ruby function, but skip it on anything that is
directly inserted from YAML. */ -}}
---
# ----------------------------------------------------------------------------
#
# *** AUTO GENERATED CODE *** Type: MMv1 ***
#
# ----------------------------------------------------------------------------
#
# This file is automatically generated by Magic Modules and manual
# changes will be clobbered when the file is regenerated.
#
# Please read more about how to change this file in
# .github/CONTRIBUTING.md.
#
# ----------------------------------------------------------------------------
subcategory: "{{$.ProductMetadata.DisplayName}}"
description: |-
A datasource to retrieve the IAM policy state for {{$.ProductMetadata.DisplayName}} {{$.Name}}
---


# `{{ $.IamTerraformName }}_policy`
Retrieves the current IAM policy data for {{ lower $.Name }}

{{ if or (eq $.MinVersionObj.Name "beta") (eq $.IamPolicy.MinVersion "beta") }}
~> **Warning:** This datasource is in beta, and should be used with the terraform-provider-google-beta provider.
See [Provider Versions](https://terraform.io/docs/providers/google/guides/provider_versions.html) for more details on beta resources.
{{- end }}

## example

```hcl
data "{{ $.IamTerraformName }}_policy" "policy" {
{{ if eq $.MinVersionObj.Name "beta" }}
provider = google-beta
{{- end }}
{{- $.CustomTemplate $.IamPolicy.ExampleConfigBody}}
}
```

## Argument Reference

The following arguments are supported:

{{ range $param := $.IamSelfLinkProperties }}
{{- if eq $param.Name "name" -}}
* `{{if $.IamPolicy.ParentResourceAttribute}}{{$.IamPolicy.ParentResourceAttribute}}{{else}}{{underscore $.Name}}{{end}}` - (Required) Used to find the parent resource to bind the IAM policy to
{{- else if or (eq (underscore $param.Name) "region") (eq (underscore $param.Name) "zone") }}
* `{{ underscore $param.Name }}` - (Optional) {{ $param.Description }} Used to find the parent resource to bind the IAM policy to. If not specified,
the value will be parsed from the identifier of the parent resource. If no {{ underscore $param.Name }} is provided in the parent identifier and no
{{ underscore $param.Name }} is specified, it is taken from the provider configuration.
{{- else }}
* `{{ underscore $param.Name }}` - (Required) {{ $param.Description }} Used to find the parent resource to bind the IAM policy to
{{- end }}
{{- end }}
{{- if $.IamPolicy.BaseUrl }}
{{- if contains $.IamPolicy.BaseUrl "{{project}}" }}
{{- /* The following new line allow for project to be bullet-formatted properly. */}}

* `project` - (Optional) The ID of the project in which the resource belongs.
If it is not provided, the project will be parsed from the identifier of the parent resource. If no project is provided in the parent identifier and no project is specified, the provider project is used.
{{- end }}
{{- else if contains $.BaseUrl "{{project}}" }}
{{- /* The following new line allow for project to be bullet-formatted properly. */}}

* `project` - (Optional) The ID of the project in which the resource belongs.
If it is not provided, the project will be parsed from the identifier of the parent resource. If no project is provided in the parent identifier and no project is specified, the provider project is used.
{{- end }}

## Attributes Reference

The attributes are exported:

* `etag` - (Computed) The etag of the IAM policy.

* `policy_data` - (Required only by `{{ $.IamTerraformName }}_policy`) The policy data generated by
a `google_iam_policy` data source.

0 comments on commit 0d984f4

Please sign in to comment.