From b4cb33e460a094ecd0eb026794f93522bd04ca90 Mon Sep 17 00:00:00 2001 From: Ryan Oaks Date: Wed, 10 Feb 2021 10:15:31 -0500 Subject: [PATCH] Update TGW resource to properly handle failures that occur during create --- ...resource_aws_transit_gateway_attachment.go | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/internal/provider/resource_aws_transit_gateway_attachment.go b/internal/provider/resource_aws_transit_gateway_attachment.go index a533ba6c1..6a0b6a8ac 100644 --- a/internal/provider/resource_aws_transit_gateway_attachment.go +++ b/internal/provider/resource_aws_transit_gateway_attachment.go @@ -179,13 +179,6 @@ func resourceAwsTransitGatewayAttachmentCreate(ctx context.Context, d *schema.Re tgwAtt := createTGWAttachmentResponse.Payload.TgwAttachment - // Wait for TGW attachment creation to complete - if err := clients.WaitForOperation(ctx, client, "create Transit gateway attachment", loc, createTGWAttachmentResponse.Payload.Operation.ID); err != nil { - return diag.Errorf("unable to create Transit gateway attachment (%s) for HVN (%s) and Transit gateway (%s): %v", tgwAtt.ID, tgwAtt.Hvn.ID, tgwAtt.ProviderData.AwsData.TgwID, err) - } - - log.Printf("[INFO] Created Transit gateway attachment (%s) for HVN (%s) and Transit gateway (%s)", tgwAtt.ID, tgwAtt.Hvn.ID, tgwAtt.ProviderData.AwsData.TgwID) - // Set the globally unique id of this TGW attachment in the state now since // it has been created, and from this point forward should be deletable link := newLink(tgwAtt.Location, TgwAttachmentResourceType, tgwAtt.ID) @@ -195,6 +188,13 @@ func resourceAwsTransitGatewayAttachmentCreate(ctx context.Context, d *schema.Re } d.SetId(url) + // Wait for TGW attachment creation to complete + if err := clients.WaitForOperation(ctx, client, "create Transit gateway attachment", loc, createTGWAttachmentResponse.Payload.Operation.ID); err != nil { + return diag.Errorf("unable to create Transit gateway attachment (%s) for HVN (%s) and Transit gateway (%s): %v", tgwAtt.ID, tgwAtt.Hvn.ID, tgwAtt.ProviderData.AwsData.TgwID, err) + } + + log.Printf("[INFO] Created Transit gateway attachment (%s) for HVN (%s) and Transit gateway (%s)", tgwAtt.ID, tgwAtt.Hvn.ID, tgwAtt.ProviderData.AwsData.TgwID) + // Wait for TGW attachment to transition into PENDING_ACCEPTANCE state tgwAtt, err = clients.WaitForTGWAttachmentState(ctx, client, tgwAtt.ID, hvnID, loc, "PENDING_ACCEPTANCE") if err != nil { @@ -234,6 +234,14 @@ func resourceAwsTransitGatewayAttachmentRead(ctx context.Context, d *schema.Reso return diag.Errorf("unable to retrieve Transit gateway attachment (%s): %v", tgwAttID, err) } + // The TGW attachment failed to provision properly so we want to let the user know and + // remove it from state + if tgwAtt.State == networkmodels.HashicorpCloudNetwork20200907TGWAttachmentStateFAILED { + log.Printf("[WARN] Transit gateway attachment (%s) failed to provision, removing from state", tgwAtt.ID) + d.SetId("") + return nil + } + // TGW attachment has been found, update resource data if err := setTransitGatewayAttachmentResourceData(d, tgwAtt); err != nil { return diag.FromErr(err)