|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package sfn |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 11 | + "github.com/aws/aws-sdk-go-v2/service/sfn" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/action" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/action/schema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 16 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 17 | + "github.com/hashicorp/terraform-provider-aws/internal/framework" |
| 18 | + "github.com/hashicorp/terraform-provider-aws/internal/framework/validators" |
| 19 | + "github.com/hashicorp/terraform-provider-aws/names" |
| 20 | +) |
| 21 | + |
| 22 | +// @Action(aws_sfn_start_execution, name="Start Execution") |
| 23 | +func newStartExecutionAction(_ context.Context) (action.ActionWithConfigure, error) { |
| 24 | + return &startExecutionAction{}, nil |
| 25 | +} |
| 26 | + |
| 27 | +var ( |
| 28 | + _ action.Action = (*startExecutionAction)(nil) |
| 29 | +) |
| 30 | + |
| 31 | +type startExecutionAction struct { |
| 32 | + framework.ActionWithModel[startExecutionActionModel] |
| 33 | +} |
| 34 | + |
| 35 | +type startExecutionActionModel struct { |
| 36 | + framework.WithRegionModel |
| 37 | + StateMachineArn types.String `tfsdk:"state_machine_arn"` |
| 38 | + Input types.String `tfsdk:"input"` |
| 39 | + Name types.String `tfsdk:"name"` |
| 40 | + TraceHeader types.String `tfsdk:"trace_header"` |
| 41 | +} |
| 42 | + |
| 43 | +func (a *startExecutionAction) Schema(ctx context.Context, req action.SchemaRequest, resp *action.SchemaResponse) { |
| 44 | + resp.Schema = schema.Schema{ |
| 45 | + Description: "Starts a Step Functions state machine execution with the specified input data.", |
| 46 | + Attributes: map[string]schema.Attribute{ |
| 47 | + "state_machine_arn": schema.StringAttribute{ |
| 48 | + Description: "The ARN of the state machine to execute. Can be unqualified, version-qualified, or alias-qualified.", |
| 49 | + Required: true, |
| 50 | + }, |
| 51 | + "input": schema.StringAttribute{ |
| 52 | + Description: "JSON input data for the execution. Defaults to '{}'.", |
| 53 | + Optional: true, |
| 54 | + Validators: []validator.String{ |
| 55 | + validators.JSON(), |
| 56 | + }, |
| 57 | + }, |
| 58 | + names.AttrName: schema.StringAttribute{ |
| 59 | + Description: "Name of the execution. Must be unique within the account/region/state machine for 90 days. Auto-generated if not provided.", |
| 60 | + Optional: true, |
| 61 | + }, |
| 62 | + "trace_header": schema.StringAttribute{ |
| 63 | + Description: "AWS X-Ray trace header for distributed tracing.", |
| 64 | + Optional: true, |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func (a *startExecutionAction) Invoke(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) { |
| 71 | + var config startExecutionActionModel |
| 72 | + |
| 73 | + resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) |
| 74 | + if resp.Diagnostics.HasError() { |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + conn := a.Meta().SFNClient(ctx) |
| 79 | + |
| 80 | + stateMachineArn := config.StateMachineArn.ValueString() |
| 81 | + input := "{}" |
| 82 | + if !config.Input.IsNull() { |
| 83 | + input = config.Input.ValueString() |
| 84 | + } |
| 85 | + |
| 86 | + tflog.Info(ctx, "Starting Step Functions execution", map[string]any{ |
| 87 | + "state_machine_arn": stateMachineArn, |
| 88 | + "input_length": len(input), |
| 89 | + "has_name": !config.Name.IsNull(), |
| 90 | + "has_trace_header": !config.TraceHeader.IsNull(), |
| 91 | + }) |
| 92 | + |
| 93 | + resp.SendProgress(action.InvokeProgressEvent{ |
| 94 | + Message: fmt.Sprintf("Starting execution for state machine %s...", stateMachineArn), |
| 95 | + }) |
| 96 | + |
| 97 | + startInput := &sfn.StartExecutionInput{ |
| 98 | + StateMachineArn: aws.String(stateMachineArn), |
| 99 | + Input: aws.String(input), |
| 100 | + } |
| 101 | + |
| 102 | + if !config.Name.IsNull() { |
| 103 | + startInput.Name = config.Name.ValueStringPointer() |
| 104 | + } |
| 105 | + |
| 106 | + if !config.TraceHeader.IsNull() { |
| 107 | + startInput.TraceHeader = config.TraceHeader.ValueStringPointer() |
| 108 | + } |
| 109 | + |
| 110 | + output, err := conn.StartExecution(ctx, startInput) |
| 111 | + if err != nil { |
| 112 | + resp.Diagnostics.AddError( |
| 113 | + "Failed to Start Step Functions Execution", |
| 114 | + fmt.Sprintf("Could not start execution for state machine %s: %s", stateMachineArn, err), |
| 115 | + ) |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + executionArn := aws.ToString(output.ExecutionArn) |
| 120 | + resp.SendProgress(action.InvokeProgressEvent{ |
| 121 | + Message: fmt.Sprintf("Execution started successfully with ARN %s", executionArn), |
| 122 | + }) |
| 123 | + |
| 124 | + tflog.Info(ctx, "Step Functions execution started successfully", map[string]any{ |
| 125 | + "state_machine_arn": stateMachineArn, |
| 126 | + "execution_arn": executionArn, |
| 127 | + "start_date": output.StartDate, |
| 128 | + }) |
| 129 | +} |
0 commit comments