Skip to content

Commit

Permalink
d/aws_nat_gateway: Add 'secondary_allocation_ids', 'secondary_private…
Browse files Browse the repository at this point in the history
…_ip_addresses' and 'secondary_private_ip_address_count' attributes.

Acceptance test output:

% make testacc TESTARGS='-run=TestAccVPCNATGatewayDataSource_' PKG=ec2 ACCTEST_PARALLELISM=2
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./internal/service/ec2/... -v -count 1 -parallel 2  -run=TestAccVPCNATGatewayDataSource_ -timeout 180m
=== RUN   TestAccVPCNATGatewayDataSource_basic
=== PAUSE TestAccVPCNATGatewayDataSource_basic
=== CONT  TestAccVPCNATGatewayDataSource_basic
--- PASS: TestAccVPCNATGatewayDataSource_basic (213.95s)
PASS
ok  	github.com/hashicorp/terraform-provider-aws/internal/service/ec2	218.773s
  • Loading branch information
ewbankkit committed Jul 24, 2023
1 parent a8820ab commit 6bfcb5c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 82 deletions.
4 changes: 4 additions & 0 deletions .changelog/31778.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ resource/aws_nat_gateway: Add `secondary_allocation_ids`, `secondary_private_ip_

```release-note:enhancement
resource/aws_nat_gateway: Add configurable timeouts
```

```release-note:enhancement
data-source/aws_nat_gateway: Add `secondary_allocation_ids`, `secondary_private_ip_addresses` and `secondary_private_ip_address_count` attributes
```
30 changes: 28 additions & 2 deletions internal/service/ec2/vpc_nat_gateway_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ func DataSourceNATGateway() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"secondary_allocation_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"secondary_private_ip_address_count": {
Type: schema.TypeInt,
Computed: true,
},
"secondary_private_ip_addresses": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"state": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -120,18 +134,30 @@ func dataSourceNATGatewayRead(ctx context.Context, d *schema.ResourceData, meta
d.Set("subnet_id", ngw.SubnetId)
d.Set("vpc_id", ngw.VpcId)

var secondaryAllocationIDs, secondaryPrivateIPAddresses []string

for _, address := range ngw.NatGatewayAddresses {
// Length check guarantees the attributes are always set (#30865).
if len(ngw.NatGatewayAddresses) == 1 || aws.BoolValue(address.IsPrimary) {
if isPrimary := aws.BoolValue(address.IsPrimary); isPrimary || len(ngw.NatGatewayAddresses) == 1 {
d.Set("allocation_id", address.AllocationId)
d.Set("association_id", address.AssociationId)
d.Set("network_interface_id", address.NetworkInterfaceId)
d.Set("private_ip", address.PrivateIp)
d.Set("public_ip", address.PublicIp)
break
} else if !isPrimary {
if allocationID := aws.StringValue(address.AllocationId); allocationID != "" {
secondaryAllocationIDs = append(secondaryAllocationIDs, allocationID)
}
if privateIP := aws.StringValue(address.PrivateIp); privateIP != "" {
secondaryPrivateIPAddresses = append(secondaryPrivateIPAddresses, privateIP)
}
}
}

d.Set("secondary_allocation_ids", secondaryAllocationIDs)
d.Set("secondary_private_ip_address_count", len(secondaryPrivateIPAddresses))
d.Set("secondary_private_ip_addresses", secondaryPrivateIPAddresses)

if err := d.Set("tags", KeyValueTags(ctx, ngw.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return diag.Errorf("setting tags: %s", err)
}
Expand Down
91 changes: 38 additions & 53 deletions internal/service/ec2/vpc_nat_gateway_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
func TestAccVPCNATGatewayDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceNameById := "data.aws_nat_gateway.test_by_id"
dataSourceNameBySubnetId := "data.aws_nat_gateway.test_by_subnet_id"
dataSourceNameByID := "data.aws_nat_gateway.test_by_id"
dataSourceNameBySubnetID := "data.aws_nat_gateway.test_by_subnet_id"
dataSourceNameByTags := "data.aws_nat_gateway.test_by_tags"
resourceName := "aws_nat_gateway.test"

Expand All @@ -28,68 +28,53 @@ func TestAccVPCNATGatewayDataSource_basic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccVPCNATGatewayDataSourceConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceNameById, "connectivity_type", resourceName, "connectivity_type"),
resource.TestCheckResourceAttrPair(dataSourceNameById, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceNameBySubnetId, "subnet_id", resourceName, "subnet_id"),
resource.TestCheckResourceAttrPair(dataSourceNameByTags, "tags.Name", resourceName, "tags.Name"),
resource.TestCheckResourceAttrSet(dataSourceNameById, "state"),
resource.TestCheckResourceAttrSet(dataSourceNameById, "allocation_id"),
resource.TestCheckResourceAttrSet(dataSourceNameById, "network_interface_id"),
resource.TestCheckResourceAttrSet(dataSourceNameById, "public_ip"),
resource.TestCheckResourceAttrSet(dataSourceNameById, "private_ip"),
resource.TestCheckNoResourceAttr(dataSourceNameById, "attached_vpc_id"),
resource.TestCheckResourceAttrSet(dataSourceNameById, "tags.OtherTag"),
resource.TestCheckResourceAttrPair(dataSourceNameById, "association_id", resourceName, "association_id"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceNameByID, "allocation_id", resourceName, "allocation_id"),
resource.TestCheckResourceAttrPair(dataSourceNameByID, "association_id", resourceName, "association_id"),
resource.TestCheckResourceAttrPair(dataSourceNameByID, "connectivity_type", resourceName, "connectivity_type"),
resource.TestCheckResourceAttrPair(dataSourceNameByID, "network_interface_id", resourceName, "network_interface_id"),
resource.TestCheckResourceAttrPair(dataSourceNameByID, "private_ip", resourceName, "private_ip"),
resource.TestCheckResourceAttrPair(dataSourceNameByID, "public_ip", resourceName, "public_ip"),
resource.TestCheckResourceAttrPair(dataSourceNameByID, "secondary_allocation_ids.#", resourceName, "secondary_allocation_ids.#"),
resource.TestCheckResourceAttrPair(dataSourceNameByID, "secondary_private_ip_address_count", resourceName, "secondary_private_ip_address_count"),
resource.TestCheckResourceAttrPair(dataSourceNameByID, "secondary_private_ip_addresses.#", resourceName, "secondary_private_ip_addresses.#"),
resource.TestCheckResourceAttrPair(dataSourceNameByID, "tags.#", resourceName, "tags.#"),

resource.TestCheckResourceAttrPair(dataSourceNameBySubnetID, "allocation_id", resourceName, "allocation_id"),
resource.TestCheckResourceAttrPair(dataSourceNameBySubnetID, "association_id", resourceName, "association_id"),
resource.TestCheckResourceAttrPair(dataSourceNameBySubnetID, "connectivity_type", resourceName, "connectivity_type"),
resource.TestCheckResourceAttrPair(dataSourceNameBySubnetID, "network_interface_id", resourceName, "network_interface_id"),
resource.TestCheckResourceAttrPair(dataSourceNameBySubnetID, "private_ip", resourceName, "private_ip"),
resource.TestCheckResourceAttrPair(dataSourceNameBySubnetID, "public_ip", resourceName, "public_ip"),
resource.TestCheckResourceAttrPair(dataSourceNameBySubnetID, "secondary_allocation_ids.#", resourceName, "secondary_allocation_ids.#"),
resource.TestCheckResourceAttrPair(dataSourceNameBySubnetID, "secondary_private_ip_address_count", resourceName, "secondary_private_ip_address_count"),
resource.TestCheckResourceAttrPair(dataSourceNameBySubnetID, "secondary_private_ip_addresses.#", resourceName, "secondary_private_ip_addresses.#"),
resource.TestCheckResourceAttrPair(dataSourceNameBySubnetID, "tags.#", resourceName, "tags.#"),

resource.TestCheckResourceAttrPair(dataSourceNameByTags, "allocation_id", resourceName, "allocation_id"),
resource.TestCheckResourceAttrPair(dataSourceNameByTags, "association_id", resourceName, "association_id"),
resource.TestCheckResourceAttrPair(dataSourceNameByTags, "connectivity_type", resourceName, "connectivity_type"),
resource.TestCheckResourceAttrPair(dataSourceNameByTags, "network_interface_id", resourceName, "network_interface_id"),
resource.TestCheckResourceAttrPair(dataSourceNameByTags, "private_ip", resourceName, "private_ip"),
resource.TestCheckResourceAttrPair(dataSourceNameByTags, "public_ip", resourceName, "public_ip"),
resource.TestCheckResourceAttrPair(dataSourceNameByTags, "secondary_allocation_ids.#", resourceName, "secondary_allocation_ids.#"),
resource.TestCheckResourceAttrPair(dataSourceNameByTags, "secondary_private_ip_address_count", resourceName, "secondary_private_ip_address_count"),
resource.TestCheckResourceAttrPair(dataSourceNameByTags, "secondary_private_ip_addresses.#", resourceName, "secondary_private_ip_addresses.#"),
resource.TestCheckResourceAttrPair(dataSourceNameByTags, "tags.#", resourceName, "tags.#"),
),
},
},
})
}

func testAccVPCNATGatewayDataSourceConfig_basic(rName string) string {
return acctest.ConfigCompose(acctest.ConfigAvailableAZsNoOptIn(), fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "172.5.0.0/16"
tags = {
Name = %[1]q
}
}
resource "aws_subnet" "test" {
vpc_id = aws_vpc.test.id
cidr_block = "172.5.123.0/24"
availability_zone = data.aws_availability_zones.available.names[0]
tags = {
Name = %[1]q
}
}
resource "aws_eip" "test" {
domain = "vpc"
tags = {
Name = %[1]q
}
}
resource "aws_internet_gateway" "test" {
vpc_id = aws_vpc.test.id
tags = {
Name = %[1]q
}
}
return acctest.ConfigCompose(testAccNATGatewayConfig_base(rName), fmt.Sprintf(`
resource "aws_nat_gateway" "test" {
subnet_id = aws_subnet.test.id
subnet_id = aws_subnet.public.id
allocation_id = aws_eip.test.id
tags = {
Name = %[1]q
OtherTag = "some-value"
Name = %[1]q
}
depends_on = [aws_internet_gateway.test]
Expand Down
36 changes: 18 additions & 18 deletions website/docs/d/nat_gateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ subcategory: "VPC (Virtual Private Cloud)"
layout: "aws"
page_title: "AWS: aws_nat_gateway"
description: |-
Provides details about a specific Nat Gateway
Provides details about a specific VPC NAT Gateway.
---

# Data Source: aws_nat_gateway

Provides details about a specific Nat Gateway.
Provides details about a specific VPC NAT Gateway.

## Example Usage

Expand All @@ -18,7 +18,7 @@ data "aws_nat_gateway" "default" {
}
```

Usage with tags:
### With tags

```terraform
data "aws_nat_gateway" "default" {
Expand All @@ -33,15 +33,15 @@ data "aws_nat_gateway" "default" {
## Argument Reference

The arguments of this data source act as filters for querying the available
Nat Gateways in the current region. The given filters must match exactly one
Nat Gateway whose data will be exported as attributes.
NAT Gateways in the current Region. The given filters must match exactly one
NAT Gateway whose data will be exported as attributes.

* `id` - (Optional) ID of the specific Nat Gateway to retrieve.
* `subnet_id` - (Optional) ID of subnet that the Nat Gateway resides in.
* `vpc_id` - (Optional) ID of the VPC that the Nat Gateway resides in.
* `state` - (Optional) State of the NAT gateway (pending | failed | available | deleting | deleted ).
* `id` - (Optional) ID of the specific NAT Gateway to retrieve.
* `subnet_id` - (Optional) ID of subnet that the NAT Gateway resides in.
* `vpc_id` - (Optional) ID of the VPC that the NAT Gateway resides in.
* `state` - (Optional) State of the NAT Gateway (pending | failed | available | deleting | deleted ).
* `tags` - (Optional) Map of tags, each pair of which must exactly match
a pair on the desired Nat Gateway.
a pair on the desired NAT Gateway.
* `filter` - (Optional) Custom filter block as described below.

More complex filters can be expressed using one or more `filter` sub-blocks,
Expand All @@ -59,15 +59,15 @@ result attributes. This data source will complete the data by populating
any fields that are not included in the configuration with the data for
the selected Nat Gateway.

`addresses` are also exported with the following attributes, when they are relevant:
Each attachment supports the following:

* `allocation_id` - ID of the EIP allocated to the selected Nat Gateway.
* `association_id` - The association ID of the Elastic IP address that's associated with the NAT gateway. Only available when `connectivity_type` is `public`.
* `allocation_id` - ID of the EIP allocated to the selected NAT Gateway.
* `association_id` - The association ID of the Elastic IP address that's associated with the NAT Gateway. Only available when `connectivity_type` is `public`.
* `connectivity_type` - Connectivity type of the NAT Gateway.
* `network_interface_id` - The ID of the ENI allocated to the selected Nat Gateway.
* `private_ip` - Private Ip address of the selected Nat Gateway.
* `public_ip` - Public Ip (EIP) address of the selected Nat Gateway.
* `network_interface_id` - The ID of the ENI allocated to the selected NAT Gateway.
* `private_ip` - Private IP address of the selected NAT Gateway.
* `public_ip` - Public IP (EIP) address of the selected NAT Gateway.
* `secondary_allocation_ids` - Secondary allocation EIP IDs for the selected NAT Gateway.
* `secondary_private_ip_address_count` - The number of secondary private IPv4 addresses assigned to the selected NAT Gateway.
* `secondary_private_ip_addresses` - Secondary private IPv4 addresses assigned to the selected NAT Gateway.

## Timeouts

Expand Down
18 changes: 9 additions & 9 deletions website/docs/r/nat_gateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,23 @@ resource "aws_nat_gateway" "example" {

This resource supports the following arguments:

* `allocation_id` - (Optional) The Allocation ID of the Elastic IP address for the gateway. Required for `connectivity_type` of `public`.
* `connectivity_type` - (Optional) Connectivity type for the gateway. Valid values are `private` and `public`. Defaults to `public`.
* `private_ip` - (Optional) The private IPv4 address to assign to the NAT gateway. If you don't provide an address, a private IPv4 address will be automatically assigned.
* `subnet_id` - (Required) The Subnet ID of the subnet in which to place the gateway.
* `secondary_allocation_ids` - (Optional) A list of secondary allocation EIP IDs for this NAT gateway.
* `secondary_private_ip_address_count` - (Optional) [Private NAT gateway only] The number of secondary private IPv4 addresses you want to assign to the NAT gateway.
* `allocation_id` - (Optional) The Allocation ID of the Elastic IP address for the NAT Gateway. Required for `connectivity_type` of `public`.
* `connectivity_type` - (Optional) Connectivity type for the NAT Gateway. Valid values are `private` and `public`. Defaults to `public`.
* `private_ip` - (Optional) The private IPv4 address to assign to the NAT Gateway. If you don't provide an address, a private IPv4 address will be automatically assigned.
* `subnet_id` - (Required) The Subnet ID of the subnet in which to place the NAT Gateway.
* `secondary_allocation_ids` - (Optional) A list of secondary allocation EIP IDs for this NAT Gateway.
* `secondary_private_ip_address_count` - (Optional) [Private NAT Gateway only] The number of secondary private IPv4 addresses you want to assign to the NAT Gateway.
* `secondary_private_ip_addresses` - (Optional) A list of secondary private IPv4 addresses to assign to the NAT Gateway.
* `tags` - (Optional) A map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

## Attribute Reference

This resource exports the following attributes in addition to the arguments above:

* `association_id` - The association ID of the Elastic IP address that's associated with the NAT gateway. Only available when `connectivity_type` is `public`.
* `association_id` - The association ID of the Elastic IP address that's associated with the NAT Gateway. Only available when `connectivity_type` is `public`.
* `id` - The ID of the NAT Gateway.
* `network_interface_id` - The ID of the network interface associated with the NAT gateway.
* `public_ip` - The Elastic IP address associated with the NAT gateway.
* `network_interface_id` - The ID of the network interface associated with the NAT Gateway.
* `public_ip` - The Elastic IP address associated with the NAT Gateway.
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block).

## Timeouts
Expand Down

0 comments on commit 6bfcb5c

Please sign in to comment.