From 6f388202bdcff486237ced0cc4d2ff330bb1af73 Mon Sep 17 00:00:00 2001 From: Avan Thakkar Date: Mon, 15 Feb 2021 19:48:08 +0530 Subject: [PATCH] mgr/dashboard: fix PUT - /api/host/{hostname} while adding labels Fixes: https://tracker.ceph.com/issues/49292 Signed-off-by: Avan Thakkar Fixes PUT - /api/host/{hostname} for adding labels. (cherry picked from commit 0dee0d3c3f837754c75c5f4ab3363829a8769b9f) --- src/pybind/mgr/dashboard/controllers/host.py | 12 ++++++++++++ src/pybind/mgr/dashboard/openapi.yaml | 12 +++++++++--- src/pybind/mgr/dashboard/tests/test_host.py | 4 ++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/pybind/mgr/dashboard/controllers/host.py b/src/pybind/mgr/dashboard/controllers/host.py index cdce5c895a4c9..d0129777d1575 100644 --- a/src/pybind/mgr/dashboard/controllers/host.py +++ b/src/pybind/mgr/dashboard/controllers/host.py @@ -375,6 +375,12 @@ class Host(RESTController): @raise_if_no_orchestrator([OrchFeature.HOST_LABEL_ADD, OrchFeature.HOST_LABEL_REMOVE]) @handle_orchestrator_error('host') + @EndpointDoc('', + parameters={ + 'hostname': (str, 'Hostname'), + 'labels': ([str], 'Host Labels'), + }, + responses={200: None, 204: None}) def set(self, hostname: str, labels: List[str]): """ Update the specified host. @@ -384,6 +390,12 @@ class Host(RESTController): """ orch = OrchClient.instance() host = get_host(hostname) + # only allow List[str] type for labels + if not isinstance(labels, list): + raise DashboardException( + msg='Expected list of labels. Please check API documentation.', + http_status_code=400, + component='orchestrator') current_labels = set(host['labels']) # Remove labels. remove_labels = list(current_labels.difference(set(labels))) diff --git a/src/pybind/mgr/dashboard/openapi.yaml b/src/pybind/mgr/dashboard/openapi.yaml index cd2a570a8be2e..3b583399001c7 100644 --- a/src/pybind/mgr/dashboard/openapi.yaml +++ b/src/pybind/mgr/dashboard/openapi.yaml @@ -3334,7 +3334,8 @@ paths: \ name of the host to be processed.\n :param labels: List of labels.\n\ \ " parameters: - - in: path + - description: Hostname + in: path name: hostname required: true schema: @@ -3345,7 +3346,10 @@ paths: schema: properties: labels: - type: string + description: Host Labels + items: + type: string + type: array required: - labels type: object @@ -3353,7 +3357,9 @@ paths: '200': content: application/vnd.ceph.api.v1.0+json: - type: object + schema: + properties: {} + type: object description: Resource updated. '202': content: diff --git a/src/pybind/mgr/dashboard/tests/test_host.py b/src/pybind/mgr/dashboard/tests/test_host.py index 050e28e881076..bd0deb5f82191 100644 --- a/src/pybind/mgr/dashboard/tests/test_host.py +++ b/src/pybind/mgr/dashboard/tests/test_host.py @@ -136,6 +136,10 @@ class HostControllerTest(ControllerTestCase): fake_client.hosts.remove_label.assert_called_once_with('node0', 'aaa') fake_client.hosts.add_label.assert_called_once_with('node0', 'ccc') + # return 400 if type other than List[str] + self._put('{}/node0'.format(self.URL_HOST), {'labels': 'ddd'}) + self.assertStatus(400) + @mock.patch('dashboard.controllers.host.time') def test_identify_device(self, mock_time): url = '{}/host-0/identify_device'.format(self.URL_HOST) -- 2.39.5