Make commands work from list and info pages
[invirt/packages/invirt-web.git] / code / main.py
index d938450..ba892bf 100755 (executable)
@@ -58,10 +58,12 @@ class InvirtWeb(View):
 
     @cherrypy.expose
     @cherrypy.tools.mako(filename="/list.mako")
-    def list(self):
+    def list(self, result=None):
         """Handler for list requests."""
         checkpoint.checkpoint('Getting list dict')
         d = getListDict(cherrypy.request.login, cherrypy.request.state)
+        if result is not None:
+            d['result'] = result
         checkpoint.checkpoint('Got list dict')
         return d
     index=list
@@ -214,6 +216,33 @@ console will suffer artifacts.
                      port=port,
                      authtoken=token)
             return d
+        @cherrypy.expose
+        @cherrypy.tools.mako(filename="/command.mako")
+        def command(self, command_name, machine_id, **kwargs):
+            """Handler for running commands like boot and delete on a VM."""
+            if cherrypy.request.method != "POST":
+                raise InvalidInput("request.method", command_name, "You must execute commands via POST")
+            back = kwargs.get('back', None)
+            try:
+                d = controls.commandResult(cherrypy.request.login, cherrypy.request.state, command_name, machine_id, kwargs)
+                if d['command'] == 'Delete VM':
+                    back = 'list'
+            except InvalidInput, err:
+                if not back:
+                    raise
+                print >> sys.stderr, err
+                result = err
+            else:
+                result = 'Success!'
+                if not back:
+                    return d
+            if back == 'list':
+                cherrypy.request.state.clear() #Changed global state
+                raise cherrypy.InternalRedirect('/list?result=%s' % urllib.quote(result))
+            elif back == 'info':
+                raise cherrypy.HTTPRedirect(cherrypy.request.base + '/machine/%d/' % machine_id, status=303)
+            else:
+                raise InvalidInput('back', back, 'Not a known back page.')
 
     machine = MachineView()
 
@@ -431,35 +460,6 @@ def getDiskInfo(data_dict, machine):
         data_dict['%s_size' % name] = "%0.1f GiB" % (disk.size / 1024.)
     return disk_fields
 
-def command(username, state, path, fields):
-    """Handler for running commands like boot and delete on a VM."""
-    back = fields.getfirst('back')
-    try:
-        d = controls.commandResult(username, state, fields)
-        if d['command'] == 'Delete VM':
-            back = 'list'
-    except InvalidInput, err:
-        if not back:
-            raise
-        print >> sys.stderr, err
-        result = err
-    else:
-        result = 'Success!'
-        if not back:
-            return templates.command(searchList=[d])
-    if back == 'list':
-        state.clear() #Changed global state
-        d = getListDict(username, state)
-        d['result'] = result
-        return templates.list(searchList=[d])
-    elif back == 'info':
-        machine = validation.Validate(username, state, machine_id=fields.getfirst('machine_id')).machine
-        return ({'Status': '303 See Other',
-                 'Location': 'info?machine_id=%d' % machine.machine_id},
-                "You shouldn't see this message.")
-    else:
-        raise InvalidInput('back', back, 'Not a known back page.')
-
 def modifyDict(username, state, fields):
     """Modify a machine as specified by CGI arguments.
 
@@ -651,7 +651,6 @@ def throwError(_, __, ___, ____):
     raise RuntimeError("test of the emergency broadcast system")
 
 mapping = dict(
-               command=command,
                modify=modify,
                create=create,
                unauth=unauthFront,