-
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
Added data source for aws_route_tables #4841
Conversation
This allow lookup of a list of route table ids similar to the existing aws_subnet_ids data source.
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.
Hi @jesseaukeman, thanks for submitting this! I left you some initial feedback below. Can you please let us know if you have any questions or do not have time to implement them?
aws/provider.go
Outdated
@@ -233,6 +233,7 @@ func Provider() terraform.ResourceProvider { | |||
"aws_region": dataSourceAwsRegion(), | |||
"aws_route": dataSourceAwsRoute(), | |||
"aws_route_table": dataSourceAwsRouteTable(), | |||
"aws_route_table_ids": dataSourceAwsRouteTableIDs(), |
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.
Sorry this is going to seem real pedantic, but can you please:
- Rename the data source
aws_route_tables
along with the file names - Rename the functions to match, e.g.
dataSourceAwsRouteTables()
anddataSourceAwsRouteTablesRead()
This is mainly for future maintainability as the data source may be adjusted to return information other than just IDs. We unfortunately have some other _ids
data sources (e.g. aws_subnet_ids
) that will likely need to be renamed at some point. 😅
|
||
"vpc_id": { | ||
Type: schema.TypeString, | ||
Required: true, |
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.
According the EC2 API Reference, vpc_id
should not be a required argument. Its valid to search for route tables across multiple VPCs. Can you please:
- Change the attribute to
Optional: true
- Use
d.GetOk("vpc_id")
instead of expecting it to always exist:
if v, ok := d.GetOk("vpc_id"); ok {
req.Filters = buildEC2AttributeFilterList(
map[string]string{
"vpc-id": v.(string),
},
)
}
- Change
d.SetId(d.Get("vpc_id").(string))
tod.SetId(resource.UniqueId())
- Ensure the documentation is updated for the attribute
routeTables := make([]string, 0) | ||
|
||
for _, routeTable := range resp.RouteTables { | ||
routeTables = append(routeTables, *routeTable.RouteTableId) |
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.
To prevent potential panics, we should prefer to use the SDK helper aws.StringValue(routeTable.RouteTableId)
instead of directly dereferencing via *
} | ||
|
||
d.SetId(d.Get("vpc_id").(string)) | ||
d.Set("ids", routeTables) |
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.
When using d.Set()
with aggregate types (e.g. TypeList/TypeSet/TypeMap) we should perform error checking: https://www.terraform.io/docs/extend/best-practices/detecting-drift.html#error-checking-aggregate-types
if err := d.Set("ids", routeTables); err != nil {
return fmt.Errorf("error setting ids: %s", err)
}
Renamed the files and functions to match.
…an aws_route_table_ids). - Change vpc_id to be optional rather than required attribute. - Update to use aws.StringValue helper rather than directly dereferencing routeTable.RouteTableId - Add check for error when using d.Set with aggregate type.
Hi @bflad thanks for the feedback. I believe I have updated as you requested. please let me know. |
{ | ||
Config: testAccDataSourceAwsRouteTablesConfigWithDataSource(rInt), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.aws_route_tables.selected", "ids.#", "3"), |
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 check is currently failing:
=== RUN TestAccDataSourceAwsRouteTables
--- FAIL: TestAccDataSourceAwsRouteTables (11.13s)
testing.go:518: Step 1 error: Check failed: Check 1/2 error: data.aws_route_tables.selected: Attribute 'ids.#' expected "3", got "4"
If I had to venture a guess, its because VPCs include a default route table in addition to the 3 being created in the test.
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.
@bflad of course you are correct. I've updated the test cases to account for that, now it succeeds in my tests.
jmpb:aws jaukeman$ TF_ACC=1 go test -v -run TestAccDataSourceAwsRouteTables
=== RUN TestAccDataSourceAwsRouteTables
--- PASS: TestAccDataSourceAwsRouteTables (54.93s)
PASS
ok github.com/terraform-providers/terraform-provider-aws/aws 54.974s
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.
Great work, @jesseaukeman! LGTM and I'll fix the one documentation thing on merge. 🚀
1 test passed (all tests)
=== RUN TestAccDataSourceAwsRouteTables
--- PASS: TestAccDataSourceAwsRouteTables (15.34s)
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_route_tables" | ||
sidebar_current: "docs-aws-datasource-route-tables" |
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.
This has been released in version 1.25.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks! |
This allows lookup of a list of route table ids similar to the
existing functionality of the aws_subnet_ids data source.
Changes proposed in this pull request: