Skip to content

Commit

Permalink
Port types and extra specs to volume api
Browse files Browse the repository at this point in the history
 * Fixes bug 979089
 * Adds pollicy for new extensions
 * Fixes __init__ in testing directories
 * Cleans up existing volume types tests
 * Adds tests for type management
 * Adds tests for extra specs management
 * Removed unused Quota handling
 * Fixed typo in db volume_type_get

Change-Id: Ic80190ecf1d6d6ad0229e5af642a50c7c53bbbf9
  • Loading branch information
vishvananda committed Apr 13, 2012
1 parent 042a4d0 commit 15c0847
Show file tree
Hide file tree
Showing 17 changed files with 883 additions and 50 deletions.
4 changes: 4 additions & 0 deletions etc/nova/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
"volume:get_all_snapshots": [],


"volume_extension:types_manage": [["rule:admin_api"]],
"volume_extension:types_extra_specs": [["rule:admin_api"]],


"network:get_all_networks": [],
"network:get_network": [],
"network:delete_network": [],
Expand Down
27 changes: 6 additions & 21 deletions nova/api/openstack/compute/contrib/volumetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ def create(self, req, body):
try:
volume_types.create(context, name, specs)
vol_type = volume_types.get_volume_type_by_name(context, name)
except exception.QuotaError as error:
self._handle_quota_error(error)
except exception.NotFound:
raise exc.HTTPNotFound()

Expand Down Expand Up @@ -171,12 +169,9 @@ def create(self, req, vol_type_id, body):
authorize(context)
self._check_body(body)
specs = body.get('extra_specs')
try:
db.volume_type_extra_specs_update_or_create(context,
vol_type_id,
specs)
except exception.QuotaError as error:
self._handle_quota_error(error)
db.volume_type_extra_specs_update_or_create(context,
vol_type_id,
specs)
return body

@wsgi.serializers(xml=VolumeTypeExtraSpecTemplate)
Expand All @@ -190,13 +185,9 @@ def update(self, req, vol_type_id, id, body):
if len(body) > 1:
expl = _('Request body contains too many items')
raise exc.HTTPBadRequest(explanation=expl)
try:
db.volume_type_extra_specs_update_or_create(context,
vol_type_id,
body)
except exception.QuotaError as error:
self._handle_quota_error(error)

db.volume_type_extra_specs_update_or_create(context,
vol_type_id,
body)
return body

@wsgi.serializers(xml=VolumeTypeExtraSpecTemplate)
Expand All @@ -216,12 +207,6 @@ def delete(self, req, vol_type_id, id):
authorize(context)
db.volume_type_extra_specs_delete(context, vol_type_id, id)

def _handle_quota_error(self, error):
"""Reraise quota errors as api-specific http exceptions."""
if error.code == "MetadataLimitExceeded":
raise exc.HTTPBadRequest(explanation=error.message)
raise error


class Volumetypes(extensions.ExtensionDescriptor):
"""Volume types support"""
Expand Down
152 changes: 152 additions & 0 deletions nova/api/openstack/volume/contrib/types_extra_specs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright (c) 2011 Zadara Storage Inc.
# Copyright (c) 2011 OpenStack LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""The volume types extra specs extension"""

import webob

from nova.api.openstack import extensions
from nova.api.openstack import wsgi
from nova.api.openstack import xmlutil
from nova import db
from nova import exception
from nova.volume import volume_types


authorize = extensions.extension_authorizer('volume', 'types_extra_specs')


class VolumeTypeExtraSpecsTemplate(xmlutil.TemplateBuilder):
def construct(self):
root = xmlutil.make_flat_dict('extra_specs', selector='extra_specs')
return xmlutil.MasterTemplate(root, 1)


class VolumeTypeExtraSpecTemplate(xmlutil.TemplateBuilder):
def construct(self):
tagname = xmlutil.Selector('key')

def extraspec_sel(obj, do_raise=False):
# Have to extract the key and value for later use...
key, value = obj.items()[0]
return dict(key=key, value=value)

root = xmlutil.TemplateElement(tagname, selector=extraspec_sel)
root.text = 'value'
return xmlutil.MasterTemplate(root, 1)


class VolumeTypeExtraSpecsController(object):
""" The volume type extra specs API controller for the OpenStack API """

def _get_extra_specs(self, context, type_id):
extra_specs = db.volume_type_extra_specs_get(context, type_id)
specs_dict = {}
for key, value in extra_specs.iteritems():
specs_dict[key] = value
return dict(extra_specs=specs_dict)

def _check_body(self, body):
if not body:
expl = _('No Request Body')
raise webob.exc.HTTPBadRequest(explanation=expl)

def _check_type(self, context, type_id):
try:
volume_types.get_volume_type(context, type_id)
except exception.NotFound as ex:
raise webob.exc.HTTPNotFound(explanation=unicode(ex))

@wsgi.serializers(xml=VolumeTypeExtraSpecsTemplate)
def index(self, req, type_id):
""" Returns the list of extra specs for a given volume type """
context = req.environ['nova.context']
authorize(context)
self._check_type(context, type_id)
return self._get_extra_specs(context, type_id)

@wsgi.serializers(xml=VolumeTypeExtraSpecsTemplate)
def create(self, req, type_id, body=None):
context = req.environ['nova.context']
authorize(context)
self._check_type(context, type_id)
self._check_body(body)
specs = body.get('extra_specs')
if not isinstance(specs, dict):
expl = _('Malformed extra specs')
raise webob.exc.HTTPBadRequest(explanation=expl)
db.volume_type_extra_specs_update_or_create(context,
type_id,
specs)
return body

