Skip to content
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

Add aws_nat_gateways data source #24190

Merged
merged 6 commits into from
Apr 12, 2022
Merged
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/24190.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_nat_gateways
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ func Provider() *schema.Provider {
"aws_key_pair": ec2.DataSourceKeyPair(),
"aws_launch_template": ec2.DataSourceLaunchTemplate(),
"aws_nat_gateway": ec2.DataSourceNATGateway(),
"aws_nat_gateways": ec2.DataSourceNATGateways(),
"aws_network_acls": ec2.DataSourceNetworkACLs(),
"aws_network_interface": ec2.DataSourceNetworkInterface(),
"aws_network_interfaces": ec2.DataSourceNetworkInterfaces(),
Expand Down
76 changes: 76 additions & 0 deletions internal/service/ec2/nat_gateways_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package ec2

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

func DataSourceNATGateways() *schema.Resource {
return &schema.Resource{
Read: dataSourceNATGatewaysRead,

Schema: map[string]*schema.Schema{
"filter": DataSourceFiltersSchema(),
"ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tftags.TagsSchemaComputed(),
"vpc_id": {
Type: schema.TypeString,
Optional: true,
},
},
}
}

func dataSourceNATGatewaysRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).EC2Conn

input := &ec2.DescribeNatGatewaysInput{}

if v, ok := d.GetOk("vpc_id"); ok {
input.Filter = append(input.Filter, BuildAttributeFilterList(
map[string]string{
"vpc-id": v.(string),
},
)...)
}

if tags, ok := d.GetOk("tags"); ok {
input.Filter = append(input.Filter, BuildTagFilterList(
Tags(tftags.New(tags.(map[string]interface{}))),
)...)
}

input.Filter = append(input.Filter, BuildFiltersDataSource(
d.Get("filter").(*schema.Set),
)...)

if len(input.Filter) == 0 {
input.Filter = nil
}

output, err := FindNATGateways(conn, input)

if err != nil {
return fmt.Errorf("error reading EC2 NAT Gateways: %w", err)
}

var natGatewayIDs []string

for _, v := range output {
natGatewayIDs = append(natGatewayIDs, aws.StringValue(v.NatGatewayId))
}

d.SetId(meta.(*conns.AWSClient).Region)
d.Set("ids", natGatewayIDs)

return nil
}
196 changes: 196 additions & 0 deletions internal/service/ec2/nat_gateways_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package ec2_test

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/ec2"
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
Providers: acctest.Providers,
Steps: []resource.TestStep{
{
Config: testAccNATGatewaysDataSourceConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_nat_gateways.by_vpc_id", "ids.#", "2"),
resource.TestCheckResourceAttr("data.aws_nat_gateways.by_tags", "ids.#", "1"),
resource.TestCheckResourceAttr("data.aws_nat_gateways.by_filter", "ids.#", "3"),
resource.TestCheckResourceAttr("data.aws_nat_gateways.empty", "ids.#", "0"),
),
},
},
})
}

func testAccNATGatewaysDataSourceConfig(rName string) string {
return acctest.ConfigCompose(acctest.ConfigAvailableAZsNoOptIn(), fmt.Sprintf(`
resource "aws_vpc" "test1" {
cidr_block = "172.5.0.0/16"

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

resource "aws_vpc" "test2" {
cidr_block = "172.5.0.0/16"

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

resource "aws_subnet" "test1" {
vpc_id = aws_vpc.test1.id
cidr_block = "172.5.123.0/24"
availability_zone = data.aws_availability_zones.available.names[0]

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

resource "aws_subnet" "test2" {
vpc_id = aws_vpc.test2.id
cidr_block = "172.5.123.0/24"
availability_zone = data.aws_availability_zones.available.names[0]

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

resource "aws_subnet" "test3" {
vpc_id = aws_vpc.test2.id
cidr_block = "172.5.124.0/24"
availability_zone = data.aws_availability_zones.available.names[0]

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

resource "aws_eip" "test1" {
vpc = true

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

resource "aws_eip" "test2" {
vpc = true

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

resource "aws_eip" "test3" {
vpc = true

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

resource "aws_internet_gateway" "test1" {
vpc_id = aws_vpc.test1.id

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

resource "aws_internet_gateway" "test2" {
vpc_id = aws_vpc.test2.id

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

resource "aws_nat_gateway" "test1" {
subnet_id = aws_subnet.test1.id
allocation_id = aws_eip.test1.id

tags = {
Name = %[1]q
OtherTag = "some-value"
}

depends_on = [aws_internet_gateway.test1]
}

resource "aws_nat_gateway" "test2" {
subnet_id = aws_subnet.test2.id
allocation_id = aws_eip.test2.id

tags = {
Name = %[1]q
OtherTag = "some-other-value"
}

depends_on = [aws_internet_gateway.test2]
}

resource "aws_nat_gateway" "test3" {
subnet_id = aws_subnet.test3.id
allocation_id = aws_eip.test3.id

tags = {
Name = %[1]q
OtherTag = "some-other-value"
}

depends_on = [aws_internet_gateway.test2]
}

data "aws_nat_gateways" "by_vpc_id" {
vpc_id = aws_vpc.test2.id

depends_on = [aws_nat_gateway.test1, aws_nat_gateway.test2, aws_nat_gateway.test3]
}

data "aws_nat_gateways" "by_tags" {
filter {
name = "state"
values = ["available"]
}

tags = {
OtherTag = "some-value"
}

depends_on = [aws_nat_gateway.test1, aws_nat_gateway.test2, aws_nat_gateway.test3]
}

data "aws_nat_gateways" "by_filter" {
filter {
name = "vpc-id"
values = [aws_vpc.test1.id, aws_vpc.test2.id]
}

depends_on = [aws_nat_gateway.test1, aws_nat_gateway.test2, aws_nat_gateway.test3]
}

data "aws_nat_gateways" "empty" {
vpc_id = aws_vpc.test2.id

tags = {
OtherTag = "some-value"
}

depends_on = [aws_nat_gateway.test1, aws_nat_gateway.test2, aws_nat_gateway.test3]
}
`, rName))
}
51 changes: 51 additions & 0 deletions website/docs/d/nat_gateways.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
subcategory: "VPC (Virtual Private Cloud)"
layout: "aws"
page_title: "AWS: aws_nat_gateways"
description: |-
Get information on Amazon NAT Gateways.
---

# Data Source: aws_nat_gateways

This resource can be useful for getting back a list of NAT gateway ids to be referenced elsewhere.

## Example Usage

The following returns all NAT gateways in a specified VPC that are marked as available

```terraform
data "aws_nat_gateways" "ngws" {
vpc_id = var.vpc_id

filter {
name = "state"
values = ["available"]
}
}

data "aws_nat_gateway" "ngw" {
count = length(data.aws_nat_gateways.ngws.ids)
id = tolist(data.aws_nat_gateways.ngws.ids)[count.index]
}
```

## Argument Reference

* `filter` - (Optional) Custom filter block as described below.
* `vpc_id` - (Optional) The VPC ID that you want to filter from.
* `tags` - (Optional) A map of tags, each pair of which must exactly match
a pair on the desired NAT Gateways.

More complex filters can be expressed using one or more `filter` sub-blocks,
which take the following arguments:

* `name` - (Required) The name of the field to filter by, as defined by
[the underlying AWS API](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeNatGateways.html).
* `values` - (Required) Set of values that are accepted for the given field.
A Nat Gateway will be selected if any one of the given values matches.

## Attributes Reference

* `id` - AWS Region.
* `ids` - A list of all the NAT gateway ids found.