-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Added data source for aws_route_tables #4841
Merged
bflad
merged 4 commits into
hashicorp:master
from
jesseaukeman:add-data-source-aws-route-table-ids
Jun 26, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
38e9987
Added data source for aws_route_table_ids
jesseaukeman b4cf044
Renamed data source from aws_route_table_ids to aws_route_tables.
jesseaukeman 1eeea37
- Update naming references consistent for aws_route_tables (rather th…
jesseaukeman 43ea438
Updated and fixed test cases
jesseaukeman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This page needs a sidebar link added in
website/aws.erb
, will add post-merge.