From c75cad3d683a5f946eedfb1a36fbfce614d1ba2e Mon Sep 17 00:00:00 2001
From: "Yuanbin.Chen" <cybing4@gmail.com>
Date: Fri, 5 May 2017 14:37:48 +0800
Subject: [PATCH] Add services list

This patch add senlinclient support display senlin service status

Change-Id: I8ea92d425d0a0ce5913abafba5d66fdf3d09b80f
Signed-off-by: Yuanbin.Chen <cybing4@gmail.com>
---
 senlinclient/tests/unit/v1/test_service.py | 43 ++++++++++++++++++++++
 senlinclient/tests/unit/v1/test_shell.py   | 13 +++++++
 senlinclient/v1/client.py                  |  8 ++++
 senlinclient/v1/service.py                 | 39 ++++++++++++++++++++
 senlinclient/v1/shell.py                   | 13 +++++++
 setup.cfg                                  |  1 +
 6 files changed, 117 insertions(+)
 create mode 100644 senlinclient/tests/unit/v1/test_service.py
 create mode 100644 senlinclient/v1/service.py

diff --git a/senlinclient/tests/unit/v1/test_service.py b/senlinclient/tests/unit/v1/test_service.py
new file mode 100644
index 00000000..44bd31f6
--- /dev/null
+++ b/senlinclient/tests/unit/v1/test_service.py
@@ -0,0 +1,43 @@
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+import mock
+
+from senlinclient.tests.unit.v1 import fakes
+from senlinclient.v1 import service as osc_service
+
+
+class TestServiceList(fakes.TestClusteringv1):
+    columns = ['Binary', 'Host', 'Status', 'State', 'Updated_at',
+               'Disabled Reason']
+
+    def setUp(self):
+        super(TestServiceList, self).setUp()
+        self.mock_client = self.app.client_manager.clustering
+        self.cmd = osc_service.Service(self.app, None)
+        fake_service = mock.Mock(
+            Binary='senlin-engine',
+            Host='Host1',
+            Status='enabled',
+            State='up',
+            Updated_at=None,
+            Disabled_Reason=None,
+        )
+        fake_service.name = 'test_service'
+        fake_service.to_dict = mock.Mock(return_value={})
+        self.mock_client.get_service = mock.Mock(return_value=[fake_service])
+
+    def test_service(self):
+        arglist = []
+        parsed_args = self.check_parser(self.cmd, arglist, [])
+        columns, data = self.cmd.take_action(parsed_args)
+        self.mock_client.get_service.assert_called_with()
+        self.assertEqual(self.columns, columns)
diff --git a/senlinclient/tests/unit/v1/test_shell.py b/senlinclient/tests/unit/v1/test_shell.py
index 94148b5a..b286830b 100644
--- a/senlinclient/tests/unit/v1/test_shell.py
+++ b/senlinclient/tests/unit/v1/test_shell.py
@@ -1716,3 +1716,16 @@ class ShellTest(testtools.TestCase):
                                service, args)
         msg = _('Action not found: fake_id')
         self.assertEqual(msg, six.text_type(ex))
+
+    @mock.patch.object(utils, 'print_list')
+    def test_do_service_list(self, mock_print):
+        service = mock.Mock()
+        fields = ['Binary', 'Host', 'Status', 'State', 'Updated_at',
+                  'Disabled Reason']
+
+        result = mock.Mock()
+        service.services.return_value = result
+        formatters = {}
+        sh.do_service_list(service)
+        mock_print.assert_called_once_with(result, fields,
+                                           formatters=formatters)
diff --git a/senlinclient/v1/client.py b/senlinclient/v1/client.py
index 20ebd21e..53bb789e 100644
--- a/senlinclient/v1/client.py
+++ b/senlinclient/v1/client.py
@@ -433,3 +433,11 @@ class Client(object):
         http://developer.openstack.org/api-ref-clustering-v1.html#showAction
         """
         return self.service.get_action(action)
+
+    def get_service(self, **queries):
+        """List service
+
+        Doc link:
+        http://developer.openstack.org/api-ref-clustering-v1.html#showAction
+        """
+        return self.service.services(**queries)
diff --git a/senlinclient/v1/service.py b/senlinclient/v1/service.py
new file mode 100644
index 00000000..81398c68
--- /dev/null
+++ b/senlinclient/v1/service.py
@@ -0,0 +1,39 @@
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import logging
+
+from osc_lib.command import command
+from osc_lib import utils
+
+
+class Service(command.ShowOne):
+    """Retrieve build information."""
+
+    log = logging.getLogger(__name__ + ".Service")
+
+    def get_parser(self, prog_name):
+        parser = super(Service, self).get_parser(prog_name)
+        return parser
+
+    def take_action(self, parsed_args):
+        self.log.debug("take_action(%s)", parsed_args)
+
+        senlin_client = self.app.client_manager.clustering
+        queries = {}
+        result = senlin_client.get_service(**queries)
+
+        formatters = {}
+        columns = ['Binary', 'Host', 'Status', 'State', 'Updated_at',
+                   'Disabled Reason']
+        return columns, utils.get_dict_properties(result, columns,
+                                                  formatters=formatters)
diff --git a/senlinclient/v1/shell.py b/senlinclient/v1/shell.py
index 0e5574d0..ffee6a37 100644
--- a/senlinclient/v1/shell.py
+++ b/senlinclient/v1/shell.py
@@ -1680,3 +1680,16 @@ def do_action_show(service, args):
     }
 
     utils.print_dict(action.to_dict(), formatters=formatters)
+
+
+def do_service_list(service, args=None):
+    """Show a list of all running services."""
+    show_deprecated('senlin service-list',
+                    'openstack cluster service list')
+    fields = ['Binary', 'Host', 'Status', 'State', 'Updated_at',
+              'Disabled Reason']
+    queries = {}
+    result = service.services(**queries)
+
+    formatters = {}
+    utils.print_list(result, fields, formatters=formatters)
diff --git a/setup.cfg b/setup.cfg
index 7ad22213..b4ffb9ac 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -83,6 +83,7 @@ openstack.clustering.v1 =
     cluster_update = senlinclient.v1.cluster:UpdateCluster
     cluster_collect = senlinclient.v1.cluster:ClusterCollect
     cluster_run = senlinclient.v1.cluster:ClusterRun
+    cluster_service = senlinclient.v1.cluster.service.Service
 
 [global]
 setup-hooks =