From 0e38ef84844ba406cb18fe3893dc6bebd205ef51 Mon Sep 17 00:00:00 2001 From: Richard Theis Date: Wed, 23 Dec 2015 13:44:22 -0600 Subject: [PATCH] Improve output for "os security group show" Improve the security group rules output when running the "os security group show" command. Each security group rule is now displayed on a separate line. Current output example: $ openstack security group show default +-------------+------------------------- ... ---+ | Field | Value ... | +-------------+------------------------- ... ---+ | description | Default security group ... | | id | 048a5fc3-3be1-407d-ae47-9... | | name | default ... | | project_id | 3b96bb2020c1459da76963f9e... | | rules | [u"id='5d812367-9829-4340...t"] | +-------------+------------------------- ... ---+ New output example: +-------------+------------------------- ... ---+ | Field | Value ... | +-------------+------------------------- ... ---+ | description | Default security group ... | | id | 048a5fc3-3be1-407d-ae47-9... | | name | default ... | | project_id | 3b96bb2020c1459da76963f9e... | | rules | id='5d812367-9829-4340-95...lt' | | | id='ee451d1c-ade3-4975-8e...lt' | +-------------+------------------------- ... ---+ Change-Id: I1386075310896c58a2b776e2bbec3603bd00eff1 Partial-Bug: #1519511 Related-To: blueprint neutron-client --- openstackclient/common/utils.py | 7 ++++--- openstackclient/compute/v2/security_group.py | 2 +- openstackclient/tests/common/test_utils.py | 7 +++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/openstackclient/common/utils.py b/openstackclient/common/utils.py index 42630d913a..783ca8c050 100644 --- a/openstackclient/common/utils.py +++ b/openstackclient/common/utils.py @@ -154,14 +154,15 @@ def format_dict(data): return output[:-2] -def format_list(data): +def format_list(data, separator=', '): """Return a formatted strings :param data: a list of strings - :rtype: a string formatted to a,b,c + :param separator: the separator to use between strings (default: ', ') + :rtype: a string formatted based on separator """ - return ', '.join(sorted(data)) + return separator.join(sorted(data)) def get_field(item, field): diff --git a/openstackclient/compute/v2/security_group.py b/openstackclient/compute/v2/security_group.py index e3f542b564..a6f060f60c 100644 --- a/openstackclient/compute/v2/security_group.py +++ b/openstackclient/compute/v2/security_group.py @@ -390,7 +390,7 @@ class ShowSecurityGroup(show.ShowOne): # Format rules into a list of strings info.update( - {'rules': rules} + {'rules': utils.format_list(rules, separator='\n')} ) # Map 'tenant_id' column to 'project_id' info.update( diff --git a/openstackclient/tests/common/test_utils.py b/openstackclient/tests/common/test_utils.py index b564ffab48..064ad417e6 100644 --- a/openstackclient/tests/common/test_utils.py +++ b/openstackclient/tests/common/test_utils.py @@ -347,3 +347,10 @@ class TestFindResource(test_utils.TestCase): expected = 'a, b, c' self.assertEqual(expected, utils.format_list(['a', 'b', 'c'])) self.assertEqual(expected, utils.format_list(['c', 'b', 'a'])) + + def test_format_list_separator(self): + expected = 'a\nb\nc' + actual_pre_sorted = utils.format_list(['a', 'b', 'c'], separator='\n') + actual_unsorted = utils.format_list(['c', 'b', 'a'], separator='\n') + self.assertEqual(expected, actual_pre_sorted) + self.assertEqual(expected, actual_unsorted)