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