Fix image delete multiple arguments error

Fix image delete command, support processing multiple arguments
delete error.
Fix doc/source/command-errors.rst, make the msg format correct.

Change-Id: Icbe347fe077bc148bf71ea8f7399b0e934b7cdf9
Partially-Implements: blueprint multi-argument-image
This commit is contained in:
sunyajing 2016-06-01 13:48:23 +08:00
parent 0695d1495e
commit 8c7e34d65c
3 changed files with 52 additions and 10 deletions

View File

@ -197,9 +197,6 @@ multiple ``delete_network()`` calls.
if ret > 0:
total = len(parsed_args.network)
msg = _("Failed to delete %(ret)s of %(total)s networks.") %
{
"ret": ret,
"total": total,
}
msg = (_("Failed to delete %(ret)s of %(total)s networks.")
% {"ret": ret, "total": total})
raise exceptions.CommandError(msg)

View File

@ -372,13 +372,27 @@ class DeleteImage(command.Command):
return parser
def take_action(self, parsed_args):
del_result = 0
image_client = self.app.client_manager.image
for image in parsed_args.images:
try:
image_obj = utils.find_resource(
image_client.images,
image,
)
image_client.images.delete(image_obj.id)
except Exception as e:
del_result += 1
self.app.log.error(_("Failed to delete image with "
"name or ID '%(image)s': %(e)s")
% {'image': image, 'e': e})
total = len(parsed_args.images)
if (del_result > 0):
msg = (_("Failed to delete %(dresult)s of %(total)s images.")
% {'dresult': del_result, 'total': total})
raise exceptions.CommandError(msg)
class ListImage(command.Lister):

View File

@ -473,6 +473,37 @@ class TestImageDelete(TestImage):
self.images_mock.delete.assert_has_calls(calls)
self.assertIsNone(result)
def test_image_delete_multi_images_exception(self):
images = image_fakes.FakeImage.create_images(count=2)
arglist = [
images[0].id,
images[1].id,
'x-y-x',
]
verifylist = [
('images', arglist)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
# Fake exception in utils.find_resource()
# In image v2, we use utils.find_resource() to find a network.
# It calls get() several times, but find() only one time. So we
# choose to fake get() always raise exception, then pass through.
# And fake find() to find the real network or not.
ret_find = [
images[0],
images[1],
exceptions.NotFound('404'),
]
self.images_mock.get = Exception()
self.images_mock.find.side_effect = ret_find
self.assertRaises(exceptions.CommandError, self.cmd.take_action,
parsed_args)
calls = [mock.call(i.id) for i in images]
self.images_mock.delete.assert_has_calls(calls)
class TestImageList(TestImage):