]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Allow v2 Volume API to create volume with type name
authorZhiteng Huang <zhithuang@ebaysf.com>
Tue, 24 Sep 2013 08:09:42 +0000 (16:09 +0800)
committerZhiteng Huang <zhithuang@ebaysf.com>
Tue, 24 Sep 2013 08:09:42 +0000 (16:09 +0800)
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
cinder/api/v2/volumes.py
cinder/tests/api/v1/test_volumes.py
cinder/tests/api/v2/test_volumes.py

index 1cef310585aa8a89f7cd45a331db9d84df342f66..2c4077551c672d9ce542c5972d29ed2fead4b7ce 100644 (file)
@@ -357,21 +357,17 @@ class VolumeController(wsgi.Controller):
 
         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)
 
index 182edf2517bfba53f96fe58d478f948979dc556d..71cd4a272c6f73fc24db35a5e678614ab037a6c2 100644 (file)
@@ -330,10 +330,15 @@ class VolumeController(wsgi.Controller):
         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)
index b09b39ed22ae547159d6f08b5784884464465804..1e9f7c4166c0d788a1e8e4ca35828833bbfe70d1 100644 (file)
@@ -108,10 +108,27 @@ class VolumeApiTest(test.TestCase):
                "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'])
 
index b31a3076d0d56739afa3178e96e115f0e1092fa6..a44dc737e4f78fe979b45e3635a9d35a4dcbd9d3 100644 (file)
@@ -114,10 +114,24 @@ class VolumeApiTest(test.TestCase):
             "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)