From 33e89f99a43ca0ef4273d1896e849b7980f4769b Mon Sep 17 00:00:00 2001 From: Takashi NATSUME Date: Mon, 6 Aug 2018 00:16:29 +0900 Subject: [PATCH] Fix server strings in reboot operation The following message is shown currently in the reboot operation. Request to reboot server has been accepted. The server name string is a bit odd. So fix it as follows. Request to reboot server server1 (ff79e91e-e2a7-4e0f-b4c3-7157676d43c9) has been accepted. Change-Id: I62df4589dc950f69fdc23eafcbb5792e897cb635 Closes-Bug: #1785495 --- novaclient/tests/unit/test_utils.py | 25 +++++++++++++++++++++++++ novaclient/utils.py | 14 +++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/novaclient/tests/unit/test_utils.py b/novaclient/tests/unit/test_utils.py index 46ffbc13a..5927e65a1 100644 --- a/novaclient/tests/unit/test_utils.py +++ b/novaclient/tests/unit/test_utils.py @@ -23,6 +23,7 @@ from novaclient import exceptions from novaclient.tests.unit import fakes from novaclient.tests.unit import utils as test_utils from novaclient import utils +from novaclient.v2 import servers UUID = '8e8ec658-c7b0-4243-bdf8-6f7f2952c0d0' @@ -402,6 +403,30 @@ class DoActionOnManyTestCase(test_utils.TestCase): def test_do_action_on_many_last_fails(self): self._test_do_action_on_many([None, Exception()], fail=True) + @mock.patch('sys.stdout', new_callable=six.StringIO) + def _test_do_action_on_many_resource_string( + self, resource, expected_string, mock_stdout): + utils.do_action_on_many(mock.Mock(), [resource], 'success with %s', + 'error') + self.assertIn('success with %s' % expected_string, + mock_stdout.getvalue()) + + def test_do_action_on_many_resource_string_with_str(self): + self._test_do_action_on_many_resource_string('resource1', 'resource1') + + def test_do_action_on_many_resource_string_with_human_id(self): + resource = servers.Server(None, {'name': 'resource1'}) + self._test_do_action_on_many_resource_string(resource, 'resource1') + + def test_do_action_on_many_resource_string_with_id(self): + resource = servers.Server(None, {'id': UUID}) + self._test_do_action_on_many_resource_string(resource, UUID) + + def test_do_action_on_many_resource_string_with_id_and_human_id(self): + resource = servers.Server(None, {'name': 'resource1', 'id': UUID}) + self._test_do_action_on_many_resource_string(resource, + 'resource1 (%s)' % UUID) + class RecordTimeTestCase(test_utils.TestCase): diff --git a/novaclient/utils.py b/novaclient/utils.py index 6f3cf6408..6de659977 100644 --- a/novaclient/utils.py +++ b/novaclient/utils.py @@ -361,6 +361,18 @@ def safe_issubclass(*args): return False +def _get_resource_string(resource): + if hasattr(resource, 'human_id') and resource.human_id: + if hasattr(resource, 'id') and resource.id: + return "%s (%s)" % (resource.human_id, resource.id) + else: + return resource.human_id + elif hasattr(resource, 'id') and resource.id: + return resource.id + else: + return resource + + def do_action_on_many(action, resources, success_msg, error_msg): """Helper to run an action on many resources.""" failure_flag = False @@ -368,7 +380,7 @@ def do_action_on_many(action, resources, success_msg, error_msg): for resource in resources: try: action(resource) - print(success_msg % resource) + print(success_msg % _get_resource_string(resource)) except Exception as e: failure_flag = True print(encodeutils.safe_encode(six.text_type(e)))