|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package ec2 |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go-v2/service/ec2" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 17 | + "github.com/hashicorp/terraform-provider-aws/internal/create" |
| 18 | + "github.com/hashicorp/terraform-provider-aws/internal/framework" |
| 19 | + "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" |
| 20 | + fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types" |
| 21 | + tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" |
| 22 | + "github.com/hashicorp/terraform-provider-aws/names" |
| 23 | +) |
| 24 | + |
| 25 | +// @FrameworkDataSource("aws_egress_only_internet_gateway", name="Egress Only Internet Gateway") |
| 26 | +func newDataSourceVpcEgressOnlyInternetGateway(context.Context) (datasource.DataSourceWithConfigure, error) { |
| 27 | + return &dataSourceVpcEgressOnlyInternetGateway{}, nil |
| 28 | +} |
| 29 | + |
| 30 | +const ( |
| 31 | + DSNameVpcEgressOnlyInternetGateway = "Vpc Egress Only Internet Gateway Data Source" |
| 32 | +) |
| 33 | + |
| 34 | +type dataSourceVpcEgressOnlyInternetGateway struct { |
| 35 | + framework.DataSourceWithModel[dataSourceVpcEgressOnlyInternetGatewayModel] |
| 36 | +} |
| 37 | + |
| 38 | +func (d *dataSourceVpcEgressOnlyInternetGateway) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 39 | + resp.Schema = schema.Schema{ |
| 40 | + Attributes: map[string]schema.Attribute{ |
| 41 | + names.AttrARN: framework.ARNAttributeComputedOnly(), |
| 42 | + names.AttrID: framework.IDAttribute(), |
| 43 | + "egress_only_internet_gateway_id": schema.StringAttribute{ |
| 44 | + Optional: true, |
| 45 | + Computed: true, |
| 46 | + Validators: []validator.String{ |
| 47 | + stringvalidator.AtLeastOneOf( |
| 48 | + path.MatchRelative().AtParent().AtName(names.AttrTags), |
| 49 | + path.MatchRelative().AtParent().AtName("egress_only_internet_gateway_id"), |
| 50 | + ), |
| 51 | + }, |
| 52 | + }, |
| 53 | + names.AttrOwnerID: schema.StringAttribute{ |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + names.AttrTags: tftags.TagsAttribute(), |
| 57 | + "attachments": schema.ListAttribute{ |
| 58 | + CustomType: fwtypes.NewListNestedObjectTypeOf[eoigAttachmentModel](ctx), |
| 59 | + Computed: true, |
| 60 | + ElementType: types.ObjectType{ |
| 61 | + AttrTypes: map[string]attr.Type{ |
| 62 | + "state": types.StringType, |
| 63 | + "vpc_id": types.StringType, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func (d *dataSourceVpcEgressOnlyInternetGateway) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 72 | + conn := d.Meta().EC2Client(ctx) |
| 73 | + |
| 74 | + var data dataSourceVpcEgressOnlyInternetGatewayModel |
| 75 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 76 | + if resp.Diagnostics.HasError() { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + input := &ec2.DescribeEgressOnlyInternetGatewaysInput{} |
| 81 | + |
| 82 | + if !data.EgressOnlyInternetGatewayID.IsNull() { |
| 83 | + input.EgressOnlyInternetGatewayIds = []string{data.EgressOnlyInternetGatewayID.ValueString()} |
| 84 | + } |
| 85 | + |
| 86 | + input.Filters = newTagFilterList(svcTags(tftags.New(ctx, data.Tags))) |
| 87 | + |
| 88 | + eoigw, err := findEgressOnlyInternetGateway(ctx, conn, input) |
| 89 | + if err != nil { |
| 90 | + resp.Diagnostics.AddError( |
| 91 | + create.ProblemStandardMessage(names.EC2, create.ErrActionReading, DSNameVpcEgressOnlyInternetGateway, data.ID.ValueString(), err), |
| 92 | + err.Error(), |
| 93 | + ) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + resp.Diagnostics.Append(flex.Flatten(ctx, eoigw, &data, flex.WithFieldNamePrefix("EgressOnlyInternetGateway"))...) |
| 98 | + if resp.Diagnostics.HasError() { |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 103 | +} |
| 104 | + |
| 105 | +type dataSourceVpcEgressOnlyInternetGatewayModel struct { |
| 106 | + framework.WithRegionModel |
| 107 | + ARN types.String `tfsdk:"arn"` |
| 108 | + ID types.String `tfsdk:"id"` |
| 109 | + OwnerID types.String `tfsdk:"owner_id"` |
| 110 | + EgressOnlyInternetGatewayID types.String `tfsdk:"egress_only_internet_gateway_id"` |
| 111 | + Attachments fwtypes.ListNestedObjectValueOf[eoigAttachmentModel] `tfsdk:"attachments"` |
| 112 | + Tags tftags.Map `tfsdk:"tags"` |
| 113 | +} |
| 114 | + |
| 115 | +type eoigAttachmentModel struct { |
| 116 | + State types.String `tfsdk:"state"` |
| 117 | + VpcID types.String `tfsdk:"vpc_id"` |
| 118 | +} |
0 commit comments