From eacab523be3d8f2f97809c785dba98e5e2e75c7b Mon Sep 17 00:00:00 2001 From: ling-yun Date: Fri, 4 Apr 2014 21:58:14 +0800 Subject: [PATCH] Create volume fail when image id is "" When call create volume api with image id "", it will create an empty volume. But when create volume with snapshot id "" or srv volume "", it will return an 404 error. So it should create volume fail when call create volume api with image id "". Change-Id: If2c7b061753bbbd172a09d35b343e73e90a1b7b4 Closes-Bug: #1302609 --- cinder/api/v1/volumes.py | 2 +- cinder/api/v2/volumes.py | 2 +- cinder/tests/api/v1/test_volumes.py | 15 +++++++++++++++ cinder/tests/api/v2/test_volumes.py | 15 +++++++++++++++ cinder/volume/flows/api/create_volume.py | 2 +- 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/cinder/api/v1/volumes.py b/cinder/api/v1/volumes.py index 1b1f1b0c2..56458ddae 100644 --- a/cinder/api/v1/volumes.py +++ b/cinder/api/v1/volumes.py @@ -419,7 +419,7 @@ class VolumeController(wsgi.Controller): if self.ext_mgr.is_loaded('os-image-create'): # NOTE(jdg): misleading name "imageRef" as it's an image-id image_href = volume.get('imageRef') - if image_href: + if image_href is not None: image_uuid = self._image_uuid_from_href(image_href) kwargs['image_id'] = image_uuid diff --git a/cinder/api/v2/volumes.py b/cinder/api/v2/volumes.py index 4a938bac0..a0910ba0e 100644 --- a/cinder/api/v2/volumes.py +++ b/cinder/api/v2/volumes.py @@ -332,7 +332,7 @@ class VolumeController(wsgi.Controller): if self.ext_mgr.is_loaded('os-image-create'): image_href = volume.get('imageRef') - if image_href: + if image_href is not None: image_uuid = self._image_uuid_from_href(image_href) kwargs['image_id'] = image_uuid diff --git a/cinder/tests/api/v1/test_volumes.py b/cinder/tests/api/v1/test_volumes.py index 1dcf653ec..4553f4ca6 100644 --- a/cinder/tests/api/v1/test_volumes.py +++ b/cinder/tests/api/v1/test_volumes.py @@ -231,6 +231,21 @@ class VolumeApiTest(test.TestCase): req, body) + def test_volume_create_with_image_id_with_empty_string(self): + self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) + self.ext_mgr.extensions = {'os-image-create': 'fake'} + vol = {"size": 1, + "display_name": "Volume Test Name", + "display_description": "Volume Test Desc", + "availability_zone": "cinder", + "imageRef": ''} + body = {"volume": vol} + req = fakes.HTTPRequest.blank('/v1/volumes') + self.assertRaises(webob.exc.HTTPBadRequest, + self.controller.create, + req, + body) + def test_volume_update(self): self.stubs.Set(db, 'volume_get', stubs.stub_volume_get_db) self.stubs.Set(volume_api.API, "update", stubs.stub_volume_update) diff --git a/cinder/tests/api/v2/test_volumes.py b/cinder/tests/api/v2/test_volumes.py index fc6c183fe..662537a00 100644 --- a/cinder/tests/api/v2/test_volumes.py +++ b/cinder/tests/api/v2/test_volumes.py @@ -257,6 +257,21 @@ class VolumeApiTest(test.TestCase): req, body) + def test_volume_create_with_image_id_with_empty_string(self): + self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) + self.ext_mgr.extensions = {'os-image-create': 'fake'} + vol = {"size": 1, + "display_name": "Volume Test Name", + "display_description": "Volume Test Desc", + "availability_zone": "cinder", + "imageRef": ''} + body = {"volume": vol} + req = fakes.HTTPRequest.blank('/v2/volumes') + self.assertRaises(webob.exc.HTTPBadRequest, + self.controller.create, + req, + body) + def test_volume_update(self): self.stubs.Set(volume_api.API, 'get', stubs.stub_volume_get) self.stubs.Set(volume_api.API, "update", stubs.stub_volume_update) diff --git a/cinder/volume/flows/api/create_volume.py b/cinder/volume/flows/api/create_volume.py index da985cbc2..8ef6bb506 100644 --- a/cinder/volume/flows/api/create_volume.py +++ b/cinder/volume/flows/api/create_volume.py @@ -172,7 +172,7 @@ class ExtractVolumeRequestTask(flow_utils.CinderTask): """Checks image existence and validates that the image metadata.""" # Check image existence - if not image_id: + if image_id is None: return # NOTE(harlowja): this should raise an error if the image does not -- 2.45.2