Support multi REST API calls error handling for "volume set" command

Support the error handling follow the rule in
doc/source/command-errors.rst
Also add a unit test for testing the error handling

Change-Id: I98064f4b8c1dc17eb3874f7b25c827a568463c0f
This commit is contained in:
Huanxuan Ao 2016-08-05 18:54:29 +08:00
parent 0472b9ec6d
commit 3202fefc65
2 changed files with 62 additions and 14 deletions

View File

@ -860,6 +860,27 @@ class TestVolumeSet(TestVolume):
self.new_volume.id, 'error') self.new_volume.id, 'error')
self.assertIsNone(result) self.assertIsNone(result)
def test_volume_set_state_failed(self):
self.volumes_mock.reset_state.side_effect = exceptions.CommandError()
arglist = [
'--state', 'error',
self.new_volume.id
]
verifylist = [
('state', 'error'),
('volume', self.new_volume.id)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
try:
self.cmd.take_action(parsed_args)
self.fail('CommandError should be raised.')
except exceptions.CommandError as e:
self.assertEqual('One or more of the set operations failed',
str(e))
self.volumes_mock.reset_state.assert_called_with(
self.new_volume.id, 'error')
class TestVolumeShow(TestVolume): class TestVolumeShow(TestVolume):

View File

@ -394,24 +394,42 @@ class SetVolume(command.Command):
volume_client = self.app.client_manager.volume volume_client = self.app.client_manager.volume
volume = utils.find_resource(volume_client.volumes, parsed_args.volume) volume = utils.find_resource(volume_client.volumes, parsed_args.volume)
result = 0
if parsed_args.size: if parsed_args.size:
try:
if volume.status != 'available': if volume.status != 'available':
LOG.error(_("Volume is in %s state, it must be available " msg = (_("Volume is in %s state, it must be available "
"before size can be extended"), volume.status) "before size can be extended"), volume.status)
return raise exceptions.CommandError(msg)
if parsed_args.size <= volume.size: if parsed_args.size <= volume.size:
LOG.error(_("New size must be greater than %s GB"), msg = _("New size must be greater than %s GB"), volume.size
volume.size) raise exceptions.CommandError(msg)
return
volume_client.volumes.extend(volume.id, parsed_args.size) volume_client.volumes.extend(volume.id, parsed_args.size)
except Exception as e:
LOG.error(_("Failed to set volume size: %s"), e)
result += 1
if parsed_args.property: if parsed_args.property:
volume_client.volumes.set_metadata(volume.id, parsed_args.property) try:
volume_client.volumes.set_metadata(
volume.id, parsed_args.property)
except Exception as e:
LOG.error(_("Failed to set volume property: %s"), e)
result += 1
if parsed_args.image_property: if parsed_args.image_property:
try:
volume_client.volumes.set_image_metadata( volume_client.volumes.set_image_metadata(
volume.id, parsed_args.image_property) volume.id, parsed_args.image_property)
except Exception as e:
LOG.error(_("Failed to set image property: %s"), e)
result += 1
if parsed_args.state: if parsed_args.state:
volume_client.volumes.reset_state(volume.id, parsed_args.state) try:
volume_client.volumes.reset_state(
volume.id, parsed_args.state)
except Exception as e:
LOG.error(_("Failed to set volume state: %s"), e)
result += 1
kwargs = {} kwargs = {}
if parsed_args.name: if parsed_args.name:
@ -419,7 +437,16 @@ class SetVolume(command.Command):
if parsed_args.description: if parsed_args.description:
kwargs['display_description'] = parsed_args.description kwargs['display_description'] = parsed_args.description
if kwargs: if kwargs:
try:
volume_client.volumes.update(volume.id, **kwargs) volume_client.volumes.update(volume.id, **kwargs)
except Exception as e:
LOG.error(_("Failed to update volume display name "
"or display description: %s"), e)
result += 1
if result > 0:
raise exceptions.CommandError(_("One or more of the "
"set operations failed"))
class ShowVolume(command.ShowOne): class ShowVolume(command.ShowOne):