|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package rdsdata |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go-v2/service/rdsdata" |
| 10 | + rdsdatatypes "github.com/aws/aws-sdk-go-v2/service/rdsdata/types" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 16 | + "github.com/hashicorp/terraform-provider-aws/internal/framework" |
| 17 | + "github.com/hashicorp/terraform-provider-aws/names" |
| 18 | +) |
| 19 | + |
| 20 | +// @FrameworkResource("aws_rdsdata_query", name="Query") |
| 21 | +func newResourceQuery(context.Context) (resource.ResourceWithConfigure, error) { |
| 22 | + return &resourceQuery{}, nil |
| 23 | +} |
| 24 | + |
| 25 | +type resourceQuery struct { |
| 26 | + framework.ResourceWithModel[resourceQueryModel] |
| 27 | +} |
| 28 | + |
| 29 | +func (r *resourceQuery) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 30 | + resp.Schema = schema.Schema{ |
| 31 | + Attributes: map[string]schema.Attribute{ |
| 32 | + names.AttrID: framework.IDAttribute(), |
| 33 | + names.AttrDatabase: schema.StringAttribute{ |
| 34 | + Optional: true, |
| 35 | + PlanModifiers: []planmodifier.String{ |
| 36 | + stringplanmodifier.RequiresReplace(), |
| 37 | + }, |
| 38 | + }, |
| 39 | + names.AttrResourceARN: schema.StringAttribute{ |
| 40 | + Required: true, |
| 41 | + PlanModifiers: []planmodifier.String{ |
| 42 | + stringplanmodifier.RequiresReplace(), |
| 43 | + }, |
| 44 | + }, |
| 45 | + "secret_arn": schema.StringAttribute{ |
| 46 | + Required: true, |
| 47 | + PlanModifiers: []planmodifier.String{ |
| 48 | + stringplanmodifier.RequiresReplace(), |
| 49 | + }, |
| 50 | + }, |
| 51 | + "sql": schema.StringAttribute{ |
| 52 | + Required: true, |
| 53 | + PlanModifiers: []planmodifier.String{ |
| 54 | + stringplanmodifier.RequiresReplace(), |
| 55 | + }, |
| 56 | + }, |
| 57 | + "records": schema.StringAttribute{ |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + "number_of_records_updated": schema.Int64Attribute{ |
| 61 | + Computed: true, |
| 62 | + }, |
| 63 | + }, |
| 64 | + Blocks: map[string]schema.Block{ |
| 65 | + names.AttrParameters: schema.ListNestedBlock{ |
| 66 | + NestedObject: schema.NestedBlockObject{ |
| 67 | + Attributes: map[string]schema.Attribute{ |
| 68 | + names.AttrName: schema.StringAttribute{ |
| 69 | + Required: true, |
| 70 | + }, |
| 71 | + names.AttrValue: schema.StringAttribute{ |
| 72 | + Required: true, |
| 73 | + }, |
| 74 | + "type_hint": schema.StringAttribute{ |
| 75 | + Optional: true, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +type resourceQueryModel struct { |
| 85 | + framework.WithRegionModel |
| 86 | + ID types.String `tfsdk:"id"` |
| 87 | + Database types.String `tfsdk:"database"` |
| 88 | + ResourceARN types.String `tfsdk:"resource_arn"` |
| 89 | + SecretARN types.String `tfsdk:"secret_arn"` |
| 90 | + SQL types.String `tfsdk:"sql"` |
| 91 | + Parameters []resourceQueryParameterModel `tfsdk:"parameters"` |
| 92 | + Records types.String `tfsdk:"records"` |
| 93 | + NumberOfRecordsUpdated types.Int64 `tfsdk:"number_of_records_updated"` |
| 94 | +} |
| 95 | + |
| 96 | +type resourceQueryParameterModel struct { |
| 97 | + Name types.String `tfsdk:"name"` |
| 98 | + Value types.String `tfsdk:"value"` |
| 99 | + TypeHint types.String `tfsdk:"type_hint"` |
| 100 | +} |
| 101 | + |
| 102 | +func (r *resourceQuery) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 103 | + var data resourceQueryModel |
| 104 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 105 | + if resp.Diagnostics.HasError() { |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + conn := r.Meta().RDSDataClient(ctx) |
| 110 | + |
| 111 | + input := rdsdata.ExecuteStatementInput{ |
| 112 | + ResourceArn: data.ResourceARN.ValueStringPointer(), |
| 113 | + SecretArn: data.SecretARN.ValueStringPointer(), |
| 114 | + Sql: data.SQL.ValueStringPointer(), |
| 115 | + FormatRecordsAs: rdsdatatypes.RecordsFormatTypeJson, |
| 116 | + } |
| 117 | + |
| 118 | + if !data.Database.IsNull() { |
| 119 | + input.Database = data.Database.ValueStringPointer() |
| 120 | + } |
| 121 | + |
| 122 | + if len(data.Parameters) > 0 { |
| 123 | + // Convert resource parameter model to data source parameter model for compatibility |
| 124 | + var params []dataSourceQueryParameterModel |
| 125 | + for _, p := range data.Parameters { |
| 126 | + params = append(params, dataSourceQueryParameterModel(p)) |
| 127 | + } |
| 128 | + input.Parameters = expandSQLParameters(params) |
| 129 | + } |
| 130 | + |
| 131 | + output, err := conn.ExecuteStatement(ctx, &input) |
| 132 | + if err != nil { |
| 133 | + resp.Diagnostics.AddError("executing RDS Data API statement", err.Error()) |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + data.ID = types.StringValue(data.ResourceARN.ValueString() + ":" + data.SQL.ValueString()) |
| 138 | + data.Records = types.StringPointerValue(output.FormattedRecords) |
| 139 | + data.NumberOfRecordsUpdated = types.Int64Value(output.NumberOfRecordsUpdated) |
| 140 | + |
| 141 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 142 | +} |
| 143 | + |
| 144 | +func (r *resourceQuery) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 145 | + // No-op: query results are stored in state and don't need to be refreshed |
| 146 | +} |
| 147 | + |
| 148 | +func (r *resourceQuery) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 149 | + // No-op: all changes require replacement |
| 150 | +} |
| 151 | + |
| 152 | +func (r *resourceQuery) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 153 | + // No-op: no API call needed, just remove from state |
| 154 | +} |
0 commit comments