Skip to content

Commit 0fe8159

Browse files
authored
generate: Update action docs to support multiple examples (#527)
* add action test to eventually fail * add support for actions
1 parent 6ddafb7 commit 0fe8159

File tree

5 files changed

+125
-26
lines changed

5 files changed

+125
-26
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ For examples:
233233
|------------------------------------------------------------------------------|--------------------------------------------|
234234
| `examples/` | Root of examples |
235235
| `examples/provider/provider<*>.tf` | Provider example config(s) |
236-
| `examples/actions/<action_type>/action.tf` | Action example config |
236+
| `examples/actions/<action_type>/action<*>.tf` | Action example config(s) |
237237
| `examples/data-sources/<data source name>/data-source<*>.tf` | Data source example config(s) |
238238
| `examples/ephemeral-resources/<ephemeral resource>/ephemeral-resource<*>.tf` | Ephemeral resource example config(s) |
239239
| `examples/functions/<function name>/function<*>.tf` | Function example config(s) |
@@ -356,8 +356,10 @@ using the following data fields and functions:
356356
| `.Name` | string | Name of the action (ex. `examplecloud_do_thing`) |
357357
| `.Type` | string | `Action` |
358358
| `.Description` | string | Action description |
359-
| `.HasExample` | bool | Is there an example file? |
360-
| `.ExampleFile` | string | Path to the file with the terraform configuration example |
359+
| `.HasExample` | bool | (Legacy) Is there an example file? |
360+
| `.HasExamples` | bool | Are there example files? Always true if HasExample is true. |
361+
| `.ExampleFile` | string | (Legacy) Path to the file with the Terraform configuration example. |
362+
| `.ExampleFiles` | string | Paths to the files with Terraform configuration examples. Includes ExampleFile. |
361363
| `.ProviderName` | string | Canonical provider name (ex. `terraform-provider-random`) |
362364
| `.ProviderShortName` | string | Short version of the rendered provider name (ex. `random`) |
363365
| `.RenderedProviderName` | string | Value provided via argument `--rendered-provider-name`, otherwise same as `.ProviderName` |

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
exec tfplugindocs --provider-name=terraform-provider-scaffolding --providers-schema=schema.json --rendered-provider-name=Scaffolding
77
cmp stdout expected-output.txt
88
cmp docs/index.md expected-index.md
9+
cmp docs/actions/example.md expected-action.md
910
cmp docs/data-sources/example.md expected-datasource.md
1011
cmp docs/resources/example.md expected-resource.md
1112
cmp docs/functions/example.md expected-function.md
@@ -27,18 +28,61 @@ generating new template for function "no-variadic"
2728
generating missing ephemeral resource content
2829
generating new template for "scaffolding_example"
2930
generating missing action content
31+
generating new template for "scaffolding_example"
3032
generating missing list resource content
3133
generating missing provider content
3234
generating new template for "terraform-provider-scaffolding"
3335
rendering static website
3436
cleaning rendered website dir
3537
rendering templated website to static markdown
38+
rendering "actions/example.md.tmpl"
3639
rendering "data-sources/example.md.tmpl"
3740
rendering "ephemeral-resources/example.md.tmpl"
3841
rendering "functions/example.md.tmpl"
3942
rendering "functions/no-variadic.md.tmpl"
4043
rendering "index.md.tmpl"
4144
rendering "resources/example.md.tmpl"
45+
-- expected-action.md --
46+
---
47+
# generated by https://github.com/hashicorp/terraform-plugin-docs
48+
page_title: "scaffolding_example Action - Scaffolding"
49+
subcategory: ""
50+
description: |-
51+
Example action
52+
---
53+
54+
# scaffolding_example (Action)
55+
56+
Example action
57+
58+
## Example Usage
59+
60+
```terraform
61+
action "scaffolding_example" "example" {
62+
config {
63+
required_attr = "some-value-1"
64+
}
65+
}
66+
```
67+
68+
```terraform
69+
action "scaffolding_example" "example" {
70+
config {
71+
required_attr = "some-value-2"
72+
}
73+
}
74+
```
75+
76+
<!-- action schema generated by tfplugindocs -->
77+
## Schema
78+
79+
### Required
80+
81+
- `required_attr` (String) Example required attribute
82+
83+
### Optional
84+
85+
- `optional_attr` (String) Example optional attribute
4286
-- expected-datasource.md --
4387
---
4488
# generated by https://github.com/hashicorp/terraform-plugin-docs
@@ -318,6 +362,18 @@ The document generation tool looks for files in the following locations by defau
318362
* **provider/provider.tf** example file for the provider index page
319363
* **data-sources/`full data source name`/data-source.tf** example file for the named data source page
320364
* **resources/`full resource name`/resource.tf** example file for the named data source page
365+
-- examples/actions/scaffolding_example/action.tf --
366+
action "scaffolding_example" "example" {
367+
config {
368+
required_attr = "some-value-1"
369+
}
370+
}
371+
-- examples/actions/scaffolding_example/action2.tf --
372+
action "scaffolding_example" "example" {
373+
config {
374+
required_attr = "some-value-2"
375+
}
376+
}
321377
-- examples/data-sources/scaffolding_example/data-source.tf --
322378
data "scaffolding_example" "example" {
323379
configurable_attribute = "some-value1"
@@ -408,6 +464,29 @@ resource "scaffolding_example" "example" {
408464
"description_kind": "markdown"
409465
}
410466
},
467+
"action_schemas": {
468+
"scaffolding_example": {
469+
"version": 0,
470+
"block": {
471+
"attributes": {
472+
"required_attr": {
473+
"type": "string",
474+
"description": "Example required attribute",
475+
"description_kind": "plain",
476+
"required": true
477+
},
478+
"optional_attr": {
479+
"type": "string",
480+
"description": "Example optional attribute",
481+
"description_kind": "plain",
482+
"optional": true
483+
}
484+
},
485+
"description": "Example action",
486+
"description_kind": "plain"
487+
}
488+
}
489+
},
411490
"resource_schemas": {
412491
"scaffolding_example": {
413492
"version": 0,

internal/provider/action_template.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,25 @@ const actionSchemaComment = "<!-- action schema generated by tfplugindocs -->"
1414

1515
type actionTemplate string
1616

17-
func (t actionTemplate) Render(providerDir, name, providerName, renderedProviderName, typeName, exampleFile string, schema *tfjson.ActionSchema) (string, error) {
17+
type ActionTemplateType struct {
18+
Type string
19+
Name string
20+
Description string
21+
22+
HasExample bool
23+
HasExamples bool
24+
ExampleFile string
25+
ExampleFiles []string
26+
27+
ProviderName string
28+
ProviderShortName string
29+
30+
SchemaMarkdown string
31+
32+
RenderedProviderName string
33+
}
34+
35+
func (t actionTemplate) Render(providerDir, name, providerName, renderedProviderName, typeName, exampleFile string, exampleFiles []string, schema *tfjson.ActionSchema) (string, error) {
1836
schemaBuffer := bytes.NewBuffer(nil)
1937
err := schemamd.RenderAction(schema, schemaBuffer)
2038
if err != nil {
@@ -26,27 +44,15 @@ func (t actionTemplate) Render(providerDir, name, providerName, renderedProvider
2644
return "", nil
2745
}
2846

29-
return renderStringTemplate(providerDir, "actionTemplate", s, struct {
30-
Type string
31-
Name string
32-
Description string
33-
34-
HasExample bool
35-
ExampleFile string
36-
37-
ProviderName string
38-
ProviderShortName string
39-
40-
SchemaMarkdown string
41-
42-
RenderedProviderName string
43-
}{
47+
return renderStringTemplate(providerDir, "actionTemplate", s, ActionTemplateType{
4448
Type: typeName,
4549
Name: name,
4650
Description: schema.Block.Description,
4751

48-
HasExample: exampleFile != "" && fileExists(exampleFile),
49-
ExampleFile: exampleFile,
52+
HasExample: exampleFile != "" && fileExists(exampleFile),
53+
HasExamples: len(exampleFiles) > 0,
54+
ExampleFile: exampleFile,
55+
ExampleFiles: exampleFiles,
5056

5157
ProviderName: providerName,
5258
ProviderShortName: providerShortName(renderedProviderName),
@@ -69,10 +75,13 @@ description: |-
6975
7076
{{ .Description | trimspace }}
7177
72-
{{ if .HasExample -}}
78+
{{ if .HasExamples -}}
7379
## Example Usage
7480
75-
{{tffile .ExampleFile }}
81+
{{- range .ExampleFiles }}
82+
83+
{{ tffile . }}
84+
{{- end }}
7685
{{- end }}
7786
7887
{{ .SchemaMarkdown | trimspace }}

internal/provider/action_template_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ action "scaffolding_example" "example1" {
4949
},
5050
}
5151

52-
result, err := tpl.Render("testdata/test-action-dir", "testTemplate", "test-action", "test-action", "action", "action.tf", &schema)
52+
result, err := tpl.Render("testdata/test-action-dir", "testTemplate", "test-action", "test-action", "action", "action.tf", []string{"action.tf"}, &schema)
5353
if err != nil {
5454
t.Error(err)
5555
}

internal/provider/generate.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,11 +868,20 @@ func (g *generator) renderStaticWebsite(providerSchema *tfjson.ProviderSchema) e
868868
g.warnf("ephemeral resource entitled %q, or %q does not exist", shortName, resName)
869869
case "actions/":
870870
actionSchema, resName := actionSchema(providerSchema.ActionSchemas, shortName, relFile)
871-
exampleFilePath := filepath.Join(g.ProviderExamplesDir(), "actions", resName, "action.tf")
872871

873872
if actionSchema != nil {
873+
exampleFilePath := filepath.Join(g.ProviderExamplesDir(), "actions", resName, "action.tf")
874+
exampleFilesPattern := filepath.Join(g.ProviderExamplesDir(), "actions", resName, "action*.tf")
875+
exampleFiles, err := filepath.Glob(exampleFilesPattern)
876+
877+
if err != nil {
878+
return fmt.Errorf("unable to glob example files with pattern %q: %w", exampleFilesPattern, err)
879+
}
880+
881+
slices.Sort(exampleFiles)
882+
874883
tmpl := actionTemplate(tmplData)
875-
render, err := tmpl.Render(g.providerDir, resName, g.providerName, g.renderedProviderName, "Action", exampleFilePath, actionSchema)
884+
render, err := tmpl.Render(g.providerDir, resName, g.providerName, g.renderedProviderName, "Action", exampleFilePath, exampleFiles, actionSchema)
876885
if err != nil {
877886
return fmt.Errorf("unable to render action template %q: %w", rel, err)
878887
}

0 commit comments

Comments
 (0)