Merge "Error handling of multi REST API calls for "snapshot set" command"

This commit is contained in:
Jenkins 2016-09-11 05:50:40 +00:00 committed by Gerrit Code Review
commit 30afdb9684
3 changed files with 94 additions and 10 deletions

View File

@ -376,6 +376,55 @@ class TestSnapshotSet(TestSnapshot):
self.snapshot.id, "error") self.snapshot.id, "error")
self.assertIsNone(result) self.assertIsNone(result)
def test_volume_set_state_failed(self):
self.snapshots_mock.reset_state.side_effect = exceptions.CommandError()
arglist = [
'--state', 'error',
self.snapshot.id
]
verifylist = [
('state', 'error'),
('snapshot', self.snapshot.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.snapshots_mock.reset_state.assert_called_once_with(
self.snapshot.id, 'error')
def test_volume_set_name_and_state_failed(self):
self.snapshots_mock.reset_state.side_effect = exceptions.CommandError()
arglist = [
'--state', 'error',
"--name", "new_snapshot",
self.snapshot.id
]
verifylist = [
('state', 'error'),
("name", "new_snapshot"),
('snapshot', self.snapshot.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))
kwargs = {
"name": "new_snapshot",
}
self.snapshots_mock.update.assert_called_once_with(
self.snapshot.id, **kwargs)
self.snapshots_mock.reset_state.assert_called_once_with(
self.snapshot.id, 'error')
class TestSnapshotShow(TestSnapshot): class TestSnapshotShow(TestSnapshot):

View File

@ -218,17 +218,31 @@ class SetSnapshot(command.Command):
snapshot = utils.find_resource(volume_client.volume_snapshots, snapshot = utils.find_resource(volume_client.volume_snapshots,
parsed_args.snapshot) parsed_args.snapshot)
result = 0
if parsed_args.property: if parsed_args.property:
volume_client.volume_snapshots.set_metadata(snapshot.id, try:
parsed_args.property) volume_client.volume_snapshots.set_metadata(
snapshot.id, parsed_args.property)
except Exception as e:
LOG.error(_("Failed to set snapshot property: %s"), e)
result += 1
kwargs = {} kwargs = {}
if parsed_args.name: if parsed_args.name:
kwargs['display_name'] = parsed_args.name kwargs['display_name'] = parsed_args.name
if parsed_args.description: if parsed_args.description:
kwargs['display_description'] = parsed_args.description kwargs['display_description'] = parsed_args.description
if kwargs:
try:
snapshot.update(**kwargs) snapshot.update(**kwargs)
except Exception as e:
LOG.error(_("Failed to update snapshot display name "
"or display description: %s"), e)
result += 1
if result > 0:
raise exceptions.CommandError(_("One or more of the "
"set operations failed"))
class ShowSnapshot(command.ShowOne): class ShowSnapshot(command.ShowOne):

View File

@ -240,19 +240,40 @@ class SetSnapshot(command.Command):
snapshot = utils.find_resource(volume_client.volume_snapshots, snapshot = utils.find_resource(volume_client.volume_snapshots,
parsed_args.snapshot) parsed_args.snapshot)
result = 0
if parsed_args.property:
try:
volume_client.volume_snapshots.set_metadata(
snapshot.id, parsed_args.property)
except Exception as e:
LOG.error(_("Failed to set snapshot property: %s"), e)
result += 1
if parsed_args.state:
try:
volume_client.volume_snapshots.reset_state(
snapshot.id, parsed_args.state)
except Exception as e:
LOG.error(_("Failed to set snapshot state: %s"), e)
result += 1
kwargs = {} kwargs = {}
if parsed_args.name: if parsed_args.name:
kwargs['name'] = parsed_args.name kwargs['name'] = parsed_args.name
if parsed_args.description: if parsed_args.description:
kwargs['description'] = parsed_args.description kwargs['description'] = parsed_args.description
if kwargs:
try:
volume_client.volume_snapshots.update(
snapshot.id, **kwargs)
except Exception as e:
LOG.error(_("Failed to update snapshot name "
"or description: %s"), e)
result += 1
if parsed_args.property: if result > 0:
volume_client.volume_snapshots.set_metadata(snapshot.id, raise exceptions.CommandError(_("One or more of the "
parsed_args.property) "set operations failed"))
if parsed_args.state:
volume_client.volume_snapshots.reset_state(snapshot.id,
parsed_args.state)
volume_client.volume_snapshots.update(snapshot.id, **kwargs)
class ShowSnapshot(command.ShowOne): class ShowSnapshot(command.ShowOne):