Skip to content

[New Data Source] aws_egress_only_internet_gateway #43319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/43319.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_egress_only_internet_gateway
```
6 changes: 6 additions & 0 deletions internal/service/ec2/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions internal/service/ec2/vpc_egress_only_internet_gateway_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package ec2

import (
"context"

"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkDataSource("aws_egress_only_internet_gateway", name="Egress Only Internet Gateway")
func newDataSourceVpcEgressOnlyInternetGateway(context.Context) (datasource.DataSourceWithConfigure, error) {
return &dataSourceVpcEgressOnlyInternetGateway{}, nil
}

const (
DSNameVpcEgressOnlyInternetGateway = "Vpc Egress Only Internet Gateway Data Source"
)

type dataSourceVpcEgressOnlyInternetGateway struct {
framework.DataSourceWithModel[dataSourceVpcEgressOnlyInternetGatewayModel]
}

func (d *dataSourceVpcEgressOnlyInternetGateway) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
names.AttrARN: framework.ARNAttributeComputedOnly(),
names.AttrID: framework.IDAttribute(),
"egress_only_internet_gateway_id": schema.StringAttribute{
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.AtLeastOneOf(
path.MatchRelative().AtParent().AtName(names.AttrTags),
path.MatchRelative().AtParent().AtName("egress_only_internet_gateway_id"),
),
},
},
names.AttrOwnerID: schema.StringAttribute{
Computed: true,
},
names.AttrTags: tftags.TagsAttribute(),
"attachments": schema.ListAttribute{
CustomType: fwtypes.NewListNestedObjectTypeOf[eoigAttachmentModel](ctx),
Computed: true,
ElementType: types.ObjectType{
AttrTypes: map[string]attr.Type{
"state": types.StringType,
"vpc_id": types.StringType,
},
},
},
},
}
}

func (d *dataSourceVpcEgressOnlyInternetGateway) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
conn := d.Meta().EC2Client(ctx)

var data dataSourceVpcEgressOnlyInternetGatewayModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

input := &ec2.DescribeEgressOnlyInternetGatewaysInput{}

if !data.EgressOnlyInternetGatewayID.IsNull() {
input.EgressOnlyInternetGatewayIds = []string{data.EgressOnlyInternetGatewayID.ValueString()}
}

input.Filters = newTagFilterList(svcTags(tftags.New(ctx, data.Tags)))

eoigw, err := findEgressOnlyInternetGateway(ctx, conn, input)
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.EC2, create.ErrActionReading, DSNameVpcEgressOnlyInternetGateway, data.ID.ValueString(), err),
err.Error(),
)
return
}

resp.Diagnostics.Append(flex.Flatten(ctx, eoigw, &data, flex.WithFieldNamePrefix("EgressOnlyInternetGateway"))...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

type dataSourceVpcEgressOnlyInternetGatewayModel struct {
framework.WithRegionModel
ARN types.String `tfsdk:"arn"`
ID types.String `tfsdk:"id"`
OwnerID types.String `tfsdk:"owner_id"`
EgressOnlyInternetGatewayID types.String `tfsdk:"egress_only_internet_gateway_id"`
Attachments fwtypes.ListNestedObjectValueOf[eoigAttachmentModel] `tfsdk:"attachments"`
Tags tftags.Map `tfsdk:"tags"`
}

type eoigAttachmentModel struct {
State types.String `tfsdk:"state"`
VpcID types.String `tfsdk:"vpc_id"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package ec2_test

import (
"fmt"
"testing"

sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
)

func TestVPCEgressOnlyInternetGatewayDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccVPCEgressOnlyInternetGatewayDataSourceConfig_basic(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_id", "egress_only_internet_gateway_id", "aws_egress_only_internet_gateway.test", names.AttrID),
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_id", names.AttrOwnerID, "aws_egress_only_internet_gateway.test", names.AttrOwnerID),
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_id", "attachments.0.vpc_id", "aws_vpc.test", names.AttrID),
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_id", names.AttrARN, "aws_egress_only_internet_gateway.test", names.AttrARN),
),
},
},
})
}

func TestVPCEgressOnlyInternetGatewayDataSource_tags(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccVPCEgressOnlyInternetGatewayDataSourceConfig_tags(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_tags", "egress_only_internet_gateway_id", "aws_egress_only_internet_gateway.test", names.AttrID),
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_tags", names.AttrOwnerID, "aws_egress_only_internet_gateway.test", names.AttrOwnerID),
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_tags", "attachments.0.vpc_id", "aws_vpc.test", names.AttrID),
resource.TestCheckResourceAttrPair("data.aws_egress_only_internet_gateway.by_tags", names.AttrARN, "aws_egress_only_internet_gateway.test", names.AttrARN),
),
},
},
})
}

func testAccVPCEgressOnlyInternetGatewayDataSourceConfig_basic() string {
return `
resource "aws_vpc" "test" {
cidr_block = "172.16.0.0/16"
}

resource "aws_egress_only_internet_gateway" "test" {
vpc_id = aws_vpc.test.id
}

data "aws_egress_only_internet_gateway" "by_id" {
egress_only_internet_gateway_id = aws_egress_only_internet_gateway.test.id
}
`
}

func testAccVPCEgressOnlyInternetGatewayDataSourceConfig_tags(rName string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "172.16.0.0/16"
}

resource "aws_egress_only_internet_gateway" "test" {
vpc_id = aws_vpc.test.id

tags = {
Name = %[1]q
}
}

data "aws_egress_only_internet_gateway" "by_tags" {
tags = {
Name = %[1]q
}

depends_on = [
aws_egress_only_internet_gateway.test,
]
}
`, rName)
}
45 changes: 45 additions & 0 deletions website/docs/d/egress_only_internet_gateway.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
subcategory: "VPC (Virtual Private Cloud)"
layout: "aws"
page_title: "AWS: aws_egress_only_internet_gateway"
description: |-
Provides details about a specific Egress-Only Internet Gateway.
---

# Data Source: aws_egress_only_internet_gateway

`aws_egress_only_internet_gateway` provides details about a specific Egress-Only Internet Gateway.

## Example Usage

### Basic Usage

```terraform
variable "eoig_id" {}

data "aws_egress_only_internet_gateway" "default" {
egress_only_internet_gateway_id = var.eoig_id
}
```

## Argument Reference

This data source supports the following arguments:

* `region` - (Optional) Region where this resource will be [managed](https://docs.aws.amazon.com/general/latest/gr/rande.html#regional-endpoints). Defaults to the Region set in the [provider configuration](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#aws-configuration-reference).
* `egress_only_internet_gateway_id` - (Optional) ID of the specific Egress-Only Internet Gateway to retrieve.
* `tags` - (Optional) Map of tags, each pair of which must exactly match
a pair on the desired Egress-Only Internet Gateway.

## Attribute Reference

This data source exports the following attributes in addition to the arguments above:

* `arn` - ARN of the Egress-Only Internet Gateway.
* `owner_id` - ID of the AWS account that owns the egress-only internet gateway.

`attachments` are also exported with the following attributes, when there are relevants:
Each attachment supports the following:

* `state` - Current state of the attachment between the gateway and the VPC. Present only if a VPC is attached
* `vpc_id` - ID of an attached VPC.
Loading