From 42579be47374338f62e44a67d2b4eae65490ffc5 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Mon, 2 Dec 2019 16:07:18 +0800 Subject: [PATCH] mgr/orchestrator: add optional "format" param for "orchestrator host ls" and update the test accordingly Fixes: https://tracker.ceph.com/issues/43078 Signed-off-by: Kefu Chai --- qa/tasks/mgr/test_orchestrator_cli.py | 6 +++-- src/pybind/mgr/orchestrator_cli/module.py | 29 ++++++++++++++--------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/qa/tasks/mgr/test_orchestrator_cli.py b/qa/tasks/mgr/test_orchestrator_cli.py index 0bcbdfd4ed1..b41c61f5ab7 100644 --- a/qa/tasks/mgr/test_orchestrator_cli.py +++ b/qa/tasks/mgr/test_orchestrator_cli.py @@ -144,8 +144,10 @@ class TestOrchestratorCli(MgrTestCase): self._orch_cmd("nfs", "rm", "service_name") def test_host_ls(self): - out = self._orch_cmd("host", "ls") - self.assertEqual(out, "localhost\n") + out = self._orch_cmd("host", "ls", "--format=json") + hosts = json.loads(out) + self.assertEqual(len(hosts), 1) + self.assertEqual(hosts[0]["host"], "localhost") def test_host_add(self): self._orch_cmd("host", "add", "hostname") diff --git a/src/pybind/mgr/orchestrator_cli/module.py b/src/pybind/mgr/orchestrator_cli/module.py index fef10673c98..cdf50d57d6c 100644 --- a/src/pybind/mgr/orchestrator_cli/module.py +++ b/src/pybind/mgr/orchestrator_cli/module.py @@ -166,20 +166,27 @@ class OrchestratorCli(orchestrator.OrchestratorClientMixin, MgrModule): @orchestrator._cli_read_command( 'orchestrator host ls', - desc='List hosts') - def _get_hosts(self): + 'name=format,type=CephChoices,strings=json|plain,req=false', + 'List hosts') + def _get_hosts(self, format='plain'): completion = self.get_hosts() self._orchestrator_wait([completion]) orchestrator.raise_if_exception(completion) - table = PrettyTable( - ['HOST', 'LABELS'], - border=False) - table.align = 'l' - table.left_padding_width = 0 - table.right_padding_width = 1 - for node in completion.result: - table.add_row((node.name, ' '.join(node.labels))) - return HandleCommandResult(stdout=table.get_string()) + if format == 'json': + hosts = [dict(host=node.name, labels=node.labels) + for node in completion.result] + output = json.dumps(hosts) + else: + table = PrettyTable( + ['HOST', 'LABELS'], + border=False) + table.align = 'l' + table.left_padding_width = 0 + table.right_padding_width = 1 + for node in completion.result: + table.add_row((node.name, ' '.join(node.labels))) + output = table.get_string() + return HandleCommandResult(stdout=output) @orchestrator._cli_write_command( 'orchestrator host label add', -- 2.39.5