allow Failed VM to be stoped with --force
@@ -598,13 +598,19 @@ func (app *SubresourceAPIApp) StopVMRequestHandler(request *restful.Request, res
if !errors.IsNotFound(err) {
writeError(errors.NewInternalError(err), response)
return
- } else {
- writeError(errors.NewConflict(v1.Resource("virtualmachine"), name, fmt.Errorf("VM is not running")), response)
+ }
+
+ writeError(errors.NewConflict(v1.Resource("virtualmachine"), name, fmt.Errorf("VM has no associated VMI running")), response)
return
}
+
+ if vmi == nil || vmi.Status.Phase == v1.Succeeded || vmi.Status.Phase == v1.Unknown || vmi.Status.Phase == v1.VmPhaseUnset {
+ writeError(errors.NewConflict(v1.Resource("virtualmachine"), name, fmt.Errorf("VM has status %q and is not running", vmi.Status.Phase)), response)
+ return
}
- if vmi == nil || vmi.IsFinal() || vmi.Status.Phase == v1.Unknown || vmi.Status.Phase == v1.VmPhaseUnset {
- writeError(errors.NewConflict(v1.Resource("virtualmachine"), name, fmt.Errorf("VM is not running")), response)
+
+ if (vmi.Status.Phase == v1.Failed && bodyStruct.GracePeriod == nil) || (vmi.Status.Phase == v1.Failed && *bodyStruct.GracePeriod != 0) {
+ writeError(errors.NewConflict(v1.Resource("virtualmachine"), name, fmt.Errorf("VM is Failed. Use `--force --grace-period=0` to stop the VM")), response)
return
}
