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 NatGateway data source #1294

Merged
merged 39 commits into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
be7aea2
copied vpn_gateway to nat_gateway
shadycuz Jul 31, 2017
457eaaf
replaced VpnGateway with NatGateway
shadycuz Jul 31, 2017
c239530
replaced avalability_zone with subnet_id
shadycuz Jul 31, 2017
28cd8e3
replaced attached_vpc_id with vpc_id
shadycuz Jul 31, 2017
a9b8ced
removed attachment.state and attachment.vpc references
shadycuz Jul 31, 2017
704cfcf
replaced s/VPN/NAT in logs/debugs/prints and s/vgw/ngw in var names
shadycuz Jul 31, 2017
d9d565d
removed attachment loop
shadycuz Jul 31, 2017
6026f3e
replaced s/VpnGateway/NatGateway
shadycuz Jul 31, 2017
3740a02
replaced s/vpn/nat
shadycuz Jul 31, 2017
a5f9b28
removed references to attached/unattached
shadycuz Jul 31, 2017
5a0f332
ran go fmt
shadycuz Jul 31, 2017
099fcd1
changed s/Filters/Filter
shadycuz Jul 31, 2017
57ad64d
fixed returns, removed tags reference
shadycuz Jul 31, 2017
37c2bd3
fixed var names to be capital
shadycuz Jul 31, 2017
07944dd
removed unused import
shadycuz Jul 31, 2017
9bba67a
added loop to set address variables
shadycuz Jul 31, 2017
e58d0f0
Made NatGatewayAddresses plural
shadycuz Jul 31, 2017
b6c7edb
changed empty string check
shadycuz Jul 31, 2017
64267b0
Correct resource references in acctest config
clintoncwolfe Aug 1, 2017
c168d46
Add NGW to aws datasource list
clintoncwolfe Aug 1, 2017
c644192
Add EIP, VPC, and Subnet to test fixture
clintoncwolfe Aug 1, 2017
15f01f9
Working test fixture config - added IGW, removed tagging on NGW
clintoncwolfe Aug 1, 2017
1ee6a3f
s/VPN/NAT/
clintoncwolfe Aug 1, 2017
622c2be
Working test fixture by clintoncwolfe
shadycuz Aug 1, 2017
c91e709
added missing rInt arg
shadycuz Aug 1, 2017
82566ae
added aws_nat_gateway to sidebar
shadycuz Aug 1, 2017
0d3e9ff
created documentation page for nat gateway
shadycuz Aug 1, 2017
22d1df8
changed logic for building req
shadycuz Aug 1, 2017
bebcbeb
removed comment about EIP search
shadycuz Aug 1, 2017
a41d94c
reran make fmt
shadycuz Aug 1, 2017
9e60200
fixed copy paste typos
shadycuz Aug 1, 2017
8b5b0aa
added tags to schema and moved log statement
shadycuz Oct 18, 2017
88de827
removed plus signs
shadycuz Oct 18, 2017
baf3749
removed escape characters in title
shadycuz Oct 18, 2017
7da3f4c
renamed id and removed extra spaces
shadycuz Oct 18, 2017
e8e5fef
Merge branch 'master' into feat/natgatewayDatasource
shadycuz Oct 18, 2017
eedf611
ran make fmt
shadycuz Oct 18, 2017
fcbd7d9
Merge branch 'feat/natgatewayDatasource' of github.com:shadycuz/terra…
shadycuz Oct 18, 2017
230e2b6
made attributes and documentation similar to nat gateway resource
shadycuz Oct 24, 2017
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
114 changes: 114 additions & 0 deletions aws/data_source_aws_nat_gateway.go
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(),
Copy link
Contributor

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?

},
}
}

func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

log.Printf("[DEBUG] Reading NAT Gateways.")
Copy link
Contributor

Choose a reason for hiding this comment

The 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:

log.Printf("[DEBUG] Reading NAT Gateway: %s", req)

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
}
89 changes: 89 additions & 0 deletions aws/data_source_aws_nat_gateway_test.go
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)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func Provider() terraform.ResourceProvider {
"aws_kms_alias": dataSourceAwsKmsAlias(),
"aws_kms_ciphertext": dataSourceAwsKmsCiphetext(),
"aws_kms_secret": dataSourceAwsKmsSecret(),
"aws_nat_gateway": dataSourceAwsNatGateway(),
"aws_partition": dataSourceAwsPartition(),
"aws_prefix_list": dataSourceAwsPrefixList(),
"aws_redshift_service_account": dataSourceAwsRedshiftServiceAccount(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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") %>>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the extra plus signs on the lines below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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>
Expand Down
61 changes: 61 additions & 0 deletions website/docs/d/nat_gateway.html.markdown
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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}"

Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be id but I'm waiting to see if any other bugs exist first.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can name it id indeed


* `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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.