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

[Enhancement]: add tgw route table in aws_networkmanager_attachment_accepter ✨ #32023

Merged
merged 6 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/32023.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_networkmanager_attachment_accepter: Added support for Transit Gateway route table attachments
```
43 changes: 35 additions & 8 deletions internal/service/networkmanager/attachment_accepter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,10 @@ func ResourceAttachmentAccepter() *schema.Resource {
// querying attachments requires knowing the type ahead of time
// therefore type is required in provider, though not on the API
"attachment_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
networkmanager.AttachmentTypeVpc,
networkmanager.AttachmentTypeSiteToSiteVpn,
networkmanager.AttachmentTypeConnect,
}, false),
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(networkmanager.AttachmentType_Values(), false),
},
"core_network_arn": {
Type: schema.TypeString,
Expand Down Expand Up @@ -123,6 +119,17 @@ func resourceAttachmentAccepterCreate(ctx context.Context, d *schema.ResourceDat

d.SetId(attachmentID)

case networkmanager.AttachmentTypeTransitGatewayRouteTable:
tgwAttachment, err := FindTransitGatewayRouteTableAttachmentByID(ctx, conn, attachmentID)

if err != nil {
return diag.Errorf("reading Network Manager Transit Gateway Route Table Attachment (%s): %s", attachmentID, err)
}

state = aws.StringValue(tgwAttachment.Attachment.State)

d.SetId(attachmentID)

default:
return diag.Errorf("unsupported Network Manager Attachment type: %s", attachmentType)
}
Expand Down Expand Up @@ -153,6 +160,11 @@ func resourceAttachmentAccepterCreate(ctx context.Context, d *schema.ResourceDat
if _, err := waitConnectAttachmentAvailable(ctx, conn, attachmentID, d.Timeout(schema.TimeoutCreate)); err != nil {
return diag.Errorf("waiting for Network Manager Connect Attachment (%s) create: %s", attachmentID, err)
}

case networkmanager.AttachmentTypeTransitGatewayRouteTable:
if _, err := waitTransitGatewayRouteTableAttachmentAvailable(ctx, conn, attachmentID, d.Timeout(schema.TimeoutCreate)); err != nil {
return diag.Errorf("waiting for Network Manager Transit Gateway Route Table Attachment (%s) create: %s", attachmentID, err)
}
}
}

Expand Down Expand Up @@ -209,6 +221,21 @@ func resourceAttachmentAccepterRead(ctx context.Context, d *schema.ResourceData,
}

a = connectAttachment.Attachment

case networkmanager.AttachmentTypeTransitGatewayRouteTable:
tgwAttachment, err := FindTransitGatewayRouteTableAttachmentByID(ctx, conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Network Manager Transit Gateway Route Table Attachment %s not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return diag.Errorf("reading Network Manager Transit Gateway Route Table Attachment (%s): %s", d.Id(), err)
}

a = tgwAttachment.Attachment
}

d.Set("attachment_policy_rule_number", a.AttachmentPolicyRuleNumber)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,17 @@ resource "aws_networkmanager_global_network" "test" {

resource "aws_networkmanager_core_network" "test" {
global_network_id = aws_networkmanager_global_network.test.id
policy_document = data.aws_networkmanager_core_network_policy_document.test.json

tags = {
Name = %[1]q
}
}

resource "aws_networkmanager_core_network_policy_attachment" "test" {
core_network_id = aws_networkmanager_core_network.test.id
policy_document = data.aws_networkmanager_core_network_policy_document.test.json
}

data "aws_networkmanager_core_network_policy_document" "test" {
core_network_configuration {
# Don't overlap with default TGW ASN: 64512.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func FindTransitGatewayRouteTableAttachmentByID(ctx context.Context, conn *netwo
return output.TransitGatewayRouteTableAttachment, nil
}

func StatusTransitGatewayRouteTableAttachmentState(ctx context.Context, conn *networkmanager.NetworkManager, id string) retry.StateRefreshFunc {
func statusTransitGatewayRouteTableAttachmentState(ctx context.Context, conn *networkmanager.NetworkManager, id string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindTransitGatewayRouteTableAttachmentByID(ctx, conn, id)

Expand All @@ -239,7 +239,7 @@ func waitTransitGatewayRouteTableAttachmentCreated(ctx context.Context, conn *ne
Pending: []string{networkmanager.AttachmentStateCreating, networkmanager.AttachmentStatePendingNetworkUpdate},
Target: []string{networkmanager.AttachmentStateAvailable, networkmanager.AttachmentStatePendingAttachmentAcceptance},
Timeout: timeout,
Refresh: StatusTransitGatewayRouteTableAttachmentState(ctx, conn, id),
Refresh: statusTransitGatewayRouteTableAttachmentState(ctx, conn, id),
}

outputRaw, err := stateConf.WaitForStateContext(ctx)
Expand All @@ -256,7 +256,7 @@ func waitTransitGatewayRouteTableAttachmentDeleted(ctx context.Context, conn *ne
Pending: []string{networkmanager.AttachmentStateDeleting},
Target: []string{},
Timeout: timeout,
Refresh: StatusTransitGatewayRouteTableAttachmentState(ctx, conn, id),
Refresh: statusTransitGatewayRouteTableAttachmentState(ctx, conn, id),
NotFoundChecks: 1,
}

Expand All @@ -268,3 +268,20 @@ func waitTransitGatewayRouteTableAttachmentDeleted(ctx context.Context, conn *ne

return nil, err
}

func waitTransitGatewayRouteTableAttachmentAvailable(ctx context.Context, conn *networkmanager.NetworkManager, id string, timeout time.Duration) (*networkmanager.TransitGatewayRouteTableAttachment, error) {
stateConf := &retry.StateChangeConf{
Pending: []string{networkmanager.AttachmentStateCreating, networkmanager.AttachmentStatePendingAttachmentAcceptance, networkmanager.AttachmentStatePendingNetworkUpdate},
Target: []string{networkmanager.AttachmentStateAvailable},
Timeout: timeout,
Refresh: statusTransitGatewayRouteTableAttachmentState(ctx, conn, id),
}

outputRaw, err := stateConf.WaitForStateContext(ctx)

if output, ok := outputRaw.(*networkmanager.TransitGatewayRouteTableAttachment); ok {
return output, err
}

return nil, err
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ resource "aws_networkmanager_transit_gateway_peering" "test" {
Name = %[1]q
}

depends_on = [aws_ec2_transit_gateway_policy_table.test]
depends_on = [aws_ec2_transit_gateway_policy_table.test, aws_networkmanager_core_network_policy_attachment.test]
}

resource "aws_ec2_transit_gateway_route_table" "test" {
Expand All @@ -209,6 +209,11 @@ resource "aws_networkmanager_transit_gateway_route_table_attachment" "test" {

depends_on = [aws_ec2_transit_gateway_policy_table_association.test]
}

resource "aws_networkmanager_attachment_accepter" "test" {
attachment_id = aws_networkmanager_transit_gateway_route_table_attachment.test.id
attachment_type = aws_networkmanager_transit_gateway_route_table_attachment.test.attachment_type
}
`)
}

