Skip to content

Commit

Permalink
resource/aws_ec2_transit_gateway_route: Prevent crash with externally…
Browse files Browse the repository at this point in the history
… removed attachment

Reference: #7111

Previous output from acceptance testing:

```
=== CONT  TestAccAWSEc2TransitGatewayRoute_disappears_TransitGatewayAttachment
panic: runtime error: index out of range

goroutine 1025 [running]:
github.com/terraform-providers/terraform-provider-aws/aws.resourceAwsEc2TransitGatewayRouteRead(0xc0007f84d0, 0x42c6e00, 0xc000904700, 0xc0007f84d0, 0x0)
	/Users/bflad/go/src/github.com/terraform-providers/terraform-provider-aws/aws/resource_aws_ec2_transit_gateway_route.go:128 +0x8e6
github.com/terraform-providers/terraform-provider-aws/vendor/github.com/hashicorp/terraform/helper/schema.(*Resource).Refresh(0xc0004f8b60, 0xc0009630e0, 0x42c6e00, 0xc000904700, 0xc000547cb0, 0xc00071cf01, 0xc00074c550)
	/Users/bflad/go/src/github.com/terraform-providers/terraform-provider-aws/vendor/github.com/hashicorp/terraform/helper/schema/resource.go:352 +0x160
```

Output from acceptance testing:

```
--- PASS: TestAccAWSEc2TransitGatewayRoute_disappears (310.87s)
--- PASS: TestAccAWSEc2TransitGatewayRoute_basic (314.08s)
--- PASS: TestAccAWSEc2TransitGatewayRoute_disappears_TransitGatewayAttachment (336.06s)
```
  • Loading branch information
bflad committed Jan 11, 2019
1 parent c308ad1 commit 804644d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion aws/resource_aws_ec2_transit_gateway_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func resourceAwsEc2TransitGatewayRouteRead(d *schema.ResourceData, meta interfac
d.Set("destination_cidr_block", transitGatewayRoute.DestinationCidrBlock)

d.Set("transit_gateway_attachment_id", "")
if len(transitGatewayRoute.TransitGatewayAttachments) == 0 || transitGatewayRoute.TransitGatewayAttachments[0] != nil {
if len(transitGatewayRoute.TransitGatewayAttachments) > 0 && transitGatewayRoute.TransitGatewayAttachments[0] != nil {
d.Set("transit_gateway_attachment_id", transitGatewayRoute.TransitGatewayAttachments[0].TransitGatewayAttachmentId)
}

Expand Down
27 changes: 27 additions & 0 deletions aws/resource_aws_ec2_transit_gateway_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ func TestAccAWSEc2TransitGatewayRoute_disappears(t *testing.T) {
})
}

func TestAccAWSEc2TransitGatewayRoute_disappears_TransitGatewayAttachment(t *testing.T) {
var transitGateway1 ec2.TransitGateway
var transitGatewayRoute1 ec2.TransitGatewayRoute
var transitGatewayVpcAttachment1 ec2.TransitGatewayVpcAttachment
resourceName := "aws_ec2_transit_gateway_route.test"
transitGatewayVpcAttachmentResourceName := "aws_ec2_transit_gateway_vpc_attachment.test"
transitGatewayResourceName := "aws_ec2_transit_gateway.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSEc2TransitGateway(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEc2TransitGatewayRouteDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEc2TransitGatewayRouteConfigDestinationCidrBlock(),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEc2TransitGatewayExists(transitGatewayResourceName, &transitGateway1),
testAccCheckAWSEc2TransitGatewayRouteExists(resourceName, &transitGatewayRoute1),
testAccCheckAWSEc2TransitGatewayVpcAttachmentExists(transitGatewayVpcAttachmentResourceName, &transitGatewayVpcAttachment1),
testAccCheckAWSEc2TransitGatewayVpcAttachmentDisappears(&transitGatewayVpcAttachment1),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckAWSEc2TransitGatewayRouteExists(resourceName string, transitGatewayRoute *ec2.TransitGatewayRoute) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
Expand Down

0 comments on commit 804644d

Please sign in to comment.