|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package rdsdata |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/rdsdata" |
| 11 | + "github.com/aws/aws-sdk-go-v2/service/rdsdata/types" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/action" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/action/schema" |
| 14 | + fwtypes2 "github.com/hashicorp/terraform-plugin-framework/types" |
| 15 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 16 | + "github.com/hashicorp/terraform-provider-aws/internal/conns" |
| 17 | + "github.com/hashicorp/terraform-provider-aws/internal/framework" |
| 18 | + fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types" |
| 19 | + "github.com/hashicorp/terraform-provider-aws/names" |
| 20 | +) |
| 21 | + |
| 22 | +// @Action(aws_rdsdata_query, name="RDS Data Query") |
| 23 | +func newQueryAction(_ context.Context) (action.ActionWithConfigure, error) { |
| 24 | + return &queryAction{}, nil |
| 25 | +} |
| 26 | + |
| 27 | +// NewQueryAction creates a new query action for testing |
| 28 | +func NewQueryAction(ctx context.Context) (action.ActionWithConfigure, error) { |
| 29 | + return newQueryAction(ctx) |
| 30 | +} |
| 31 | + |
| 32 | +var ( |
| 33 | + _ action.Action = (*queryAction)(nil) |
| 34 | +) |
| 35 | + |
| 36 | +type queryAction struct { |
| 37 | + framework.ActionWithModel[queryActionModel] |
| 38 | + client *rdsdata.Client |
| 39 | +} |
| 40 | + |
| 41 | +type queryActionModel struct { |
| 42 | + framework.WithRegionModel |
| 43 | + ResourceArn fwtypes2.String `tfsdk:"resource_arn"` |
| 44 | + SecretArn fwtypes2.String `tfsdk:"secret_arn"` |
| 45 | + SQL fwtypes2.String `tfsdk:"sql"` |
| 46 | + Database fwtypes2.String `tfsdk:"database"` |
| 47 | + Parameters fwtypes.ListNestedObjectValueOf[sqlParameterModel] `tfsdk:"parameters"` |
| 48 | + IncludeResultMetadata fwtypes2.Bool `tfsdk:"include_result_metadata"` |
| 49 | +} |
| 50 | + |
| 51 | +type sqlParameterModel struct { |
| 52 | + Name fwtypes2.String `tfsdk:"name"` |
| 53 | + Value fwtypes2.String `tfsdk:"value"` |
| 54 | +} |
| 55 | + |
| 56 | +func (a *queryAction) Schema(ctx context.Context, req action.SchemaRequest, resp *action.SchemaResponse) { |
| 57 | + resp.Schema = schema.Schema{ |
| 58 | + Description: "Executes SQL queries against Aurora Serverless clusters using RDS Data API", |
| 59 | + Attributes: map[string]schema.Attribute{ |
| 60 | + names.AttrResourceARN: schema.StringAttribute{ |
| 61 | + Description: "ARN of the Aurora Serverless cluster", |
| 62 | + Required: true, |
| 63 | + }, |
| 64 | + "secret_arn": schema.StringAttribute{ |
| 65 | + Description: "ARN of the Secrets Manager secret containing database credentials", |
| 66 | + Required: true, |
| 67 | + }, |
| 68 | + "sql": schema.StringAttribute{ |
| 69 | + Description: "SQL statement to execute", |
| 70 | + Required: true, |
| 71 | + }, |
| 72 | + names.AttrDatabase: schema.StringAttribute{ |
| 73 | + Description: "Name of the database", |
| 74 | + Optional: true, |
| 75 | + }, |
| 76 | + "include_result_metadata": schema.BoolAttribute{ |
| 77 | + Description: "Include column metadata in query results", |
| 78 | + Optional: true, |
| 79 | + }, |
| 80 | + }, |
| 81 | + Blocks: map[string]schema.Block{ |
| 82 | + names.AttrParameters: schema.ListNestedBlock{ |
| 83 | + Description: "SQL parameters for prepared statements", |
| 84 | + CustomType: fwtypes.NewListNestedObjectTypeOf[sqlParameterModel](ctx), |
| 85 | + NestedObject: schema.NestedBlockObject{ |
| 86 | + Attributes: map[string]schema.Attribute{ |
| 87 | + names.AttrName: schema.StringAttribute{ |
| 88 | + Description: "Parameter name", |
| 89 | + Required: true, |
| 90 | + }, |
| 91 | + names.AttrValue: schema.StringAttribute{ |
| 92 | + Description: "Parameter value", |
| 93 | + Required: true, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func (a *queryAction) Configure(ctx context.Context, req action.ConfigureRequest, resp *action.ConfigureResponse) { |
| 103 | + if req.ProviderData == nil { |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + meta, ok := req.ProviderData.(*conns.AWSClient) |
| 108 | + if !ok { |
| 109 | + resp.Diagnostics.AddError( |
| 110 | + "Unexpected Action Configure Type", |
| 111 | + fmt.Sprintf("Expected *conns.AWSClient, got: %T", req.ProviderData), |
| 112 | + ) |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + a.client = meta.RDSDataClient(ctx) |
| 117 | +} |
| 118 | + |
| 119 | +func (a *queryAction) Invoke(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) { |
| 120 | + var model queryActionModel |
| 121 | + resp.Diagnostics.Append(req.Config.Get(ctx, &model)...) |
| 122 | + if resp.Diagnostics.HasError() { |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + tflog.Info(ctx, "Starting RDS Data query execution", map[string]any{ |
| 127 | + names.AttrResourceARN: model.ResourceArn.ValueString(), |
| 128 | + "sql_length": len(model.SQL.ValueString()), |
| 129 | + }) |
| 130 | + |
| 131 | + resp.SendProgress(action.InvokeProgressEvent{ |
| 132 | + Message: "Executing SQL query...", |
| 133 | + }) |
| 134 | + |
| 135 | + input := rdsdata.ExecuteStatementInput{ |
| 136 | + ResourceArn: model.ResourceArn.ValueStringPointer(), |
| 137 | + SecretArn: model.SecretArn.ValueStringPointer(), |
| 138 | + Sql: model.SQL.ValueStringPointer(), |
| 139 | + } |
| 140 | + |
| 141 | + if !model.Database.IsNull() { |
| 142 | + input.Database = model.Database.ValueStringPointer() |
| 143 | + } |
| 144 | + |
| 145 | + if !model.IncludeResultMetadata.IsNull() { |
| 146 | + input.IncludeResultMetadata = model.IncludeResultMetadata.ValueBool() |
| 147 | + } |
| 148 | + |
| 149 | + if !model.Parameters.IsNull() { |
| 150 | + var params []sqlParameterModel |
| 151 | + resp.Diagnostics.Append(model.Parameters.ElementsAs(ctx, ¶ms, false)...) |
| 152 | + if resp.Diagnostics.HasError() { |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + var sqlParams []types.SqlParameter |
| 157 | + for _, param := range params { |
| 158 | + sqlParams = append(sqlParams, types.SqlParameter{ |
| 159 | + Name: param.Name.ValueStringPointer(), |
| 160 | + Value: &types.FieldMemberStringValue{Value: param.Value.ValueString()}, |
| 161 | + }) |
| 162 | + } |
| 163 | + input.Parameters = sqlParams |
| 164 | + } |
| 165 | + |
| 166 | + output, err := a.client.ExecuteStatement(ctx, &input) |
| 167 | + if err != nil { |
| 168 | + resp.Diagnostics.AddError("SQL Execution Failed", err.Error()) |
| 169 | + return |
| 170 | + } |
| 171 | + |
| 172 | + resp.SendProgress(action.InvokeProgressEvent{ |
| 173 | + Message: fmt.Sprintf("Query executed successfully. Records affected: %d", output.NumberOfRecordsUpdated), |
| 174 | + }) |
| 175 | + |
| 176 | + tflog.Info(ctx, "RDS Data query completed successfully", map[string]any{ |
| 177 | + "records_updated": output.NumberOfRecordsUpdated, |
| 178 | + "has_records": len(output.Records) > 0, |
| 179 | + }) |
| 180 | +} |
0 commit comments