-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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 NatGateway data source #1294
Changes from 31 commits
be7aea2
457eaaf
c239530
28cd8e3
a9b8ced
704cfcf
d9d565d
6026f3e
3740a02
a5f9b28
5a0f332
099fcd1
57ad64d
37c2bd3
07944dd
9bba67a
e58d0f0
b6c7edb
64267b0
c168d46
c644192
15f01f9
1ee6a3f
622c2be
c91e709
82566ae
0d3e9ff
22d1df8
bebcbeb
a41d94c
9e60200
8b5b0aa
88de827
baf3749
7da3f4c
e8e5fef
eedf611
fcbd7d9
230e2b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceAwsNatGateway() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsNatGatewayRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"state": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"vpc_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"subnet_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"filter": ec2CustomFiltersSchema(), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
log.Printf("[DEBUG] Reading NAT Gateways.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move this above line 85 and update it using something like:
This will print every parameters, for a better debugging :) |
||
|
||
req := &ec2.DescribeNatGatewaysInput{} | ||
|
||
if id, ok := d.GetOk("id"); ok { | ||
req.NatGatewayIds = aws.StringSlice([]string{id.(string)}) | ||
} | ||
|
||
if vpc_id, ok := d.GetOk("vpc_id"); ok { | ||
req.Filter = append(req.Filter, buildEC2AttributeFilterList( | ||
map[string]string{ | ||
"vpc-id": vpc_id.(string), | ||
}, | ||
)...) | ||
} | ||
|
||
if state, ok := d.GetOk("state"); ok { | ||
req.Filter = append(req.Filter, buildEC2AttributeFilterList( | ||
map[string]string{ | ||
"state": state.(string), | ||
}, | ||
)...) | ||
} | ||
|
||
if subnet_id, ok := d.GetOk("subnet_id"); ok { | ||
req.Filter = append(req.Filter, buildEC2AttributeFilterList( | ||
map[string]string{ | ||
"subnet-id": subnet_id.(string), | ||
}, | ||
)...) | ||
} | ||
|
||
req.Filter = append(req.Filter, buildEC2CustomFilterList( | ||
d.Get("filter").(*schema.Set), | ||
)...) | ||
if len(req.Filter) == 0 { | ||
// Don't send an empty filters list; the EC2 API won't accept it. | ||
req.Filter = nil | ||
} | ||
|
||
resp, err := conn.DescribeNatGateways(req) | ||
if err != nil { | ||
return err | ||
} | ||
if resp == nil || len(resp.NatGateways) == 0 { | ||
return fmt.Errorf("no matching NAT gateway found: %#v", req) | ||
} | ||
if len(resp.NatGateways) > 1 { | ||
return fmt.Errorf("multiple NAT gateways matched; use additional constraints to reduce matches to a single NAT gateway") | ||
} | ||
|
||
ngw := resp.NatGateways[0] | ||
|
||
d.SetId(aws.StringValue(ngw.NatGatewayId)) | ||
d.Set("state", ngw.State) | ||
d.Set("subnet_id", ngw.SubnetId) | ||
d.Set("vpc_id", ngw.VpcId) | ||
|
||
for _, address := range ngw.NatGatewayAddresses { | ||
if *address.AllocationId != "" { | ||
d.Set("allocated_eip_id", address.AllocationId) | ||
d.Set("allocated_eni_id", address.NetworkInterfaceId) | ||
d.Set("allocated_private_ip", address.PrivateIp) | ||
d.Set("allocated_public_ip", address.PublicIp) | ||
break | ||
} | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceAwsNatGateway(t *testing.T) { | ||
// This is used as a portion of CIDR network addresses. | ||
rInt := acctest.RandIntRange(4, 254) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccDataSourceAwsNatGatewayConfig(rInt), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrPair( | ||
"data.aws_nat_gateway.test_by_id", "id", | ||
"aws_nat_gateway.test", "id"), | ||
resource.TestCheckResourceAttrPair( | ||
"data.aws_nat_gateway.test_by_subnet_id", "subnet_id", | ||
"aws_nat_gateway.test", "subnet_id"), | ||
resource.TestCheckResourceAttrSet("data.aws_nat_gateway.test_by_id", "state"), | ||
resource.TestCheckNoResourceAttr("data.aws_nat_gateway.test_by_id", "attached_vpc_id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAwsNatGatewayConfig(rInt int) string { | ||
return fmt.Sprintf(` | ||
provider "aws" { | ||
region = "us-west-2" | ||
} | ||
|
||
resource "aws_vpc" "test" { | ||
cidr_block = "172.%d.0.0/16" | ||
tags { | ||
Name = "terraform-testacc-nat-gateway-data-source-%d" | ||
} | ||
} | ||
|
||
resource "aws_subnet" "test" { | ||
vpc_id = "${aws_vpc.test.id}" | ||
cidr_block = "172.%d.123.0/24" | ||
availability_zone = "us-west-2a" | ||
|
||
tags { | ||
Name = "terraform-testacc-nat-gateway-data-source-%d" | ||
} | ||
} | ||
|
||
# EIPs are not taggable | ||
resource "aws_eip" "test" { | ||
vpc = true | ||
} | ||
|
||
# IGWs are required for an NGW to spin up; manual dependency | ||
resource "aws_internet_gateway" "test" { | ||
vpc_id = "${aws_vpc.test.id}" | ||
tags { | ||
Name = "terraform-testacc-nat-gateway-data-source-%d" | ||
} | ||
} | ||
|
||
# NGWs are not taggable, either | ||
resource "aws_nat_gateway" "test" { | ||
subnet_id = "${aws_subnet.test.id}" | ||
allocation_id = "${aws_eip.test.id}" | ||
|
||
depends_on = ["aws_internet_gateway.test"] | ||
} | ||
|
||
data "aws_nat_gateway" "test_by_id" { | ||
id = "${aws_nat_gateway.test.id}" | ||
} | ||
|
||
data "aws_nat_gateway" "test_by_subnet_id" { | ||
subnet_id = "${aws_nat_gateway.test.subnet_id}" | ||
} | ||
|
||
`, rInt, rInt, rInt, rInt, rInt) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,6 +140,9 @@ | |
<li<%= sidebar_current("docs-aws-datasource-kms-secret") %>> | ||
<a href="/docs/providers/aws/d/kms_secret.html">aws_kms_secret</a> | ||
</li> | ||
<li<%= sidebar_current("docs-aws-datasource-nat-gateway") %>> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove the extra plus signs on the lines below? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They have been removed |
||
+ <a href="/docs/providers/aws/d/nat_gateway.html">aws_nat_gateway</a> | ||
+ </li> | ||
<li<%= sidebar_current("docs-aws-datasource-partition") %>> | ||
<a href="/docs/providers/aws/d/partition.html">aws_partition</a> | ||
</li> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_nat_gateway" | ||
sidebar_current: "docs-aws-datasource-nat_gateway" | ||
description: |- | ||
Provides details about a specific Nat Gateway | ||
--- | ||
|
||
# aws\_nat\_gateway | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Antislashes are not needed anymore in titles, as per one of @sethvargo 's contributions :) |
||
|
||
`aws_nat_gateway` provides details about a specific Nat Gateway. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "subnet_id" {} | ||
|
||
data "aws_nat_gateway" "default" { | ||
|
||
subnet_id = "${var.subnet_id}" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove additional spaces also? will make it easier to read |
||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The arguments of this data source act as filters for querying the available | ||
Nat Gateway in the current region. The given filters must match exactly one | ||
Nat Gateway whose data will be exported as attributes. | ||
|
||
* `nat_gateway_id` - (Optional) The id of the specific Nat Gateway to retrieve. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can name it |
||
|
||
* `subnet_id` - (Optional) The id of subnet that the Nat Gateway resides in. | ||
|
||
* `vpc_id` - (Optional) The id of the VPC that the Nat Gateway resides in. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove additional spaces between list items? thanks 😄 |
||
* `filter` - (Optional) Custom filter block as described below. | ||
|
||
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. | ||
An Nat Gateway will be selected if any one of the given values matches. | ||
|
||
## Attributes Reference | ||
|
||
All of the argument attributes except `filter` block are also exported as | ||
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 attachement supports the following: | ||
|
||
* `allocated_eip_id` - The Id of the EIP allocated to the selected Nat Gateway. | ||
* `allocated_eni_id` - The Id of the ENI allocated to the selected Nat Gateway. | ||
* `allocated_private_ip` - The private Ip address of the selected Nat Gateway. | ||
* `allocated_public_ip` - The public Ip (EIP) address of the selected Nat Gateway. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add the tags part, following @ewbankkit 's comment?