Skip to content

Commit ba0074f

Browse files
authored
Fix incorrect rendering of example and import files for providers with no templates or fallback templates (#300)
* Add legacy docs and static files schema-json acceptance tests * Refactor `renderMissingResourceDoc` method * Update default templates to use `tffile` and `codefile` functions directly * Add changelog entry
1 parent 583123a commit ba0074f

15 files changed

+571
-238
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
kind: BREAKING CHANGES
2+
body: 'generate: templates using `printf` with either `codefile` or `tffile` to render code examples in markdown will need to switch to using those
3+
functions directly.
4+
5+
For example, switch the following template code:
6+
7+
`{{printf "{{codefile \"shell\" %q}}" .ImportFile}}`
8+
9+
to
10+
11+
`{{codefile "shell" .ImportFile}}`
12+
'
13+
time: 2023-12-06T14:09:32.757057-05:00
14+
custom:
15+
Issue: "300"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: BUG FIXES
2+
body: 'generate: fix incorrect rendering of example and import files for providers
3+
with no docs templates or with generic fallback templates.'
4+
time: 2023-11-21T15:00:34.216261-05:00
5+
custom:
6+
Issue: "300"

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

+6-18
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ compiling provider "scaffolding"
2020
using Terraform CLI binary from PATH if available, otherwise downloading latest Terraform CLI binary
2121
running terraform init
2222
getting provider schema
23-
rendering missing docs
23+
generating missing templates
2424
generating missing resource content
25-
resource "scaffolding_example" fallback template exists
26-
generating template for "scaffolding_example"
25+
resource "scaffolding_example" fallback template exists, creating template
2726
generating missing data source content
28-
resource "scaffolding_example" fallback template exists
29-
generating template for "scaffolding_example"
27+
data-source "scaffolding_example" fallback template exists, creating template
3028
generating missing provider content
3129
provider "terraform-provider-scaffolding" template exists, skipping
3230
rendering static website
@@ -78,11 +76,7 @@ printf codefile:
7876
printf tffile:
7977
## Example Usage
8078

81-
```terraform
82-
data "scaffolding_example" "example" {
83-
configurable_attribute = "some-value"
84-
}
85-
```
79+
{{tffile "$WORK/examples/data-sources/scaffolding_example/data-source.tf"}}
8680

8781
codefile:
8882

@@ -182,18 +176,12 @@ printf codefile:
182176

183177
Import is supported using the following syntax:
184178

185-
```shell
186-
terraform import scaffolding_example.example
187-
```
179+
{{codefile "shell" "$WORK/examples/resources/scaffolding_example/import.sh"}}
188180

189181
printf tffile:
190182
## Example Usage
191183

192-
```terraform
193-
resource "scaffolding_example" "example" {
194-
configurable_attribute = "some-value"
195-
}
196-
```
184+
{{tffile "$WORK/examples/resources/scaffolding_example/resource.tf"}}
197185

198186
codefile:
199187
## Import

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ compiling provider "scaffolding"
2020
using Terraform CLI binary from PATH if available, otherwise downloading latest Terraform CLI binary
2121
running terraform init
2222
getting provider schema
23-
rendering missing docs
23+
generating missing templates
2424
generating missing resource content
2525
resource "scaffolding_example" template exists, skipping
2626
generating missing data source content
27-
resource "scaffolding_example" template exists, skipping
27+
data-source "scaffolding_example" template exists, skipping
2828
generating missing provider content
2929
provider "terraform-provider-scaffolding" template exists, skipping
3030
rendering static website

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ compiling provider "scaffolding"
1818
using Terraform CLI binary from PATH if available, otherwise downloading latest Terraform CLI binary
1919
running terraform init
2020
getting provider schema
21-
rendering missing docs
21+
generating missing templates
2222
generating missing resource content
23-
generating template for "scaffolding_example"
23+
generating new template for "scaffolding_example"
2424
generating missing data source content
25-
generating template for "scaffolding_example"
25+
generating new template for data-source "scaffolding_example"
2626
generating missing provider content
27-
generating template for "terraform-provider-scaffolding"
27+
generating new template for "terraform-provider-scaffolding"
2828
rendering static website
2929
cleaning rendered website dir
3030
rendering templated website to static markdown

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ compiling provider "null"
1717
using Terraform CLI binary from PATH if available, otherwise downloading latest Terraform CLI binary
1818
running terraform init
1919
getting provider schema
20-
rendering missing docs
20+
generating missing templates
2121
generating missing resource content
22-
resource "null_resource" fallback template exists
23-
generating template for "null_resource"
22+
resource "null_resource" fallback template exists, creating template
2423
generating missing data source content
25-
resource "null_data_source" fallback template exists
26-
generating template for "null_data_source"
24+
data-source "null_data_source" fallback template exists, creating template
2725
generating missing provider content
2826
provider "terraform-provider-null" template exists, skipping
2927
rendering static website

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

+6-18
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ rendering website for provider "terraform-provider-scaffolding" (as "terraform-p
1515
copying any existing content to tmp dir
1616
exporting schema from JSON file
1717
getting provider schema
18-
rendering missing docs
18+
generating missing templates
1919
generating missing resource content
20-
resource "scaffolding_example" fallback template exists
21-
generating template for "scaffolding_example"
20+
resource "scaffolding_example" fallback template exists, creating template
2221
generating missing data source content
23-
resource "scaffolding_example" fallback template exists
24-
generating template for "scaffolding_example"
22+
data-source "scaffolding_example" fallback template exists, creating template
2523
generating missing provider content
2624
provider "terraform-provider-scaffolding" template exists, skipping
2725
rendering static website
@@ -73,11 +71,7 @@ printf codefile:
7371
printf tffile:
7472
## Example Usage
7573

76-
```terraform
77-
data "scaffolding_example" "example" {
78-
configurable_attribute = "some-value"
79-
}
80-
```
74+
{{tffile "$WORK/examples/data-sources/scaffolding_example/data-source.tf"}}
8175

8276
codefile:
8377

@@ -177,18 +171,12 @@ printf codefile:
177171

178172
Import is supported using the following syntax:
179173

180-
```shell
181-
terraform import scaffolding_example.example
182-
```
174+
{{codefile "shell" "$WORK/examples/resources/scaffolding_example/import.sh"}}
183175

184176
printf tffile:
185177
## Example Usage
186178

187-
```terraform
188-
resource "scaffolding_example" "example" {
189-
configurable_attribute = "some-value"
190-
}
191-
```
179+
{{tffile "$WORK/examples/resources/scaffolding_example/resource.tf"}}
192180

193181
codefile:
194182
## Import
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
# Successful run of tfplugindocs on a Framework provider with docs in the legacy directory structure (i.e. r/<resource name>.md.tmpl)
5+
[!unix] skip
6+
exec tfplugindocs --provider-name=terraform-provider-scaffolding --providers-schema=schema.json
7+
cmp stdout expected-output.txt
8+
9+
# Check that static files copied successfully to rendered docs directory
10+
cmp templates/r/example.md docs/r/example.md
11+
cmp templates/r/example.markdown docs/r/example.markdown
12+
cmp templates/r/example.html.markdown docs/r/example.html.markdown
13+
cmp templates/r/example.html.md docs/r/example.html.md
14+
15+
cmp templates/d/example.md docs/d/example.md
16+
cmp templates/d/example.markdown docs/d/example.markdown
17+
cmp templates/d/example.html.markdown docs/d/example.html.markdown
18+
cmp templates/d/example.html.md docs/d/example.html.md
19+
20+
cmp templates/index.md docs/index.md
21+
cmp templates/index.markdown docs/index.markdown
22+
cmp templates/index.html.markdown docs/index.html.markdown
23+
cmp templates/index.html.md docs/index.html.md
24+
-- expected-output.txt --
25+
rendering website for provider "terraform-provider-scaffolding" (as "terraform-provider-scaffolding")
26+
copying any existing content to tmp dir
27+
exporting schema from JSON file
28+
getting provider schema
29+
generating missing templates
30+
generating missing resource content
31+
resource "scaffolding_example" static file exists, skipping
32+
generating missing data source content
33+
data-source "scaffolding_example" static file exists, skipping
34+
generating missing provider content
35+
provider "terraform-provider-scaffolding" static file exists, skipping
36+
rendering static website
37+
cleaning rendered website dir
38+
rendering templated website to static markdown
39+
copying non-template file: "d/example.html.markdown"
40+
copying non-template file: "d/example.html.md"
41+
copying non-template file: "d/example.markdown"
42+
copying non-template file: "d/example.md"
43+
copying non-template file: "index.html.markdown"
44+
copying non-template file: "index.html.md"
45+
copying non-template file: "index.markdown"
46+
copying non-template file: "index.md"
47+
copying non-template file: "r/example.html.markdown"
48+
copying non-template file: "r/example.html.md"
49+
copying non-template file: "r/example.markdown"
50+
copying non-template file: "r/example.md"
51+
-- templates/r/example.md --
52+
# Data Fields
53+
54+
Name: {{.Name}}
55+
Type: {{.Type}}
56+
-- templates/r/example.markdown --
57+
# Data Fields
58+
59+
Name: {{.Name}}
60+
Type: {{.Type}}
61+
-- templates/r/example.html.markdown --
62+
# Data Fields
63+
64+
Name: {{.Name}}
65+
Type: {{.Type}}
66+
-- templates/r/example.html.md --
67+
# Data Fields
68+
69+
Name: {{.Name}}
70+
Type: {{.Type}}
71+
-- templates/d/example.md --
72+
# Data Fields
73+
74+
Name: {{.Name}}
75+
Type: {{.Type}}
76+
-- templates/d/example.markdown --
77+
# Data Fields
78+
79+
Name: {{.Name}}
80+
Type: {{.Type}}
81+
-- templates/d/example.html.markdown --
82+
# Data Fields
83+
84+
Name: {{.Name}}
85+
Type: {{.Type}}
86+
-- templates/d/example.html.md --
87+
# Data Fields
88+
89+
Name: {{.Name}}
90+
Type: {{.Type}}
91+
-- templates/index.md --
92+
# Data Fields
93+
94+
Name: {{.Name}}
95+
Type: {{.Type}}
96+
-- templates/index.markdown --
97+
# Data Fields
98+
99+
Name: {{.Name}}
100+
Type: {{.Type}}
101+
-- templates/index.html.markdown --
102+
# Data Fields
103+
104+
Name: {{.Name}}
105+
Type: {{.Type}}
106+
-- templates/index.html.md --
107+
# Data Fields
108+
109+
Name: {{.Name}}
110+
Type: {{.Type}}
111+
-- examples/README.md --
112+
# Examples
113+
114+
This directory contains examples that are mostly used for documentation, but can also be run/tested manually via the Terraform CLI.
115+
116+
The document generation tool looks for files in the following locations by default. All other *.tf files besides the ones mentioned below are ignored by the documentation tool. This is useful for creating examples that can run and/or ar testable even if some parts are not relevant for the documentation.
117+
118+
* **provider/provider.tf** example file for the provider index page
119+
* **data-sources/`full data source name`/data-source.tf** example file for the named data source page
120+
* **resources/`full resource name`/resource.tf** example file for the named data source page
121+
-- examples/data-sources/scaffolding_example/data-source.tf --
122+
data "scaffolding_example" "example" {
123+
configurable_attribute = "some-value"
124+
}
125+
-- examples/provider/provider.tf --
126+
provider "scaffolding" {
127+
# example configuration here
128+
}
129+
-- examples/resources/scaffolding_example/resource.tf --
130+
resource "scaffolding_example" "example" {
131+
configurable_attribute = "some-value"
132+
}
133+
-- examples/resources/scaffolding_example/import.sh --
134+
terraform import scaffolding_example.example
135+
-- schema.json --
136+
{
137+
"format_version": "1.0",
138+
"provider_schemas": {
139+
"registry.terraform.io/hashicorp/scaffolding": {
140+
"provider": {
141+
"version": 0,
142+
"block": {
143+
"attributes": {
144+
"endpoint": {
145+
"type": "string",
146+
"description": "Example provider attribute",
147+
"description_kind": "markdown",
148+
"optional": true
149+
}
150+
},
151+
"description": "Example provider",
152+
"description_kind": "markdown"
153+
}
154+
},
155+
"resource_schemas": {
156+
"scaffolding_example": {
157+
"version": 0,
158+
"block": {
159+
"attributes": {
160+
"configurable_attribute": {
161+
"type": "string",
162+
"description": "Example configurable attribute",
163+
"description_kind": "markdown",
164+
"optional": true
165+
},
166+
"defaulted": {
167+
"type": "string",
168+
"description": "Example configurable attribute with default value",
169+
"description_kind": "markdown",
170+
"optional": true,
171+
"computed": true
172+
},
173+
"id": {
174+
"type": "string",
175+
"description": "Example identifier",
176+
"description_kind": "markdown",
177+
"computed": true
178+
}
179+
},
180+
"description": "Example resource",
181+
"description_kind": "markdown"
182+
}
183+
}
184+
},
185+
"data_source_schemas": {
186+
"scaffolding_example": {
187+
"version": 0,
188+
"block": {
189+
"attributes": {
190+
"configurable_attribute": {
191+
"type": "string",
192+
"description": "Example configurable attribute",
193+
"description_kind": "markdown",
194+
"optional": true
195+
},
196+
"id": {
197+
"type": "string",
198+
"description": "Example identifier",
199+
"description_kind": "markdown",
200+
"computed": true
201+
}
202+
},
203+
"description": "Example data source",
204+
"description_kind": "markdown"
205+
}
206+
}
207+
}
208+
}
209+
}
210+
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ rendering website for provider "terraform-provider-scaffolding" (as "terraform-p
1515
copying any existing content to tmp dir
1616
exporting schema from JSON file
1717
getting provider schema
18-
rendering missing docs
18+
generating missing templates
1919
generating missing resource content
2020
resource "scaffolding_example" template exists, skipping
2121
generating missing data source content
22-
resource "scaffolding_example" template exists, skipping
22+
data-source "scaffolding_example" template exists, skipping
2323
generating missing provider content
2424
provider "terraform-provider-scaffolding" template exists, skipping
2525
rendering static website

0 commit comments

Comments
 (0)