Enforce exclusive options snapshot-id, source-volid and image-id

Fixed checks in api that volume create receives only one of
snapshot-id, source-volid or image-id.

Fixes bug #1161437

Change-Id: Ibe5ca4bb81e69b0f8e1abe1c2cffe587dc10e3ca
This commit is contained in:
Stephen Mulcahy 2013-03-28 15:46:16 +00:00
parent 4d0985ea85
commit a4540aae66
6 changed files with 19 additions and 46 deletions

View File

@ -342,9 +342,6 @@ class VolumeController(wsgi.Controller):
image_uuid = None
if self.ext_mgr.is_loaded('os-image-create'):
image_href = volume.get('imageRef')
if snapshot_id and image_href:
msg = _("Snapshot and image cannot be specified together.")
raise exc.HTTPBadRequest(explanation=msg)
if image_href:
image_uuid = self._image_uuid_from_href(image_href)
kwargs['image_id'] = image_uuid

View File

@ -275,9 +275,6 @@ class VolumeController(wsgi.Controller):
image_uuid = None
if self.ext_mgr.is_loaded('os-image-create'):
image_href = volume.get('imageRef')
if snapshot_id and image_href:
msg = _("Snapshot and image cannot be specified together.")
raise exc.HTTPBadRequest(explanation=msg)
if image_href:
image_uuid = self._image_uuid_from_href(image_href)
kwargs['image_id'] = image_uuid

View File

@ -155,24 +155,6 @@ class VolumeApiTest(test.TestCase):
res_dict = self.controller.create(req, body)
self.assertEqual(res_dict, expected)
def test_volume_create_with_image_id_and_snapshot_id(self):
self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create)
self.stubs.Set(volume_api.API, "get_snapshot", stub_snapshot_get)
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": 'c905cedb-7281-47e4-8a62-f26bc5fc4c77',
"source_volid": None,
"snapshot_id": TEST_SNAPSHOT_UUID}
body = {"volume": vol}
req = fakes.HTTPRequest.blank('/v1/volumes')
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.create,
req,
body)
def test_volume_create_with_image_id_is_integer(self):
self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create)
self.ext_mgr.extensions = {'os-image-create': 'fake'}

View File

@ -166,25 +166,6 @@ class VolumeApiTest(test.TestCase):
res_dict = self.controller.create(req, body)
self.assertEqual(res_dict, expected)
def test_volume_create_with_image_id_and_snapshot_id(self):
self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create)
self.stubs.Set(volume_api.API, "get_snapshot", stub_snapshot_get)
self.ext_mgr.extensions = {'os-image-create': 'fake'}
vol = {
"size": '1',
"name": "Volume Test Name",
"description": "Volume Test Desc",
"availability_zone": "cinder",
"imageRef": 'c905cedb-7281-47e4-8a62-f26bc5fc4c77',
"snapshot_id": TEST_SNAPSHOT_UUID
}
body = {"volume": vol}
req = fakes.HTTPRequest.blank('/v2/volumes')
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.create,
req,
body)
def test_volume_create_with_image_id_is_integer(self):
self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create)
self.ext_mgr.extensions = {'os-image-create': 'fake'}

View File

@ -253,6 +253,19 @@ class VolumeTestCase(test.TestCase):
self.volume.delete_snapshot(self.context, snapshot_id)
self.volume.delete_volume(self.context, volume_src['id'])
def test_create_volume_with_invalid_exclusive_options(self):
"""Test volume create with multiple exclusive options fails."""
volume_api = cinder.volume.api.API()
self.assertRaises(exception.InvalidInput,
volume_api.create,
self.context,
1,
'name',
'description',
snapshot='fake_id',
image_id='fake_id',
source_volume='fake_id')
def test_too_big_volume(self):
"""Ensure failure if a too large of a volume is requested."""
# FIXME(vish): validation needs to move into the data layer in

View File

@ -89,9 +89,12 @@ class API(base.Base):
image_id=None, volume_type=None, metadata=None,
availability_zone=None, source_volume=None):
if ((snapshot is not None) and (source_volume is not None)):
msg = (_("May specify either snapshot, "
"or src volume but not both!"))
exclusive_options = (snapshot, image_id, source_volume)
exclusive_options_set = sum(1 for option in
exclusive_options if option is not None)
if exclusive_options_set > 1:
msg = (_("May specify only one of snapshot, imageRef "
"or source volume"))
raise exception.InvalidInput(reason=msg)
check_policy(context, 'create')