1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright (c) 2011 Zadara Storage Inc.
4
# Copyright (c) 2011 OpenStack LLC.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
18
"""The volume types extra specs extension"""
22
from cinder.api.openstack import extensions
23
from cinder.api.openstack import wsgi
24
from cinder.api.openstack import xmlutil
26
from cinder import exception
27
from cinder.volume import volume_types
30
authorize = extensions.extension_authorizer('volume', 'types_extra_specs')
33
class VolumeTypeExtraSpecsTemplate(xmlutil.TemplateBuilder):
35
root = xmlutil.make_flat_dict('extra_specs', selector='extra_specs')
36
return xmlutil.MasterTemplate(root, 1)
39
class VolumeTypeExtraSpecTemplate(xmlutil.TemplateBuilder):
41
tagname = xmlutil.Selector('key')
43
def extraspec_sel(obj, do_raise=False):
44
# Have to extract the key and value for later use...
45
key, value = obj.items()[0]
46
return dict(key=key, value=value)
48
root = xmlutil.TemplateElement(tagname, selector=extraspec_sel)
50
return xmlutil.MasterTemplate(root, 1)
53
class VolumeTypeExtraSpecsController(wsgi.Controller):
54
""" The volume type extra specs API controller for the OpenStack API """
56
def _get_extra_specs(self, context, type_id):
57
extra_specs = db.volume_type_extra_specs_get(context, type_id)
59
for key, value in extra_specs.iteritems():
60
specs_dict[key] = value
61
return dict(extra_specs=specs_dict)
63
def _check_type(self, context, type_id):
65
volume_types.get_volume_type(context, type_id)
66
except exception.NotFound as ex:
67
raise webob.exc.HTTPNotFound(explanation=unicode(ex))
69
@wsgi.serializers(xml=VolumeTypeExtraSpecsTemplate)
70
def index(self, req, type_id):
71
""" Returns the list of extra specs for a given volume type """
72
context = req.environ['cinder.context']
74
self._check_type(context, type_id)
75
return self._get_extra_specs(context, type_id)
77
@wsgi.serializers(xml=VolumeTypeExtraSpecsTemplate)
78
def create(self, req, type_id, body=None):
79
context = req.environ['cinder.context']
82
if not self.is_valid_body(body, 'extra_specs'):
83
raise webob.exc.HTTPUnprocessableEntity()
85
self._check_type(context, type_id)
87
specs = body['extra_specs']
88
db.volume_type_extra_specs_update_or_create(context,
93
@wsgi.serializers(xml=VolumeTypeExtraSpecTemplate)
94
def update(self, req, type_id, id, body=None):
95
context = req.environ['cinder.context']
98
raise webob.exc.HTTPUnprocessableEntity()
99
self._check_type(context, type_id)
101
expl = _('Request body and URI mismatch')
102
raise webob.exc.HTTPBadRequest(explanation=expl)
104
expl = _('Request body contains too many items')
105
raise webob.exc.HTTPBadRequest(explanation=expl)
106
db.volume_type_extra_specs_update_or_create(context,
111
@wsgi.serializers(xml=VolumeTypeExtraSpecTemplate)
112
def show(self, req, type_id, id):
113
"""Return a single extra spec item."""
114
context = req.environ['cinder.context']
116
self._check_type(context, type_id)
117
specs = self._get_extra_specs(context, type_id)
118
if id in specs['extra_specs']:
119
return {id: specs['extra_specs'][id]}
121
raise webob.exc.HTTPNotFound()
123
def delete(self, req, type_id, id):
124
""" Deletes an existing extra spec """
125
context = req.environ['cinder.context']
126
self._check_type(context, type_id)
128
db.volume_type_extra_specs_delete(context, type_id, id)
129
return webob.Response(status_int=202)
132
class Types_extra_specs(extensions.ExtensionDescriptor):
133
"""Types extra specs support"""
135
name = "TypesExtraSpecs"
136
alias = "os-types-extra-specs"
137
namespace = "http://docs.openstack.org/volume/ext/types-extra-specs/api/v1"
138
updated = "2011-08-24T00:00:00+00:00"
140
def get_resources(self):
142
res = extensions.ResourceExtension('extra_specs',
143
VolumeTypeExtraSpecsController(),
146
collection_name='types'))
147
resources.append(res)