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

Set resource id before polling operation and re-create failed deployments #59

Merged
merged 2 commits into from
Feb 9, 2021
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
22 changes: 15 additions & 7 deletions internal/provider/resource_aws_network_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,6 @@ func resourceAwsNetworkPeeringCreate(ctx context.Context, d *schema.ResourceData

peering := peeringResponse.Payload.Peering

// Wait for Network peering to be created
if err := clients.WaitForOperation(ctx, client, "create Network peering", loc, peeringResponse.Payload.Operation.ID); err != nil {
return diag.Errorf("unable to create Network peering (%s) between HVN (%s) and peer (%s): %v", peering.ID, peering.Hvn.ID, peering.Target.AwsTarget.VpcID, err)
}

log.Printf("[INFO] Created Network peering (%s) between HVN (%s) and peer (%s)", peering.ID, peering.Hvn.ID, peering.Target.AwsTarget.VpcID)

// Set the globally unique id of this peering in the state now since it has
// been created, and from this point forward should be deletable
link := newLink(peering.Hvn.Location, PeeringResourceType, peering.ID)
Expand All @@ -197,6 +190,13 @@ func resourceAwsNetworkPeeringCreate(ctx context.Context, d *schema.ResourceData
}
d.SetId(url)

// Wait for Network peering to be created
if err := clients.WaitForOperation(ctx, client, "create Network peering", loc, peeringResponse.Payload.Operation.ID); err != nil {
return diag.Errorf("unable to create Network peering (%s) between HVN (%s) and peer (%s): %v", peering.ID, peering.Hvn.ID, peering.Target.AwsTarget.VpcID, err)
}

log.Printf("[INFO] Created Network peering (%s) between HVN (%s) and peer (%s)", peering.ID, peering.Hvn.ID, peering.Target.AwsTarget.VpcID)

peering, err = clients.WaitForPeeringToBePendingAcceptance(ctx, client, peering.ID, hvnID, loc)
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -235,6 +235,14 @@ func resourceAwsNetworkPeeringRead(ctx context.Context, d *schema.ResourceData,
return diag.Errorf("unable to retrieve Network peering (%s): %v", peeringID, err)
}

// The Network peering failed to provision properly so we want to let the user know and
// remove it from state
if peering.State == networkmodels.HashicorpCloudNetwork20200907PeeringStateFAILED {
log.Printf("[WARN] network peering (%s) failed to provision, removing from state", peering.ID)
d.SetId("")
return nil
}

// Network peering found, update resource data
if err := setPeeringResourceData(d, peering); err != nil {
return diag.FromErr(err)
Expand Down
22 changes: 15 additions & 7 deletions internal/provider/resource_consul_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,6 @@ func resourceConsulClusterCreate(ctx context.Context, d *schema.ResourceData, me
return diag.Errorf("unable to create Consul cluster (%s): %v", clusterID, err)
}

// wait for the Consul cluster to be created
if err := clients.WaitForOperation(ctx, client, "create Consul cluster", loc, payload.Operation.ID); err != nil {
return diag.Errorf("unable to create Consul cluster (%s): %v", payload.Cluster.ID, err)
}

log.Printf("[INFO] Created Consul cluster (%s)", payload.Cluster.ID)

link := newLink(loc, ConsulClusterResourceType, clusterID)
url, err := linkURL(link)
if err != nil {
Expand All @@ -296,6 +289,13 @@ func resourceConsulClusterCreate(ctx context.Context, d *schema.ResourceData, me

d.SetId(url)

// wait for the Consul cluster to be created
if err := clients.WaitForOperation(ctx, client, "create Consul cluster", loc, payload.Operation.ID); err != nil {
return diag.Errorf("unable to create Consul cluster (%s): %v", payload.Cluster.ID, err)
}

log.Printf("[INFO] Created Consul cluster (%s)", payload.Cluster.ID)

// get the created Consul cluster
cluster, err := clients.GetConsulClusterByID(ctx, client, loc, payload.Cluster.ID)
if err != nil {
Expand Down Expand Up @@ -444,6 +444,14 @@ func resourceConsulClusterRead(ctx context.Context, d *schema.ResourceData, meta
return diag.Errorf("unable to fetch Consul cluster (%s): %v", clusterID, err)
}

// The Consul cluster failed to provision properly so we want to let the user know and
// remove it from state
if cluster.State == consulmodels.HashicorpCloudConsul20200826ClusterStateFAILED {
log.Printf("[WARN] Consul cluster (%s) failed to provision, removing from state", clusterID)
d.SetId("")
return nil
}

// get the cluster's Consul client config files
clientConfigFiles, err := clients.GetConsulClientConfigFiles(ctx, client, loc, clusterID)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions internal/provider/resource_consul_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func resourceConsulSnapshotRead(ctx context.Context, d *schema.ResourceData, met
snapshotID := snapshotLink.ID
loc := snapshotLink.Location

snapshot, err := clients.GetSnapshotByID(ctx, client, loc, snapshotID)
snapshotResp, err := clients.GetSnapshotByID(ctx, client, loc, snapshotID)
if err != nil {
if clients.IsResponseCodeNotFound(err) {
log.Printf("[WARN] Consul snapshot (%s) not found, removing from state", snapshotID)
Expand All @@ -167,7 +167,15 @@ func resourceConsulSnapshotRead(ctx context.Context, d *schema.ResourceData, met
return diag.Errorf("unable to fetch Consul snapshot (%s): %v", snapshotID, err)
}

if err := setConsulSnapshotResourceData(d, snapshot.Snapshot); err != nil {
// The Consul snapshot failed to provision properly so we want to let the user know and
// remove it from state
if snapshotResp.Snapshot.State == consulmodels.HashicorpCloudConsul20200826SnapshotSnapshotStateCREATINGFAILED {
log.Printf("[WARN] Consul snapshot (%s) failed to provision, removing from state", snapshotResp.Snapshot.ID)
d.SetId("")
return nil
}

if err := setConsulSnapshotResourceData(d, snapshotResp.Snapshot); err != nil {
return diag.FromErr(err)
}

Expand Down
22 changes: 15 additions & 7 deletions internal/provider/resource_hvn.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,20 @@ func resourceHvnCreate(ctx context.Context, d *schema.ResourceData, meta interfa
return diag.Errorf("unable to create HVN (%s): %v", hvnID, err)
}

// Wait for HVN to be created
if err := clients.WaitForOperation(ctx, client, "create HVN", loc, createNetworkResponse.Payload.Operation.ID); err != nil {
return diag.Errorf("unable to create HVN (%s): %v", createNetworkResponse.Payload.Network.ID, err)
}

log.Printf("[INFO] Created HVN (%s)", createNetworkResponse.Payload.Network.ID)

link := newLink(loc, HvnResourceType, hvnID)
url, err := linkURL(link)
if err != nil {
return diag.FromErr(err)
}
d.SetId(url)

// Wait for HVN to be created
if err := clients.WaitForOperation(ctx, client, "create HVN", loc, createNetworkResponse.Payload.Operation.ID); err != nil {
return diag.Errorf("unable to create HVN (%s): %v", createNetworkResponse.Payload.Network.ID, err)
}

log.Printf("[INFO] Created HVN (%s)", createNetworkResponse.Payload.Network.ID)

// Get the updated HVN
hvn, err := clients.GetHvnByID(ctx, client, loc, createNetworkResponse.Payload.Network.ID)
if err != nil {
Expand Down Expand Up @@ -192,6 +192,14 @@ func resourceHvnRead(ctx context.Context, d *schema.ResourceData, meta interface
return diag.Errorf("unable to retrieve HVN (%s): %v", hvnID, err)
}

// The HVN failed to provision properly so we want to let the user know and remove it from
// state
if hvn.State == networkmodels.HashicorpCloudNetwork20200907NetworkStateFAILED {
log.Printf("[WARN] HVN (%s) failed to provision, removing from state", hvnID)
d.SetId("")
return nil
}

// HVN found, update resource data
if err := setHvnResourceData(d, hvn); err != nil {
return diag.FromErr(err)
Expand Down