From 64f24b9323b0860ddf01bcd012f986c65d2956cc Mon Sep 17 00:00:00 2001 From: Zhiteng Huang Date: Tue, 24 Sep 2013 16:09:42 +0800 Subject: [PATCH] Allow v2 Volume API to create volume with type name v2 API only accepts volume type ID when creating volume, while v1 API allows both volume type name and ID (volume type name is unique in Cinder). This patch enables v2 API to accept volume type name as create() parameter. Aslo volume type validation code block in v1 API create() is refactored to a slimmer version. Change-Id: I4ee98b67a0a0e98ff4d402749e12b1335d45f64d Close-bug: 1228835 --- cinder/api/v1/volumes.py | 16 ++++++---------- cinder/api/v2/volumes.py | 11 ++++++++--- cinder/tests/api/v1/test_volumes.py | 19 ++++++++++++++++++- cinder/tests/api/v2/test_volumes.py | 16 +++++++++++++++- 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/cinder/api/v1/volumes.py b/cinder/api/v1/volumes.py index 1cef310585a..2c4077551c6 100644 --- a/cinder/api/v1/volumes.py +++ b/cinder/api/v1/volumes.py @@ -357,21 +357,17 @@ def create(self, req, body): req_volume_type = volume.get('volume_type', None) if req_volume_type: - if not uuidutils.is_uuid_like(req_volume_type): - try: + try: + if not uuidutils.is_uuid_like(req_volume_type): kwargs['volume_type'] = \ volume_types.get_volume_type_by_name( context, req_volume_type) - except exception.VolumeTypeNotFound: - explanation = 'Volume type not found.' - raise exc.HTTPNotFound(explanation=explanation) - else: - try: + else: kwargs['volume_type'] = volume_types.get_volume_type( context, req_volume_type) - except exception.VolumeTypeNotFound: - explanation = 'Volume type not found.' - raise exc.HTTPNotFound(explanation=explanation) + except exception.VolumeTypeNotFound: + explanation = 'Volume type not found.' + raise exc.HTTPNotFound(explanation=explanation) kwargs['metadata'] = volume.get('metadata', None) diff --git a/cinder/api/v2/volumes.py b/cinder/api/v2/volumes.py index 182edf2517b..71cd4a272c6 100644 --- a/cinder/api/v2/volumes.py +++ b/cinder/api/v2/volumes.py @@ -330,10 +330,15 @@ def create(self, req, body): req_volume_type = volume.get('volume_type', None) if req_volume_type: try: - kwargs['volume_type'] = volume_types.get_volume_type( - context, req_volume_type) + if not uuidutils.is_uuid_like(req_volume_type): + kwargs['volume_type'] = \ + volume_types.get_volume_type_by_name( + context, req_volume_type) + else: + kwargs['volume_type'] = volume_types.get_volume_type( + context, req_volume_type) except exception.VolumeTypeNotFound: - msg = _("Volume type not found") + msg = _("Volume type not found.") raise exc.HTTPNotFound(explanation=msg) kwargs['metadata'] = volume.get('metadata', None) diff --git a/cinder/tests/api/v1/test_volumes.py b/cinder/tests/api/v1/test_volumes.py index b09b39ed22a..1e9f7c4166c 100644 --- a/cinder/tests/api/v1/test_volumes.py +++ b/cinder/tests/api/v1/test_volumes.py @@ -108,10 +108,27 @@ def test_volume_create_with_type(self): "display_name": "Volume Test Name", "display_description": "Volume Test Desc", "availability_zone": "zone1:host1", - "volume_type": db_vol_type['name'], } + "volume_type": "FakeTypeName"} body = {"volume": vol} req = fakes.HTTPRequest.blank('/v1/volumes') + # Raise 404 when type name isn't valid + self.assertRaises(webob.exc.HTTPNotFound, self.controller.create, + req, body) + # Use correct volume type name + vol.update(dict(volume_type=CONF.default_volume_type)) + body.update(dict(volume=vol)) + res_dict = self.controller.create(req, body) + volume_id = res_dict['volume']['id'] + self.assertEqual(len(res_dict), 1) + self.assertEqual(res_dict['volume']['volume_type'], + db_vol_type['name']) + + # Use correct volume type id + vol.update(dict(volume_type=db_vol_type['id'])) + body.update(dict(volume=vol)) res_dict = self.controller.create(req, body) + volume_id = res_dict['volume']['id'] + self.assertEqual(len(res_dict), 1) self.assertEqual(res_dict['volume']['volume_type'], db_vol_type['name']) diff --git a/cinder/tests/api/v2/test_volumes.py b/cinder/tests/api/v2/test_volumes.py index b31a3076d0d..a44dc737e4f 100644 --- a/cinder/tests/api/v2/test_volumes.py +++ b/cinder/tests/api/v2/test_volumes.py @@ -114,10 +114,24 @@ def test_volume_create_with_type(self): "name": "Volume Test Name", "description": "Volume Test Desc", "availability_zone": "zone1:host1", - "volume_type": db_vol_type['id'], + "volume_type": "FakeTypeName", } body = {"volume": vol} req = fakes.HTTPRequest.blank('/v2/volumes') + # Raise 404 when type name isn't valid + self.assertRaises(webob.exc.HTTPNotFound, self.controller.create, + req, body) + + # Use correct volume type name + vol.update(dict(volume_type=CONF.default_volume_type)) + body.update(dict(volume=vol)) + res_dict = self.controller.create(req, body) + volume_id = res_dict['volume']['id'] + self.assertEqual(len(res_dict), 1) + + # Use correct volume type id + vol.update(dict(volume_type=db_vol_type['id'])) + body.update(dict(volume=vol)) res_dict = self.controller.create(req, body) volume_id = res_dict['volume']['id'] self.assertEqual(len(res_dict), 1)