~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/api/openstack/volume/types.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-01-20 11:54:15 UTC
  • mto: This revision was merged to the branch mainline in revision 62.
  • Revision ID: package-import@ubuntu.com-20120120115415-h2ujma9o536o1ut6
Tags: upstream-2012.1~e3~20120120.12170
ImportĀ upstreamĀ versionĀ 2012.1~e3~20120120.12170

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 type & volume types extra specs extension"""
 
19
 
 
20
from webob import exc
 
21
 
 
22
from nova.api.openstack import wsgi
 
23
from nova.api.openstack import xmlutil
 
24
from nova import exception
 
25
from nova.volume import volume_types
 
26
 
 
27
 
 
28
class VolumeTypesController(object):
 
29
    """ The volume types API controller for the Openstack API """
 
30
 
 
31
    def index(self, req):
 
32
        """ Returns the list of volume types """
 
33
        context = req.environ['nova.context']
 
34
        return volume_types.get_all_types(context)
 
35
 
 
36
    def show(self, req, id):
 
37
        """ Return a single volume type item """
 
38
        context = req.environ['nova.context']
 
39
 
 
40
        try:
 
41
            vol_type = volume_types.get_volume_type(context, id)
 
42
        except exception.NotFound or exception.ApiError:
 
43
            raise exc.HTTPNotFound()
 
44
 
 
45
        return {'volume_type': vol_type}
 
46
 
 
47
 
 
48
def make_voltype(elem):
 
49
    elem.set('id')
 
50
    elem.set('name')
 
51
    extra_specs = xmlutil.make_flat_dict('extra_specs', selector='extra_specs')
 
52
    elem.append(extra_specs)
 
53
 
 
54
 
 
55
class VolumeTypeTemplate(xmlutil.TemplateBuilder):
 
56
    def construct(self):
 
57
        root = xmlutil.TemplateElement('volume_type', selector='volume_type')
 
58
        make_voltype(root)
 
59
        return xmlutil.MasterTemplate(root, 1)
 
60
 
 
61
 
 
62
class VolumeTypesTemplate(xmlutil.TemplateBuilder):
 
63
    def construct(self):
 
64
        root = xmlutil.TemplateElement('volume_types')
 
65
        sel = lambda obj, do_raise=False: obj.values()
 
66
        elem = xmlutil.SubTemplateElement(root, 'volume_type', selector=sel)
 
67
        make_voltype(elem)
 
68
        return xmlutil.MasterTemplate(root, 1)
 
69
 
 
70
 
 
71
class VolumeTypesSerializer(xmlutil.XMLTemplateSerializer):
 
72
    def index(self):
 
73
        return VolumeTypesTemplate()
 
74
 
 
75
    def default(self):
 
76
        return VolumeTypeTemplate()
 
77
 
 
78
 
 
79
def create_resource():
 
80
    body_serializers = {
 
81
        'application/xml': VolumeTypesSerializer(),
 
82
        }
 
83
    serializer = wsgi.ResponseSerializer(body_serializers)
 
84
 
 
85
    deserializer = wsgi.RequestDeserializer()
 
86
 
 
87
    return wsgi.Resource(VolumeTypesController(), serializer=serializer)