From 38e99878534ce68faf53433e02001a7e84478788 Mon Sep 17 00:00:00 2001 From: Jesse Aukeman Date: Thu, 14 Jun 2018 17:22:01 -0400 Subject: [PATCH 1/4] Added data source for aws_route_table_ids This allow lookup of a list of route table ids similar to the existing aws_subnet_ids data source. --- aws/data_source_aws_route_table_ids.go | 68 +++++++++++ aws/data_source_aws_route_table_ids_test.go | 119 +++++++++++++++++++ aws/provider.go | 1 + website/docs/d/route_table_ids.html.markdown | 44 +++++++ 4 files changed, 232 insertions(+) create mode 100644 aws/data_source_aws_route_table_ids.go create mode 100644 aws/data_source_aws_route_table_ids_test.go create mode 100644 website/docs/d/route_table_ids.html.markdown diff --git a/aws/data_source_aws_route_table_ids.go b/aws/data_source_aws_route_table_ids.go new file mode 100644 index 00000000000..04a3fa7cf6e --- /dev/null +++ b/aws/data_source_aws_route_table_ids.go @@ -0,0 +1,68 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceAwsRouteTableIDs() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsRouteTableIDsRead, + Schema: map[string]*schema.Schema{ + + "tags": tagsSchemaComputed(), + + "vpc_id": { + Type: schema.TypeString, + Required: true, + }, + + "ids": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + }, + } +} + +func dataSourceAwsRouteTableIDsRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + + req := &ec2.DescribeRouteTablesInput{} + + req.Filters = buildEC2AttributeFilterList( + map[string]string{ + "vpc-id": d.Get("vpc_id").(string), + }, + ) + + req.Filters = append(req.Filters, buildEC2TagFilterList( + tagsFromMap(d.Get("tags").(map[string]interface{})), + )...) + + log.Printf("[DEBUG] DescribeRouteTables %s\n", req) + resp, err := conn.DescribeRouteTables(req) + if err != nil { + return err + } + + if resp == nil || len(resp.RouteTables) == 0 { + return fmt.Errorf("no matching route tables found for vpc with id %s", d.Get("vpc_id").(string)) + } + + routeTables := make([]string, 0) + + for _, routeTable := range resp.RouteTables { + routeTables = append(routeTables, *routeTable.RouteTableId) + } + + d.SetId(d.Get("vpc_id").(string)) + d.Set("ids", routeTables) + + return nil +} diff --git a/aws/data_source_aws_route_table_ids_test.go b/aws/data_source_aws_route_table_ids_test.go new file mode 100644 index 00000000000..0a9440ea627 --- /dev/null +++ b/aws/data_source_aws_route_table_ids_test.go @@ -0,0 +1,119 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAwsRouteTableIDs(t *testing.T) { + rInt := acctest.RandIntRange(0, 256) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckVpcDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAwsRouteTableIDsConfig(rInt), + }, + { + Config: testAccDataSourceAwsRouteTableIDsConfigWithDataSource(rInt), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_route_table_ids.selected", "ids.#", "3"), + resource.TestCheckResourceAttr("data.aws_route_table_ids.private", "ids.#", "2"), + ), + }, + }, + }) +} + +func testAccDataSourceAwsRouteTableIDsConfigWithDataSource(rInt int) string { + return fmt.Sprintf(` +resource "aws_vpc" "test" { + cidr_block = "172.%d.0.0/16" + + tags { + Name = "terraform-testacc-route-table-ids-data-source" + } +} + +resource "aws_route_table" "test_public_a" { + vpc_id = "${aws_vpc.test.id}" + + tags { + Name = "tf-acc-route-table-ids-data-source-public-a" + Tier = "Public" + } +} + +resource "aws_route_table" "test_private_a" { + vpc_id = "${aws_vpc.test.id}" + + tags { + Name = "tf-acc-route-table-ids-data-source-private-a" + Tier = "Private" + } +} + +resource "aws_route_table" "test_private_b" { + vpc_id = "${aws_vpc.test.id}" + + tags { + Name = "tf-acc-route-table-ids-data-source-private-b" + Tier = "Private" + } +} + +data "aws_route_table_ids" "selected" { + vpc_id = "${aws_vpc.test.id}" +} + +data "aws_route_table_ids" "private" { + vpc_id = "${aws_vpc.test.id}" + tags { + Tier = "Private" + } +} +`, rInt) +} + +func testAccDataSourceAwsRouteTableIDsConfig(rInt int) string { + return fmt.Sprintf(` +resource "aws_vpc" "test" { + cidr_block = "172.%d.0.0/16" + + tags { + Name = "terraform-testacc-route-table-ids-data-source" + } +} + +resource "aws_route_table" "test_public_a" { + vpc_id = "${aws_vpc.test.id}" + + tags { + Name = "tf-acc-route-table-ids-data-source-public-a" + Tier = "Public" + } +} + +resource "aws_route_table" "test_private_a" { + vpc_id = "${aws_vpc.test.id}" + + tags { + Name = "tf-acc-route-table-ids-data-source-private-a" + Tier = "Private" + } +} + +resource "aws_route_table" "test_private_b" { + vpc_id = "${aws_vpc.test.id}" + + tags { + Name = "tf-acc-route-table-ids-data-source-private-b" + Tier = "Private" + } +} +`, rInt) +} diff --git a/aws/provider.go b/aws/provider.go index 74a0f701bb3..561b2baaf3d 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -233,6 +233,7 @@ func Provider() terraform.ResourceProvider { "aws_region": dataSourceAwsRegion(), "aws_route": dataSourceAwsRoute(), "aws_route_table": dataSourceAwsRouteTable(), + "aws_route_table_ids": dataSourceAwsRouteTableIDs(), "aws_route53_zone": dataSourceAwsRoute53Zone(), "aws_s3_bucket": dataSourceAwsS3Bucket(), "aws_s3_bucket_object": dataSourceAwsS3BucketObject(), diff --git a/website/docs/d/route_table_ids.html.markdown b/website/docs/d/route_table_ids.html.markdown new file mode 100644 index 00000000000..5f3f7c1bae4 --- /dev/null +++ b/website/docs/d/route_table_ids.html.markdown @@ -0,0 +1,44 @@ +--- +layout: "aws" +page_title: "AWS: aws_route_table_ids" +sidebar_current: "docs-aws-datasource-route-table-ids" +description: |- + Provides a list of route table Ids for a VPC +--- + +# Data Source: aws_route_table_ids + +`aws_route_table_ids` provides a list of ids for a vpc_id + +This resource can be useful for getting back a list of route table ids for a vpc. + +## Example Usage + +The following adds a route for a particular cidr block to every route table +in the vpc to use a particular vpc peering connection. + +```hcl + +data "aws_route_table_ids" "rts" { + vpc_id = "${var.vpc_id}" +} + +resource "aws_route" "r" { + count = "${length(data.aws_route_table_ids.rts.ids)}" + route_table_id = "${data.aws_route_table_ids.rts.ids[count.index]}" + destination_cidr_block = "10.0.1.0/22" + vpc_peering_connection_id = "pcx-0e9a7a9ecd137dc54" +} + +``` + +## Argument Reference + +* `vpc_id` - (Required) The VPC ID that you want to filter from. + +* `tags` - (Optional) A mapping of tags, each pair of which must exactly match + a pair on the desired route tables. + +## Attributes Reference + +* `ids` - A list of all the route table ids found. This data source will fail if none are found. From b4cf044b39e06617826fa9e64c80bcc9a782ca35 Mon Sep 17 00:00:00 2001 From: Jesse Aukeman Date: Tue, 26 Jun 2018 11:33:03 -0400 Subject: [PATCH 2/4] Renamed data source from aws_route_table_ids to aws_route_tables. Renamed the files and functions to match. --- ...e_aws_route_table_ids.go => data_source_aws_route_tables.go} | 2 +- ...e_table_ids_test.go => data_source_aws_route_tables_test.go} | 0 aws/provider.go | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename aws/{data_source_aws_route_table_ids.go => data_source_aws_route_tables.go} (96%) rename aws/{data_source_aws_route_table_ids_test.go => data_source_aws_route_tables_test.go} (100%) diff --git a/aws/data_source_aws_route_table_ids.go b/aws/data_source_aws_route_tables.go similarity index 96% rename from aws/data_source_aws_route_table_ids.go rename to aws/data_source_aws_route_tables.go index 04a3fa7cf6e..831d420debb 100644 --- a/aws/data_source_aws_route_table_ids.go +++ b/aws/data_source_aws_route_tables.go @@ -8,7 +8,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" ) -func dataSourceAwsRouteTableIDs() *schema.Resource { +func dataSourceAwsRouteTables() *schema.Resource { return &schema.Resource{ Read: dataSourceAwsRouteTableIDsRead, Schema: map[string]*schema.Schema{ diff --git a/aws/data_source_aws_route_table_ids_test.go b/aws/data_source_aws_route_tables_test.go similarity index 100% rename from aws/data_source_aws_route_table_ids_test.go rename to aws/data_source_aws_route_tables_test.go diff --git a/aws/provider.go b/aws/provider.go index 561b2baaf3d..7a220b560f4 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -233,7 +233,7 @@ func Provider() terraform.ResourceProvider { "aws_region": dataSourceAwsRegion(), "aws_route": dataSourceAwsRoute(), "aws_route_table": dataSourceAwsRouteTable(), - "aws_route_table_ids": dataSourceAwsRouteTableIDs(), + "aws_route_tables": dataSourceAwsRouteTables(), "aws_route53_zone": dataSourceAwsRoute53Zone(), "aws_s3_bucket": dataSourceAwsS3Bucket(), "aws_s3_bucket_object": dataSourceAwsS3BucketObject(), From 1eeea372b27c17d5d8f7154d50f38654d2468e56 Mon Sep 17 00:00:00 2001 From: Jesse Aukeman Date: Tue, 26 Jun 2018 12:25:29 -0400 Subject: [PATCH 3/4] - Update naming references consistent for aws_route_tables (rather than aws_route_table_ids). - Change vpc_id to be optional rather than required attribute. - Update to use aws.StringValue helper rather than directly dereferencing routeTable.RouteTableId - Add check for error when using d.Set with aggregate type. --- aws/data_source_aws_route_tables.go | 28 +++++++++------ aws/data_source_aws_route_tables_test.go | 34 +++++++++---------- ...ml.markdown => route_tables.html.markdown} | 22 ++++++------ 3 files changed, 44 insertions(+), 40 deletions(-) rename website/docs/d/{route_table_ids.html.markdown => route_tables.html.markdown} (53%) diff --git a/aws/data_source_aws_route_tables.go b/aws/data_source_aws_route_tables.go index 831d420debb..2864ae5ac76 100644 --- a/aws/data_source_aws_route_tables.go +++ b/aws/data_source_aws_route_tables.go @@ -4,20 +4,22 @@ import ( "fmt" "log" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" ) func dataSourceAwsRouteTables() *schema.Resource { return &schema.Resource{ - Read: dataSourceAwsRouteTableIDsRead, + Read: dataSourceAwsRouteTablesRead, Schema: map[string]*schema.Schema{ "tags": tagsSchemaComputed(), "vpc_id": { Type: schema.TypeString, - Required: true, + Optional: true, }, "ids": { @@ -30,16 +32,18 @@ func dataSourceAwsRouteTables() *schema.Resource { } } -func dataSourceAwsRouteTableIDsRead(d *schema.ResourceData, meta interface{}) error { +func dataSourceAwsRouteTablesRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn req := &ec2.DescribeRouteTablesInput{} - req.Filters = buildEC2AttributeFilterList( - map[string]string{ - "vpc-id": d.Get("vpc_id").(string), - }, - ) + if v, ok := d.GetOk("vpc_id"); ok { + req.Filters = buildEC2AttributeFilterList( + map[string]string{ + "vpc-id": v.(string), + }, + ) + } req.Filters = append(req.Filters, buildEC2TagFilterList( tagsFromMap(d.Get("tags").(map[string]interface{})), @@ -58,11 +62,13 @@ func dataSourceAwsRouteTableIDsRead(d *schema.ResourceData, meta interface{}) er routeTables := make([]string, 0) for _, routeTable := range resp.RouteTables { - routeTables = append(routeTables, *routeTable.RouteTableId) + routeTables = append(routeTables, aws.StringValue(routeTable.RouteTableId)) } - d.SetId(d.Get("vpc_id").(string)) - d.Set("ids", routeTables) + d.SetId(resource.UniqueId()) + if err = d.Set("ids", routeTables); err != nil { + return fmt.Errorf("error setting ids: %s", err) + } return nil } diff --git a/aws/data_source_aws_route_tables_test.go b/aws/data_source_aws_route_tables_test.go index 0a9440ea627..802f48ff01b 100644 --- a/aws/data_source_aws_route_tables_test.go +++ b/aws/data_source_aws_route_tables_test.go @@ -8,7 +8,7 @@ import ( "github.com/hashicorp/terraform/helper/resource" ) -func TestAccDataSourceAwsRouteTableIDs(t *testing.T) { +func TestAccDataSourceAwsRouteTables(t *testing.T) { rInt := acctest.RandIntRange(0, 256) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -16,26 +16,26 @@ func TestAccDataSourceAwsRouteTableIDs(t *testing.T) { CheckDestroy: testAccCheckVpcDestroy, Steps: []resource.TestStep{ { - Config: testAccDataSourceAwsRouteTableIDsConfig(rInt), + Config: testAccDataSourceAwsRouteTablesConfig(rInt), }, { - Config: testAccDataSourceAwsRouteTableIDsConfigWithDataSource(rInt), + Config: testAccDataSourceAwsRouteTablesConfigWithDataSource(rInt), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.aws_route_table_ids.selected", "ids.#", "3"), - resource.TestCheckResourceAttr("data.aws_route_table_ids.private", "ids.#", "2"), + resource.TestCheckResourceAttr("data.aws_route_tables.selected", "ids.#", "3"), + resource.TestCheckResourceAttr("data.aws_route_tables.private", "ids.#", "2"), ), }, }, }) } -func testAccDataSourceAwsRouteTableIDsConfigWithDataSource(rInt int) string { +func testAccDataSourceAwsRouteTablesConfigWithDataSource(rInt int) string { return fmt.Sprintf(` resource "aws_vpc" "test" { cidr_block = "172.%d.0.0/16" tags { - Name = "terraform-testacc-route-table-ids-data-source" + Name = "terraform-testacc-route-tables-data-source" } } @@ -43,7 +43,7 @@ resource "aws_route_table" "test_public_a" { vpc_id = "${aws_vpc.test.id}" tags { - Name = "tf-acc-route-table-ids-data-source-public-a" + Name = "tf-acc-route-tables-data-source-public-a" Tier = "Public" } } @@ -52,7 +52,7 @@ resource "aws_route_table" "test_private_a" { vpc_id = "${aws_vpc.test.id}" tags { - Name = "tf-acc-route-table-ids-data-source-private-a" + Name = "tf-acc-route-tables-data-source-private-a" Tier = "Private" } } @@ -61,16 +61,16 @@ resource "aws_route_table" "test_private_b" { vpc_id = "${aws_vpc.test.id}" tags { - Name = "tf-acc-route-table-ids-data-source-private-b" + Name = "tf-acc-route-tables-data-source-private-b" Tier = "Private" } } -data "aws_route_table_ids" "selected" { +data "aws_route_tables" "selected" { vpc_id = "${aws_vpc.test.id}" } -data "aws_route_table_ids" "private" { +data "aws_route_tables" "private" { vpc_id = "${aws_vpc.test.id}" tags { Tier = "Private" @@ -79,13 +79,13 @@ data "aws_route_table_ids" "private" { `, rInt) } -func testAccDataSourceAwsRouteTableIDsConfig(rInt int) string { +func testAccDataSourceAwsRouteTablesConfig(rInt int) string { return fmt.Sprintf(` resource "aws_vpc" "test" { cidr_block = "172.%d.0.0/16" tags { - Name = "terraform-testacc-route-table-ids-data-source" + Name = "terraform-testacc-route-tables-data-source" } } @@ -93,7 +93,7 @@ resource "aws_route_table" "test_public_a" { vpc_id = "${aws_vpc.test.id}" tags { - Name = "tf-acc-route-table-ids-data-source-public-a" + Name = "tf-acc-route-tables-data-source-public-a" Tier = "Public" } } @@ -102,7 +102,7 @@ resource "aws_route_table" "test_private_a" { vpc_id = "${aws_vpc.test.id}" tags { - Name = "tf-acc-route-table-ids-data-source-private-a" + Name = "tf-acc-route-tables-data-source-private-a" Tier = "Private" } } @@ -111,7 +111,7 @@ resource "aws_route_table" "test_private_b" { vpc_id = "${aws_vpc.test.id}" tags { - Name = "tf-acc-route-table-ids-data-source-private-b" + Name = "tf-acc-route-tables-data-source-private-b" Tier = "Private" } } diff --git a/website/docs/d/route_table_ids.html.markdown b/website/docs/d/route_tables.html.markdown similarity index 53% rename from website/docs/d/route_table_ids.html.markdown rename to website/docs/d/route_tables.html.markdown index 5f3f7c1bae4..7456017b91c 100644 --- a/website/docs/d/route_table_ids.html.markdown +++ b/website/docs/d/route_tables.html.markdown @@ -1,31 +1,29 @@ --- layout: "aws" -page_title: "AWS: aws_route_table_ids" -sidebar_current: "docs-aws-datasource-route-table-ids" +page_title: "AWS: aws_route_tables" +sidebar_current: "docs-aws-datasource-route-tables" description: |- - Provides a list of route table Ids for a VPC + Get information on Amazon route tables. --- -# Data Source: aws_route_table_ids +# Data Source: aws_route_tables -`aws_route_table_ids` provides a list of ids for a vpc_id - -This resource can be useful for getting back a list of route table ids for a vpc. +This resource can be useful for getting back a list of route table ids to be referenced elsewhere. ## Example Usage The following adds a route for a particular cidr block to every route table -in the vpc to use a particular vpc peering connection. +in a specified vpc to use a particular vpc peering connection. ```hcl -data "aws_route_table_ids" "rts" { +data "aws_route_tables" "rts" { vpc_id = "${var.vpc_id}" } resource "aws_route" "r" { - count = "${length(data.aws_route_table_ids.rts.ids)}" - route_table_id = "${data.aws_route_table_ids.rts.ids[count.index]}" + count = "${length(data.aws_route_tables.rts.ids)}" + route_table_id = "${data.aws_route_tables.rts.ids[count.index]}" destination_cidr_block = "10.0.1.0/22" vpc_peering_connection_id = "pcx-0e9a7a9ecd137dc54" } @@ -34,7 +32,7 @@ resource "aws_route" "r" { ## Argument Reference -* `vpc_id` - (Required) The VPC ID that you want to filter from. +* `vpc_id` - (Optional) The VPC ID that you want to filter from. * `tags` - (Optional) A mapping of tags, each pair of which must exactly match a pair on the desired route tables. From 43ea4382d43a8918fb44cc9179ab2e3fb9b5b2b5 Mon Sep 17 00:00:00 2001 From: Jesse Aukeman Date: Tue, 26 Jun 2018 13:42:28 -0400 Subject: [PATCH 4/4] Updated and fixed test cases --- aws/data_source_aws_route_tables_test.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/aws/data_source_aws_route_tables_test.go b/aws/data_source_aws_route_tables_test.go index 802f48ff01b..7843157a44b 100644 --- a/aws/data_source_aws_route_tables_test.go +++ b/aws/data_source_aws_route_tables_test.go @@ -21,8 +21,9 @@ func TestAccDataSourceAwsRouteTables(t *testing.T) { { Config: testAccDataSourceAwsRouteTablesConfigWithDataSource(rInt), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.aws_route_tables.selected", "ids.#", "3"), + resource.TestCheckResourceAttr("data.aws_route_tables.test", "ids.#", "4"), resource.TestCheckResourceAttr("data.aws_route_tables.private", "ids.#", "2"), + resource.TestCheckResourceAttr("data.aws_route_tables.test2", "ids.#", "1"), ), }, }, @@ -39,6 +40,14 @@ resource "aws_vpc" "test" { } } +resource "aws_vpc" "test2" { + cidr_block = "172.%d.0.0/16" + + tags { + Name = "terraform-test2acc-route-tables-data-source" + } +} + resource "aws_route_table" "test_public_a" { vpc_id = "${aws_vpc.test.id}" @@ -66,17 +75,21 @@ resource "aws_route_table" "test_private_b" { } } -data "aws_route_tables" "selected" { +data "aws_route_tables" "test" { vpc_id = "${aws_vpc.test.id}" } +data "aws_route_tables" "test2" { + vpc_id = "${aws_vpc.test2.id}" +} + data "aws_route_tables" "private" { vpc_id = "${aws_vpc.test.id}" tags { Tier = "Private" } } -`, rInt) +`, rInt, rInt) } func testAccDataSourceAwsRouteTablesConfig(rInt int) string {