-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24190 from iandrewt/aws_nat_gateways
Add aws_nat_gateways data source
- Loading branch information
Showing
5 changed files
with
327 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-data-source | ||
aws_nat_gateways | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |