Skip to content

Commit

Permalink
Try recovering form HTTP 50x errors
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Lo-A-Foe <[email protected]>
  • Loading branch information
loafoe committed Jan 27, 2021
1 parent f5e4c0f commit b4c99a6
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions hsdp/resource_container_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,30 @@ func resourceContainerHostCreate(ctx context.Context, d *schema.ResourceData, m
cartel.Tags(tags),
cartel.InSubnet(subnet),
)
instanceID := ""
ipAddress := ""
if err != nil {
if resp == nil {
return diag.FromErr(fmt.Errorf("create error (resp=nil): %w", err))
}
if ch == nil {
return diag.FromErr(fmt.Errorf("create error (instance=nil): %w", err))
}
return diag.FromErr(fmt.Errorf("create error (description=[%s]): %w", ch.Description, err))
if resp.StatusCode >= 500 { // Possible 504, or other timeout, try to recover anyway!
if details := findInstanceByName(client, tagName); details != nil {
instanceID = details.InstanceID
ipAddress = details.PrivateAddress
} else {
return diag.FromErr(fmt.Errorf("create error (status=%d): %w", resp.StatusCode, err))
}
} else {
return diag.FromErr(fmt.Errorf("create error (description=[%s]): %w", ch.Description, err))
}
} else {
instanceID = ch.InstanceID()
ipAddress = ch.IPAddress()
}
d.SetId(ch.InstanceID())
d.SetId(instanceID)

stateConf := &resource.StateChangeConf{
Pending: []string{"provisioning", "indeterminate"},
Expand All @@ -356,10 +370,10 @@ func resourceContainerHostCreate(ctx context.Context, d *schema.ResourceData, m
}
d.SetConnInfo(map[string]string{
"type": "ssh",
"host": ch.IPAddress(),
"host": ipAddress,
})
// Collect SSH details
privateIP := ch.IPAddress()
privateIP := ipAddress
ssh := &easyssh.MakeConfig{
User: user,
Server: privateIP,
Expand Down Expand Up @@ -396,6 +410,19 @@ func resourceContainerHostCreate(ctx context.Context, d *schema.ResourceData, m
return append(diags, readDiags...)
}

func findInstanceByName(client *cartel.Client, name string) *cartel.InstanceDetails {
instances, _, err := client.GetAllInstances()
if err != nil {
return nil
}
for _, i := range *instances {
if i.NameTag == name {
return &i
}
}
return nil
}

func copyFiles(ssh *easyssh.MakeConfig, config *Config, createFiles []provisionFile) error {
for _, f := range createFiles {
if f.Source != "" {
Expand Down

0 comments on commit b4c99a6

Please sign in to comment.