Skip to content

Commit c9ac0b4

Browse files
committed
Add an example action
1 parent 05fa1de commit c9ac0b4

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed

docs/actions/example.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "scaffolding_example Action - scaffolding"
4+
subcategory: ""
5+
description: |-
6+
Example action
7+
---
8+
9+
# scaffolding_example (Action)
10+
11+
Example action
12+
13+
## Example Usage
14+
15+
```terraform
16+
resource "terraform_data" "example" {
17+
input = "fake-string"
18+
19+
lifecycle {
20+
action_trigger {
21+
events = [before_create]
22+
actions = [action.scaffolding_example.example]
23+
}
24+
}
25+
}
26+
27+
action "scaffolding_example" "example" {
28+
config {
29+
configurable_attribute = "some-value"
30+
}
31+
}
32+
```
33+
34+
<!-- action schema generated by tfplugindocs -->
35+
## Schema
36+
37+
### Optional
38+
39+
- `configurable_attribute` (String) Example configurable attribute
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
resource "terraform_data" "example" {
2+
input = "fake-string"
3+
4+
lifecycle {
5+
action_trigger {
6+
events = [before_create]
7+
actions = [action.scaffolding_example.example]
8+
}
9+
}
10+
}
11+
12+
action "scaffolding_example" "example" {
13+
config {
14+
configurable_attribute = "some-value"
15+
}
16+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package provider
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"net/http"
10+
11+
"github.com/hashicorp/terraform-plugin-framework/action"
12+
"github.com/hashicorp/terraform-plugin-framework/action/schema"
13+
"github.com/hashicorp/terraform-plugin-framework/types"
14+
"github.com/hashicorp/terraform-plugin-log/tflog"
15+
)
16+
17+
// Ensure provider defined types fully satisfy framework interfaces.
18+
var _ action.Action = &ExampleAction{}
19+
var _ action.ActionWithConfigure = &ExampleAction{}
20+
21+
func NewExampleAction() action.Action {
22+
return &ExampleAction{}
23+
}
24+
25+
// ExampleAction defines the action implementation.
26+
type ExampleAction struct {
27+
client *http.Client
28+
}
29+
30+
// ExampleActionModel describes the action data model.
31+
type ExampleActionModel struct {
32+
ConfigurableAttribute types.String `tfsdk:"configurable_attribute"`
33+
}
34+
35+
func (e *ExampleAction) Metadata(ctx context.Context, req action.MetadataRequest, resp *action.MetadataResponse) {
36+
resp.TypeName = req.ProviderTypeName + "_example"
37+
}
38+
39+
func (e *ExampleAction) Schema(ctx context.Context, req action.SchemaRequest, resp *action.SchemaResponse) {
40+
resp.Schema = schema.Schema{
41+
// This description is used by the documentation generator and the language server.
42+
MarkdownDescription: "Example action",
43+
44+
Attributes: map[string]schema.Attribute{
45+
"configurable_attribute": schema.StringAttribute{
46+
MarkdownDescription: "Example configurable attribute",
47+
Optional: true,
48+
},
49+
},
50+
}
51+
}
52+
53+
func (e *ExampleAction) Configure(ctx context.Context, req action.ConfigureRequest, resp *action.ConfigureResponse) {
54+
// Prevent panic if the provider has not been configured.
55+
if req.ProviderData == nil {
56+
return
57+
}
58+
59+
client, ok := req.ProviderData.(*http.Client)
60+
61+
if !ok {
62+
resp.Diagnostics.AddError(
63+
"Unexpected Data Source Configure Type",
64+
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
65+
)
66+
67+
return
68+
}
69+
70+
e.client = client
71+
}
72+
73+
func (e *ExampleAction) Invoke(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) {
74+
// Send a progress message back to Terraform
75+
resp.SendProgress = func(event action.InvokeProgressEvent) {
76+
event.Message = "starting action invocation"
77+
}
78+
79+
var data ExampleActionModel
80+
81+
// Read Terraform configuration data into the model
82+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
83+
84+
if resp.Diagnostics.HasError() {
85+
return
86+
}
87+
88+
// If applicable, this is a great opportunity to initialize any necessary
89+
// provider client data and make a call using it.
90+
// httpResp, err := d.client.Do(httpReq)
91+
// if err != nil {
92+
// resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read example, got error: %s", err))
93+
// return
94+
// }
95+
96+
// Write logs using the tflog package
97+
// Documentation: https://terraform.io/plugin/log
98+
tflog.Trace(ctx, "invoke an action")
99+
100+
// Send a progress message back to Terraform
101+
resp.SendProgress = func(event action.InvokeProgressEvent) {
102+
event.Message = "finished action invocation"
103+
}
104+
}

internal/provider/provider.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"net/http"
99

10+
"github.com/hashicorp/terraform-plugin-framework/action"
1011
"github.com/hashicorp/terraform-plugin-framework/datasource"
1112
"github.com/hashicorp/terraform-plugin-framework/ephemeral"
1213
"github.com/hashicorp/terraform-plugin-framework/function"
@@ -20,6 +21,7 @@ import (
2021
var _ provider.Provider = &ScaffoldingProvider{}
2122
var _ provider.ProviderWithFunctions = &ScaffoldingProvider{}
2223
var _ provider.ProviderWithEphemeralResources = &ScaffoldingProvider{}
24+
var _ provider.ProviderWithActions = &ScaffoldingProvider{}
2325

2426
// ScaffoldingProvider defines the provider implementation.
2527
type ScaffoldingProvider struct {
@@ -92,6 +94,12 @@ func (p *ScaffoldingProvider) Functions(ctx context.Context) []func() function.F
9294
}
9395
}
9496

97+
func (p *ScaffoldingProvider) Actions(ctx context.Context) []func() action.Action {
98+
return []func() action.Action{
99+
NewExampleAction,
100+
}
101+
}
102+
95103
func New(version string) func() provider.Provider {
96104
return func() provider.Provider {
97105
return &ScaffoldingProvider{

0 commit comments

Comments
 (0)