|
| 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 | +} |
0 commit comments