Return 204 when image data does not exist

Currently a GET on image-data in glance v2 returns 404 when data does not
exist. But according to the API docs the HTTP status code returned in this
case should be 204.

This fix makes Glance return the right HTTP status code.

Change-Id: I34cf68e96f631f05cff349329245f74aee42397c
Closes-Bug: #1251055
This commit is contained in:
David Koo 2013-12-12 12:00:11 +08:00
parent 59d149aad7
commit 55937308c2
5 changed files with 17 additions and 5 deletions

View File

@ -210,7 +210,8 @@ class CacheFilter(wsgi.Middleware):
images Resource, removing image file from the cache images Resource, removing image file from the cache
if necessary if necessary
""" """
if not 200 <= self.get_status_code(resp) < 300: status_code = self.get_status_code(resp)
if not 200 <= status_code < 300:
return resp return resp
try: try:
@ -218,6 +219,12 @@ class CacheFilter(wsgi.Middleware):
except TypeError: except TypeError:
return resp return resp
if method == 'GET' and status_code == 204:
# Bugfix:1251055 - Don't cache non-existent image files.
# NOTE: Both GET for an image without locations and DELETE return
# 204 but DELETE should be processed.
return resp
method_str = '_process_%s_response' % method method_str = '_process_%s_response' % method
try: try:
process_response_method = getattr(self, method_str) process_response_method = getattr(self, method_str)

View File

@ -126,8 +126,9 @@ class ImageDataController(object):
try: try:
image = image_repo.get(image_id) image = image_repo.get(image_id)
if not image.locations: if not image.locations:
reason = _("No image data could be found") raise exception.ImageDataNotFound()
raise exception.NotFound(reason) except exception.ImageDataNotFound as e:
raise webob.exc.HTTPNoContent(explanation=unicode(e))
except exception.NotFound as e: except exception.NotFound as e:
raise webob.exc.HTTPNotFound(explanation=unicode(e)) raise webob.exc.HTTPNotFound(explanation=unicode(e))
except exception.Forbidden as e: except exception.Forbidden as e:

View File

@ -319,3 +319,7 @@ class InvalidTaskStatusTransition(TaskException, Invalid):
class DuplicateLocation(Duplicate): class DuplicateLocation(Duplicate):
message = _("The location %(location)s already exists") message = _("The location %(location)s already exists")
class ImageDataNotFound(NotFound):
message = _("No image data could be found")

View File

@ -303,7 +303,7 @@ class TestImages(functional.FunctionalTest):
path = self._url('/v2/images/%s/file' % image_id) path = self._url('/v2/images/%s/file' % image_id)
headers = self._headers() headers = self._headers()
response = requests.get(path, headers=headers) response = requests.get(path, headers=headers)
self.assertEqual(404, response.status_code) self.assertEqual(204, response.status_code)
# Upload some image data # Upload some image data
path = self._url('/v2/images/%s/file' % image_id) path = self._url('/v2/images/%s/file' % image_id)

View File

@ -109,7 +109,7 @@ class TestImagesController(base.StoreClearingUnitTest):
def test_download_no_location(self): def test_download_no_location(self):
request = unit_test_utils.get_fake_request() request = unit_test_utils.get_fake_request()
self.image_repo.result = FakeImage('abcd') self.image_repo.result = FakeImage('abcd')
self.assertRaises(webob.exc.HTTPNotFound, self.controller.download, self.assertRaises(webob.exc.HTTPNoContent, self.controller.download,
request, unit_test_utils.UUID2) request, unit_test_utils.UUID2)
def test_download_non_existent_image(self): def test_download_non_existent_image(self):