|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package framework |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/action" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/action/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | + "github.com/hashicorp/terraform-provider-aws/internal/conns" |
| 15 | + "github.com/hashicorp/terraform-provider-aws/internal/framework" |
| 16 | +) |
| 17 | + |
| 18 | +// testAction implements action.Action for testing |
| 19 | +type testAction struct{} |
| 20 | + |
| 21 | +func (t *testAction) Metadata(ctx context.Context, req action.MetadataRequest, resp *action.MetadataResponse) { |
| 22 | + resp.TypeName = "aws_test_action" |
| 23 | +} |
| 24 | + |
| 25 | +func (t *testAction) Schema(ctx context.Context, req action.SchemaRequest, resp *action.SchemaResponse) { |
| 26 | + resp.Schema = schema.UnlinkedSchema{ |
| 27 | + Description: "Test action for framework integration", |
| 28 | + Attributes: map[string]schema.Attribute{ |
| 29 | + "test_param": schema.StringAttribute{ |
| 30 | + Required: true, |
| 31 | + Description: "Test parameter", |
| 32 | + }, |
| 33 | + }, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func (t *testAction) Invoke(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) { |
| 38 | + // Test implementation - just validate we can access the config |
| 39 | + var config testActionModel |
| 40 | + resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) |
| 41 | + if resp.Diagnostics.HasError() { |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + // Send progress update |
| 46 | + resp.SendProgress(action.InvokeProgressEvent{ |
| 47 | + Message: "Test action executed successfully", |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +// testActionModel represents the configuration model for the test action |
| 52 | +type testActionModel struct { |
| 53 | + TestParam types.String `tfsdk:"test_param"` |
| 54 | +} |
| 55 | + |
| 56 | +// Implement ActionValidateModel interface |
| 57 | +func (t *testAction) ValidateModel(ctx context.Context, schema *schema.UnlinkedSchema) diag.Diagnostics { |
| 58 | + var diags diag.Diagnostics |
| 59 | + // Basic validation - ensure required attributes exist |
| 60 | + if _, ok := schema.Attributes["test_param"]; !ok { |
| 61 | + diags.AddError("Missing required attribute", "test_param attribute is required") |
| 62 | + } |
| 63 | + return diags |
| 64 | +} |
| 65 | + |
| 66 | +// Ensure testAction implements required interfaces |
| 67 | +var ( |
| 68 | + _ action.Action = (*testAction)(nil) |
| 69 | + _ framework.ActionValidateModel = (*testAction)(nil) |
| 70 | +) |
| 71 | + |
| 72 | +func TestWrappedAction_Basic(t *testing.T) { |
| 73 | + ctx := context.Background() |
| 74 | + |
| 75 | + // Create test action |
| 76 | + inner := &testAction{} |
| 77 | + |
| 78 | + // Create wrapped action with minimal options |
| 79 | + opts := wrappedActionOptions{ |
| 80 | + bootstrapContext: func(ctx context.Context, getAttribute getAttributeFunc, c *conns.AWSClient) (context.Context, diag.Diagnostics) { |
| 81 | + return ctx, nil |
| 82 | + }, |
| 83 | + interceptors: interceptorInvocations{}, |
| 84 | + typeName: "aws_test_action", |
| 85 | + } |
| 86 | + |
| 87 | + wrapped := newWrappedAction(inner, opts) |
| 88 | + |
| 89 | + // Test Metadata |
| 90 | + metaReq := action.MetadataRequest{ |
| 91 | + ProviderTypeName: "aws", |
| 92 | + } |
| 93 | + var metaResp action.MetadataResponse |
| 94 | + wrapped.Metadata(ctx, metaReq, &metaResp) |
| 95 | + |
| 96 | + if metaResp.TypeName != "aws_test_action" { |
| 97 | + t.Errorf("Expected TypeName 'aws_test_action', got '%s'", metaResp.TypeName) |
| 98 | + } |
| 99 | + |
| 100 | + // Test Schema |
| 101 | + schemaReq := action.SchemaRequest{} |
| 102 | + var schemaResp action.SchemaResponse |
| 103 | + wrapped.Schema(ctx, schemaReq, &schemaResp) |
| 104 | + |
| 105 | + if schemaResp.Diagnostics.HasError() { |
| 106 | + t.Errorf("Schema method returned errors: %v", schemaResp.Diagnostics) |
| 107 | + } |
| 108 | + |
| 109 | + if unlinkedSchema, ok := schemaResp.Schema.(schema.UnlinkedSchema); ok { |
| 110 | + if _, exists := unlinkedSchema.Attributes["test_param"]; !exists { |
| 111 | + t.Error("Expected 'test_param' attribute in schema") |
| 112 | + } |
| 113 | + } else { |
| 114 | + t.Error("Expected UnlinkedSchema type") |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func TestActionInterceptors_RegionInjection(t *testing.T) { |
| 119 | + ctx := context.Background() |
| 120 | + |
| 121 | + // Create test action |
| 122 | + inner := &testAction{} |
| 123 | + |
| 124 | + // Create wrapped action with region interceptor |
| 125 | + opts := wrappedActionOptions{ |
| 126 | + bootstrapContext: func(ctx context.Context, getAttribute getAttributeFunc, c *conns.AWSClient) (context.Context, diag.Diagnostics) { |
| 127 | + return ctx, nil |
| 128 | + }, |
| 129 | + interceptors: interceptorInvocations{ |
| 130 | + actionInjectRegionAttribute(), |
| 131 | + }, |
| 132 | + typeName: "aws_test_action", |
| 133 | + } |
| 134 | + |
| 135 | + wrapped := newWrappedAction(inner, opts) |
| 136 | + |
| 137 | + // Test Schema with region injection |
| 138 | + schemaReq := action.SchemaRequest{} |
| 139 | + var schemaResp action.SchemaResponse |
| 140 | + wrapped.Schema(ctx, schemaReq, &schemaResp) |
| 141 | + |
| 142 | + if schemaResp.Diagnostics.HasError() { |
| 143 | + t.Errorf("Schema method returned errors: %v", schemaResp.Diagnostics) |
| 144 | + } |
| 145 | + |
| 146 | + if unlinkedSchema, ok := schemaResp.Schema.(schema.UnlinkedSchema); ok { |
| 147 | + if _, exists := unlinkedSchema.Attributes["region"]; !exists { |
| 148 | + t.Error("Expected 'region' attribute to be injected into schema") |
| 149 | + } |
| 150 | + if _, exists := unlinkedSchema.Attributes["test_param"]; !exists { |
| 151 | + t.Error("Expected original 'test_param' attribute to remain in schema") |
| 152 | + } |
| 153 | + } else { |
| 154 | + t.Error("Expected UnlinkedSchema type") |
| 155 | + } |
| 156 | +} |
0 commit comments