diff --git a/aws/data_source_aws_route_tables.go b/aws/data_source_aws_route_tables.go new file mode 100644 index 00000000000..2864ae5ac76 --- /dev/null +++ b/aws/data_source_aws_route_tables.go @@ -0,0 +1,74 @@ +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/resource" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceAwsRouteTables() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsRouteTablesRead, + Schema: map[string]*schema.Schema{ + + "tags": tagsSchemaComputed(), + + "vpc_id": { + Type: schema.TypeString, + Optional: true, + }, + + "ids": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + }, + } +} + +func dataSourceAwsRouteTablesRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + + req := &ec2.DescribeRouteTablesInput{} + + 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{})), + )...) + + 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, aws.StringValue(routeTable.RouteTableId)) + } + + 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 new file mode 100644 index 00000000000..7843157a44b --- /dev/null +++ b/aws/data_source_aws_route_tables_test.go @@ -0,0 +1,132 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAwsRouteTables(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: testAccDataSourceAwsRouteTablesConfig(rInt), + }, + { + 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.test2", "ids.#", "1"), + ), + }, + }, + }) +} + +func testAccDataSourceAwsRouteTablesConfigWithDataSource(rInt int) string { + return fmt.Sprintf(` +resource "aws_vpc" "test" { + cidr_block = "172.%d.0.0/16" + + tags { + Name = "terraform-testacc-route-tables-data-source" + } +} + +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}" + + tags { + Name = "tf-acc-route-tables-data-source-public-a" + Tier = "Public" + } +} + +resource "aws_route_table" "test_private_a" { + vpc_id = "${aws_vpc.test.id}" + + tags { + Name = "tf-acc-route-tables-data-source-private-a" + Tier = "Private" + } +} + +resource "aws_route_table" "test_private_b" { + vpc_id = "${aws_vpc.test.id}" + + tags { + Name = "tf-acc-route-tables-data-source-private-b" + Tier = "Private" + } +} + +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) +} + +func testAccDataSourceAwsRouteTablesConfig(rInt int) string { + return fmt.Sprintf(` +resource "aws_vpc" "test" { + cidr_block = "172.%d.0.0/16" + + tags { + Name = "terraform-testacc-route-tables-data-source" + } +} + +resource "aws_route_table" "test_public_a" { + vpc_id = "${aws_vpc.test.id}" + + tags { + Name = "tf-acc-route-tables-data-source-public-a" + Tier = "Public" + } +} + +resource "aws_route_table" "test_private_a" { + vpc_id = "${aws_vpc.test.id}" + + tags { + Name = "tf-acc-route-tables-data-source-private-a" + Tier = "Private" + } +} + +resource "aws_route_table" "test_private_b" { + vpc_id = "${aws_vpc.test.id}" + + tags { + Name = "tf-acc-route-tables-data-source-private-b" + Tier = "Private" + } +} +`, rInt) +} diff --git a/aws/provider.go b/aws/provider.go index 74a0f701bb3..7a220b560f4 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_tables": dataSourceAwsRouteTables(), "aws_route53_zone": dataSourceAwsRoute53Zone(), "aws_s3_bucket": dataSourceAwsS3Bucket(), "aws_s3_bucket_object": dataSourceAwsS3BucketObject(), diff --git a/website/docs/d/route_tables.html.markdown b/website/docs/d/route_tables.html.markdown new file mode 100644 index 00000000000..7456017b91c --- /dev/null +++ b/website/docs/d/route_tables.html.markdown @@ -0,0 +1,42 @@ +--- +layout: "aws" +page_title: "AWS: aws_route_tables" +sidebar_current: "docs-aws-datasource-route-tables" +description: |- + Get information on Amazon route tables. +--- + +# Data Source: aws_route_tables + +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 a specified vpc to use a particular vpc peering connection. + +```hcl + +data "aws_route_tables" "rts" { + vpc_id = "${var.vpc_id}" +} + +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" + vpc_peering_connection_id = "pcx-0e9a7a9ecd137dc54" +} + +``` + +## Argument Reference + +* `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. + +## Attributes Reference + +* `ids` - A list of all the route table ids found. This data source will fail if none are found.