From 728b4e5f5f24db398e44d233b992ca0bc38fe763 Mon Sep 17 00:00:00 2001 From: "jaypipes@gmail.com" <> Date: Thu, 14 Oct 2010 13:23:37 -0400 Subject: [PATCH] Adds DELETE to the Parallax REST API. --- glance/parallax/controllers.py | 12 ++++++++-- tests/stubs.py | 6 +++-- tests/unit/test_parallax_api.py | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/glance/parallax/controllers.py b/glance/parallax/controllers.py index 75f5a2be6f..b4b8537776 100644 --- a/glance/parallax/controllers.py +++ b/glance/parallax/controllers.py @@ -74,8 +74,16 @@ class ImageController(wsgi.Controller): return dict(image=self._make_image_dict(image)) def delete(self, req, id): - """Delete is not currently supported """ - raise exc.HTTPNotImplemented() + """Deletes an existing image with the registry. + + :param req: Request body. Ignored. + :param id: The opaque internal identifier for the image + + :retval Returns 200 if delete was successful, a fault if not. + + """ + context = None + updated_image = db.image_destroy(context, id) def create(self, req): """Registers a new image with the registry. diff --git a/tests/stubs.py b/tests/stubs.py index 7dcf0d3acc..c8594f84a5 100644 --- a/tests/stubs.py +++ b/tests/stubs.py @@ -202,7 +202,8 @@ def stub_out_parallax_db_image_api(stubs): values['status'] = 'available' else: if not values['status'] in self.VALID_STATUSES: - raise exception.Invalid("Invalid status '%s' for image" % values['status']) + raise exception.Invalid("Invalid status '%s' for image" % + values['status']) self.next_id += 1 self.images.append(values) @@ -222,7 +223,8 @@ def stub_out_parallax_db_image_api(stubs): images = [i for i in self.images if str(i['id']) == str(image_id)] if len(images) != 1 or images[0]['deleted']: - new_exc = exception.NotFound("No model for id %s %s" % (image_id, str(self.images))) + new_exc = exception.NotFound("No model for id %s %s" % + (image_id, str(self.images))) raise new_exc.__class__, new_exc, sys.exc_info()[2] else: return images[0] diff --git a/tests/unit/test_parallax_api.py b/tests/unit/test_parallax_api.py index d004d2c7c1..33a59f9a22 100644 --- a/tests/unit/test_parallax_api.py +++ b/tests/unit/test_parallax_api.py @@ -180,3 +180,45 @@ class TestImageController(unittest.TestCase): # over to Glance to support exception catching into # standard HTTP errors. self.assertRaises(exception.NotFound, req.get_response, controllers.API()) + + def test_delete_image(self): + """Tests that the /images DELETE parallax API deletes the image""" + + # Grab the original number of images + req = webob.Request.blank('/images') + res = req.get_response(controllers.API()) + res_dict = json.loads(res.body) + self.assertEquals(res.status_int, 200) + + orig_num_images = len(res_dict['images']) + + # Delete image #2 + req = webob.Request.blank('/images/2') + + req.method = 'DELETE' + + res = req.get_response(controllers.API()) + + self.assertEquals(res.status_int, 200) + + # Verify one less image + req = webob.Request.blank('/images') + res = req.get_response(controllers.API()) + res_dict = json.loads(res.body) + self.assertEquals(res.status_int, 200) + + new_num_images = len(res_dict['images']) + self.assertEquals(new_num_images, orig_num_images - 1) + + def test_delete_image_not_existing(self): + """Tests proper exception is raised if attempt to delete non-existing + image""" + + req = webob.Request.blank('/images/3') + + req.method = 'DELETE' + + # TODO(jaypipes): Port Nova's Fault infrastructure + # over to Glance to support exception catching into + # standard HTTP errors. + self.assertRaises(exception.NotFound, req.get_response, controllers.API())