Skip to content

Commit

Permalink
Update TGW resource to properly handle failures that occur during create
Browse files Browse the repository at this point in the history
  • Loading branch information
roaks3 committed Feb 10, 2021
1 parent c1d4273 commit b4cb33e
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions internal/provider/resource_aws_transit_gateway_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b4cb33e

Please sign in to comment.