[Kimchi-devel] [PATCH v2] Issues #682/#684/#685: Change some code errors when start, shutdown or poweroff vms

Ramon Medeiros ramonn at linux.vnet.ibm.com
Thu Aug 20 16:53:54 UTC 2015


Changes:

v2:
Fix pep8 issues

When trying to start a running vm, shutoff or poweroff a shutdown vm,
the retrieved error was 500 (Internal Server Error). A better error
would be 400 (Invalid Request).

Signed-off-by: Ramon Medeiros <ramonn at linux.vnet.ibm.com>
---
 src/kimchi/model/vms.py | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/kimchi/model/vms.py b/src/kimchi/model/vms.py
index 106e9bc..9dfd9fe 100644
--- a/src/kimchi/model/vms.py
+++ b/src/kimchi/model/vms.py
@@ -1105,24 +1105,36 @@ class VMModel(object):
         try:
             dom.create()
         except libvirt.libvirtError as e:
-            raise OperationFailed("KCHVM0019E",
-                                  {'name': name, 'err': e.get_error_message()})
+            # vm already running: return error 403
+            error_params = {'name': name, 'err': e.get_error_message()}
+            if DOM_STATE_MAP[dom.info()[0]] == "running":
+                raise InvalidOperation("KCHVM0019E", error_params)
+            else:
+                raise OperationFailed("KCHVM0019E", error_params)
 
     def poweroff(self, name):
         dom = self.get_vm(name, self.conn)
         try:
             dom.destroy()
         except libvirt.libvirtError as e:
-            raise OperationFailed("KCHVM0020E",
-                                  {'name': name, 'err': e.get_error_message()})
+            # vm already powered off: return error 403
+            error_params = {'name': name, 'err': e.get_error_message()}
+            if DOM_STATE_MAP[dom.info()[0]] == "shutoff":
+                raise InvalidOperation("KCHVM0020E", error_params)
+            else:
+                raise OperationFailed("KCHVM0020E", error_params)
 
     def shutdown(self, name):
         dom = self.get_vm(name, self.conn)
         try:
             dom.shutdown()
         except libvirt.libvirtError as e:
-            raise OperationFailed("KCHVM0029E",
-                                  {'name': name, 'err': e.get_error_message()})
+            # vm already powered off: return error 403
+            error_params = {'name': name, 'err': e.get_error_message()}
+            if DOM_STATE_MAP[dom.info()[0]] == "shutoff":
+                raise InvalidOperation("KCHVM0029E", error_params)
+            else:
+                raise OperationFailed("KCHVM0029E", error_params)
 
     def reset(self, name):
         dom = self.get_vm(name, self.conn)
-- 
2.1.0




More information about the Kimchi-devel mailing list