Merge "Define helper to run an action on many resources"
This commit is contained in:
commit
525a510b5a
novaclient
@ -312,3 +312,26 @@ class ResourceManagerExtraKwargsHookTestCase(test_utils.TestCase):
|
||||
except_error = ("Hook 'hook2' is attempting to redefine "
|
||||
"attributes")
|
||||
self.assertIn(except_error, six.text_type(exc))
|
||||
|
||||
|
||||
class DoActionOnManyTestCase(test_utils.TestCase):
|
||||
|
||||
def _test_do_action_on_many(self, side_effect, fail):
|
||||
action = mock.Mock(side_effect=side_effect)
|
||||
|
||||
if fail:
|
||||
self.assertRaises(exceptions.CommandError,
|
||||
utils.do_action_on_many,
|
||||
action, [1, 2], 'success with %s', 'error')
|
||||
else:
|
||||
utils.do_action_on_many(action, [1, 2], 'success with %s', 'error')
|
||||
action.assert_has_calls([mock.call(1), mock.call(2)])
|
||||
|
||||
def test_do_action_on_many_success(self):
|
||||
self._test_do_action_on_many([None, None], fail=False)
|
||||
|
||||
def test_do_action_on_many_first_fails(self):
|
||||
self._test_do_action_on_many([Exception(), None], fail=True)
|
||||
|
||||
def test_do_action_on_many_last_fails(self):
|
||||
self._test_do_action_on_many([None, Exception()], fail=True)
|
||||
|
@ -300,6 +300,22 @@ def safe_issubclass(*args):
|
||||
return False
|
||||
|
||||
|
||||
def do_action_on_many(action, resources, success_msg, error_msg):
|
||||
"""Helper to run an action on many resources."""
|
||||
failure_flag = False
|
||||
|
||||
for resource in resources:
|
||||
try:
|
||||
action(resource)
|
||||
print(success_msg % resource)
|
||||
except Exception as e:
|
||||
failure_flag = True
|
||||
print(e)
|
||||
|
||||
if failure_flag:
|
||||
raise exceptions.CommandError(error_msg)
|
||||
|
||||
|
||||
def _load_entry_point(ep_name, name=None):
|
||||
"""Try to load the entry point ep_name that matches name."""
|
||||
for ep in pkg_resources.iter_entry_points(ep_name, name=name):
|
||||
|
@ -1692,19 +1692,11 @@ def do_show(cs, args):
|
||||
help=_('Name or ID of server(s).'))
|
||||
def do_delete(cs, args):
|
||||
"""Immediately shut down and delete specified server(s)."""
|
||||
failure_flag = False
|
||||
|
||||
for server in args.server:
|
||||
try:
|
||||
_find_server(cs, server).delete()
|
||||
print(_("Request to delete server %s has been accepted.") % server)
|
||||
except Exception as e:
|
||||
failure_flag = True
|
||||
print(e)
|
||||
|
||||
if failure_flag:
|
||||
raise exceptions.CommandError(_("Unable to delete the "
|
||||
"specified server(s)."))
|
||||
utils.do_action_on_many(
|
||||
lambda s: _find_server(cs, s).delete(),
|
||||
args.server,
|
||||
_("Request to delete server %s has been accepted."),
|
||||
_("Unable to delete the specified server(s)."))
|
||||
|
||||
|
||||
def _find_server(cs, server):
|
||||
|
Loading…
x
Reference in New Issue
Block a user