Skip to content
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
merged 4 commits into from
Jun 26, 2018
Merged

Added data source for aws_route_tables #4841

merged 4 commits into from
Jun 26, 2018

Conversation

jesseaukeman
Copy link
Contributor

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:

  • add a new data source aws_route_table_ids

This allow lookup of a list of route table ids similar to the
existing aws_subnet_ids data source.
@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Jun 15, 2018
@bflad bflad added new-data-source Introduces a new data source. service/ec2 Issues and PRs that pertain to the ec2 service. labels Jun 15, 2018
Copy link
Contributor

@bflad bflad left a 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(),
Copy link
Contributor

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() and dataSourceAwsRouteTablesRead()

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,
Copy link
Contributor

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)) to d.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)
Copy link
Contributor

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)
Copy link
Contributor

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)
}

@bflad bflad added the waiting-response Maintainers are waiting on response from community or contributor. label Jun 26, 2018
…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.
@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Jun 26, 2018
@jesseaukeman
Copy link
Contributor Author

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"),
Copy link
Contributor

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.

Copy link
Contributor Author

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

@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Jun 26, 2018
@bflad bflad removed the waiting-response Maintainers are waiting on response from community or contributor. label Jun 26, 2018
@bflad bflad added this to the v1.25.0 milestone Jun 26, 2018
Copy link
Contributor

@bflad bflad left a 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"
Copy link
Contributor

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.

@bflad bflad merged commit 1ebf18d into hashicorp:master Jun 26, 2018
@bflad bflad changed the title Added data source for aws_route_table_ids Added data source for aws_route_tables Jun 26, 2018
bflad added a commit that referenced this pull request Jun 26, 2018
@bflad
Copy link
Contributor

bflad commented Jun 27, 2018

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.

@jesseaukeman jesseaukeman deleted the add-data-source-aws-route-table-ids branch June 27, 2018 14:03
@ghost
Copy link

ghost commented Apr 5, 2020

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!

@ghost ghost locked and limited conversation to collaborators Apr 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
new-data-source Introduces a new data source. service/ec2 Issues and PRs that pertain to the ec2 service. size/L Managed by automation to categorize the size of a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants