From 8664a2f8aecd3d91a929e7e2b76772cae20f41e1 Mon Sep 17 00:00:00 2001
From: Rui Chen <chenrui.momo@gmail.com>
Date: Mon, 14 Mar 2016 18:07:23 +0800
Subject: [PATCH] Support "--long" option in ListService

Add "--long" option in ListService so that compute service
disabled reason can be showed.

Change-Id: I1ace8f1c4e4efe0a1a8f6710425d73eb5db9e5e1
Closes-Bug: #1556815
---
 .../command-objects/compute-service.rst       |  5 +++
 openstackclient/compute/v2/service.py         | 36 ++++++++++++++-----
 openstackclient/tests/compute/v2/fakes.py     |  2 ++
 .../tests/compute/v2/test_service.py          | 26 +++++++++++++-
 4 files changed, 59 insertions(+), 10 deletions(-)

diff --git a/doc/source/command-objects/compute-service.rst b/doc/source/command-objects/compute-service.rst
index 25a133d97c..aefe55db7a 100644
--- a/doc/source/command-objects/compute-service.rst
+++ b/doc/source/command-objects/compute-service.rst
@@ -31,6 +31,7 @@ List service command
     os compute service list
         [--host <host>]
         [--service <service>]
+        [--long]
 
 .. _compute-service-list:
 .. describe:: --host <host>
@@ -41,6 +42,10 @@ List service command
 
     Name of service
 
+.. describe:: --long
+
+    List additional fields in output
+
 
 compute service set
 -------------------
diff --git a/openstackclient/compute/v2/service.py b/openstackclient/compute/v2/service.py
index 1cc91711eb..89f5cad94f 100644
--- a/openstackclient/compute/v2/service.py
+++ b/openstackclient/compute/v2/service.py
@@ -49,19 +49,37 @@ class ListService(command.Lister):
             "--service",
             metavar="<service>",
             help="Name of service")
+        parser.add_argument(
+            "--long",
+            action="store_true",
+            default=False,
+            help="List additional fields in output"
+        )
         return parser
 
     def take_action(self, parsed_args):
         compute_client = self.app.client_manager.compute
-        columns = (
-            "Id",
-            "Binary",
-            "Host",
-            "Zone",
-            "Status",
-            "State",
-            "Updated At"
-        )
+        if parsed_args.long:
+            columns = (
+                "Id",
+                "Binary",
+                "Host",
+                "Zone",
+                "Status",
+                "State",
+                "Updated At",
+                "Disabled Reason"
+            )
+        else:
+            columns = (
+                "Id",
+                "Binary",
+                "Host",
+                "Zone",
+                "Status",
+                "State",
+                "Updated At"
+            )
         data = compute_client.services.list(parsed_args.host,
                                             parsed_args.service)
         return (columns,
diff --git a/openstackclient/tests/compute/v2/fakes.py b/openstackclient/tests/compute/v2/fakes.py
index 26d3a28309..ccd8cc6bcc 100644
--- a/openstackclient/tests/compute/v2/fakes.py
+++ b/openstackclient/tests/compute/v2/fakes.py
@@ -79,10 +79,12 @@ QUOTA_data = tuple(QUOTA[x] for x in sorted(QUOTA))
 service_host = 'host_test'
 service_binary = 'compute_test'
 service_status = 'enabled'
+service_disabled_reason = 'earthquake'
 SERVICE = {
     'host': service_host,
     'binary': service_binary,
     'status': service_status,
+    'disabled_reason': service_disabled_reason,
 }
 
 
diff --git a/openstackclient/tests/compute/v2/test_service.py b/openstackclient/tests/compute/v2/test_service.py
index 0246fbc86b..2feaf1566f 100644
--- a/openstackclient/tests/compute/v2/test_service.py
+++ b/openstackclient/tests/compute/v2/test_service.py
@@ -85,13 +85,37 @@ class TestServiceList(TestService):
         # In base command class Lister in cliff, abstract method take_action()
         # returns a tuple containing the column names and an iterable
         # containing the data to be listed.
-        self.cmd.take_action(parsed_args)
+        columns, data = self.cmd.take_action(parsed_args)
 
         self.service_mock.list.assert_called_with(
             compute_fakes.service_host,
             compute_fakes.service_binary,
         )
 
+        self.assertNotIn("Disabled Reason", columns)
+        self.assertNotIn(compute_fakes.service_disabled_reason, list(data)[0])
+
+    def test_service_list_with_long_option(self):
+        arglist = [
+            '--host', compute_fakes.service_host,
+            '--service', compute_fakes.service_binary,
+            '--long'
+        ]
+        verifylist = [
+            ('host', compute_fakes.service_host),
+            ('service', compute_fakes.service_binary),
+            ('long', True)
+        ]
+        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+        # In base command class Lister in cliff, abstract method take_action()
+        # returns a tuple containing the column names and an iterable
+        # containing the data to be listed.
+        columns, data = self.cmd.take_action(parsed_args)
+
+        self.assertIn("Disabled Reason", columns)
+        self.assertIn(compute_fakes.service_disabled_reason, list(data)[0])
+
 
 class TestServiceSet(TestService):