summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakashi NATSUME <natsume.takashi@lab.ntt.co.jp>2018-08-06 00:16:29 +0900
committerTakashi NATSUME <natsume.takashi@lab.ntt.co.jp>2018-08-07 15:30:48 +0900
commit33e89f99a43ca0ef4273d1896e849b7980f4769b (patch)
tree3e8765a5591896a3f67be7d81778ced07b702b0c
parent045f641cec19c6d2826ef20e3ed1409b1991836f (diff)
downloadpython-novaclient-33e89f99a43ca0ef4273d1896e849b7980f4769b.tar.gz
Fix server strings in reboot operation
The following message is shown currently in the reboot operation. Request to reboot server <Server: server1> 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
-rw-r--r--novaclient/tests/unit/test_utils.py25
-rw-r--r--novaclient/utils.py14
2 files changed, 38 insertions, 1 deletions
diff --git a/novaclient/tests/unit/test_utils.py b/novaclient/tests/unit/test_utils.py
index 46ffbc13..5927e65a 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 6f3cf640..6de65997 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)))