Skip to content

Commit 69163cd

Browse files
authored
Add Support for Provider-defined Function Documentation (#328)
* Implement `functionmd` package * Implement provider-defined function support for `generate` command * Fix spacing for `functionmd.RenderFunctions()` output * Update README.md * Implement provider-defined function support for `migrate` command * Add copywrite headers * Add `templates` directory to list of accepted directory names in `validate` * Update `provider-build` acceptance tests * Add Changelog entries * Update README.md to include Terraform binary requirement for functions
1 parent 56c7fae commit 69163cd

26 files changed

+1107
-39
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: ENHANCEMENTS
2+
body: 'validate: Add `functions` to list of allowed template and rendered website
3+
subdirectories'
4+
time: 2024-01-22T16:03:17.035362-05:00
5+
custom:
6+
Issue: "328"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: FEATURES
2+
body: 'generate: Add support for Provider-defined Function documentation'
3+
time: 2024-01-22T16:00:26.052538-05:00
4+
custom:
5+
Issue: "328"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: FEATURES
2+
body: 'migrate: Add support for Provider-defined Function documentation'
3+
time: 2024-01-22T16:00:50.279982-05:00
4+
custom:
5+
Issue: "328"

README.md

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ When you run `tfplugindocs`, by default from the root directory of a provider co
105105
* Generate a default provider template file, if missing (**index.md**)
106106
* Generate resource template files, if missing
107107
* Generate data source template files, if missing
108+
* Generate function template files, if missing (Requires Terraform v1.8.0+)
108109
* Copy all non-template files to the output website directory
109110
* Process all the remaining templates to generate files for the output website directory
110111
@@ -165,19 +166,21 @@ The `migrate` subcommand takes the following actions:
165166

166167
The generation of missing documentation is based on a number of assumptions / conventional paths.
167168

168-
> **NOTE:** In the following conventional paths, `<data source name>` and `<resource name>` include the provider prefix as well.
169+
> **NOTE:** In the following conventional paths, `<data source name>` and `<resource name>` include the provider prefix as well, but the provider prefix is **NOT** included in`<function name>`.
169170
> For example, the data source [`caller_identity`](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) in the `aws` provider would have an "example" conventional path of: `examples/data-sources/aws_caller_identity/data-source.tf`
170171

171172
For templates:
172173

173-
| Path | Description |
174-
|-----------------------------------------------------------|----------------------------------------|
175-
| `templates/` | Root of templated docs |
176-
| `templates/index.md[.tmpl]` | Docs index page (or template) |
177-
| `templates/data-sources.md[.tmpl]` | Generic data source page (or template) |
178-
| `templates/data-sources/<data source name>.md[.tmpl]` | Data source page (or template) |
179-
| `templates/resources.md[.tmpl]` | Generic resource page (or template) |
180-
| `templates/resources/<resource name>.md[.tmpl]` | Resource page (or template) |
174+
| Path | Description |
175+
|-------------------------------------------------------|----------------------------------------|
176+
| `templates/` | Root of templated docs |
177+
| `templates/index.md[.tmpl]` | Docs index page (or template) |
178+
| `templates/data-sources.md[.tmpl]` | Generic data source page (or template) |
179+
| `templates/data-sources/<data source name>.md[.tmpl]` | Data source page (or template) |
180+
| `templates/functions.md[.tmpl]` | Generic function page (or template) |
181+
| `templates/functions/<function name>.md[.tmpl]` | Function page (or template) |
182+
| `templates/resources.md[.tmpl]` | Generic resource page (or template) |
183+
| `templates/resources/<resource name>.md[.tmpl]` | Resource page (or template) |
181184

182185
Note: the `.tmpl` extension is necessary, for the file to be correctly handled as a template.
183186

@@ -188,6 +191,7 @@ For examples:
188191
| `examples/` | Root of examples |
189192
| `examples/provider/provider.tf` | Provider example config |
190193
| `examples/data-sources/<data source name>/data-source.tf` | Data source example config |
194+
| `examples/functions/<function name>/function.tf` | Function example config |
191195
| `examples/resources/<resource name>/resource.tf` | Resource example config |
192196
| `examples/resources/<resource name>/import.sh` | Resource example import command |
193197

@@ -197,13 +201,14 @@ The `migrate` subcommand assumes the following conventional paths for the render
197201

198202
Legacy website directory structure:
199203

200-
| Path | Description |
201-
|---------------------------------------------------|-----------------------------|
202-
| `website/` | Root of website docs |
203-
| `website/docs/guides` | Root of guides subdirectory |
204-
| `website/docs/index.html.markdown` | Docs index page |
205-
| `website/docs/d/<data source name>.html.markdown` | Data source page |
206-
| `website/docs/r/<resource name>.html.markdown` | Resource page |
204+
| Path | Description |
205+
|-------------------------------------------------------|-----------------------------|
206+
| `website/` | Root of website docs |
207+
| `website/docs/guides` | Root of guides subdirectory |
208+
| `website/docs/index.html.markdown` | Docs index page |
209+
| `website/docs/d/<data source name>.html.markdown` | Data source page |
210+
| `website/docs/functons/<function name>.html.markdown` | Functions page |
211+
| `website/docs/r/<resource name>.html.markdown` | Resource page |
207212

208213
Docs website directory structure:
209214

@@ -213,6 +218,7 @@ Docs website directory structure:
213218
| `docs/guides` | Root of guides subdirectory |
214219
| `docs/index.html.markdown` | Docs index page |
215220
| `docs/data-sources/<data source name>.html.markdown` | Data source page |
221+
| `docs/functions/<function name>.html.markdown` | Function page |
216222
| `docs/resources/<resource name>.html.markdown` | Resource page |
217223

218224
Files named `index` (before the first `.`) in the website docs root directory and files in the `website/docs/d/`, `website/docs/r/`, `docs/data-sources/`,
@@ -229,7 +235,7 @@ using the following data fields and functions:
229235

230236
#### Data fields
231237

232-
##### Provider
238+
##### Provider Fields
233239

234240
| Field | Type | Description |
235241
|------------------------:|:------:|-------------------------------------------------------------------------------------------|
@@ -241,7 +247,7 @@ using the following data fields and functions:
241247
| `.RenderedProviderName` | string | Value provided via argument `--rendered-provider-name`, otherwise same as `.ProviderName` |
242248
| `.SchemaMarkdown` | string | a Markdown formatted Provider Schema definition |
243249

244-
##### Resources / Data Source
250+
##### Resources / Data Source Fields
245251

246252
| Field | Type | Description |
247253
|------------------------:|:------:|-------------------------------------------------------------------------------------------|
@@ -257,7 +263,25 @@ using the following data fields and functions:
257263
| `.RenderedProviderName` | string | Value provided via argument `--rendered-provider-name`, otherwise same as `.ProviderName` |
258264
| `.SchemaMarkdown` | string | a Markdown formatted Resource / Data Source Schema definition |
259265

260-
#### Functions
266+
##### Provider-defined Function Fields
267+
268+
| Field | Type | Description |
269+
|------------------------------------:|:------:|-------------------------------------------------------------------------------------------|
270+
| `.Name` | string | Name of the function (ex. `echo`) |
271+
| `.Type` | string | Returns `Function` |
272+
| `.Description` | string | Function description |
273+
| `.Summary` | string | Function summary |
274+
| `.HasExample` | bool | Is there an example file? |
275+
| `.ExampleFile` | string | Path to the file with the terraform configuration example |
276+
| `.ProviderName` | string | Canonical provider name (ex. `terraform-provider-random`) |
277+
| `.ProviderShortName` | string | Short version of the provider name (ex. `random`) |
278+
| `.RenderedProviderName` | string | Value provided via argument `--rendered-provider-name`, otherwise same as `.ProviderName` |
279+
| `.FunctionSignatureMarkdown` | string | a Markdown formatted Function signature |
280+
| `.FunctionArgumentsMarkdown` | string | a Markdown formatted Function arguments definition |
281+
| `.HasVariadic` | bool | Does this function have a variadic argument? |
282+
| `.FunctionVariadicArgumentMarkdown` | string | a Markdown formatted Function variadic argument definition |
283+
284+
#### Template Functions
261285

262286
| Function | Description |
263287
|-----------------|---------------------------------------------------------------------------------------------------|

cmd/tfplugindocs/testdata/scripts/provider-build/generate/framework_provider_success_generic_templates.txtar

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ generating missing resource content
2525
resource "scaffolding_example" fallback template exists, creating template
2626
generating missing data source content
2727
data-source "scaffolding_example" fallback template exists, creating template
28+
generating missing function content
2829
generating missing provider content
2930
provider "terraform-provider-scaffolding" template exists, skipping
3031
rendering static website

cmd/tfplugindocs/testdata/scripts/provider-build/generate/framework_provider_success_named_templates.txtar

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ generating missing resource content
2525
resource "scaffolding_example" template exists, skipping
2626
generating missing data source content
2727
data-source "scaffolding_example" template exists, skipping
28+
generating missing function content
2829
generating missing provider content
2930
provider "terraform-provider-scaffolding" template exists, skipping
3031
rendering static website

cmd/tfplugindocs/testdata/scripts/provider-build/generate/framework_provider_success_no_templates.txtar

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ generating missing resource content
2323
generating new template for "scaffolding_example"
2424
generating missing data source content
2525
generating new template for data-source "scaffolding_example"
26+
generating missing function content
2627
generating missing provider content
2728
generating new template for "terraform-provider-scaffolding"
2829
rendering static website

cmd/tfplugindocs/testdata/scripts/provider-build/generate/null_provider_success.txtar

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ generating missing resource content
2222
resource "null_resource" fallback template exists, creating template
2323
generating missing data source content
2424
data-source "null_data_source" fallback template exists, creating template
25+
generating missing function content
2526
generating missing provider content
2627
provider "terraform-provider-null" template exists, skipping
2728
rendering static website

cmd/tfplugindocs/testdata/scripts/schema-json/generate/framework_provider_success_generic_templates.txtar

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ cmp stdout expected-output.txt
99
cmpenv docs/index.md expected-index.md
1010
cmpenv docs/data-sources/example.md expected-datasource.md
1111
cmpenv docs/resources/example.md expected-resource.md
12+
cmpenv docs/functions/example.md expected-function.md
13+
1214

1315
-- expected-output.txt --
1416
rendering website for provider "terraform-provider-scaffolding" (as "terraform-provider-scaffolding")
@@ -20,12 +22,15 @@ generating missing resource content
2022
resource "scaffolding_example" fallback template exists, creating template
2123
generating missing data source content
2224
data-source "scaffolding_example" fallback template exists, creating template
25+
generating missing function content
26+
function "example" fallback template exists, creating template
2327
generating missing provider content
2428
provider "terraform-provider-scaffolding" template exists, skipping
2529
rendering static website
2630
cleaning rendered website dir
2731
rendering templated website to static markdown
2832
rendering "data-sources/example.md.tmpl"
33+
rendering "functions/example.md.tmpl"
2934
rendering "index.md.tmpl"
3035
rendering "resources/example.md.tmpl"
3136
-- expected-datasource.md --
@@ -84,6 +89,53 @@ data "scaffolding_example" "example" {
8489
configurable_attribute = "some-value"
8590
}
8691
```
92+
-- expected-function.md --
93+
# Data Fields
94+
95+
Name: example
96+
Type: function
97+
Description: Given a string value, returns the same value.
98+
Summary: Echo a string
99+
HasExample: true
100+
ExampleFile: $WORK/examples/functions/example/function.tf
101+
ProviderName: terraform-provider-scaffolding
102+
ProviderShortName: scaffolding
103+
RenderedProviderName: terraform-provider-scaffolding
104+
FunctionSignatureMarkdown: <!-- signature generated by tfplugindocs -->
105+
```text
106+
example(input string, variadicInput string...) string
107+
```
108+
FunctionArgumentsMarkdown: <!-- arguments generated by tfplugindocs -->
109+
1. `input` (String) Value to echo.
110+
HasVariadic: true
111+
FunctionVariadicArgumentMarkdown: <!-- variadic argument generated by tfplugindocs -->
112+
1. `variadicInput` (Variadic, String) Variadic input to echo.
113+
114+
# Functions
115+
116+
lower: function
117+
plainmarkdown: function
118+
prefixlines: Prefix: function
119+
split: [example]
120+
title: Function
121+
trimspace: function
122+
upper: FUNCTION
123+
124+
# Conditionals and File Functions
125+
126+
printf tffile:
127+
## Example Usage
128+
129+
{{tffile "$WORK/examples/functions/example/function.tf"}}
130+
131+
tffile:
132+
## Example Usage
133+
134+
```terraform
135+
output "test" {
136+
value = provider::scaffolding::example("testvalue1", "testvalue2")
137+
}
138+
```
87139
-- expected-index.md --
88140
# Data Fields
89141

@@ -250,6 +302,48 @@ tffile:
250302
{{ if .HasExample -}}
251303
## Example Usage
252304

305+
{{tffile .ExampleFile }}
306+
{{- end }}
307+
-- templates/functions.md.tmpl --
308+
# Data Fields
309+
310+
Name: {{.Name}}
311+
Type: {{.Type}}
312+
Description: {{.Description}}
313+
Summary: {{.Summary}}
314+
HasExample: {{.HasExample}}
315+
ExampleFile: {{.ExampleFile}}
316+
ProviderName: {{.ProviderName}}
317+
ProviderShortName: {{.ProviderShortName}}
318+
RenderedProviderName: {{.RenderedProviderName}}
319+
FunctionSignatureMarkdown: {{.FunctionSignatureMarkdown}}
320+
FunctionArgumentsMarkdown: {{.FunctionArgumentsMarkdown}}
321+
HasVariadic: {{.HasVariadic}}
322+
FunctionVariadicArgumentMarkdown: {{.FunctionVariadicArgumentMarkdown}}
323+
324+
# Functions
325+
326+
lower: {{ .Type | lower }}
327+
plainmarkdown: {{ .Type | plainmarkdown }}
328+
prefixlines: {{ .Type | prefixlines "Prefix: " }}
329+
split: {{ split .Name "_" }}
330+
title: {{ .Type | title }}
331+
trimspace: {{ .Type | trimspace }}
332+
upper: {{ .Type | upper }}
333+
334+
# Conditionals and File Functions
335+
336+
printf tffile:
337+
{{ if .HasExample -}}
338+
## Example Usage
339+
340+
{{ printf "{{tffile %q}}" .ExampleFile }}
341+
{{- end }}
342+
343+
tffile:
344+
{{ if .HasExample -}}
345+
## Example Usage
346+
253347
{{tffile .ExampleFile }}
254348
{{- end }}
255349
-- templates/index.md.tmpl --
@@ -360,6 +454,10 @@ The document generation tool looks for files in the following locations by defau
360454
data "scaffolding_example" "example" {
361455
configurable_attribute = "some-value"
362456
}
457+
-- examples/functions/example/function.tf --
458+
output "test" {
459+
value = provider::scaffolding::example("testvalue1", "testvalue2")
460+
}
363461
-- examples/provider/provider.tf --
364462
provider "scaffolding" {
365463
# example configuration here
@@ -442,6 +540,25 @@ terraform import scaffolding_example.example
442540
"description_kind": "markdown"
443541
}
444542
}
543+
},
544+
"functions": {
545+
"example": {
546+
"description": "Given a string value, returns the same value.",
547+
"summary": "Echo a string",
548+
"return_type": "string",
549+
"parameters": [
550+
{
551+
"name": "input",
552+
"description": "Value to echo.",
553+
"type": "string"
554+
}
555+
],
556+
"variadic_parameter": {
557+
"name": "variadicInput",
558+
"description": "Variadic input to echo.",
559+
"type": "string"
560+
}
561+
}
445562
}
446563
}
447564
}

0 commit comments

Comments
 (0)