-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4841 from jesseaukeman/add-data-source-aws-route-…
…table-ids Added data source for aws_route_table_ids
- Loading branch information
Showing
4 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |