From be7aea2a14da279402b0485e375e6585c36621d7 Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 15:53:22 -0400 Subject: [PATCH 01/36] copied vpn_gateway to nat_gateway --- aws/data_source_aws_nat_gateway.go | 105 ++++++++++++++++++++++ aws/data_source_aws_nat_gateway_test.go | 113 ++++++++++++++++++++++++ 2 files changed, 218 insertions(+) create mode 100644 aws/data_source_aws_nat_gateway.go create mode 100644 aws/data_source_aws_nat_gateway_test.go diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go new file mode 100644 index 00000000000..5d088e54845 --- /dev/null +++ b/aws/data_source_aws_nat_gateway.go @@ -0,0 +1,105 @@ +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 dataSourceAwsVpnGateway() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsVpnGatewayRead, + + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "state": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "attached_vpc_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "availability_zone": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "filter": ec2CustomFiltersSchema(), + "tags": tagsSchemaComputed(), + }, + } +} + +func dataSourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + + log.Printf("[DEBUG] Reading VPN Gateways.") + + req := &ec2.DescribeVpnGatewaysInput{} + + if id, ok := d.GetOk("id"); ok { + req.VpnGatewayIds = aws.StringSlice([]string{id.(string)}) + } + + req.Filters = buildEC2AttributeFilterList( + map[string]string{ + "state": d.Get("state").(string), + "availability-zone": d.Get("availability_zone").(string), + }, + ) + if id, ok := d.GetOk("attached_vpc_id"); ok { + req.Filters = append(req.Filters, buildEC2AttributeFilterList( + map[string]string{ + "attachment.state": "attached", + "attachment.vpc-id": id.(string), + }, + )...) + } + req.Filters = append(req.Filters, buildEC2TagFilterList( + tagsFromMap(d.Get("tags").(map[string]interface{})), + )...) + req.Filters = append(req.Filters, buildEC2CustomFilterList( + d.Get("filter").(*schema.Set), + )...) + if len(req.Filters) == 0 { + // Don't send an empty filters list; the EC2 API won't accept it. + req.Filters = nil + } + + resp, err := conn.DescribeVpnGateways(req) + if err != nil { + return err + } + if resp == nil || len(resp.VpnGateways) == 0 { + return fmt.Errorf("no matching VPN gateway found: %#v", req) + } + if len(resp.VpnGateways) > 1 { + return fmt.Errorf("multiple VPN gateways matched; use additional constraints to reduce matches to a single VPN gateway") + } + + vgw := resp.VpnGateways[0] + + d.SetId(aws.StringValue(vgw.VpnGatewayId)) + d.Set("state", vgw.State) + d.Set("availability_zone", vgw.AvailabilityZone) + d.Set("tags", tagsToMap(vgw.Tags)) + + for _, attachment := range vgw.VpcAttachments { + if *attachment.State == "attached" { + d.Set("attached_vpc_id", attachment.VpcId) + break + } + } + + return nil +} diff --git a/aws/data_source_aws_nat_gateway_test.go b/aws/data_source_aws_nat_gateway_test.go new file mode 100644 index 00000000000..29bfb516fc8 --- /dev/null +++ b/aws/data_source_aws_nat_gateway_test.go @@ -0,0 +1,113 @@ +package aws + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAwsVpnGateway_unattached(t *testing.T) { + rInt := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccDataSourceAwsVpnGatewayUnattachedConfig(rInt), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair( + "data.aws_vpn_gateway.test_by_id", "id", + "aws_vpn_gateway.unattached", "id"), + resource.TestCheckResourceAttrPair( + "data.aws_vpn_gateway.test_by_tags", "id", + "aws_vpn_gateway.unattached", "id"), + resource.TestCheckResourceAttrSet("data.aws_vpn_gateway.test_by_id", "state"), + resource.TestCheckResourceAttr("data.aws_vpn_gateway.test_by_tags", "tags.%", "3"), + resource.TestCheckNoResourceAttr("data.aws_vpn_gateway.test_by_id", "attached_vpc_id"), + ), + }, + }, + }) +} + +func TestAccDataSourceAwsVpnGateway_attached(t *testing.T) { + rInt := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccDataSourceAwsVpnGatewayAttachedConfig(rInt), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair( + "data.aws_vpn_gateway.test_by_attached_vpc_id", "id", + "aws_vpn_gateway.attached", "id"), + resource.TestCheckResourceAttrPair( + "data.aws_vpn_gateway.test_by_attached_vpc_id", "attached_vpc_id", + "aws_vpc.foo", "id"), + resource.TestMatchResourceAttr("data.aws_vpn_gateway.test_by_attached_vpc_id", "state", regexp.MustCompile("(?i)available")), + ), + }, + }, + }) +} + +func testAccDataSourceAwsVpnGatewayUnattachedConfig(rInt int) string { + return fmt.Sprintf(` +provider "aws" { + region = "us-west-2" +} + +resource "aws_vpn_gateway" "unattached" { + tags { + Name = "terraform-testacc-vpn-gateway-data-source-unattached-%d" + ABC = "testacc-%d" + XYZ = "testacc-%d" + } +} + +data "aws_vpn_gateway" "test_by_id" { + id = "${aws_vpn_gateway.unattached.id}" +} + +data "aws_vpn_gateway" "test_by_tags" { + tags = "${aws_vpn_gateway.unattached.tags}" +} +`, rInt, rInt+1, rInt-1) +} + +func testAccDataSourceAwsVpnGatewayAttachedConfig(rInt int) string { + return fmt.Sprintf(` +provider "aws" { + region = "us-west-2" +} + +resource "aws_vpc" "foo" { + cidr_block = "10.1.0.0/16" + + tags { + Name = "terraform-testacc-vpn-gateway-data-source-foo-%d" + } +} + +resource "aws_vpn_gateway" "attached" { + tags { + Name = "terraform-testacc-vpn-gateway-data-source-attached-%d" + } +} + +resource "aws_vpn_gateway_attachment" "vpn_attachment" { + vpc_id = "${aws_vpc.foo.id}" + vpn_gateway_id = "${aws_vpn_gateway.attached.id}" +} + +data "aws_vpn_gateway" "test_by_attached_vpc_id" { + attached_vpc_id = "${aws_vpn_gateway_attachment.vpn_attachment.vpc_id}" +} +`, rInt, rInt) +} From 457eaaf781979d180d7bd112b175afb1d01a1744 Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 15:56:48 -0400 Subject: [PATCH 02/36] replaced VpnGateway with NatGateway --- aws/data_source_aws_nat_gateway.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index 5d088e54845..6afbd2378e9 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -9,9 +9,9 @@ import ( "github.com/hashicorp/terraform/helper/schema" ) -func dataSourceAwsVpnGateway() *schema.Resource { +func dataSourceAwsNatGateway() *schema.Resource { return &schema.Resource{ - Read: dataSourceAwsVpnGatewayRead, + Read: dataSourceAwsNatGatewayRead, Schema: map[string]*schema.Schema{ "id": { @@ -40,15 +40,15 @@ func dataSourceAwsVpnGateway() *schema.Resource { } } -func dataSourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error { +func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn log.Printf("[DEBUG] Reading VPN Gateways.") - req := &ec2.DescribeVpnGatewaysInput{} + req := &ec2.DescribeNatGatewaysInput{} if id, ok := d.GetOk("id"); ok { - req.VpnGatewayIds = aws.StringSlice([]string{id.(string)}) + req.NatGatewayIds = aws.StringSlice([]string{id.(string)}) } req.Filters = buildEC2AttributeFilterList( @@ -76,20 +76,20 @@ func dataSourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error req.Filters = nil } - resp, err := conn.DescribeVpnGateways(req) + resp, err := conn.DescribeNatGateways(req) if err != nil { return err } - if resp == nil || len(resp.VpnGateways) == 0 { + if resp == nil || len(resp.NatGateways) == 0 { return fmt.Errorf("no matching VPN gateway found: %#v", req) } - if len(resp.VpnGateways) > 1 { + if len(resp.NatGateways) > 1 { return fmt.Errorf("multiple VPN gateways matched; use additional constraints to reduce matches to a single VPN gateway") } - vgw := resp.VpnGateways[0] + vgw := resp.NatGateways[0] - d.SetId(aws.StringValue(vgw.VpnGatewayId)) + d.SetId(aws.StringValue(vgw.NatGatewayId)) d.Set("state", vgw.State) d.Set("availability_zone", vgw.AvailabilityZone) d.Set("tags", tagsToMap(vgw.Tags)) From c239530cc0ba98e165d698e46819efb30ab72ce0 Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 16:00:23 -0400 Subject: [PATCH 03/36] replaced avalability_zone with subnet_id --- aws/data_source_aws_nat_gateway.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index 6afbd2378e9..0a97b0a0333 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -29,7 +29,7 @@ func dataSourceAwsNatGateway() *schema.Resource { Optional: true, Computed: true, }, - "availability_zone": { + "subnet_id": { Type: schema.TypeString, Optional: true, Computed: true, @@ -54,7 +54,7 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error req.Filters = buildEC2AttributeFilterList( map[string]string{ "state": d.Get("state").(string), - "availability-zone": d.Get("availability_zone").(string), + "availability-zone": d.Get("subnet_id").(string), }, ) if id, ok := d.GetOk("attached_vpc_id"); ok { @@ -91,7 +91,7 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error d.SetId(aws.StringValue(vgw.NatGatewayId)) d.Set("state", vgw.State) - d.Set("availability_zone", vgw.AvailabilityZone) + d.Set("subnet_id", vgw.AvailabilityZone) d.Set("tags", tagsToMap(vgw.Tags)) for _, attachment := range vgw.VpcAttachments { From 28cd8e3dc2df00fa7771e177b39d192bbe6a8a62 Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 16:07:30 -0400 Subject: [PATCH 04/36] replaced attached_vpc_id with vpc_id --- aws/data_source_aws_nat_gateway.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index 0a97b0a0333..2926cd7c605 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -24,7 +24,7 @@ func dataSourceAwsNatGateway() *schema.Resource { Optional: true, Computed: true, }, - "attached_vpc_id": { + "vpc_id": { Type: schema.TypeString, Optional: true, Computed: true, @@ -54,10 +54,10 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error req.Filters = buildEC2AttributeFilterList( map[string]string{ "state": d.Get("state").(string), - "availability-zone": d.Get("subnet_id").(string), + "subnet-id": d.Get("subnet_id").(string), }, ) - if id, ok := d.GetOk("attached_vpc_id"); ok { + if id, ok := d.GetOk("vpc_id"); ok { req.Filters = append(req.Filters, buildEC2AttributeFilterList( map[string]string{ "attachment.state": "attached", @@ -96,7 +96,7 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error for _, attachment := range vgw.VpcAttachments { if *attachment.State == "attached" { - d.Set("attached_vpc_id", attachment.VpcId) + d.Set("vpc_id", attachment.VpcId) break } } From a9b8ced4cda4bc6e16b6ce7107c69a7e19567b6f Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 16:12:53 -0400 Subject: [PATCH 05/36] removed attachment.state and attachment.vpc references --- aws/data_source_aws_nat_gateway.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index 2926cd7c605..8a212e0ea49 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -60,8 +60,7 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error if id, ok := d.GetOk("vpc_id"); ok { req.Filters = append(req.Filters, buildEC2AttributeFilterList( map[string]string{ - "attachment.state": "attached", - "attachment.vpc-id": id.(string), + "vpc-id": id.(string), }, )...) } From 704cfcf9c73c1ed6319e333c31e4e3268c49bc2a Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 16:16:01 -0400 Subject: [PATCH 06/36] replaced s/VPN/NAT in logs/debugs/prints and s/vgw/ngw in var names --- aws/data_source_aws_nat_gateway.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index 8a212e0ea49..92e0ae66b18 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -80,20 +80,20 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error return err } if resp == nil || len(resp.NatGateways) == 0 { - return fmt.Errorf("no matching VPN gateway found: %#v", req) + return fmt.Errorf("no matching NAT gateway found: %#v", req) } if len(resp.NatGateways) > 1 { - return fmt.Errorf("multiple VPN gateways matched; use additional constraints to reduce matches to a single VPN gateway") + return fmt.Errorf("multiple NAT gateways matched; use additional constraints to reduce matches to a single NAT gateway") } - vgw := resp.NatGateways[0] + ngw := resp.NatGateways[0] - d.SetId(aws.StringValue(vgw.NatGatewayId)) - d.Set("state", vgw.State) - d.Set("subnet_id", vgw.AvailabilityZone) - d.Set("tags", tagsToMap(vgw.Tags)) + d.SetId(aws.StringValue(ngw.NatGatewayId)) + d.Set("state", ngw.State) + d.Set("subnet_id", ngw.AvailabilityZone) + d.Set("tags", tagsToMap(ngw.Tags)) - for _, attachment := range vgw.VpcAttachments { + for _, attachment := range ngw.VpcAttachments { if *attachment.State == "attached" { d.Set("vpc_id", attachment.VpcId) break From d9d565d6678e5e9630bf5847b35cb75fb74f4d8e Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 16:19:26 -0400 Subject: [PATCH 07/36] removed attachment loop --- aws/data_source_aws_nat_gateway.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index 92e0ae66b18..d41b8df9be6 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -93,12 +93,6 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error d.Set("subnet_id", ngw.AvailabilityZone) d.Set("tags", tagsToMap(ngw.Tags)) - for _, attachment := range ngw.VpcAttachments { - if *attachment.State == "attached" { - d.Set("vpc_id", attachment.VpcId) - break - } - } return nil } From 6026f3e022de38bc132334591a265b1d359fe59f Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 16:22:22 -0400 Subject: [PATCH 08/36] replaced s/VpnGateway/NatGateway --- aws/data_source_aws_nat_gateway_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/aws/data_source_aws_nat_gateway_test.go b/aws/data_source_aws_nat_gateway_test.go index 29bfb516fc8..bcba99c1ae7 100644 --- a/aws/data_source_aws_nat_gateway_test.go +++ b/aws/data_source_aws_nat_gateway_test.go @@ -9,7 +9,7 @@ import ( "github.com/hashicorp/terraform/helper/resource" ) -func TestAccDataSourceAwsVpnGateway_unattached(t *testing.T) { +func TestAccDataSourceAwsNatGateway_unattached(t *testing.T) { rInt := acctest.RandInt() resource.Test(t, resource.TestCase{ @@ -17,7 +17,7 @@ func TestAccDataSourceAwsVpnGateway_unattached(t *testing.T) { Providers: testAccProviders, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccDataSourceAwsVpnGatewayUnattachedConfig(rInt), + Config: testAccDataSourceAwsNatGatewayUnattachedConfig(rInt), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair( "data.aws_vpn_gateway.test_by_id", "id", @@ -34,7 +34,7 @@ func TestAccDataSourceAwsVpnGateway_unattached(t *testing.T) { }) } -func TestAccDataSourceAwsVpnGateway_attached(t *testing.T) { +func TestAccDataSourceAwsNatGateway_attached(t *testing.T) { rInt := acctest.RandInt() resource.Test(t, resource.TestCase{ @@ -42,7 +42,7 @@ func TestAccDataSourceAwsVpnGateway_attached(t *testing.T) { Providers: testAccProviders, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccDataSourceAwsVpnGatewayAttachedConfig(rInt), + Config: testAccDataSourceAwsNatGatewayAttachedConfig(rInt), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair( "data.aws_vpn_gateway.test_by_attached_vpc_id", "id", @@ -57,7 +57,7 @@ func TestAccDataSourceAwsVpnGateway_attached(t *testing.T) { }) } -func testAccDataSourceAwsVpnGatewayUnattachedConfig(rInt int) string { +func testAccDataSourceAwsNatGatewayUnattachedConfig(rInt int) string { return fmt.Sprintf(` provider "aws" { region = "us-west-2" @@ -81,7 +81,7 @@ data "aws_vpn_gateway" "test_by_tags" { `, rInt, rInt+1, rInt-1) } -func testAccDataSourceAwsVpnGatewayAttachedConfig(rInt int) string { +func testAccDataSourceAwsNatGatewayAttachedConfig(rInt int) string { return fmt.Sprintf(` provider "aws" { region = "us-west-2" From 3740a02099337a99e8dc8e47463690c7b33c1212 Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 16:24:43 -0400 Subject: [PATCH 09/36] replaced s/vpn/nat --- aws/data_source_aws_nat_gateway_test.go | 48 ++++++++++++------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/aws/data_source_aws_nat_gateway_test.go b/aws/data_source_aws_nat_gateway_test.go index bcba99c1ae7..f045878531e 100644 --- a/aws/data_source_aws_nat_gateway_test.go +++ b/aws/data_source_aws_nat_gateway_test.go @@ -20,14 +20,14 @@ func TestAccDataSourceAwsNatGateway_unattached(t *testing.T) { Config: testAccDataSourceAwsNatGatewayUnattachedConfig(rInt), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair( - "data.aws_vpn_gateway.test_by_id", "id", - "aws_vpn_gateway.unattached", "id"), + "data.aws_nat_gateway.test_by_id", "id", + "aws_nat_gateway.unattached", "id"), resource.TestCheckResourceAttrPair( - "data.aws_vpn_gateway.test_by_tags", "id", - "aws_vpn_gateway.unattached", "id"), - resource.TestCheckResourceAttrSet("data.aws_vpn_gateway.test_by_id", "state"), - resource.TestCheckResourceAttr("data.aws_vpn_gateway.test_by_tags", "tags.%", "3"), - resource.TestCheckNoResourceAttr("data.aws_vpn_gateway.test_by_id", "attached_vpc_id"), + "data.aws_nat_gateway.test_by_tags", "id", + "aws_nat_gateway.unattached", "id"), + resource.TestCheckResourceAttrSet("data.aws_nat_gateway.test_by_id", "state"), + resource.TestCheckResourceAttr("data.aws_nat_gateway.test_by_tags", "tags.%", "3"), + resource.TestCheckNoResourceAttr("data.aws_nat_gateway.test_by_id", "attached_vpc_id"), ), }, }, @@ -45,12 +45,12 @@ func TestAccDataSourceAwsNatGateway_attached(t *testing.T) { Config: testAccDataSourceAwsNatGatewayAttachedConfig(rInt), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair( - "data.aws_vpn_gateway.test_by_attached_vpc_id", "id", - "aws_vpn_gateway.attached", "id"), + "data.aws_nat_gateway.test_by_attached_vpc_id", "id", + "aws_nat_gateway.attached", "id"), resource.TestCheckResourceAttrPair( - "data.aws_vpn_gateway.test_by_attached_vpc_id", "attached_vpc_id", + "data.aws_nat_gateway.test_by_attached_vpc_id", "attached_vpc_id", "aws_vpc.foo", "id"), - resource.TestMatchResourceAttr("data.aws_vpn_gateway.test_by_attached_vpc_id", "state", regexp.MustCompile("(?i)available")), + resource.TestMatchResourceAttr("data.aws_nat_gateway.test_by_attached_vpc_id", "state", regexp.MustCompile("(?i)available")), ), }, }, @@ -63,20 +63,20 @@ provider "aws" { region = "us-west-2" } -resource "aws_vpn_gateway" "unattached" { +resource "aws_nat_gateway" "unattached" { tags { - Name = "terraform-testacc-vpn-gateway-data-source-unattached-%d" + Name = "terraform-testacc-nat-gateway-data-source-unattached-%d" ABC = "testacc-%d" XYZ = "testacc-%d" } } -data "aws_vpn_gateway" "test_by_id" { - id = "${aws_vpn_gateway.unattached.id}" +data "aws_nat_gateway" "test_by_id" { + id = "${aws_nat_gateway.unattached.id}" } -data "aws_vpn_gateway" "test_by_tags" { - tags = "${aws_vpn_gateway.unattached.tags}" +data "aws_nat_gateway" "test_by_tags" { + tags = "${aws_nat_gateway.unattached.tags}" } `, rInt, rInt+1, rInt-1) } @@ -91,23 +91,23 @@ resource "aws_vpc" "foo" { cidr_block = "10.1.0.0/16" tags { - Name = "terraform-testacc-vpn-gateway-data-source-foo-%d" + Name = "terraform-testacc-nat-gateway-data-source-foo-%d" } } -resource "aws_vpn_gateway" "attached" { +resource "aws_nat_gateway" "attached" { tags { - Name = "terraform-testacc-vpn-gateway-data-source-attached-%d" + Name = "terraform-testacc-nat-gateway-data-source-attached-%d" } } -resource "aws_vpn_gateway_attachment" "vpn_attachment" { +resource "aws_nat_gateway_attachment" "nat_attachment" { vpc_id = "${aws_vpc.foo.id}" - vpn_gateway_id = "${aws_vpn_gateway.attached.id}" + nat_gateway_id = "${aws_nat_gateway.attached.id}" } -data "aws_vpn_gateway" "test_by_attached_vpc_id" { - attached_vpc_id = "${aws_vpn_gateway_attachment.vpn_attachment.vpc_id}" +data "aws_nat_gateway" "test_by_attached_vpc_id" { + attached_vpc_id = "${aws_nat_gateway_attachment.nat_attachment.vpc_id}" } `, rInt, rInt) } From a5f9b283b97b4cdd3af616e43d0f4b7f13cf76d9 Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 16:36:04 -0400 Subject: [PATCH 10/36] removed references to attached/unattached --- aws/data_source_aws_nat_gateway_test.go | 71 ++++--------------------- 1 file changed, 9 insertions(+), 62 deletions(-) diff --git a/aws/data_source_aws_nat_gateway_test.go b/aws/data_source_aws_nat_gateway_test.go index f045878531e..16808002331 100644 --- a/aws/data_source_aws_nat_gateway_test.go +++ b/aws/data_source_aws_nat_gateway_test.go @@ -9,7 +9,7 @@ import ( "github.com/hashicorp/terraform/helper/resource" ) -func TestAccDataSourceAwsNatGateway_unattached(t *testing.T) { +func TestAccDataSourceAwsNatGateway(t *testing.T) { rInt := acctest.RandInt() resource.Test(t, resource.TestCase{ @@ -17,14 +17,14 @@ func TestAccDataSourceAwsNatGateway_unattached(t *testing.T) { Providers: testAccProviders, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccDataSourceAwsNatGatewayUnattachedConfig(rInt), + Config: testAccDataSourceAwsNatGatewayConfig(rInt), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair( "data.aws_nat_gateway.test_by_id", "id", - "aws_nat_gateway.unattached", "id"), + "aws_nat_gateway", "id"), resource.TestCheckResourceAttrPair( "data.aws_nat_gateway.test_by_tags", "id", - "aws_nat_gateway.unattached", "id"), + "aws_nat_gateway", "id"), resource.TestCheckResourceAttrSet("data.aws_nat_gateway.test_by_id", "state"), resource.TestCheckResourceAttr("data.aws_nat_gateway.test_by_tags", "tags.%", "3"), resource.TestCheckNoResourceAttr("data.aws_nat_gateway.test_by_id", "attached_vpc_id"), @@ -34,80 +34,27 @@ func TestAccDataSourceAwsNatGateway_unattached(t *testing.T) { }) } -func TestAccDataSourceAwsNatGateway_attached(t *testing.T) { - rInt := acctest.RandInt() - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - resource.TestStep{ - Config: testAccDataSourceAwsNatGatewayAttachedConfig(rInt), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrPair( - "data.aws_nat_gateway.test_by_attached_vpc_id", "id", - "aws_nat_gateway.attached", "id"), - resource.TestCheckResourceAttrPair( - "data.aws_nat_gateway.test_by_attached_vpc_id", "attached_vpc_id", - "aws_vpc.foo", "id"), - resource.TestMatchResourceAttr("data.aws_nat_gateway.test_by_attached_vpc_id", "state", regexp.MustCompile("(?i)available")), - ), - }, - }, - }) -} -func testAccDataSourceAwsNatGatewayUnattachedConfig(rInt int) string { +func testAccDataSourceAwsNatGatewayConfig(rInt int) string { return fmt.Sprintf(` provider "aws" { region = "us-west-2" } -resource "aws_nat_gateway" "unattached" { +resource "aws_nat_gateway" "test" { tags { - Name = "terraform-testacc-nat-gateway-data-source-unattached-%d" + Name = "terraform-testacc-nat-gateway-data-source-%d" ABC = "testacc-%d" XYZ = "testacc-%d" } } data "aws_nat_gateway" "test_by_id" { - id = "${aws_nat_gateway.unattached.id}" + id = "${aws_nat_gateway.id}" } data "aws_nat_gateway" "test_by_tags" { - tags = "${aws_nat_gateway.unattached.tags}" + tags = "${aws_nat_gateway.tags}" } `, rInt, rInt+1, rInt-1) } - -func testAccDataSourceAwsNatGatewayAttachedConfig(rInt int) string { - return fmt.Sprintf(` -provider "aws" { - region = "us-west-2" -} - -resource "aws_vpc" "foo" { - cidr_block = "10.1.0.0/16" - - tags { - Name = "terraform-testacc-nat-gateway-data-source-foo-%d" - } -} - -resource "aws_nat_gateway" "attached" { - tags { - Name = "terraform-testacc-nat-gateway-data-source-attached-%d" - } -} - -resource "aws_nat_gateway_attachment" "nat_attachment" { - vpc_id = "${aws_vpc.foo.id}" - nat_gateway_id = "${aws_nat_gateway.attached.id}" -} - -data "aws_nat_gateway" "test_by_attached_vpc_id" { - attached_vpc_id = "${aws_nat_gateway_attachment.nat_attachment.vpc_id}" -} -`, rInt, rInt) -} From 5a0f332182fe694168eeba1b48881553ac87c498 Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 17:02:38 -0400 Subject: [PATCH 11/36] ran go fmt --- aws/data_source_aws_nat_gateway.go | 3 +-- aws/data_source_aws_nat_gateway_test.go | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index d41b8df9be6..339bb873f95 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -53,7 +53,7 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error req.Filters = buildEC2AttributeFilterList( map[string]string{ - "state": d.Get("state").(string), + "state": d.Get("state").(string), "subnet-id": d.Get("subnet_id").(string), }, ) @@ -93,6 +93,5 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error d.Set("subnet_id", ngw.AvailabilityZone) d.Set("tags", tagsToMap(ngw.Tags)) - return nil } diff --git a/aws/data_source_aws_nat_gateway_test.go b/aws/data_source_aws_nat_gateway_test.go index 16808002331..d3755035234 100644 --- a/aws/data_source_aws_nat_gateway_test.go +++ b/aws/data_source_aws_nat_gateway_test.go @@ -34,7 +34,6 @@ func TestAccDataSourceAwsNatGateway(t *testing.T) { }) } - func testAccDataSourceAwsNatGatewayConfig(rInt int) string { return fmt.Sprintf(` provider "aws" { From 099fcd13c367960064f07c5f157125f2c7cae048 Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 17:17:24 -0400 Subject: [PATCH 12/36] changed s/Filters/Filter --- aws/data_source_aws_nat_gateway.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index 339bb873f95..eb394981694 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -51,28 +51,28 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error req.NatGatewayIds = aws.StringSlice([]string{id.(string)}) } - req.Filters = buildEC2AttributeFilterList( + req.Filter = buildEC2AttributeFilterList( map[string]string{ "state": d.Get("state").(string), "subnet-id": d.Get("subnet_id").(string), }, ) if id, ok := d.GetOk("vpc_id"); ok { - req.Filters = append(req.Filters, buildEC2AttributeFilterList( + req.Filter = append(req.Filter, buildEC2AttributeFilterList( map[string]string{ "vpc-id": id.(string), }, )...) } - req.Filters = append(req.Filters, buildEC2TagFilterList( + req.Filter = append(req.Filter, buildEC2TagFilterList( tagsFromMap(d.Get("tags").(map[string]interface{})), )...) - req.Filters = append(req.Filters, buildEC2CustomFilterList( + req.Filter = append(req.Filter, buildEC2CustomFilterList( d.Get("filter").(*schema.Set), )...) - if len(req.Filters) == 0 { + if len(req.Filter) == 0 { // Don't send an empty filters list; the EC2 API won't accept it. - req.Filters = nil + req.Filter = nil } resp, err := conn.DescribeNatGateways(req) From 57ad64ddb86876f10244c4465380a91a51901899 Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 17:29:20 -0400 Subject: [PATCH 13/36] fixed returns, removed tags reference --- aws/data_source_aws_nat_gateway.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index eb394981694..9f7c48b8e02 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -35,7 +35,6 @@ func dataSourceAwsNatGateway() *schema.Resource { Computed: true, }, "filter": ec2CustomFiltersSchema(), - "tags": tagsSchemaComputed(), }, } } @@ -64,9 +63,6 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error }, )...) } - req.Filter = append(req.Filter, buildEC2TagFilterList( - tagsFromMap(d.Get("tags").(map[string]interface{})), - )...) req.Filter = append(req.Filter, buildEC2CustomFilterList( d.Get("filter").(*schema.Set), )...) @@ -88,10 +84,9 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error ngw := resp.NatGateways[0] - d.SetId(aws.StringValue(ngw.NatGatewayId)) + d.SetId(aws.StringValue(ngw.natGatewayId)) d.Set("state", ngw.State) - d.Set("subnet_id", ngw.AvailabilityZone) - d.Set("tags", tagsToMap(ngw.Tags)) + d.Set("subnet_id", ngw.subnetId) return nil } From 37c2bd3eedab419e879e438b6c64ac1a5263f68a Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 17:32:29 -0400 Subject: [PATCH 14/36] fixed var names to be capital --- aws/data_source_aws_nat_gateway.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index 9f7c48b8e02..84c95102b24 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -84,9 +84,9 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error ngw := resp.NatGateways[0] - d.SetId(aws.StringValue(ngw.natGatewayId)) + d.SetId(aws.StringValue(ngw.NatGatewayId)) d.Set("state", ngw.State) - d.Set("subnet_id", ngw.subnetId) + d.Set("subnet_id", ngw.SubnetId) return nil } From 07944dde714b298cb064c3f939bb6664f814cc8b Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 17:36:05 -0400 Subject: [PATCH 15/36] removed unused import --- aws/data_source_aws_nat_gateway_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/aws/data_source_aws_nat_gateway_test.go b/aws/data_source_aws_nat_gateway_test.go index d3755035234..42dbbb638a5 100644 --- a/aws/data_source_aws_nat_gateway_test.go +++ b/aws/data_source_aws_nat_gateway_test.go @@ -2,7 +2,6 @@ package aws import ( "fmt" - "regexp" "testing" "github.com/hashicorp/terraform/helper/acctest" From 9bba67a4b8acffeedf0663c280bd1545549465b9 Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 18:12:24 -0400 Subject: [PATCH 16/36] added loop to set address variables --- aws/data_source_aws_nat_gateway.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index 84c95102b24..ff047190fed 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -87,6 +87,17 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error 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.NatGatewayAddress { + if *address.AllocationId != nil { + 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 } From e58d0f0c903fa5c89e52e3a5b36d6857c1b2bbdc Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 18:21:11 -0400 Subject: [PATCH 17/36] Made NatGatewayAddresses plural --- aws/data_source_aws_nat_gateway.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index ff047190fed..e8527f786b6 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -89,7 +89,7 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error d.Set("subnet_id", ngw.SubnetId) d.Set("vpc_id", ngw.VpcId) - for _, address := range ngw.NatGatewayAddress { + for _, address := range ngw.NatGatewayAddresses { if *address.AllocationId != nil { d.Set("allocated_eip_id", address.AllocationId) d.Set("allocated_eni_id", address.NetworkInterfaceId) From b6c7edbb38e43eb52b058858f501d02daf793b83 Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 31 Jul 2017 18:24:11 -0400 Subject: [PATCH 18/36] changed empty string check --- aws/data_source_aws_nat_gateway.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index e8527f786b6..a0456630c6d 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -90,7 +90,7 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error d.Set("vpc_id", ngw.VpcId) for _, address := range ngw.NatGatewayAddresses { - if *address.AllocationId != nil { + if *address.AllocationId != "" { d.Set("allocated_eip_id", address.AllocationId) d.Set("allocated_eni_id", address.NetworkInterfaceId) d.Set("allocated_private_ip", address.PrivateIp) From 64267b02fb10547ab3363a10ae00b3201bab4a9a Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Mon, 31 Jul 2017 22:32:27 -0400 Subject: [PATCH 19/36] Correct resource references in acctest config --- aws/data_source_aws_nat_gateway_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aws/data_source_aws_nat_gateway_test.go b/aws/data_source_aws_nat_gateway_test.go index 42dbbb638a5..e6b34cbbf5e 100644 --- a/aws/data_source_aws_nat_gateway_test.go +++ b/aws/data_source_aws_nat_gateway_test.go @@ -42,17 +42,17 @@ provider "aws" { resource "aws_nat_gateway" "test" { tags { Name = "terraform-testacc-nat-gateway-data-source-%d" - ABC = "testacc-%d" + ABC = "testacc-%d" XYZ = "testacc-%d" } } data "aws_nat_gateway" "test_by_id" { - id = "${aws_nat_gateway.id}" + id = "${aws_nat_gateway.test.id}" } data "aws_nat_gateway" "test_by_tags" { - tags = "${aws_nat_gateway.tags}" + tags = "${aws_nat_gateway.test.tags}" } `, rInt, rInt+1, rInt-1) } From c168d46dc78b70ab4dd2096d35d309cf5d5fc22f Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Mon, 31 Jul 2017 23:18:14 -0400 Subject: [PATCH 20/36] Add NGW to aws datasource list --- aws/provider.go | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/provider.go b/aws/provider.go index 84e348534a9..a5bd3c29dcb 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -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(), From c644192053ddd7feee010a24d647753a07449215 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Mon, 31 Jul 2017 23:36:39 -0400 Subject: [PATCH 21/36] Add EIP, VPC, and Subnet to test fixture --- aws/data_source_aws_nat_gateway_test.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/aws/data_source_aws_nat_gateway_test.go b/aws/data_source_aws_nat_gateway_test.go index e6b34cbbf5e..a631eee09a6 100644 --- a/aws/data_source_aws_nat_gateway_test.go +++ b/aws/data_source_aws_nat_gateway_test.go @@ -9,7 +9,8 @@ import ( ) func TestAccDataSourceAwsNatGateway(t *testing.T) { - rInt := acctest.RandInt() + // This is used as a portion of CIDR network addresses. + rInt := acctest.RandIntRange(4, 254) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -39,7 +40,29 @@ 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" + } +} + +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" {} + resource "aws_nat_gateway" "test" { + subnet_id = "${aws_subnet.test.id}" + allocation_id = "${aws_eip.test.id}" tags { Name = "terraform-testacc-nat-gateway-data-source-%d" ABC = "testacc-%d" From 15f01f9f28eb47071147b5054f1c46088c2e6b83 Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Tue, 1 Aug 2017 00:24:04 -0400 Subject: [PATCH 22/36] Working test fixture config - added IGW, removed tagging on NGW --- aws/data_source_aws_nat_gateway_test.go | 40 +++++++++++++++---------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/aws/data_source_aws_nat_gateway_test.go b/aws/data_source_aws_nat_gateway_test.go index a631eee09a6..f34222be0ce 100644 --- a/aws/data_source_aws_nat_gateway_test.go +++ b/aws/data_source_aws_nat_gateway_test.go @@ -21,12 +21,11 @@ func TestAccDataSourceAwsNatGateway(t *testing.T) { Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrPair( "data.aws_nat_gateway.test_by_id", "id", - "aws_nat_gateway", "id"), + "aws_nat_gateway.test", "id"), resource.TestCheckResourceAttrPair( - "data.aws_nat_gateway.test_by_tags", "id", - "aws_nat_gateway", "id"), + "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.TestCheckResourceAttr("data.aws_nat_gateway.test_by_tags", "tags.%", "3"), resource.TestCheckNoResourceAttr("data.aws_nat_gateway.test_by_id", "attached_vpc_id"), ), }, @@ -43,7 +42,7 @@ provider "aws" { resource "aws_vpc" "test" { cidr_block = "172.%d.0.0/16" tags { - Name = "terraform-testacc-nat-gateway-data-source" + Name = "terraform-testacc-nat-gateway-data-source-%d" } } @@ -58,24 +57,35 @@ resource "aws_subnet" "test" { } # EIPs are not taggable -resource "aws_eip" "test" {} +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}" - tags { - Name = "terraform-testacc-nat-gateway-data-source-%d" - ABC = "testacc-%d" - XYZ = "testacc-%d" - } + + depends_on = ["aws_internet_gateway.test"] } data "aws_nat_gateway" "test_by_id" { - id = "${aws_nat_gateway.test.id}" + id = "${aws_nat_gateway.test.id}" } -data "aws_nat_gateway" "test_by_tags" { - tags = "${aws_nat_gateway.test.tags}" +data "aws_nat_gateway" "test_by_subnet_id" { + subnet_id = "${aws_nat_gateway.test.subnet_id}" } -`, rInt, rInt+1, rInt-1) + +# TODO - EIP allocation search? + +`, rInt, rInt, rInt, rInt) } From 1ee6a3f9d6b49ba296f080954a92245b3217569f Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Tue, 1 Aug 2017 00:25:05 -0400 Subject: [PATCH 23/36] s/VPN/NAT/ --- aws/data_source_aws_nat_gateway.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index a0456630c6d..7153ef769c6 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -42,7 +42,7 @@ func dataSourceAwsNatGateway() *schema.Resource { func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn - log.Printf("[DEBUG] Reading VPN Gateways.") + log.Printf("[DEBUG] Reading NAT Gateways.") req := &ec2.DescribeNatGatewaysInput{} From c91e7093de2ea86697ed718b7ce012e9adbd17d7 Mon Sep 17 00:00:00 2001 From: Levi Date: Tue, 1 Aug 2017 09:15:36 -0400 Subject: [PATCH 24/36] added missing rInt arg --- aws/data_source_aws_nat_gateway_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/data_source_aws_nat_gateway_test.go b/aws/data_source_aws_nat_gateway_test.go index f34222be0ce..c6ce5e5a337 100644 --- a/aws/data_source_aws_nat_gateway_test.go +++ b/aws/data_source_aws_nat_gateway_test.go @@ -87,5 +87,5 @@ data "aws_nat_gateway" "test_by_subnet_id" { # TODO - EIP allocation search? -`, rInt, rInt, rInt, rInt) +`, rInt, rInt, rInt, rInt, rInt) } From 82566aede0a5970f89989ceb488e71e8c3958a9b Mon Sep 17 00:00:00 2001 From: Levi Date: Tue, 1 Aug 2017 10:00:46 -0400 Subject: [PATCH 25/36] added aws_nat_gateway to sidebar --- website/aws.erb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/website/aws.erb b/website/aws.erb index 2dacfdb53de..9c918f785ab 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -140,6 +140,9 @@ > aws_kms_secret + > + + aws_nat_gateway + + > aws_partition From 0d3e9ff1fc43eccde23909189adba2497320fd74 Mon Sep 17 00:00:00 2001 From: Levi Date: Tue, 1 Aug 2017 12:43:33 -0400 Subject: [PATCH 26/36] created documentation page for nat gateway --- website/docs/d/nat_gateway.html.markdown | 61 ++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 website/docs/d/nat_gateway.html.markdown diff --git a/website/docs/d/nat_gateway.html.markdown b/website/docs/d/nat_gateway.html.markdown new file mode 100644 index 00000000000..2a6d38f1e2e --- /dev/null +++ b/website/docs/d/nat_gateway.html.markdown @@ -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 + +`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}" + +} +``` + +## 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. + +* `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. + +* `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. From 22d1df882555bd46d888d3808e9f58a9d837d5b0 Mon Sep 17 00:00:00 2001 From: Levi Date: Tue, 1 Aug 2017 13:01:25 -0400 Subject: [PATCH 27/36] changed logic for building req --- aws/data_source_aws_nat_gateway.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index 7153ef769c6..b1288b0441d 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -50,19 +50,30 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error req.NatGatewayIds = aws.StringSlice([]string{id.(string)}) } - req.Filter = buildEC2AttributeFilterList( - map[string]string{ - "state": d.Get("state").(string), - "subnet-id": d.Get("subnet_id").(string), - }, - ) - if id, ok := d.GetOk("vpc_id"); ok { + if vpc_id, ok := d.GetOk("vpc_id"); ok { req.Filter = append(req.Filter, buildEC2AttributeFilterList( map[string]string{ "vpc-id": id.(string), }, )...) } + + if state, ok := d.GetOk("state"); ok { + req.Filter = append(req.Filter, buildEC2AttributeFilterList( + map[string]string{ + "state": vpc_id.(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), )...) From bebcbeb671465942da82a2dc6cec58317badf922 Mon Sep 17 00:00:00 2001 From: Levi Date: Tue, 1 Aug 2017 13:02:32 -0400 Subject: [PATCH 28/36] removed comment about EIP search --- aws/data_source_aws_nat_gateway_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/aws/data_source_aws_nat_gateway_test.go b/aws/data_source_aws_nat_gateway_test.go index c6ce5e5a337..e1e516e288d 100644 --- a/aws/data_source_aws_nat_gateway_test.go +++ b/aws/data_source_aws_nat_gateway_test.go @@ -85,7 +85,5 @@ data "aws_nat_gateway" "test_by_subnet_id" { subnet_id = "${aws_nat_gateway.test.subnet_id}" } -# TODO - EIP allocation search? - `, rInt, rInt, rInt, rInt, rInt) } From a41d94ce1f14e146874b2824de355da13404ab02 Mon Sep 17 00:00:00 2001 From: Levi Date: Tue, 1 Aug 2017 13:04:28 -0400 Subject: [PATCH 29/36] reran make fmt --- aws/data_source_aws_nat_gateway.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index b1288b0441d..f685ee5a98a 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -58,21 +58,21 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error )...) } - if state, ok := d.GetOk("state"); ok { - req.Filter = append(req.Filter, buildEC2AttributeFilterList( - map[string]string{ - "state": vpc_id.(string), - }, - )...) - } - - if subnet_id, ok := d.GetOk("subnet_id"); ok { - req.Filter = append(req.Filter, buildEC2AttributeFilterList( - map[string]string{ - "subnet-id": subnet_id.(string), - }, - )...) - } + if state, ok := d.GetOk("state"); ok { + req.Filter = append(req.Filter, buildEC2AttributeFilterList( + map[string]string{ + "state": vpc_id.(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), From 9e6020000a48bba0677d97158dda4041a5f0f863 Mon Sep 17 00:00:00 2001 From: Levi Date: Tue, 1 Aug 2017 13:10:50 -0400 Subject: [PATCH 30/36] fixed copy paste typos --- aws/data_source_aws_nat_gateway.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index f685ee5a98a..2c416cd9fda 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -53,7 +53,7 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error if vpc_id, ok := d.GetOk("vpc_id"); ok { req.Filter = append(req.Filter, buildEC2AttributeFilterList( map[string]string{ - "vpc-id": id.(string), + "vpc-id": vpc_id.(string), }, )...) } @@ -61,7 +61,7 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error if state, ok := d.GetOk("state"); ok { req.Filter = append(req.Filter, buildEC2AttributeFilterList( map[string]string{ - "state": vpc_id.(string), + "state": state.(string), }, )...) } From 8b5b0aa343f7fed17ebc946a2d703a2533081c01 Mon Sep 17 00:00:00 2001 From: Levi Date: Wed, 18 Oct 2017 18:59:52 -0400 Subject: [PATCH 31/36] added tags to schema and moved log statement --- aws/data_source_aws_nat_gateway.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aws/data_source_aws_nat_gateway.go b/aws/data_source_aws_nat_gateway.go index 2c416cd9fda..a8e4eab6ac3 100644 --- a/aws/data_source_aws_nat_gateway.go +++ b/aws/data_source_aws_nat_gateway.go @@ -34,6 +34,8 @@ func dataSourceAwsNatGateway() *schema.Resource { Optional: true, Computed: true, }, + "tags": tagsSchemaComputed(), + "filter": ec2CustomFiltersSchema(), }, } @@ -42,8 +44,6 @@ func dataSourceAwsNatGateway() *schema.Resource { func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn - log.Printf("[DEBUG] Reading NAT Gateways.") - req := &ec2.DescribeNatGatewaysInput{} if id, ok := d.GetOk("id"); ok { @@ -81,7 +81,7 @@ func dataSourceAwsNatGatewayRead(d *schema.ResourceData, meta interface{}) error // 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 From 88de8270e9658b1c33a6c31e4546ebe3aba7a7c3 Mon Sep 17 00:00:00 2001 From: Levi Date: Wed, 18 Oct 2017 19:02:53 -0400 Subject: [PATCH 32/36] removed plus signs --- website/aws.erb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/website/aws.erb b/website/aws.erb index 9c918f785ab..159b087c9f8 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -141,8 +141,8 @@ aws_kms_secret > - + aws_nat_gateway - + + aws_nat_gateway + > aws_partition @@ -886,7 +886,7 @@ - + > IoT Resources