Skip to content

Commit 9b226e4

Browse files
committed
add documentation and example
1 parent b1ca955 commit 9b226e4

File tree

5 files changed

+155
-6
lines changed

5 files changed

+155
-6
lines changed

docs/actions/command.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
page_title: "local_command Action - terraform-provider-local"
2+
subcategory: ""
3+
description: |-
4+
Invokes an executable on the local machine. All environment variables visible to the Terraform process are passed through to the child process. After the child process successfully executes, the stdout will be returned for Terraform to display to the user.
5+
Any non-zero exit code will be treated as an error and will return a diagnostic to Terraform containing the stderr message if available.
6+
---
7+
8+
# local_command (Action)
9+
10+
Invokes an executable on the local machine. All environment variables visible to the Terraform process are passed through to the child process. After the child process successfully executes, the `stdout` will be returned for Terraform to display to the user.
11+
12+
Any non-zero exit code will be treated as an error and will return a diagnostic to Terraform containing the `stderr` message if available.
13+
14+
## Example Usage
15+
16+
For the following bash script (`example_script.sh`):
17+
```bash
18+
#!/bin/bash
19+
20+
DATA=$(</dev/stdin)
21+
echo "stdin: $DATA, args: $@"
22+
```
23+
24+
Here is an example configuration that will run the script after a resource is created:
25+
26+
```terraform
27+
resource "terraform_data" "test" {
28+
lifecycle {
29+
action_trigger {
30+
events = [after_create]
31+
actions = [action.local_command.bash_example]
32+
}
33+
}
34+
}
35+
36+
action "local_command" "bash_example" {
37+
config {
38+
command = "bash"
39+
arguments = ["example_script.sh", "arg1", "arg2"]
40+
stdin = jsonencode({
41+
"key1" : "value1"
42+
"key2" : "value2"
43+
})
44+
}
45+
}
46+
```
47+
48+
The `stdout` will be displayed when the action is invoked:
49+
```bash
50+
$ terraform apply -auto-approve
51+
52+
# .. Terraform CLI output truncated for example purposes
53+
54+
Plan: 1 to add, 0 to change, 0 to destroy. Actions: 1 to invoke.
55+
terraform_data.test: Creating...
56+
terraform_data.test: Creation complete after 0s [id=4b41b541-5550-590a-9949-657e91baa346]
57+
Action started: action.local_command.bash_example (triggered by terraform_data.test)
58+
Action action.local_command.bash_example (triggered by terraform_data.test):
59+
60+
stdin: {"key1":"value1","key2":"value2"}, args: arg1 arg2
61+
62+
63+
Action complete: action.local_command.bash_example (triggered by terraform_data.test)
64+
65+
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
66+
```
67+
68+
<!-- action schema generated by tfplugindocs -->
69+
## Schema
70+
71+
### Required
72+
73+
- `command` (String) Executable name to be discovered on the PATH or absolute path to executable.
74+
75+
### Optional
76+
77+
- `arguments` (List of String) Arguments to be passed to the given command. Any `null` arguments will be removed from the list.
78+
- `stdin` (String) Data to be passed to the given command's standard input.
79+
- `working_directory` (String) The directory where the command should be executed. Defaults to the Terraform working directory.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
resource "terraform_data" "test" {
2+
lifecycle {
3+
action_trigger {
4+
events = [after_create]
5+
actions = [action.local_command.bash_example]
6+
}
7+
}
8+
}
9+
10+
action "local_command" "bash_example" {
11+
config {
12+
command = "bash"
13+
arguments = ["example_script.sh", "arg1", "arg2"]
14+
stdin = jsonencode({
15+
"key1" : "value1"
16+
"key2" : "value2"
17+
})
18+
}
19+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
DATA=$(</dev/stdin)
4+
echo "stdin: $DATA, args: $@"

internal/provider/action_local_command.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,26 @@ func (a *localCommandAction) Metadata(ctx context.Context, req action.MetadataRe
3535

3636
func (a *localCommandAction) Schema(ctx context.Context, req action.SchemaRequest, resp *action.SchemaResponse) {
3737
resp.Schema = schema.Schema{
38-
Description: "", // TODO: Describe action, mention that actions don't have output, this is meant to execute local commands, they can be non-idempotent as they are only executed during apply.
39-
// If the external command is idempotent/you need the output, use data source (coming soon).
38+
// TODO: Once we have a local_command data source, reference that to be used if the user needs to the consume the output of the command (and it's idempotent)
39+
MarkdownDescription: "Invokes an executable on the local machine. All environment variables visible to the Terraform process are passed through " +
40+
"to the child process. After the child process successfully executes, the `stdout` will be returned for Terraform to display to the user.\n\n" +
41+
"Any non-zero exit code will be treated as an error and will return a diagnostic to Terraform containing the `stderr` message if available.",
4042
Attributes: map[string]schema.Attribute{
4143
"command": schema.StringAttribute{
4244
Description: "Executable name to be discovered on the PATH or absolute path to executable.",
4345
Required: true,
4446
},
4547
"arguments": schema.ListAttribute{
46-
Description: "Arguments to be passed to the given command.",
47-
ElementType: types.StringType,
48-
Optional: true,
48+
MarkdownDescription: "Arguments to be passed to the given command. Any `null` arguments will be removed from the list.",
49+
ElementType: types.StringType,
50+
Optional: true,
4951
},
5052
"stdin": schema.StringAttribute{
5153
Description: "Data to be passed to the given command's standard input.",
5254
Optional: true,
5355
},
5456
"working_directory": schema.StringAttribute{
55-
Description: "The directory where the command should be executed. Defaults to the current working directory.",
57+
Description: "The directory where the command should be executed. Defaults to the Terraform working directory.",
5658
Optional: true,
5759
},
5860
},

templates/actions/command.md.tmpl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
page_title: "{{.Name}} {{.Type}} - {{.RenderedProviderName}}"
2+
subcategory: ""
3+
description: |-
4+
{{ .Description | plainmarkdown | trimspace | prefixlines " " }}
5+
---
6+
7+
# {{.Name}} ({{.Type}})
8+
9+
{{ .Description | trimspace }}
10+
11+
## Example Usage
12+
13+
For the following bash script (`example_script.sh`):
14+
```bash
15+
#!/bin/bash
16+
17+
DATA=$(</dev/stdin)
18+
echo "stdin: $DATA, args: $@"
19+
```
20+
21+
Here is an example configuration that will run the script after a resource is created:
22+
23+
{{ tffile (index .ExampleFiles 0) }}
24+
25+
The `stdout` will be displayed when the action is invoked:
26+
```bash
27+
$ terraform apply -auto-approve
28+
29+
# .. Terraform CLI output truncated for example purposes
30+
31+
Plan: 1 to add, 0 to change, 0 to destroy. Actions: 1 to invoke.
32+
terraform_data.test: Creating...
33+
terraform_data.test: Creation complete after 0s [id=4b41b541-5550-590a-9949-657e91baa346]
34+
Action started: action.local_command.bash_example (triggered by terraform_data.test)
35+
Action action.local_command.bash_example (triggered by terraform_data.test):
36+
37+
stdin: {"key1":"value1","key2":"value2"}, args: arg1 arg2
38+
39+
40+
Action complete: action.local_command.bash_example (triggered by terraform_data.test)
41+
42+
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
43+
```
44+
45+
{{ .SchemaMarkdown | trimspace }}

0 commit comments

Comments
 (0)