Expand All @@ -224,6 +229,11 @@ resource "aws_networkmanager_transit_gateway_route_table_attachment" "test" {

depends_on = [aws_ec2_transit_gateway_policy_table_association.test]
}

resource "aws_networkmanager_attachment_accepter" "test" {
attachment_id = aws_networkmanager_transit_gateway_route_table_attachment.test.id
attachment_type = aws_networkmanager_transit_gateway_route_table_attachment.test.attachment_type
}
`, tagKey1, tagValue1))
}

Expand All @@ -240,5 +250,10 @@ resource "aws_networkmanager_transit_gateway_route_table_attachment" "test" {

depends_on = [aws_ec2_transit_gateway_policy_table_association.test]
}

resource "aws_networkmanager_attachment_accepter" "test" {
attachment_id = aws_networkmanager_transit_gateway_route_table_attachment.test.id
attachment_type = aws_networkmanager_transit_gateway_route_table_attachment.test.attachment_type
}
`, tagKey1, tagValue1, tagKey2, tagValue2))
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ resource "aws_networkmanager_attachment_accepter" "test" {
The following arguments are required:

- `attachment_id` - (Required) The ID of the attachment.
- `attachment_type` - The type of attachment. Valid values can be found in the [AWS Documentation](https://docs.aws.amazon.com/networkmanager/latest/APIReference/API_ListAttachments.html#API_ListAttachments_RequestSyntax)
- `attachment_type` - (Required) The type of attachment. Valid values can be found in the [AWS Documentation](https://docs.aws.amazon.com/networkmanager/latest/APIReference/API_ListAttachments.html#API_ListAttachments_RequestSyntax)

## Attributes Reference

Expand Down