From 5229c9bebc9de8266adc3a2da0e2c7d3b91a73ef Mon Sep 17 00:00:00 2001 From: Ricardo Marques Date: Mon, 28 May 2018 17:34:59 +0100 Subject: [PATCH] mgr/dashboard: Add UiApiController decorator Should be used for UI specific requests. Signed-off-by: Ricardo Marques --- src/pybind/mgr/dashboard/HACKING.rst | 32 +++++++++++++------ .../mgr/dashboard/controllers/__init__.py | 5 +++ .../dashboard/frontend/proxy.conf.json.sample | 5 +++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/pybind/mgr/dashboard/HACKING.rst b/src/pybind/mgr/dashboard/HACKING.rst index d11e27e69f7..5c06293a330 100644 --- a/src/pybind/mgr/dashboard/HACKING.rst +++ b/src/pybind/mgr/dashboard/HACKING.rst @@ -246,15 +246,18 @@ How to add a new controller? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ A controller is a Python class that extends from the ``BaseController`` class -and is decorated with either the ``@Controller`` or ``@ApiController`` -decorators. The Python class must be stored inside a Python file located under -the ``controllers`` directory. The Dashboard module will automatically load -your new controller upon start. +and is decorated with either the ``@Controller``, ``@ApiController`` or +``@UiApiController`` decorators. The Python class must be stored inside a Python +file located under the ``controllers`` directory. The Dashboard module will +automatically load your new controller upon start. -The ``@ApiController`` decorator is a specialization of the ``@Controller`` -decorator, and should be used for controllers that provide an API-like REST -interface. For any other kinds of controllers the ``@Controller`` decorator -should be used. +``@ApiController`` and ``@UiApiController`` are both specializations of the +``@Controller`` decorator. + +The ``@ApiController`` should be used for controllers that provide an API-like +REST interface and the ``@UiApiController`` should be used for endpoints consumed +by the UI but that are not part of the 'public' API. For any other kinds of +controllers the ``@Controller`` decorator should be used. A controller has a URL prefix path associated that is specified in the controller decorator, and all endpoints exposed by the controller will share @@ -268,7 +271,7 @@ following code: .. code-block:: python - from ..tools import Controller, ApiController, BaseController, Endpoint + from ..tools import Controller, ApiController, UiApiController, BaseController, Endpoint @Controller('/ping') class Ping(BaseController): @@ -282,6 +285,11 @@ following code: def hello(self): return {'msg': "Hello"} + @UiApiController('/ping') + class UiApiPing(BaseController): + @Endpoint() + def hello(self): + return {'msg': "Hello"} The ``hello`` endpoint of the ``Ping`` controller can be reached by the following URL: https://mgr_hostname:8080/ping/hello using HTTP GET requests. @@ -298,6 +306,12 @@ decorator by passing an additional decorator parameter called ``base_url``:: @ApiController('/ping') <=> @Controller('/ping', base_url="/api") +``UiApiPing`` works in a similar way than the ``ApiPing``, but the URL will be +prefixed by ``/ui-api``: https://mgr_hostname:8080/ui-api/ping/hello. ``UiApiPing`` is +also a ``@Controller`` extension:: + + @UiApiController('/ping') <=> @Controller('/ping', base_url="/ui-api") + The ``@Endpoint`` decorator also supports many parameters to customize the endpoint: diff --git a/src/pybind/mgr/dashboard/controllers/__init__.py b/src/pybind/mgr/dashboard/controllers/__init__.py index ff82a4c86ba..b0dd84e18f3 100644 --- a/src/pybind/mgr/dashboard/controllers/__init__.py +++ b/src/pybind/mgr/dashboard/controllers/__init__.py @@ -64,6 +64,11 @@ class ApiController(Controller): return cls +class UiApiController(Controller): + def __init__(self, path): + super(UiApiController, self).__init__(path, base_url="/ui-api") + + def AuthRequired(enabled=True): def decorate(cls): if not hasattr(cls, '_cp_config'): diff --git a/src/pybind/mgr/dashboard/frontend/proxy.conf.json.sample b/src/pybind/mgr/dashboard/frontend/proxy.conf.json.sample index d70abbd2c93..1b4f52de52a 100644 --- a/src/pybind/mgr/dashboard/frontend/proxy.conf.json.sample +++ b/src/pybind/mgr/dashboard/frontend/proxy.conf.json.sample @@ -3,5 +3,10 @@ "target": "https://localhost:8080", "secure": false, "logLevel": "debug" + }, + "/ui-api/": { + "target": "https://localhost:8080", + "secure": false, + "logLevel": "debug" } } -- 2.39.5