From 72f624976d1405ba379d8c4ce162f94c56bd0c1b Mon Sep 17 00:00:00 2001 From: Matthias Gatto Date: Tue, 28 Nov 2023 18:51:29 +0100 Subject: [PATCH] improve delete vm the same way I did it for nic Signed-off-by: Matthias Gatto --- outscale/resource_outscale_vm.go | 38 +++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/outscale/resource_outscale_vm.go b/outscale/resource_outscale_vm.go index abf3f51ed..f680ee77a 100644 --- a/outscale/resource_outscale_vm.go +++ b/outscale/resource_outscale_vm.go @@ -1120,9 +1120,41 @@ func resourceOAPIVMDelete(d *schema.ResourceData, meta interface{}) error { log.Printf("[DEBUG] Waiting for VM (%s) to become terminated", id) stateConf := &resource.StateChangeConf{ - Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"}, - Target: []string{"terminated"}, - Refresh: vmStateRefreshFunc(conn, id, ""), + Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"}, + Target: []string{"terminated"}, + Refresh: func() (interface{}, string, error) { + var resp oscgo.ReadVmsResponse + err := resource.Retry(30*time.Second, func() *resource.RetryError { + rp, httpResp, err := conn.VmApi.ReadVms(context.Background()).ReadVmsRequest(oscgo.ReadVmsRequest{ + Filters: &oscgo.FiltersVm{ + VmIds: &[]string{id}, + }, + }).Execute() + if err != nil { + return utils.CheckThrottling(httpResp, err) + } + resp = rp + return nil + }) + if err != nil { + log.Printf("[ERROR] error on InstanceStateRefresh: %s", err) + return nil, "", err + } + + if !resp.HasVms() || len(resp.GetVms()) < 1 { + return resp, "terminated", nil + } + + vm := resp.GetVms()[0] + state := vm.GetState() + + if state != "terminated" { + return vm, state, fmt.Errorf("Failed to reach target state. Reason: %v", *vm.State) + + } + + return vm, state, nil + }, Timeout: d.Timeout(schema.TimeoutDelete), Delay: 10 * time.Second, MinTimeout: 3 * time.Second,