Skip to content

Commit

Permalink
improve delete vm the same way I did it for nic
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Gatto <[email protected]>
  • Loading branch information
outscale-mgo committed Dec 11, 2023
1 parent e4751de commit 72f6249
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions outscale/resource_outscale_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 72f6249

Please sign in to comment.