Skip to content

Commit

Permalink
Merge pull request #11152 from hashicorp/b-aws-route-table-11134
Browse files Browse the repository at this point in the history
provider/aws: Fix panic caused by main route table lookup
  • Loading branch information
grubernaut authored Jan 11, 2017
2 parents f895f93 + 7399401 commit 3de923d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
5 changes: 4 additions & 1 deletion builtin/providers/aws/data_source_aws_route_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ func dataSourceAssociationsRead(ec2Assocations []*ec2.RouteTableAssociation) []m
m := make(map[string]interface{})
m["route_table_id"] = *a.RouteTableId
m["route_table_association_id"] = *a.RouteTableAssociationId
m["subnet_id"] = *a.SubnetId
// GH[11134]
if a.SubnetId != nil {
m["subnet_id"] = *a.SubnetId
}
m["main"] = *a.Main
associations = append(associations, m)
}
Expand Down
57 changes: 56 additions & 1 deletion builtin/providers/aws/data_source_aws_route_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ func TestAccDataSourceAwsRouteTable(t *testing.T) {
})
}

func TestAccDataSourceAwsRouteTable_main(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataSourceAwsRouteTableMainRoute,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsRouteTableCheckMain("data.aws_route_table.by_filter"),
),
},
},
})
}

func testAccDataSourceAwsRouteTableCheck(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
Expand Down Expand Up @@ -68,7 +83,7 @@ func testAccDataSourceAwsRouteTableCheck(name string) resource.TestCheckFunc {
}
if attr["associations.0.subnet_id"] != subnetRs.Primary.Attributes["id"] {
return fmt.Errorf(
"subnet_id is %v; want %s",
"subnet_id is %v; want %s",
attr["associations.0.subnet_id"],
subnetRs.Primary.Attributes["id"],
)
Expand All @@ -78,6 +93,32 @@ func testAccDataSourceAwsRouteTableCheck(name string) resource.TestCheckFunc {
}
}

func testAccDataSourceAwsRouteTableCheckMain(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]

if !ok {
return fmt.Errorf("root module has no resource called %s", name)
}

attr := rs.Primary.Attributes

// Verify attributes are set
if _, ok := attr["id"]; !ok {
return fmt.Errorf("id not set for main route table")
}
if _, ok := attr["vpc_id"]; !ok {
return fmt.Errorf("vpc_id not set for main route table")
}
// Verify it's actually the main route table that's returned
if attr["associations.0.main"] != "true" {
return fmt.Errorf("main route table not found")
}

return nil
}
}

const testAccDataSourceAwsRouteTableGroupConfig = `
provider "aws" {
region = "eu-central-1"
Expand Down Expand Up @@ -130,3 +171,17 @@ data "aws_route_table" "by_subnet" {
}
`

// Uses us-east-2, as region only has a single main route table
const testAccDataSourceAwsRouteTableMainRoute = `
provider "aws" {
region = "us-east-2"
}
data "aws_route_table" "by_filter" {
filter {
name = "association.main"
values = ["true"]
}
}
`

0 comments on commit 3de923d

Please sign in to comment.