Skip to content

Commit

Permalink
Merge pull request #4841 from jesseaukeman/add-data-source-aws-route-…
Browse files Browse the repository at this point in the history
…table-ids

Added data source for aws_route_table_ids
  • Loading branch information
bflad authored Jun 26, 2018
2 parents 1665116 + 43ea438 commit 1ebf18d
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 0 deletions.
74 changes: 74 additions & 0 deletions aws/data_source_aws_route_tables.go
Original file line number Diff line number Diff line change
@@ -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
}
132 changes: 132 additions & 0 deletions aws/data_source_aws_route_tables_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,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(),
Expand Down
42 changes: 42 additions & 0 deletions website/docs/d/route_tables.html.markdown
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 1ebf18d

Please sign in to comment.