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
This commit is contained in:
Takashi NATSUME 2018-08-06 00:16:29 +09:00
parent 045f641cec
commit 33e89f99a4
2 changed files with 38 additions and 1 deletions

View File

@ -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):

View File

@ -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)))