]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/orchestrator: add optional "format" param for "orchestrator host ls" 31930/head
authorKefu Chai <kchai@redhat.com>
Mon, 2 Dec 2019 08:07:18 +0000 (16:07 +0800)
committerKefu Chai <kchai@redhat.com>
Mon, 2 Dec 2019 11:27:57 +0000 (19:27 +0800)
and update the test accordingly

Fixes: https://tracker.ceph.com/issues/43078
Signed-off-by: Kefu Chai <kchai@redhat.com>
qa/tasks/mgr/test_orchestrator_cli.py
src/pybind/mgr/orchestrator_cli/module.py

index 0bcbdfd4ed1cc04f4bbd52afad5e8135bb51d206..b41c61f5ab7332041b1996b1847848ae92dc2355 100644 (file)
@@ -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")
index fef10673c9879c90f8b1b1f75ef38fa11dc3a917..cdf50d57d6c0c3486e5753f93890a30d9b07c344 100644 (file)
@@ -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',