Skip to content

Commit

Permalink
Merge pull request #7117 from terraform-providers/b-aws_ec2_transit_g…
Browse files Browse the repository at this point in the history
…ateway-attachment-panic

resource/aws_ec2_transit_gateway_route: Prevent crash with externally removed attachment
  • Loading branch information
bflad authored Jan 11, 2019
2 parents c308ad1 + 804644d commit 2252993
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 2252993

Please sign in to comment.