From: Boris Ranto Date: Thu, 18 May 2017 17:27:55 +0000 (+0200) Subject: restful: Use pecan hook instead of catch decorator X-Git-Tag: ses5-milestone6~9^2~47^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=48ba07ac5c8b634e866b8370c377eccf7d9505ff;p=ceph.git restful: Use pecan hook instead of catch decorator This removes the catch decorator which was useful primarily for testing purposes and instead switches to a pecan error hook which will print the traceback to the ceph-mgr log. Signed-off-by: Boris Ranto --- diff --git a/src/pybind/mgr/restful/api/__init__.py b/src/pybind/mgr/restful/api/__init__.py index e04efca8213e..a6c662a2ca13 100644 --- a/src/pybind/mgr/restful/api/__init__.py +++ b/src/pybind/mgr/restful/api/__init__.py @@ -10,8 +10,6 @@ from pool import Pool from request import Request from server import Server -from restful.decorators import catch - class Root(RestController): config = Config() @@ -24,7 +22,6 @@ class Root(RestController): server = Server() @expose(template='json') - @catch def get(self, **kwargs): """ Show the basic information for the REST API diff --git a/src/pybind/mgr/restful/api/config.py b/src/pybind/mgr/restful/api/config.py index cca6a472829a..51fabfad4d68 100644 --- a/src/pybind/mgr/restful/api/config.py +++ b/src/pybind/mgr/restful/api/config.py @@ -2,12 +2,11 @@ from pecan import expose, request from pecan.rest import RestController from restful import common, module -from restful.decorators import auth, catch +from restful.decorators import auth class ConfigOsd(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ @@ -22,7 +21,6 @@ class ConfigOsd(RestController): @expose(template='json') - @catch @auth def patch(self, **kwargs): """ @@ -58,7 +56,6 @@ class ConfigClusterKey(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ @@ -70,7 +67,6 @@ class ConfigClusterKey(RestController): class ConfigCluster(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ diff --git a/src/pybind/mgr/restful/api/crush.py b/src/pybind/mgr/restful/api/crush.py index 14338d6b566c..046676a2815f 100644 --- a/src/pybind/mgr/restful/api/crush.py +++ b/src/pybind/mgr/restful/api/crush.py @@ -4,12 +4,11 @@ from pecan.rest import RestController from restful import common, module from collections import defaultdict -from restful.decorators import auth, catch +from restful.decorators import auth class CrushRuleset(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ @@ -29,7 +28,6 @@ class CrushRuleset(RestController): class CrushRule(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ diff --git a/src/pybind/mgr/restful/api/doc.py b/src/pybind/mgr/restful/api/doc.py index 3ccd99b9df8c..96e4d6a3379b 100644 --- a/src/pybind/mgr/restful/api/doc.py +++ b/src/pybind/mgr/restful/api/doc.py @@ -2,14 +2,12 @@ from pecan import expose from pecan.rest import RestController from restful import module -from restful.decorators import catch import restful class Doc(RestController): @expose(template='json') - @catch def get(self, **kwargs): """ Show documentation information diff --git a/src/pybind/mgr/restful/api/mon.py b/src/pybind/mgr/restful/api/mon.py index c47e75335cb2..5b19aaefb299 100644 --- a/src/pybind/mgr/restful/api/mon.py +++ b/src/pybind/mgr/restful/api/mon.py @@ -2,7 +2,7 @@ from pecan import expose, response from pecan.rest import RestController from restful import module -from restful.decorators import auth, catch +from restful.decorators import auth class MonName(RestController): @@ -11,7 +11,6 @@ class MonName(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ @@ -32,7 +31,6 @@ class MonName(RestController): class Mon(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ diff --git a/src/pybind/mgr/restful/api/osd.py b/src/pybind/mgr/restful/api/osd.py index d962031d9ef1..b42f33941f1c 100644 --- a/src/pybind/mgr/restful/api/osd.py +++ b/src/pybind/mgr/restful/api/osd.py @@ -2,7 +2,7 @@ from pecan import expose, request, response from pecan.rest import RestController from restful import common, module -from restful.decorators import auth, catch +from restful.decorators import auth class OsdIdCommand(RestController): @@ -11,7 +11,6 @@ class OsdIdCommand(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ @@ -30,7 +29,6 @@ class OsdIdCommand(RestController): @expose(template='json') - @catch @auth def post(self, **kwargs): """ @@ -62,7 +60,6 @@ class OsdId(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ @@ -77,7 +74,6 @@ class OsdId(RestController): @expose(template='json') - @catch @auth def patch(self, **kwargs): """ @@ -122,7 +118,6 @@ class OsdId(RestController): class Osd(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ diff --git a/src/pybind/mgr/restful/api/pool.py b/src/pybind/mgr/restful/api/pool.py index dca450ee501e..0c8d2c92e63a 100644 --- a/src/pybind/mgr/restful/api/pool.py +++ b/src/pybind/mgr/restful/api/pool.py @@ -2,7 +2,7 @@ from pecan import expose, request, response from pecan.rest import RestController from restful import common, module -from restful.decorators import auth, catch +from restful.decorators import auth class PoolId(RestController): @@ -11,7 +11,6 @@ class PoolId(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ @@ -30,7 +29,6 @@ class PoolId(RestController): @expose(template='json') - @catch @auth def patch(self, **kwargs): """ @@ -55,7 +53,6 @@ class PoolId(RestController): @expose(template='json') - @catch @auth def delete(self, **kwargs): """ @@ -78,7 +75,6 @@ class PoolId(RestController): class Pool(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ @@ -95,7 +91,6 @@ class Pool(RestController): @expose(template='json') - @catch @auth def post(self, **kwargs): """ diff --git a/src/pybind/mgr/restful/api/request.py b/src/pybind/mgr/restful/api/request.py index ddf422bba2ca..30d248a97e7e 100644 --- a/src/pybind/mgr/restful/api/request.py +++ b/src/pybind/mgr/restful/api/request.py @@ -2,7 +2,7 @@ from pecan import expose, request, response from pecan.rest import RestController from restful import module -from restful.decorators import auth, catch, lock +from restful.decorators import auth, lock class RequestId(RestController): @@ -11,7 +11,6 @@ class RequestId(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ @@ -31,7 +30,6 @@ class RequestId(RestController): @expose(template='json') - @catch @auth @lock def delete(self, **kwargs): @@ -50,7 +48,6 @@ class RequestId(RestController): class Request(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ @@ -64,7 +61,6 @@ class Request(RestController): @expose(template='json') - @catch @auth @lock def delete(self, **kwargs): @@ -86,7 +82,6 @@ class Request(RestController): @expose(template='json') - @catch @auth def post(self, **kwargs): """ diff --git a/src/pybind/mgr/restful/api/server.py b/src/pybind/mgr/restful/api/server.py index 99bf22bc52cc..9e6a20784ec0 100644 --- a/src/pybind/mgr/restful/api/server.py +++ b/src/pybind/mgr/restful/api/server.py @@ -2,7 +2,7 @@ from pecan import expose from pecan.rest import RestController from restful import module -from restful.decorators import auth, catch +from restful.decorators import auth class ServerFqdn(RestController): @@ -11,7 +11,6 @@ class ServerFqdn(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ @@ -23,7 +22,6 @@ class ServerFqdn(RestController): class Server(RestController): @expose(template='json') - @catch @auth def get(self, **kwargs): """ diff --git a/src/pybind/mgr/restful/decorators.py b/src/pybind/mgr/restful/decorators.py index fd12b0202017..09fa64dca536 100644 --- a/src/pybind/mgr/restful/decorators.py +++ b/src/pybind/mgr/restful/decorators.py @@ -34,19 +34,6 @@ def auth(f): return decorated -# Helper function to catch and log the exceptions -def catch(f): - @wraps(f) - def decorated(*args, **kwargs): - try: - return f(*args, **kwargs) - except: - module.instance.log.error(str(traceback.format_exc())) - response.status = 500 - return {'message': str(traceback.format_exc()).split('\n')} - return decorated - - # Helper function to lock the function def lock(f): @wraps(f) diff --git a/src/pybind/mgr/restful/hooks.py b/src/pybind/mgr/restful/hooks.py new file mode 100644 index 000000000000..3fd244aa85e2 --- /dev/null +++ b/src/pybind/mgr/restful/hooks.py @@ -0,0 +1,9 @@ +from pecan.hooks import PecanHook + +import traceback + +import module + +class ErrorHook(PecanHook): + def on_error(self, stat, exc): + module.instance.log.error(str(traceback.format_exc())) diff --git a/src/pybind/mgr/restful/module.py b/src/pybind/mgr/restful/module.py index 71c9d26b3d8c..6e643e862b56 100644 --- a/src/pybind/mgr/restful/module.py +++ b/src/pybind/mgr/restful/module.py @@ -17,6 +17,7 @@ from pecan import jsonify, make_app from pecan.rest import RestController from werkzeug.serving import make_server, make_ssl_devcert +from hooks import ErrorHook from mgr_module import MgrModule, CommandResult # Global instance to share @@ -234,7 +235,10 @@ class Module(MgrModule): self.server = make_server( host='0.0.0.0', port=8003, - app=make_app('restful.api.Root'), + app=make_app( + root='restful.api.Root', + hooks = lambda: [ErrorHook()], + ), ssl_context=(cert, pkey), )