]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard_v2: pass json body keys as args to REST resource methods
authorRicardo Dias <rdias@suse.com>
Wed, 24 Jan 2018 17:08:04 +0000 (17:08 +0000)
committerRicardo Dias <rdias@suse.com>
Mon, 5 Mar 2018 13:06:59 +0000 (13:06 +0000)
Signed-off-by: Ricardo Dias <rdias@suse.com>
src/pybind/mgr/dashboard_v2/restresource.py

index ecbd5c4b1d4b86861eaf8fabd289258e48cc1852..95bb9a47c1c9b8bad819bd63d22066f167d9452e 100644 (file)
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+import inspect
 import json
 
 import cherrypy
@@ -8,12 +10,26 @@ def _takes_json(func):
         content_length = int(cherrypy.request.headers['Content-Length'])
         body = cherrypy.request.body.read(content_length)
         if not body:
-            raise cherrypy.HTTPError(400, 'Empty body. Content-Length={}'.format(content_length))
+            raise cherrypy.HTTPError(400, 'Empty body. Content-Length={}'
+                                          .format(content_length))
         try:
             data = json.loads(body.decode('utf-8'))
         except Exception as e:
-            raise cherrypy.HTTPError(400, 'Failed to decode JSON: {}'.format(str(e)))
-        return func(data, *args, **kwargs)
+            raise cherrypy.HTTPError(400, 'Failed to decode JSON: {}'
+                                          .format(str(e)))
+        if hasattr(func, '_args_from_json_'):
+            f_args = inspect.getargspec(func).args
+            n_args = []
+            for arg in args:
+                n_args.append(arg)
+            for arg in f_args[1:]:
+                if arg in data:
+                    n_args.append(data[arg])
+                    data.pop(arg)
+            kwargs.update(data)
+            return func(*n_args, **kwargs)
+        else:
+            return func(data, *args, **kwargs)
     return inner
 
 
@@ -89,12 +105,17 @@ class RESTResource(object):
         if not method:
             self._not_implemented(is_element)
 
-        if cherrypy.request.method != 'DELETE':
-            method = _returns_json(method)
-
         if cherrypy.request.method not in ['GET', 'DELETE']:
             method = _takes_json(method)
 
+        if cherrypy.request.method != 'DELETE':
+            method = _returns_json(method)
+
         cherrypy.response.status = status_code
 
         return method(*vpath, **params)
+
+    @staticmethod
+    def args_from_json(func):
+        func._args_from_json_ = True
+        return func