Skip to content

Commit 9a7beaa

Browse files
committed
feat(ec2/vpc): add datasource egress_only_internet_gateway
1 parent 4b1cc25 commit 9a7beaa

File tree

5 files changed

+270
-0
lines changed

5 files changed

+270
-0
lines changed

.changelog/43319.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-data-source
2+
aws_egress_only_internet_gateway
3+
```

internal/service/ec2/service_package_gen.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package ec2_test
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
11+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
12+
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
13+
"github.com/hashicorp/terraform-provider-aws/names"
14+
)
15+
16+
func TestVPCEgressOnlyInternetGatewayDataSource_basic(t *testing.T) {
17+
ctx := acctest.Context(t)
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
PreCheck: func() { acctest.PreCheck(ctx, t) },
21+
ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID),
22+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
23+
Steps: []resource.TestStep{
24+
{
25+
Config: testAccVPCEgressOnlyInternetGatewayDataSourceConfig_basic(),
26+
Check: resource.ComposeTestCheckFunc(
27+
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_id", "egress_only_internet_gateway_id", "aws_egress_only_internet_gateway.test", names.AttrID),
28+
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_id", names.AttrOwnerID, "aws_egress_only_internet_gateway.test", names.AttrOwnerID),
29+
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_id", "attachments.0.vpc_id", "aws_vpc.test", names.AttrID),
30+
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_id", names.AttrARN, "aws_egress_only_internet_gateway.test", names.AttrARN),
31+
),
32+
},
33+
},
34+
})
35+
}
36+
37+
func TestVPCEgressOnlyInternetGatewayDataSource_tags(t *testing.T) {
38+
ctx := acctest.Context(t)
39+
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
40+
41+
resource.ParallelTest(t, resource.TestCase{
42+
PreCheck: func() { acctest.PreCheck(ctx, t) },
43+
ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID),
44+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
45+
Steps: []resource.TestStep{
46+
{
47+
Config: testAccVPCEgressOnlyInternetGatewayDataSourceConfig_tags(rName),
48+
Check: resource.ComposeTestCheckFunc(
49+
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_tags", "egress_only_internet_gateway_id", "aws_egress_only_internet_gateway.test", names.AttrID),
50+
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_tags", names.AttrOwnerID, "aws_egress_only_internet_gateway.test", names.AttrOwnerID),
51+
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_tags", "attachments.0.vpc_id", "aws_vpc.test", names.AttrID),
52+
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_tags", names.AttrARN, "aws_egress_only_internet_gateway.test", names.AttrARN),
53+
),
54+
},
55+
},
56+
})
57+
}
58+
59+
func testAccVPCEgressOnlyInternetGatewayDataSourceConfig_basic() string {
60+
return `
61+
resource "aws_vpc" "test" {
62+
cidr_block = "172.16.0.0/16"
63+
}
64+
65+
resource "aws_egress_only_internet_gateway" "test" {
66+
vpc_id = aws_vpc.test.id
67+
}
68+
69+
data "aws_egress_only_internet_gateway" "by_id" {
70+
egress_only_internet_gateway_id = aws_egress_only_internet_gateway.test.id
71+
}
72+
`
73+
}
74+
75+
func testAccVPCEgressOnlyInternetGatewayDataSourceConfig_tags(rName string) string {
76+
return fmt.Sprintf(`
77+
resource "aws_vpc" "test" {
78+
cidr_block = "172.16.0.0/16"
79+
}
80+
81+
resource "aws_egress_only_internet_gateway" "test" {
82+
vpc_id = aws_vpc.test.id
83+
84+
tags = {
85+
Name = %[1]q
86+
}
87+
}
88+
89+
data "aws_egress_only_internet_gateway" "by_tags" {
90+
tags = {
91+
Name = %[1]q
92+
}
93+
94+
depends_on = [
95+
aws_egress_only_internet_gateway.test,
96+
]
97+
}
98+
`, rName)
99+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
subcategory: "VPC (Virtual Private Cloud)"
3+
layout: "aws"
4+
page_title: "AWS: aws_egress_only_internet_gateway"
5+
description: |-
6+
Provides details about a specific Egress-Only Internet Gateway.
7+
---
8+
9+
# Data Source: aws_egress_only_internet_gateway
10+
11+
`aws_egress_only_internet_gateway` provides details about a specific Egress-Only Internet Gateway.
12+
13+
## Example Usage
14+
15+
### Basic Usage
16+
17+
```terraform
18+
variable "eoig_id" {}
19+
20+
data "aws_egress_only_internet_gateway" "default" {
21+
egress_only_internet_gateway_id = var.eoig_id
22+
}
23+
```
24+
25+
## Argument Reference
26+
27+
This data source supports the following arguments:
28+
29+
* `egress_only_internet_gateway_id` - (Optional) ID of the specific Egress-Only Internet Gateway to retrieve.
30+
* `tags` - (Optional) Map of tags, each pair of which must exactly match
31+
a pair on the desired Egress-Only Internet Gateway.
32+
33+
## Attribute Reference
34+
35+
This data source exports the following attributes in addition to the arguments above:
36+
37+
* `arn` - ARN of the Egress-Only Internet Gateway.
38+
* `owner_id` - ID of the AWS account that owns the egress-only internet gateway.
39+
40+
`attachments` are also exported with the following attributes, when there are relevants:
41+
Each attachment supports the following:
42+
43+
* `state` - Current state of the attachment between the gateway and the VPC. Present only if a VPC is attached
44+
* `vpc_id` - ID of an attached VPC.

0 commit comments

Comments
 (0)