From 671b1348857e30172dc06be7b9a214d7fc386bf1 Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Thu, 19 Apr 2018 08:45:40 +0100 Subject: [PATCH] mgr/dashboard: restcontroller: support explicit resource ID In the current implementation, RESTController tries to infer the resource ID by looking at the declared parameters of the resource methods (get, delete, set, and patch). This inference may not always be correct, and therefore with this commit we can now explicitely specify the structure of the resource ID. If the resource ID is not explicitly specified, then we fallback to the inference algorithm. Signed-off-by: Ricardo Dias --- src/pybind/mgr/dashboard/controllers/__init__.py | 16 +++++++++++++--- src/pybind/mgr/dashboard/controllers/rbd.py | 4 ++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/pybind/mgr/dashboard/controllers/__init__.py b/src/pybind/mgr/dashboard/controllers/__init__.py index 6ad5345a2ca0c..67c489f0443c0 100644 --- a/src/pybind/mgr/dashboard/controllers/__init__.py +++ b/src/pybind/mgr/dashboard/controllers/__init__.py @@ -421,6 +421,13 @@ class RESTController(BaseController): """ + # resource id parameter for using in get, set, and delete methods + # should be overriden by subclasses. + # to specify a composite id (two parameters) use '/'. e.g., "param1/param2". + # If subclasses don't override this property we try to infer the structure of + # the resourse ID. + RESOURCE_ID = None + _method_mapping = collections.OrderedDict([ (('GET', False), ('list', 200)), (('PUT', False), ('bulk_set', 200)), @@ -428,9 +435,9 @@ class RESTController(BaseController): (('POST', False), ('create', 201)), (('DELETE', False), ('bulk_delete', 204)), (('GET', True), ('get', 200)), - (('PUT', True), ('set', 200)), - (('PATCH', True), ('set', 200)), (('DELETE', True), ('delete', 204)), + (('PUT', True), ('set', 200)), + (('PATCH', True), ('set', 200)) ]) @classmethod @@ -458,7 +465,10 @@ class RESTController(BaseController): if k[1] and hasattr(cls, v[0]): methods.append(k[0]) if not args: - args = cls._parse_function_args(getattr(cls, v[0])) + if cls.RESOURCE_ID is None: + args = cls._parse_function_args(getattr(cls, v[0])) + else: + args = cls.RESOURCE_ID.split('/') if methods: result.append((methods, None, '_element', args)) diff --git a/src/pybind/mgr/dashboard/controllers/rbd.py b/src/pybind/mgr/dashboard/controllers/rbd.py index f7e2a02393d24..aa748f51df236 100644 --- a/src/pybind/mgr/dashboard/controllers/rbd.py +++ b/src/pybind/mgr/dashboard/controllers/rbd.py @@ -114,6 +114,8 @@ def _sort_features(features, enable=True): @AuthRequired() class Rbd(RESTController): + RESOURCE_ID = "pool_name/image_name" + # set of image features that can be enable on existing images ALLOW_ENABLE_FEATURES = set(["exclusive-lock", "object-map", "fast-diff", "journaling"]) @@ -354,6 +356,8 @@ class Rbd(RESTController): @ApiController('block/image/:pool_name/:image_name/snap') class RbdSnapshot(RESTController): + RESOURCE_ID = "snapshot_name" + @RbdTask('snap/create', ['{pool_name}', '{image_name}', '{snapshot_name}'], 2.0) @RESTController.args_from_json -- 2.39.5