From b4c99a60049ca2d3a13569aca2b028a2f2fc9197 Mon Sep 17 00:00:00 2001 From: Andy Lo-A-Foe Date: Wed, 27 Jan 2021 22:31:20 +0100 Subject: [PATCH] Try recovering form HTTP 50x errors Signed-off-by: Andy Lo-A-Foe --- hsdp/resource_container_host.go | 35 +++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/hsdp/resource_container_host.go b/hsdp/resource_container_host.go index e755fd18..396c4026 100644 --- a/hsdp/resource_container_host.go +++ b/hsdp/resource_container_host.go @@ -327,6 +327,8 @@ 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)) @@ -334,9 +336,21 @@ func resourceContainerHostCreate(ctx context.Context, d *schema.ResourceData, m 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"}, @@ -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, @@ -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 != "" {