From 57e2ae19c6aa9e91121281a73180feda9e17efbe Mon Sep 17 00:00:00 2001 From: Fred Dubois Date: Fri, 29 Jun 2018 08:46:01 -0400 Subject: [PATCH 1/2] Added filter to aws_route_tables --- aws/data_source_aws_route_tables.go | 6 ++++ aws/data_source_aws_route_tables_test.go | 40 ++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/aws/data_source_aws_route_tables.go b/aws/data_source_aws_route_tables.go index 2864ae5ac76..ef34987c37f 100644 --- a/aws/data_source_aws_route_tables.go +++ b/aws/data_source_aws_route_tables.go @@ -15,6 +15,8 @@ func dataSourceAwsRouteTables() *schema.Resource { Read: dataSourceAwsRouteTablesRead, Schema: map[string]*schema.Schema{ + "filter": ec2CustomFiltersSchema(), + "tags": tagsSchemaComputed(), "vpc_id": { @@ -49,6 +51,10 @@ func dataSourceAwsRouteTablesRead(d *schema.ResourceData, meta interface{}) erro tagsFromMap(d.Get("tags").(map[string]interface{})), )...) + req.Filters = append(req.Filters, buildEC2CustomFilterList( + d.Get("filter").(*schema.Set), + )...) + log.Printf("[DEBUG] DescribeRouteTables %s\n", req) resp, err := conn.DescribeRouteTables(req) if err != nil { diff --git a/aws/data_source_aws_route_tables_test.go b/aws/data_source_aws_route_tables_test.go index 7843157a44b..8d5ae449afa 100644 --- a/aws/data_source_aws_route_tables_test.go +++ b/aws/data_source_aws_route_tables_test.go @@ -21,9 +21,10 @@ func TestAccDataSourceAwsRouteTables(t *testing.T) { { Config: testAccDataSourceAwsRouteTablesConfigWithDataSource(rInt), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.aws_route_tables.test", "ids.#", "4"), - resource.TestCheckResourceAttr("data.aws_route_tables.private", "ids.#", "2"), + resource.TestCheckResourceAttr("data.aws_route_tables.test", "ids.#", "5"), + resource.TestCheckResourceAttr("data.aws_route_tables.private", "ids.#", "3"), resource.TestCheckResourceAttr("data.aws_route_tables.test2", "ids.#", "1"), + resource.TestCheckResourceAttr("data.aws_route_tables.filter_test", "ids.#", "2"), ), }, }, @@ -54,6 +55,7 @@ resource "aws_route_table" "test_public_a" { tags { Name = "tf-acc-route-tables-data-source-public-a" Tier = "Public" + Component = "Frontend" } } @@ -63,6 +65,7 @@ resource "aws_route_table" "test_private_a" { tags { Name = "tf-acc-route-tables-data-source-private-a" Tier = "Private" + Component = "Database" } } @@ -72,6 +75,17 @@ resource "aws_route_table" "test_private_b" { tags { Name = "tf-acc-route-tables-data-source-private-b" Tier = "Private" + Component = "Backend-1" + } +} + +resource "aws_route_table" "test_private_c" { + vpc_id = "${aws_vpc.test.id}" + + tags { + Name = "tf-acc-route-tables-data-source-private-c" + Tier = "Private" + Component = "Backend-2" } } @@ -89,6 +103,15 @@ data "aws_route_tables" "private" { Tier = "Private" } } + +data "aws_route_tables" "filter_test" { + vpc_id = "${aws_vpc.test.id}" + + filter { + name = "tag:Component" + values = ["Backend*"] + } +} `, rInt, rInt) } @@ -108,6 +131,7 @@ resource "aws_route_table" "test_public_a" { tags { Name = "tf-acc-route-tables-data-source-public-a" Tier = "Public" + Component = "Frontend" } } @@ -117,6 +141,7 @@ resource "aws_route_table" "test_private_a" { tags { Name = "tf-acc-route-tables-data-source-private-a" Tier = "Private" + Component = "Database" } } @@ -126,6 +151,17 @@ resource "aws_route_table" "test_private_b" { tags { Name = "tf-acc-route-tables-data-source-private-b" Tier = "Private" + Component = "Backend-1" + } +} + +resource "aws_route_table" "test_private_c" { + vpc_id = "${aws_vpc.test.id}" + + tags { + Name = "tf-acc-route-tables-data-source-private-c" + Tier = "Private" + Component = "Backend-2" } } `, rInt) From 884792a43a87e7096b882ac58c7e7a1b329e23b6 Mon Sep 17 00:00:00 2001 From: Fred Dubois Date: Fri, 29 Jun 2018 11:04:37 -0400 Subject: [PATCH 2/2] Add documentation for new aws_route_tables filter arg --- website/docs/d/route_tables.html.markdown | 27 ++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/website/docs/d/route_tables.html.markdown b/website/docs/d/route_tables.html.markdown index 7456017b91c..f33dd113f85 100644 --- a/website/docs/d/route_tables.html.markdown +++ b/website/docs/d/route_tables.html.markdown @@ -12,19 +12,25 @@ This resource can be useful for getting back a list of route table ids to be ref ## Example Usage -The following adds a route for a particular cidr block to every route table -in a specified vpc to use a particular vpc peering connection. +The following adds a route for a particular cidr block to every (private +kops) route table in a specified vpc to use a particular vpc peering +connection. ```hcl data "aws_route_tables" "rts" { vpc_id = "${var.vpc_id}" + + filter { + name = "tag:kubernetes.io/kops/role" + values = ["private*"] + } } resource "aws_route" "r" { - 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" + 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" } @@ -32,11 +38,22 @@ resource "aws_route" "r" { ## Argument Reference +* `filter` - (Optional) Custom filter block as described below. + * `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. +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](http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeRouteTables.html). + +* `values` - (Required) Set of values that are accepted for the given field. + A Route Table will be selected if any one of the given values matches. + ## Attributes Reference * `ids` - A list of all the route table ids found. This data source will fail if none are found.