Skip to content

Commit

Permalink
Allow v2 Volume API to create volume with type name
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Zhiteng Huang committed Sep 24, 2013
1 parent 180513d commit 64f24b9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
16 changes: 6 additions & 10 deletions cinder/api/v1/volumes.py
Expand Up @@ -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)

Expand Down
11 changes: 8 additions & 3 deletions cinder/api/v2/volumes.py
Expand Up @@ -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)
Expand Down
19 changes: 18 additions & 1 deletion cinder/tests/api/v1/test_volumes.py
Expand Up @@ -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'])

Expand Down
16 changes: 15 additions & 1 deletion cinder/tests/api/v2/test_volumes.py
Expand Up @@ -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)
Expand Down

0 comments on commit 64f24b9

Please sign in to comment.