Skip to content

Commit 3202fef

Browse files
author
Huanxuan Ao
committed
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
1 parent 0472b9e commit 3202fef

2 files changed

Lines changed: 62 additions & 14 deletions

File tree

openstackclient/tests/volume/v2/test_volume.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,27 @@ def test_volume_set_state(self):
860860
self.new_volume.id, 'error')
861861
self.assertIsNone(result)
862862

863+
def test_volume_set_state_failed(self):
864+
self.volumes_mock.reset_state.side_effect = exceptions.CommandError()
865+
arglist = [
866+
'--state', 'error',
867+
self.new_volume.id
868+
]
869+
verifylist = [
870+
('state', 'error'),
871+
('volume', self.new_volume.id)
872+
]
873+
874+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
875+
try:
876+
self.cmd.take_action(parsed_args)
877+
self.fail('CommandError should be raised.')
878+
except exceptions.CommandError as e:
879+
self.assertEqual('One or more of the set operations failed',
880+
str(e))
881+
self.volumes_mock.reset_state.assert_called_with(
882+
self.new_volume.id, 'error')
883+
863884

864885
class TestVolumeShow(TestVolume):
865886

openstackclient/volume/v2/volume.py

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -394,32 +394,59 @@ def take_action(self, parsed_args):
394394
volume_client = self.app.client_manager.volume
395395
volume = utils.find_resource(volume_client.volumes, parsed_args.volume)
396396

397+
result = 0
397398
if parsed_args.size:
398-
if volume.status != 'available':
399-
LOG.error(_("Volume is in %s state, it must be available "
400-
"before size can be extended"), volume.status)
401-
return
402-
if parsed_args.size <= volume.size:
403-
LOG.error(_("New size must be greater than %s GB"),
404-
volume.size)
405-
return
406-
volume_client.volumes.extend(volume.id, parsed_args.size)
399+
try:
400+
if volume.status != 'available':
401+
msg = (_("Volume is in %s state, it must be available "
402+
"before size can be extended"), volume.status)
403+
raise exceptions.CommandError(msg)
404+
if parsed_args.size <= volume.size:
405+
msg = _("New size must be greater than %s GB"), volume.size
406+
raise exceptions.CommandError(msg)
407+
volume_client.volumes.extend(volume.id, parsed_args.size)
408+
except Exception as e:
409+
LOG.error(_("Failed to set volume size: %s"), e)
410+
result += 1
407411

408412
if parsed_args.property:
409-
volume_client.volumes.set_metadata(volume.id, parsed_args.property)
413+
try:
414+
volume_client.volumes.set_metadata(
415+
volume.id, parsed_args.property)
416+
except Exception as e:
417+
LOG.error(_("Failed to set volume property: %s"), e)
418+
result += 1
410419
if parsed_args.image_property:
411-
volume_client.volumes.set_image_metadata(
412-
volume.id, parsed_args.image_property)
420+
try:
421+
volume_client.volumes.set_image_metadata(
422+
volume.id, parsed_args.image_property)
423+
except Exception as e:
424+
LOG.error(_("Failed to set image property: %s"), e)
425+
result += 1
413426
if parsed_args.state:
414-
volume_client.volumes.reset_state(volume.id, parsed_args.state)
427+
try:
428+
volume_client.volumes.reset_state(
429+
volume.id, parsed_args.state)
430+
except Exception as e:
431+
LOG.error(_("Failed to set volume state: %s"), e)
432+
result += 1
415433

416434
kwargs = {}
417435
if parsed_args.name:
418436
kwargs['display_name'] = parsed_args.name
419437
if parsed_args.description:
420438
kwargs['display_description'] = parsed_args.description
421439
if kwargs:
422-
volume_client.volumes.update(volume.id, **kwargs)
440+
try:
441+
volume_client.volumes.update(volume.id, **kwargs)
442+
except Exception as e:
443+
LOG.error(_("Failed to update volume display name "
444+
"or display description: %s"), e)
445+
result += 1
446+
447+
if result > 0:
448+
raise exceptions.CommandError(_("One or more of the "
449+
"set operations failed"))
423450

424451

425452
class ShowVolume(command.ShowOne):

0 commit comments

Comments
 (0)