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 remove_current_attachment_association attribute to ec2_transit_gateway_route_table_association #31452

Merged
3 changes: 3 additions & 0 deletions .changelog/31452.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/ec2_transit_gateway_route_table_association: added `remove_current_attachment_association` attribute to allow for disassociating a transit gateways currently associated route table before associating the new route table
```
49 changes: 49 additions & 0 deletions internal/service/ec2/transitgateway_route_table_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func ResourceTransitGatewayRouteTableAssociation() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
"remove_current_attachment_association": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
},
},
}
}
Expand All @@ -60,6 +66,15 @@ func resourceTransitGatewayRouteTableAssociationCreate(ctx context.Context, d *s
transitGatewayAttachmentID := d.Get("transit_gateway_attachment_id").(string)
transitGatewayRouteTableID := d.Get("transit_gateway_route_table_id").(string)
id := TransitGatewayRouteTableAssociationCreateResourceID(transitGatewayRouteTableID, transitGatewayAttachmentID)

removeCurrentAttachmentAssociation := d.Get("remove_current_attachment_association").(bool)
if removeCurrentAttachmentAssociation {
err := transitGatewayDisassociateLinkedRouteTable(ctx, conn, transitGatewayAttachmentID)
if err != nil {
return sdkdiag.AppendErrorf(diags, "disassociating existing EC2 Transit Gateway Route Table from Gateway Attachment (%s %s): %s", transitGatewayAttachmentID, transitGatewayRouteTableID, err)
}
}

input := &ec2.AssociateTransitGatewayRouteTableInput{
TransitGatewayAttachmentId: aws.String(transitGatewayAttachmentID),
TransitGatewayRouteTableId: aws.String(transitGatewayRouteTableID),
Expand Down Expand Up @@ -200,6 +215,40 @@ func transitGatewayRouteTableAssociationUpdate(ctx context.Context, conn *ec2.EC
return nil
}

// transitGatewayDisassociateLinkedRouteTable is used to disassociate the currently linked route table to a Gateway Attachment without knowledge of the Route Table ID.
func transitGatewayDisassociateLinkedRouteTable(ctx context.Context, conn *ec2.EC2, transitGatewayAttachmentID string) error {
transitGatewayAttachment, err := FindTransitGatewayAttachmentByID(ctx, conn, transitGatewayAttachmentID)
if err != nil {
return err
}

if transitGatewayAttachment.Association == nil {
return nil // If no Association object was found then Gateway Attachment is not linked to a Route Table
}
transitGatewayRouteTableID := *transitGatewayAttachment.Association.TransitGatewayRouteTableId

if *transitGatewayAttachment.Association.State != ec2.AssociationStatusCodeAssociated {
return fmt.Errorf("associated Route Table with Transit Gateway Attachment is not in correct state (%s): expected attachment in %s state got %s", transitGatewayAttachmentID, ec2.AssociationStatusCodeAssociated, *transitGatewayAttachment.Association.State)
}

disassociateInput := &ec2.DisassociateTransitGatewayRouteTableInput{
TransitGatewayAttachmentId: aws.String(transitGatewayAttachmentID),
TransitGatewayRouteTableId: aws.String(transitGatewayRouteTableID),
}

_, err = conn.DisassociateTransitGatewayRouteTableWithContext(ctx, disassociateInput)
if err != nil {
return fmt.Errorf("failed to disassociate Route Table with Transit Gateway Attachment (%s %s): %w", transitGatewayRouteTableID, transitGatewayAttachmentID, err)
}

_, err = WaitTransitGatewayRouteTableAssociationDeleted(ctx, conn, transitGatewayRouteTableID, transitGatewayAttachmentID)
if err != nil {
return fmt.Errorf("waiting for EC2 Transit Gateway Route Table Association (%s %s) delete: %s", transitGatewayRouteTableID, transitGatewayAttachmentID, err)
}

return nil
}

const transitGatewayRouteTableAssociationIDSeparator = "_"

func TransitGatewayRouteTableAssociationCreateResourceID(transitGatewayRouteTableID, transitGatewayAttachmentID string) string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func testAccTransitGatewayRouteTableAssociation_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "resource_type"),
resource.TestCheckResourceAttrPair(resourceName, "transit_gateway_attachment_id", transitGatewayVpcAttachmentResourceName, "id"),
resource.TestCheckResourceAttrPair(resourceName, "transit_gateway_route_table_id", transitGatewayRouteTableResourceName, "id"),
resource.TestCheckResourceAttrSet(resourceName, "remove_current_attachment_association"),
),
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The following arguments are supported:

* `transit_gateway_attachment_id` - (Required) Identifier of EC2 Transit Gateway Attachment.
* `transit_gateway_route_table_id` - (Required) Identifier of EC2 Transit Gateway Route Table.
* `remove_current_attachment_association` - (Optional) Boolean whether the Gateway Attachment should remove its current Route Table association before associating with the new Route Table. Default value: `false`.

## Attributes Reference

Expand Down