From 8ab752e916f8266ce03c08c5303ff964c5bd1e5f Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Fri, 22 Jun 2018 13:56:34 +0100 Subject: [PATCH] mgr/dashboard: controllers: fix controllers endpoints initialization order Signed-off-by: Ricardo Dias --- .../mgr/dashboard/controllers/__init__.py | 76 +++++++++++-------- src/pybind/mgr/dashboard/tests/helper.py | 9 ++- 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/src/pybind/mgr/dashboard/controllers/__init__.py b/src/pybind/mgr/dashboard/controllers/__init__.py index 23eb6d81eb423..dfdb5fec57218 100644 --- a/src/pybind/mgr/dashboard/controllers/__init__.py +++ b/src/pybind/mgr/dashboard/controllers/__init__.py @@ -182,45 +182,45 @@ def load_controllers(): ENDPOINT_MAP = collections.defaultdict(list) -def generate_controller_routes(ctrl_class, mapper, base_url): - inst = ctrl_class() - endp_base_urls = set() +def generate_controller_routes(endpoint, mapper, base_url): + inst = endpoint.inst + ctrl_class = endpoint.ctrl + endp_base_url = None - for endpoint in ctrl_class.endpoints(): - if endpoint.proxy: - conditions = None - else: - conditions = dict(method=[endpoint.method]) + if endpoint.proxy: + conditions = None + else: + conditions = dict(method=[endpoint.method]) - endp_url = endpoint.url - if base_url == "/": - base_url = "" - if endp_url == "/" and base_url: - endp_url = "" - url = "{}{}".format(base_url, endp_url) + endp_url = endpoint.url + if base_url == "/": + base_url = "" + if endp_url == "/" and base_url: + endp_url = "" + url = "{}{}".format(base_url, endp_url) - if '/' in url[len(base_url)+1:]: - endp_base_urls.add(url[:len(base_url)+1+endp_url[1:].find('/')]) - else: - endp_base_urls.add(url) + if '/' in url[len(base_url)+1:]: + endp_base_url = url[:len(base_url)+1+endp_url[1:].find('/')] + else: + endp_base_url = url - logger.debug("Mapped [%s] to %s:%s restricted to %s", - url, ctrl_class.__name__, endpoint.action, - endpoint.method) + logger.debug("Mapped [%s] to %s:%s restricted to %s", + url, ctrl_class.__name__, endpoint.action, + endpoint.method) - ENDPOINT_MAP[endpoint.url].append(endpoint) + ENDPOINT_MAP[endpoint.url].append(endpoint) - name = ctrl_class.__name__ + ":" + endpoint.action - mapper.connect(name, url, controller=inst, action=endpoint.action, - conditions=conditions) + name = ctrl_class.__name__ + ":" + endpoint.action + mapper.connect(name, url, controller=inst, action=endpoint.action, + conditions=conditions) - # adding route with trailing slash - name += "/" - url += "/" - mapper.connect(name, url, controller=inst, action=endpoint.action, - conditions=conditions) + # adding route with trailing slash + name += "/" + url += "/" + mapper.connect(name, url, controller=inst, action=endpoint.action, + conditions=conditions) - return endp_base_urls + return endp_base_url def generate_routes(url_prefix): @@ -228,9 +228,18 @@ def generate_routes(url_prefix): ctrls = load_controllers() parent_urls = set() + + endpoint_list = [] for ctrl in ctrls: - parent_urls.update(generate_controller_routes(ctrl, mapper, - "{}".format(url_prefix))) + inst = ctrl() + for endpoint in ctrl.endpoints(): + endpoint.inst = inst + endpoint_list.append(endpoint) + + endpoint_list = sorted(endpoint_list, key=lambda e: e.url) + for endpoint in endpoint_list: + parent_urls.add(generate_controller_routes(endpoint, mapper, + "{}".format(url_prefix))) logger.debug("list of parent paths: %s", parent_urls) return mapper, parent_urls @@ -347,6 +356,7 @@ class BaseController(object): """ def __init__(self, ctrl, func): self.ctrl = ctrl + self.inst = None self.func = func if not self.config['proxy']: diff --git a/src/pybind/mgr/dashboard/tests/helper.py b/src/pybind/mgr/dashboard/tests/helper.py index 45f67bd12a040..8a0064b90f60a 100644 --- a/src/pybind/mgr/dashboard/tests/helper.py +++ b/src/pybind/mgr/dashboard/tests/helper.py @@ -23,8 +23,15 @@ class ControllerTestCase(helper.CPWebCase): if not isinstance(ctrl_classes, list): ctrl_classes = [ctrl_classes] mapper = cherrypy.dispatch.RoutesDispatcher() + endpoint_list = [] for ctrl in ctrl_classes: - generate_controller_routes(ctrl, mapper, base_url) + inst = ctrl() + for endpoint in ctrl.endpoints(): + endpoint.inst = inst + endpoint_list.append(endpoint) + endpoint_list = sorted(endpoint_list, key=lambda e: e.url) + for endpoint in endpoint_list: + generate_controller_routes(endpoint, mapper, base_url) if base_url == '': base_url = '/' cherrypy.tree.mount(None, config={ -- 2.39.5