@wsgi.serializers(xml=VolumeTypeExtraSpecTemplate)
def update(self, req, type_id, id, body=None):
context = req.environ['nova.context']
authorize(context)
self._check_type(context, type_id)
self._check_body(body)
if not id in body:
expl = _('Request body and URI mismatch')
raise webob.exc.HTTPBadRequest(explanation=expl)
if len(body) > 1:
expl = _('Request body contains too many items')
raise webob.exc.HTTPBadRequest(explanation=expl)
db.volume_type_extra_specs_update_or_create(context,
type_id,
body)
return body

@wsgi.serializers(xml=VolumeTypeExtraSpecTemplate)
def show(self, req, type_id, id):
"""Return a single extra spec item."""
context = req.environ['nova.context']
authorize(context)
self._check_type(context, type_id)
specs = self._get_extra_specs(context, type_id)
if id in specs['extra_specs']:
return {id: specs['extra_specs'][id]}
else:
raise webob.exc.HTTPNotFound()

def delete(self, req, type_id, id):
""" Deletes an existing extra spec """
context = req.environ['nova.context']
self._check_type(context, type_id)
authorize(context)
db.volume_type_extra_specs_delete(context, type_id, id)
return webob.Response(status_int=202)


class Types_extra_specs(extensions.ExtensionDescriptor):
"""Types extra specs support"""

name = "TypesExtraSpecs"
alias = "os-types-extra-specs"
namespace = "http://docs.openstack.org/volume/ext/types-extra-specs/api/v1"
updated = "2011-08-24T00:00:00+00:00"

def get_resources(self):
resources = []
res = extensions.ResourceExtension('extra_specs',
VolumeTypeExtraSpecsController(),
parent=dict(
member_name='type',
collection_name='types'))
resources.append(res)

return resources
91 changes: 91 additions & 0 deletions nova/api/openstack/volume/contrib/types_manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright (c) 2011 Zadara Storage Inc.
# Copyright (c) 2011 OpenStack LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""The volume types manage extension."""

import webob

from nova.api.openstack import extensions
from nova.api.openstack.volume import types
from nova.api.openstack import wsgi
from nova import exception
from nova.volume import volume_types


authorize = extensions.extension_authorizer('volume', 'types_manage')


class VolumeTypesManageController(wsgi.Controller):
""" The volume types API controller for the OpenStack API """

@wsgi.action("create")
@wsgi.serializers(xml=types.VolumeTypeTemplate)
def _create(self, req, body):
"""Creates a new volume type."""
context = req.environ['nova.context']
authorize(context)

if not body or body == "":
raise webob.exc.HTTPUnprocessableEntity()

vol_type = body.get('volume_type', None)
if vol_type is None or vol_type == "":
raise webob.exc.HTTPUnprocessableEntity()

name = vol_type.get('name', None)
specs = vol_type.get('extra_specs', {})

if name is None or name == "":
raise webob.exc.HTTPUnprocessableEntity()

try:
volume_types.create(context, name, specs)
vol_type = volume_types.get_volume_type_by_name(context, name)
except exception.VolumeTypeExists as err:
raise webob.exc.HTTPConflict(explanation=str(err))
except exception.NotFound:
raise webob.exc.HTTPNotFound()

return {'volume_type': vol_type}

@wsgi.action("delete")
def _delete(self, req, id):
""" Deletes an existing volume type """
context = req.environ['nova.context']
authorize(context)

try:
vol_type = volume_types.get_volume_type(context, id)
volume_types.destroy(context, vol_type['name'])
except exception.NotFound:
raise webob.exc.HTTPNotFound()

return webob.Response(status_int=202)


class Types_manage(extensions.ExtensionDescriptor):
"""Types manage support"""

name = "TypesManage"
alias = "os-types-manage"
namespace = "http://docs.openstack.org/volume/ext/types-manage/api/v1"
updated = "2011-08-24T00:00:00+00:00"

def get_controller_extensions(self):
controller = VolumeTypesManageController()
extension = extensions.ControllerExtension(self, 'types', controller)
return [extension]
2 changes: 1 addition & 1 deletion nova/db/sqlalchemy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3866,7 +3866,7 @@ def volume_type_get(context, id, session=None):
first()

if not result:
raise exception.VolumeTypeNotFound(volume_type=id)
raise exception.VolumeTypeNotFound(volume_type_id=id)

return _dict_with_extra_specs(result)

Expand Down
19 changes: 19 additions & 0 deletions nova/tests/api/openstack/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

# NOTE(vish): this forces the fixtures from tests/__init.py:setup() to work
from nova.tests import *
5 changes: 4 additions & 1 deletion nova/tests/api/openstack/compute/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 OpenStack LLC.
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
Expand All @@ -14,3 +14,6 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

# NOTE(vish): this forces the fixtures from tests/__init.py:setup() to work
from nova.tests import *
6 changes: 5 additions & 1 deletion nova/tests/api/openstack/compute/contrib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2011 OpenStack LLC
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
Expand All @@ -13,3 +14,6 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

# NOTE(vish): this forces the fixtures from tests/__init.py:setup() to work
from nova.tests import *
5 changes: 4 additions & 1 deletion nova/tests/api/openstack/volume/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 OpenStack LLC.
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
Expand All @@ -14,3 +14,6 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

# NOTE(vish): this forces the fixtures from tests/__init.py:setup() to work
from nova.tests import *

0 comments on commit 15c0847

Please sign in to comment.