-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
be7aea2
copied vpn_gateway to nat_gateway
shadycuz 457eaaf
replaced VpnGateway with NatGateway
shadycuz c239530
replaced avalability_zone with subnet_id
shadycuz 28cd8e3
replaced attached_vpc_id with vpc_id
shadycuz a9b8ced
removed attachment.state and attachment.vpc references
shadycuz 704cfcf
replaced s/VPN/NAT in logs/debugs/prints and s/vgw/ngw in var names
shadycuz d9d565d
removed attachment loop
shadycuz 6026f3e
replaced s/VpnGateway/NatGateway
shadycuz 3740a02
replaced s/vpn/nat
shadycuz a5f9b28
removed references to attached/unattached
shadycuz 5a0f332
ran go fmt
shadycuz 099fcd1
changed s/Filters/Filter
shadycuz 57ad64d
fixed returns, removed tags reference
shadycuz 37c2bd3
fixed var names to be capital
shadycuz 07944dd
removed unused import
shadycuz 9bba67a
added loop to set address variables
shadycuz e58d0f0
Made NatGatewayAddresses plural
shadycuz b6c7edb
changed empty string check
shadycuz 64267b0
Correct resource references in acctest config
clintoncwolfe c168d46
Add NGW to aws datasource list
clintoncwolfe c644192
Add EIP, VPC, and Subnet to test fixture
clintoncwolfe 15f01f9
Working test fixture config - added IGW, removed tagging on NGW
clintoncwolfe 1ee6a3f
s/VPN/NAT/
clintoncwolfe 622c2be
Working test fixture by clintoncwolfe
shadycuz c91e709
added missing rInt arg
shadycuz 82566ae
added aws_nat_gateway to sidebar
shadycuz 0d3e9ff
created documentation page for nat gateway
shadycuz 22d1df8
changed logic for building req
shadycuz bebcbeb
removed comment about EIP search
shadycuz a41d94c
reran make fmt
shadycuz 9e60200
fixed copy paste typos
shadycuz 8b5b0aa
added tags to schema and moved log statement
shadycuz 88de827
removed plus signs
shadycuz baf3749
removed escape characters in title
shadycuz 7da3f4c
renamed id and removed extra spaces
shadycuz e8e5fef
Merge branch 'master' into feat/natgatewayDatasource
shadycuz eedf611
ran make fmt
shadycuz fcbd7d9
Merge branch 'feat/natgatewayDatasource' of github.com:shadycuz/terra…
shadycuz 230e2b6
made attributes and documentation similar to nat gateway resource
shadycuz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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, | ||
}, | ||
"tags": tagsSchemaComputed(), | ||
|
||
"filter": ec2CustomFiltersSchema(), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
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 | ||
} | ||
log.Printf("[DEBUG] Reading NAT Gateway: %s", req) | ||
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("allocation_id", address.AllocationId) | ||
d.Set("network_interface_id", address.NetworkInterfaceId) | ||
d.Set("private_ip", address.PrivateIp) | ||
d.Set("public_ip", address.PublicIp) | ||
break | ||
} | ||
} | ||
|
||
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,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) | ||
} |
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 |
---|---|---|
|
@@ -143,6 +143,8 @@ | |
<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<%= sidebar_current("docs-aws-datasource-lb-x") %>> | ||
<a href="/docs/providers/aws/d/lb.html">aws_lb</a> | ||
</li> | ||
|
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,66 @@ | ||
--- | ||
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 | ||
|
||
Provides details about a specific Nat Gateway. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "subnet_id" {} | ||
|
||
data "aws_nat_gateway" "default" { | ||
subnet_id = "${aws_subnet.public.id}" | ||
} | ||
``` | ||
|
||
Usage with tags: | ||
|
||
```hcl | ||
data "aws_nat_gateway" "default" { | ||
subnet_id = "${aws_subnet.public.id}" | ||
|
||
tags { | ||
Name = "gw NAT" | ||
} | ||
} | ||
``` | ||
|
||
## 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. | ||
|
||
* `id` - (Optional) The id of the specific Nat Gateway to retrieve. | ||
* `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. | ||
* `state` - (Optional) The state of the NAT gateway (pending | failed | available | deleting | deleted ). | ||
* `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: | ||
|
||
* `allocation_id` - The Id of the EIP allocated to the selected Nat Gateway. | ||
* `network_interface_id` - The Id of the ENI allocated to the selected Nat Gateway. | ||
* `private_ip` - The private Ip address of the selected Nat Gateway. | ||
* `public_ip` - The public Ip (EIP) address of the selected Nat Gateway. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@Ninir I tried using this data source and found these attributes are not being included in the output. Running terraform with DEBUG logging I see this warning:
I do see the values in the AWS response logged by Terraform:
Does the list of output-able values have to be included in the Schema definition? https://github.com/terraform-providers/terraform-provider-aws/pull/1294/files#diff-5264f6e572425a4527111c5172455cf6R16
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.
Opened #2209