Skip to content

Commit d392803

Browse files
committed
add support for actions
1 parent 54619e5 commit d392803

File tree

5 files changed

+54
-26
lines changed

5 files changed

+54
-26
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ For examples:
230230
|------------------------------------------------------------------------------|--------------------------------------------|
231231
| `examples/` | Root of examples |
232232
| `examples/provider/provider<*>.tf` | Provider example config(s) |
233-
| `examples/actions/<action_type>/action.tf` | Action example config |
233+
| `examples/actions/<action_type>/action<*>.tf` | Action example config(s) |
234234
| `examples/data-sources/<data source name>/data-source<*>.tf` | Data source example config(s) |
235235
| `examples/ephemeral-resources/<ephemeral resource>/ephemeral-resource<*>.tf` | Ephemeral resource example config(s) |
236236
| `examples/functions/<function name>/function<*>.tf` | Function example config(s) |
@@ -350,8 +350,10 @@ using the following data fields and functions:
350350
| `.Name` | string | Name of the action (ex. `examplecloud_do_thing`) |
351351
| `.Type` | string | `Action` |
352352
| `.Description` | string | Action description |
353-
| `.HasExample` | bool | Is there an example file? |
354-
| `.ExampleFile` | string | Path to the file with the terraform configuration example |
353+
| `.HasExample` | bool | (Legacy) Is there an example file? |
354+
| `.HasExamples` | bool | Are there example files? Always true if HasExample is true. |
355+
| `.ExampleFile` | string | (Legacy) Path to the file with the Terraform configuration example. |
356+
| `.ExampleFiles` | string | Paths to the files with Terraform configuration examples. Includes ExampleFile. |
355357
| `.ProviderName` | string | Canonical provider name (ex. `terraform-provider-random`) |
356358
| `.ProviderShortName` | string | Short version of the rendered provider name (ex. `random`) |
357359
| `.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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ action "scaffolding_example" "example" {
6464
}
6565
```
6666

67+
```terraform
68+
action "scaffolding_example" "example" {
69+
config {
70+
required_attr = "some-value-2"
71+
}
72+
}
73+
```
74+
6775
<!-- action schema generated by tfplugindocs -->
6876
## Schema
6977

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
@@ -801,11 +801,20 @@ func (g *generator) renderStaticWebsite(providerSchema *tfjson.ProviderSchema) e
801801
g.warnf("ephemeral resource entitled %q, or %q does not exist", shortName, resName)
802802
case "actions/":
803803
actionSchema, resName := actionSchema(providerSchema.ActionSchemas, shortName, relFile)
804-
exampleFilePath := filepath.Join(g.ProviderExamplesDir(), "actions", resName, "action.tf")
805804

806805
if actionSchema != nil {
806+
exampleFilePath := filepath.Join(g.ProviderExamplesDir(), "actions", resName, "action.tf")
807+
exampleFilesPattern := filepath.Join(g.ProviderExamplesDir(), "actions", resName, "action*.tf")
808+
exampleFiles, err := filepath.Glob(exampleFilesPattern)
809+
810+
if err != nil {
811+
return fmt.Errorf("unable to glob example files with pattern %q: %w", exampleFilesPattern, err)
812+
}
813+
814+
slices.Sort(exampleFiles)
815+
807816
tmpl := actionTemplate(tmplData)
808-
render, err := tmpl.Render(g.providerDir, resName, g.providerName, g.renderedProviderName, "Action", exampleFilePath, actionSchema)
817+
render, err := tmpl.Render(g.providerDir, resName, g.providerName, g.renderedProviderName, "Action", exampleFilePath, exampleFiles, actionSchema)
809818
if err != nil {
810819
return fmt.Errorf("unable to render action template %q: %w", rel, err)
811820
}

0 commit comments

Comments
 (0)