|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package iam |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go-v2/service/iam" |
| 10 | + awstypes "github.com/aws/aws-sdk-go-v2/service/iam/types" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 17 | + "github.com/hashicorp/terraform-provider-aws/internal/errs" |
| 18 | + "github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag" |
| 19 | + "github.com/hashicorp/terraform-provider-aws/internal/framework" |
| 20 | + fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" |
| 21 | + "github.com/hashicorp/terraform-provider-aws/internal/retry" |
| 22 | + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" |
| 23 | +) |
| 24 | + |
| 25 | +// @FrameworkResource("aws_iam_outbound_web_identity_federation", name="Outbound Web Identity Federation") |
| 26 | +// @SingletonIdentity |
| 27 | +// @Testing(hasNoPreExistingResource=true) |
| 28 | +// @Testing(serialize=true) |
| 29 | +// @Testing(importStateIdFunc=importStateIDAccountID", importStateIdAttribute="issuer_identifier") |
| 30 | +// @Testing(generator=false) |
| 31 | +// @Testing(existsTakesT=true, destroyTakesT=true) |
| 32 | +func newOutboundWebIdentityFederationResource(_ context.Context) (resource.ResourceWithConfigure, error) { |
| 33 | + r := &outboundWebIdentityFederationResource{} |
| 34 | + |
| 35 | + return r, nil |
| 36 | +} |
| 37 | + |
| 38 | +type outboundWebIdentityFederationResource struct { |
| 39 | + framework.ResourceWithModel[outboundWebIdentityFederationResourceModel] |
| 40 | + framework.WithNoUpdate |
| 41 | + framework.WithImportByIdentity |
| 42 | +} |
| 43 | + |
| 44 | +func (r *outboundWebIdentityFederationResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 45 | + resp.Schema = schema.Schema{ |
| 46 | + Attributes: map[string]schema.Attribute{ |
| 47 | + "issuer_identifier": schema.StringAttribute{ |
| 48 | + Computed: true, |
| 49 | + PlanModifiers: []planmodifier.String{ |
| 50 | + stringplanmodifier.UseStateForUnknown(), |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func (r *outboundWebIdentityFederationResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 58 | + var data outboundWebIdentityFederationResourceModel |
| 59 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 60 | + if resp.Diagnostics.HasError() { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + conn := r.Meta().IAMClient(ctx) |
| 65 | + |
| 66 | + var input iam.EnableOutboundWebIdentityFederationInput |
| 67 | + out, err := conn.EnableOutboundWebIdentityFederation(ctx, &input) |
| 68 | + if err != nil { |
| 69 | + resp.Diagnostics.AddError("enabling IAM Outbound Web Identity Federation", err.Error()) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + // Set values for unknowns. |
| 74 | + data.IssuerIdentifier = fwflex.StringToFramework(ctx, out.IssuerIdentifier) |
| 75 | + |
| 76 | + resp.Diagnostics.Append(resp.State.Set(ctx, data)...) |
| 77 | +} |
| 78 | + |
| 79 | +func (r *outboundWebIdentityFederationResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 80 | + var data outboundWebIdentityFederationResourceModel |
| 81 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 82 | + if resp.Diagnostics.HasError() { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + conn := r.Meta().IAMClient(ctx) |
| 87 | + |
| 88 | + out, err := findOutboundWebIdentityFederation(ctx, conn) |
| 89 | + if retry.NotFound(err) { |
| 90 | + resp.Diagnostics.Append(fwdiag.NewResourceNotFoundWarningDiagnostic(err)) |
| 91 | + resp.State.RemoveResource(ctx) |
| 92 | + return |
| 93 | + } |
| 94 | + if err != nil { |
| 95 | + resp.Diagnostics.AddError("reading IAM Outbound Web Identity Federation", err.Error()) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + // Set attributes for import. |
| 100 | + data.IssuerIdentifier = fwflex.StringToFramework(ctx, out.IssuerIdentifier) |
| 101 | + |
| 102 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 103 | +} |
| 104 | + |
| 105 | +func (r *outboundWebIdentityFederationResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 106 | + conn := r.Meta().IAMClient(ctx) |
| 107 | + |
| 108 | + var input iam.DisableOutboundWebIdentityFederationInput |
| 109 | + _, err := conn.DisableOutboundWebIdentityFederation(ctx, &input) |
| 110 | + if errs.IsA[*awstypes.FeatureDisabledException](err) { |
| 111 | + return |
| 112 | + } |
| 113 | + if err != nil { |
| 114 | + resp.Diagnostics.AddError("disabling IAM Outbound Web Identity Federation", err.Error()) |
| 115 | + return |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func (r *outboundWebIdentityFederationResource) ImportState(ctx context.Context, request resource.ImportStateRequest, response *resource.ImportStateResponse) { |
| 120 | + r.WithImportByIdentity.ImportState(ctx, request, response) |
| 121 | + |
| 122 | + // Touch a value to bypass a Framework check |
| 123 | + response.Diagnostics.Append(response.State.SetAttribute(ctx, path.Root("issuer_identifier"), types.StringUnknown())...) |
| 124 | +} |
| 125 | + |
| 126 | +func findOutboundWebIdentityFederation(ctx context.Context, conn *iam.Client) (*iam.GetOutboundWebIdentityFederationInfoOutput, error) { |
| 127 | + var input iam.GetOutboundWebIdentityFederationInfoInput |
| 128 | + out, err := conn.GetOutboundWebIdentityFederationInfo(ctx, &input) |
| 129 | + |
| 130 | + if errs.IsA[*awstypes.FeatureDisabledException](err) { |
| 131 | + return nil, &retry.NotFoundError{ |
| 132 | + LastError: err, |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if err != nil { |
| 137 | + return nil, err |
| 138 | + } |
| 139 | + |
| 140 | + if out == nil { |
| 141 | + return nil, tfresource.NewEmptyResultError(&input) |
| 142 | + } |
| 143 | + |
| 144 | + return out, nil |
| 145 | +} |
| 146 | + |
| 147 | +type outboundWebIdentityFederationResourceModel struct { |
| 148 | + IssuerIdentifier types.String `tfsdk:"issuer_identifier"` |
| 149 | +} |
0 commit comments