~ubuntu-cloud-archive/ubuntu/precise/cinder/trunk

« back to all changes in this revision

Viewing changes to cinder/api/openstack/volume/contrib/types_extra_specs.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-11-23 08:39:28 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20121123083928-xvzet603cjfj9p1t
Tags: 2013.1~g1-0ubuntu1
* New upstream release.
* debian/patches/avoid_setuptools_git_dependency.patch:
  Avoid git installation. (LP: #1075948) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright (c) 2011 Zadara Storage Inc.
4
 
# Copyright (c) 2011 OpenStack LLC.
5
 
#
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
9
 
#
10
 
#         http://www.apache.org/licenses/LICENSE-2.0
11
 
#
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
16
 
#    under the License.
17
 
 
18
 
"""The volume types extra specs extension"""
19
 
 
20
 
import webob
21
 
 
22
 
from cinder.api.openstack import extensions
23
 
from cinder.api.openstack import wsgi
24
 
from cinder.api.openstack import xmlutil
25
 
from cinder import db
26
 
from cinder import exception
27
 
from cinder.volume import volume_types
28
 
 
29
 
 
30
 
authorize = extensions.extension_authorizer('volume', 'types_extra_specs')
31
 
 
32
 
 
33
 
class VolumeTypeExtraSpecsTemplate(xmlutil.TemplateBuilder):
34
 
    def construct(self):
35
 
        root = xmlutil.make_flat_dict('extra_specs', selector='extra_specs')
36
 
        return xmlutil.MasterTemplate(root, 1)
37
 
 
38
 
 
39
 
class VolumeTypeExtraSpecTemplate(xmlutil.TemplateBuilder):
40
 
    def construct(self):
41
 
        tagname = xmlutil.Selector('key')
42
 
 
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)
47
 
 
48
 
        root = xmlutil.TemplateElement(tagname, selector=extraspec_sel)
49
 
        root.text = 'value'
50
 
        return xmlutil.MasterTemplate(root, 1)
51
 
 
52
 
 
53
 
class VolumeTypeExtraSpecsController(wsgi.Controller):
54
 
    """ The volume type extra specs API controller for the OpenStack API """
55
 
 
56
 
    def _get_extra_specs(self, context, type_id):
57
 
        extra_specs = db.volume_type_extra_specs_get(context, type_id)
58
 
        specs_dict = {}
59
 
        for key, value in extra_specs.iteritems():
60
 
            specs_dict[key] = value
61
 
        return dict(extra_specs=specs_dict)
62
 
 
63
 
    def _check_type(self, context, type_id):
64
 
        try:
65
 
            volume_types.get_volume_type(context, type_id)
66
 
        except exception.NotFound as ex:
67
 
            raise webob.exc.HTTPNotFound(explanation=unicode(ex))
68
 
 
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']
73
 
        authorize(context)
74
 
        self._check_type(context, type_id)
75
 
        return self._get_extra_specs(context, type_id)
76
 
 
77
 
    @wsgi.serializers(xml=VolumeTypeExtraSpecsTemplate)
78
 
    def create(self, req, type_id, body=None):
79
 
        context = req.environ['cinder.context']
80
 
        authorize(context)
81
 
 
82
 
        if not self.is_valid_body(body, 'extra_specs'):
83
 
            raise webob.exc.HTTPUnprocessableEntity()
84
 
 
85
 
        self._check_type(context, type_id)
86
 
 
87
 
        specs = body['extra_specs']
88
 
        db.volume_type_extra_specs_update_or_create(context,
89
 
                                                    type_id,
90
 
                                                    specs)
91
 
        return body
92
 
 
93
 
    @wsgi.serializers(xml=VolumeTypeExtraSpecTemplate)
94
 
    def update(self, req, type_id, id, body=None):
95
 
        context = req.environ['cinder.context']
96
 
        authorize(context)
97
 
        if not body:
98
 
            raise webob.exc.HTTPUnprocessableEntity()
99
 
        self._check_type(context, type_id)
100
 
        if not id in body:
101
 
            expl = _('Request body and URI mismatch')
102
 
            raise webob.exc.HTTPBadRequest(explanation=expl)
103
 
        if len(body) > 1:
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,
107
 
                                                    type_id,
108
 
                                                    body)
109
 
        return body
110
 
 
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']
115
 
        authorize(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]}
120
 
        else:
121
 
            raise webob.exc.HTTPNotFound()
122
 
 
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)
127
 
        authorize(context)
128
 
        db.volume_type_extra_specs_delete(context, type_id, id)
129
 
        return webob.Response(status_int=202)
130
 
 
131
 
 
132
 
class Types_extra_specs(extensions.ExtensionDescriptor):
133
 
    """Types extra specs support"""
134
 
 
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"
139
 
 
140
 
    def get_resources(self):
141
 
        resources = []
142
 
        res = extensions.ResourceExtension('extra_specs',
143
 
                            VolumeTypeExtraSpecsController(),
144
 
                            parent=dict(
145
 
                                member_name='type',
146
 
                                collection_name='types'))
147
 
        resources.append(res)
148
 
 
149
 
        return resources