From b68517ce83031898b93d10ccb6cb72df8d390ab6 Mon Sep 17 00:00:00 2001 From: Russell Sim <russell.sim@gmail.com> Date: Wed, 23 Oct 2013 14:56:42 +1100 Subject: [PATCH] Print security groups as a human readable list Convert the security group list into a list that is more suitable for printing. Change-Id: I411d9256145986fad7659a9377494e29a6d70ced --- novaclient/tests/v1_1/fakes.py | 10 ++++++++++ novaclient/tests/v1_1/test_shell.py | 15 +++++++++++---- novaclient/v1_1/shell.py | 4 ++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/novaclient/tests/v1_1/fakes.py b/novaclient/tests/v1_1/fakes.py index 36781cf8d..8a17c61cd 100644 --- a/novaclient/tests/v1_1/fakes.py +++ b/novaclient/tests/v1_1/fakes.py @@ -320,6 +320,16 @@ class FakeHTTPClient(base_client.HTTPClient): "Server Label": "DB 1" }, "OS-EXT-SRV-ATTR:host": "computenode2", + "security_groups": [{ + 'id': 1, 'name': 'securitygroup1', + 'description': 'FAKE_SECURITY_GROUP', + 'tenant_id': '4ffc664c198e435e9853f2538fbcd7a7' + }, + { + 'id': 2, 'name': 'securitygroup2', + 'description': 'ANOTHER_FAKE_SECURITY_GROUP', + 'tenant_id': '4ffc664c198e435e9853f2538fbcd7a7' + }], }, { "id": 9012, diff --git a/novaclient/tests/v1_1/test_shell.py b/novaclient/tests/v1_1/test_shell.py index 8a1ade65c..bd2caf2a8 100644 --- a/novaclient/tests/v1_1/test_shell.py +++ b/novaclient/tests/v1_1/test_shell.py @@ -20,7 +20,6 @@ import base64 import datetime import os import mock -import sys import fixtures import six @@ -71,13 +70,13 @@ class ShellTest(utils.TestCase): lambda *_: fakes.FakeClient)) self.addCleanup(timeutils.clear_time_override) - @mock.patch('sys.stdout', six.StringIO()) - def run_command(self, cmd): + @mock.patch('sys.stdout', new_callable=six.StringIO) + def run_command(self, cmd, mock_stdout): if isinstance(cmd, list): self.shell.main(cmd) else: self.shell.main(cmd.split()) - return sys.stdout.getvalue() + return mock_stdout.getvalue() def assert_called(self, method, url, body=None, **kwargs): return self.shell.cs.assert_called(method, url, body, **kwargs) @@ -830,6 +829,14 @@ class ShellTest(utils.TestCase): self.assertRaises(exceptions.CommandError, self.run_command, 'show xxx') + @mock.patch('novaclient.v1_1.shell.utils.print_dict') + def test_print_server(self, mock_print_dict): + self.run_command('show 5678') + args, kwargs = mock_print_dict.call_args + parsed_server = args[0] + self.assertEqual('securitygroup1, securitygroup2', + parsed_server['security_groups']) + def test_delete(self): self.run_command('delete 1234') self.assert_called('DELETE', '/servers/1234') diff --git a/novaclient/v1_1/shell.py b/novaclient/v1_1/shell.py index a50c045c2..316fefe05 100644 --- a/novaclient/v1_1/shell.py +++ b/novaclient/v1_1/shell.py @@ -1465,6 +1465,10 @@ def _print_server(cs, args): info['flavor'] = '%s (%s)' % (_find_flavor(cs, flavor_id).name, flavor_id) + if 'security_groups' in info: + info['security_groups'] = \ + ', '.join(group['name'] for group in info['security_groups']) + image = info.get('image', {}) if image: image_id = image.get('id', '')