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

aws_route_tables data source filtering #5035

Merged
merged 2 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions aws/data_source_aws_route_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func dataSourceAwsRouteTables() *schema.Resource {
Read: dataSourceAwsRouteTablesRead,
Schema: map[string]*schema.Schema{

"filter": ec2CustomFiltersSchema(),

"tags": tagsSchemaComputed(),

"vpc_id": {
Expand Down Expand Up @@ -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 {
Expand Down
40 changes: 38 additions & 2 deletions aws/data_source_aws_route_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
},
Expand Down Expand Up @@ -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"
}
}

Expand All @@ -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"
}
}

Expand All @@ -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"
}
}

Expand All @@ -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)
}

Expand All @@ -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"
}
}

Expand All @@ -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"
}
}

Expand All @@ -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)
Expand Down
27 changes: 22 additions & 5 deletions website/docs/d/route_tables.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,48 @@ 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"
}

```

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