From 68cf63da7a53a88f0b977bf9f9ce875a51ecb407 Mon Sep 17 00:00:00 2001 From: Flavio Percoco Date: Wed, 16 Sep 2015 10:56:31 +0200 Subject: [PATCH] Don't get the image before deleting it The client currently downloads the image metadata to check if it's deleted or not. This logic belongs to the server and it's already implemented there. Instead of getting the image, send the delete request and catch the 404 error, which is already raised by the server. Change-Id: I1e6ef42340f8e380ff99b9d6ca7ea416e0eebfbc Closes-bug: #1496305 (cherry picked from commit b8a881f5ea89514d715e61b632bc3081a0dde2c6) (cherry picked from commit e82d9df74cbac9accc8a6724c0759760e44bb315) --- glanceclient/v2/shell.py | 8 ++++---- tests/v2/test_shell_v2.py | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/glanceclient/v2/shell.py b/glanceclient/v2/shell.py index 41bcfa71..44ec71a0 100644 --- a/glanceclient/v2/shell.py +++ b/glanceclient/v2/shell.py @@ -296,11 +296,11 @@ def do_image_upload(gc, args): @utils.arg('id', metavar='', help='ID of image to delete.') def do_image_delete(gc, args): """Delete specified image.""" - image = gc.images.get(args.id) - if image and image.status == "deleted": - msg = "No image with an ID of '%s' exists." % image.id + try: + gc.images.delete(args.id) + except exc.HTTPNotFound: + msg = "No image with an ID of '%s' exists." % args.id utils.exit(msg) - gc.images.delete(args.id) @utils.arg('image_id', metavar='', diff --git a/tests/v2/test_shell_v2.py b/tests/v2/test_shell_v2.py index fc8f2db5..56deb93b 100644 --- a/tests/v2/test_shell_v2.py +++ b/tests/v2/test_shell_v2.py @@ -20,6 +20,7 @@ import tempfile import testtools from glanceclient.common import utils +from glanceclient import exc from glanceclient.v2 import shell as test_shell @@ -454,9 +455,8 @@ class ShellV2Test(testtools.TestCase): def test_do_image_delete_deleted(self): image_id = 'deleted-img' args = self._make_args({'id': image_id}) - with mock.patch.object(self.gc.images, 'get') as mocked_get: - mocked_get.return_value = self._make_args({'id': image_id, - 'status': 'deleted'}) + with mock.patch.object(self.gc.images, 'delete') as mocked_get: + mocked_get.side_effect = exc.HTTPNotFound msg = "No image with an ID of '%s' exists." % image_id self.assert_exits_with_msg(func=test_shell.do_image_delete,