|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package quicksight |
| 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/quicksight" |
| 12 | + awstypes "github.com/aws/aws-sdk-go-v2/service/quicksight/types" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework-validators/setvalidator" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 19 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 20 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 21 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 22 | + "github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag" |
| 23 | + "github.com/hashicorp/terraform-provider-aws/internal/framework" |
| 24 | + fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" |
| 25 | + fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types" |
| 26 | + fwvalidators "github.com/hashicorp/terraform-provider-aws/internal/framework/validators" |
| 27 | + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" |
| 28 | + "github.com/hashicorp/terraform-provider-aws/names" |
| 29 | +) |
| 30 | + |
| 31 | +// @FrameworkResource("aws_quicksight_key_registration", name="Key Registration") |
| 32 | +func newKeyRegistrationResource(_ context.Context) (resource.ResourceWithConfigure, error) { |
| 33 | + r := &keyRegistrationResource{} |
| 34 | + |
| 35 | + return r, nil |
| 36 | +} |
| 37 | + |
| 38 | +type keyRegistrationResource struct { |
| 39 | + framework.ResourceWithModel[keyRegistrationResourceModel] |
| 40 | +} |
| 41 | + |
| 42 | +func (r *keyRegistrationResource) Schema(ctx context.Context, request resource.SchemaRequest, response *resource.SchemaResponse) { |
| 43 | + response.Schema = schema.Schema{ |
| 44 | + Attributes: map[string]schema.Attribute{ |
| 45 | + names.AttrAWSAccountID: schema.StringAttribute{ |
| 46 | + Optional: true, |
| 47 | + Computed: true, |
| 48 | + Validators: []validator.String{ |
| 49 | + fwvalidators.AWSAccountID(), |
| 50 | + }, |
| 51 | + PlanModifiers: []planmodifier.String{ |
| 52 | + stringplanmodifier.UseStateForUnknown(), |
| 53 | + stringplanmodifier.RequiresReplace(), |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + Blocks: map[string]schema.Block{ |
| 58 | + "key_registration": schema.SetNestedBlock{ |
| 59 | + CustomType: fwtypes.NewSetNestedObjectTypeOf[registeredCustomerManagedKeyModel](ctx), |
| 60 | + Validators: []validator.Set{ |
| 61 | + setvalidator.IsRequired(), |
| 62 | + setvalidator.SizeAtLeast(1), |
| 63 | + }, |
| 64 | + NestedObject: schema.NestedBlockObject{ |
| 65 | + Attributes: map[string]schema.Attribute{ |
| 66 | + "default_key": schema.BoolAttribute{ |
| 67 | + Optional: true, |
| 68 | + Computed: true, |
| 69 | + Default: booldefault.StaticBool(false), |
| 70 | + }, |
| 71 | + "key_arn": schema.StringAttribute{ |
| 72 | + CustomType: fwtypes.ARNType, |
| 73 | + Required: true, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func (r *keyRegistrationResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) { |
| 83 | + var data keyRegistrationResourceModel |
| 84 | + response.Diagnostics.Append(request.Plan.Get(ctx, &data)...) |
| 85 | + if response.Diagnostics.HasError() { |
| 86 | + return |
| 87 | + } |
| 88 | + if data.AWSAccountID.IsUnknown() { |
| 89 | + data.AWSAccountID = fwflex.StringValueToFramework(ctx, r.Meta().AccountID(ctx)) |
| 90 | + } |
| 91 | + |
| 92 | + conn := r.Meta().QuickSightClient(ctx) |
| 93 | + |
| 94 | + accountID := fwflex.StringValueFromFramework(ctx, data.AWSAccountID) |
| 95 | + var input quicksight.UpdateKeyRegistrationInput |
| 96 | + response.Diagnostics.Append(fwflex.Expand(ctx, data, &input)...) |
| 97 | + if response.Diagnostics.HasError() { |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + _, err := conn.UpdateKeyRegistration(ctx, &input) |
| 102 | + |
| 103 | + if err != nil { |
| 104 | + response.Diagnostics.AddError(fmt.Sprintf("creating Quicksight Key Registration (%s)", accountID), err.Error()) |
| 105 | + |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + response.Diagnostics.Append(response.State.Set(ctx, data)...) |
| 110 | +} |
| 111 | + |
| 112 | +func (r *keyRegistrationResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) { |
| 113 | + var data keyRegistrationResourceModel |
| 114 | + response.Diagnostics.Append(request.State.Get(ctx, &data)...) |
| 115 | + if response.Diagnostics.HasError() { |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + conn := r.Meta().QuickSightClient(ctx) |
| 120 | + |
| 121 | + accountID := fwflex.StringValueFromFramework(ctx, data.AWSAccountID) |
| 122 | + output, err := findKeyRegistrationByID(ctx, conn, accountID) |
| 123 | + |
| 124 | + if tfresource.NotFound(err) { |
| 125 | + response.Diagnostics.Append(fwdiag.NewResourceNotFoundWarningDiagnostic(err)) |
| 126 | + response.State.RemoveResource(ctx) |
| 127 | + |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + if err != nil { |
| 132 | + response.Diagnostics.AddError(fmt.Sprintf("reading Quicksight Key Registration (%s)", accountID), err.Error()) |
| 133 | + |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + // Set attributes for import. |
| 138 | + response.Diagnostics.Append(fwflex.Flatten(ctx, output, &data.KeyRegistration)...) |
| 139 | + if response.Diagnostics.HasError() { |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + response.Diagnostics.Append(response.State.Set(ctx, &data)...) |
| 144 | +} |
| 145 | + |
| 146 | +func (r *keyRegistrationResource) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) { |
| 147 | + var new, old keyRegistrationResourceModel |
| 148 | + response.Diagnostics.Append(request.Plan.Get(ctx, &new)...) |
| 149 | + if response.Diagnostics.HasError() { |
| 150 | + return |
| 151 | + } |
| 152 | + response.Diagnostics.Append(request.State.Get(ctx, &old)...) |
| 153 | + if response.Diagnostics.HasError() { |
| 154 | + return |
| 155 | + } |
| 156 | + |
| 157 | + conn := r.Meta().QuickSightClient(ctx) |
| 158 | + |
| 159 | + accountID := fwflex.StringValueFromFramework(ctx, new.AWSAccountID) |
| 160 | + var input quicksight.UpdateKeyRegistrationInput |
| 161 | + response.Diagnostics.Append(fwflex.Expand(ctx, new, &input)...) |
| 162 | + if response.Diagnostics.HasError() { |
| 163 | + return |
| 164 | + } |
| 165 | + |
| 166 | + _, err := conn.UpdateKeyRegistration(ctx, &input) |
| 167 | + |
| 168 | + if err != nil { |
| 169 | + response.Diagnostics.AddError(fmt.Sprintf("creating Quicksight Key Registration (%s)", accountID), err.Error()) |
| 170 | + |
| 171 | + return |
| 172 | + } |
| 173 | + |
| 174 | + response.Diagnostics.Append(response.State.Set(ctx, &new)...) |
| 175 | +} |
| 176 | + |
| 177 | +func (r *keyRegistrationResource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) { |
| 178 | + var data keyRegistrationResourceModel |
| 179 | + response.Diagnostics.Append(request.State.Get(ctx, &data)...) |
| 180 | + if response.Diagnostics.HasError() { |
| 181 | + return |
| 182 | + } |
| 183 | + |
| 184 | + conn := r.Meta().QuickSightClient(ctx) |
| 185 | + |
| 186 | + accountID := fwflex.StringValueFromFramework(ctx, data.AWSAccountID) |
| 187 | + input := quicksight.UpdateKeyRegistrationInput{ |
| 188 | + AwsAccountId: aws.String(accountID), |
| 189 | + KeyRegistration: []awstypes.RegisteredCustomerManagedKey{}, |
| 190 | + } |
| 191 | + _, err := conn.UpdateKeyRegistration(ctx, &input) |
| 192 | + |
| 193 | + if err != nil { |
| 194 | + response.Diagnostics.AddError(fmt.Sprintf("deleting Quicksight Key Registration (%s)", accountID), err.Error()) |
| 195 | + |
| 196 | + return |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +func (r *keyRegistrationResource) ImportState(ctx context.Context, request resource.ImportStateRequest, response *resource.ImportStateResponse) { |
| 201 | + resource.ImportStatePassthroughID(ctx, path.Root(names.AttrAWSAccountID), request, response) |
| 202 | +} |
| 203 | + |
| 204 | +func findKeyRegistrationByID(ctx context.Context, conn *quicksight.Client, id string) ([]awstypes.RegisteredCustomerManagedKey, error) { |
| 205 | + input := quicksight.DescribeKeyRegistrationInput{ |
| 206 | + AwsAccountId: aws.String(id), |
| 207 | + } |
| 208 | + output, err := conn.DescribeKeyRegistration(ctx, &input) |
| 209 | + |
| 210 | + if err != nil { |
| 211 | + return nil, err |
| 212 | + } |
| 213 | + |
| 214 | + if output == nil || len(output.KeyRegistration) == 0 { |
| 215 | + return nil, tfresource.NewEmptyResultError(&input) |
| 216 | + } |
| 217 | + |
| 218 | + return output.KeyRegistration, nil |
| 219 | +} |
| 220 | + |
| 221 | +type keyRegistrationResourceModel struct { |
| 222 | + framework.WithRegionModel |
| 223 | + AWSAccountID types.String `tfsdk:"aws_account_id"` |
| 224 | + KeyRegistration fwtypes.SetNestedObjectValueOf[registeredCustomerManagedKeyModel] `tfsdk:"key_registration"` |
| 225 | +} |
| 226 | + |
| 227 | +type registeredCustomerManagedKeyModel struct { |
| 228 | + DefaultKey types.Bool `tfsdk:"default_key"` |
| 229 | + KeyARN fwtypes.ARN `tfsdk:"key_arn"` |
| 230 | +} |
0 commit comments