~ubuntu-branches/ubuntu/trusty/cinder/trusty

« back to all changes in this revision

Viewing changes to cinder/api/v1/volume_metadata.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Yolanda Robla Mota, James Page, Chuck Short
  • Date: 2013-02-22 10:45:17 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20130222104517-ng3r6ace9vi4m869
Tags: 2013.1.g3-0ubuntu1
[ Yolanda Robla Mota ]
* d/control: Add BD on python-hp3parclient.
* d/patches: Drop patches related to disabling hp3parclient.

[ James Page ]
* d/control: Add Suggests: python-hp3parclient for python-cinder.
* d/control: Add BD on python-oslo-config.
* d/*: Wrapped and sorted.

[ Chuck Short ]
* New upstream release.
* debian/rules, debian/cinder-volumes.install: 
  - Fail if binaries are missing and install missing binaries.
* debian/patches/fix-ubuntu-tests.patch: Fix failing tests.
* debian/control: Add python-rtslib and python-mock.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2011 OpenStack LLC.
 
4
# All Rights Reserved.
 
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
import webob
 
19
 
 
20
from cinder.api import common
 
21
from cinder.api.openstack import wsgi
 
22
from cinder import exception
 
23
from cinder import volume
 
24
from webob import exc
 
25
 
 
26
 
 
27
class Controller(object):
 
28
    """ The volume metadata API controller for the OpenStack API """
 
29
 
 
30
    def __init__(self):
 
31
        self.volume_api = volume.API()
 
32
        super(Controller, self).__init__()
 
33
 
 
34
    def _get_metadata(self, context, volume_id):
 
35
        try:
 
36
            volume = self.volume_api.get(context, volume_id)
 
37
            meta = self.volume_api.get_volume_metadata(context, volume)
 
38
        except exception.VolumeNotFound:
 
39
            msg = _('volume does not exist')
 
40
            raise exc.HTTPNotFound(explanation=msg)
 
41
        return meta
 
42
 
 
43
    @wsgi.serializers(xml=common.MetadataTemplate)
 
44
    def index(self, req, volume_id):
 
45
        """ Returns the list of metadata for a given volume"""
 
46
        context = req.environ['cinder.context']
 
47
        return {'metadata': self._get_metadata(context, volume_id)}
 
48
 
 
49
    @wsgi.serializers(xml=common.MetadataTemplate)
 
50
    @wsgi.deserializers(xml=common.MetadataDeserializer)
 
51
    def create(self, req, volume_id, body):
 
52
        try:
 
53
            metadata = body['metadata']
 
54
        except (KeyError, TypeError):
 
55
            msg = _("Malformed request body")
 
56
            raise exc.HTTPBadRequest(explanation=msg)
 
57
 
 
58
        context = req.environ['cinder.context']
 
59
 
 
60
        new_metadata = self._update_volume_metadata(context,
 
61
                                                    volume_id,
 
62
                                                    metadata,
 
63
                                                    delete=False)
 
64
 
 
65
        return {'metadata': new_metadata}
 
66
 
 
67
    @wsgi.serializers(xml=common.MetaItemTemplate)
 
68
    @wsgi.deserializers(xml=common.MetaItemDeserializer)
 
69
    def update(self, req, volume_id, id, body):
 
70
        try:
 
71
            meta_item = body['meta']
 
72
        except (TypeError, KeyError):
 
73
            expl = _('Malformed request body')
 
74
            raise exc.HTTPBadRequest(explanation=expl)
 
75
 
 
76
        if id not in meta_item:
 
77
            expl = _('Request body and URI mismatch')
 
78
            raise exc.HTTPBadRequest(explanation=expl)
 
79
 
 
80
        if len(meta_item) > 1:
 
81
            expl = _('Request body contains too many items')
 
82
            raise exc.HTTPBadRequest(explanation=expl)
 
83
 
 
84
        context = req.environ['cinder.context']
 
85
        self._update_volume_metadata(context,
 
86
                                     volume_id,
 
87
                                     meta_item,
 
88
                                     delete=False)
 
89
 
 
90
        return {'meta': meta_item}
 
91
 
 
92
    @wsgi.serializers(xml=common.MetadataTemplate)
 
93
    @wsgi.deserializers(xml=common.MetadataDeserializer)
 
94
    def update_all(self, req, volume_id, body):
 
95
        try:
 
96
            metadata = body['metadata']
 
97
        except (TypeError, KeyError):
 
98
            expl = _('Malformed request body')
 
99
            raise exc.HTTPBadRequest(explanation=expl)
 
100
 
 
101
        context = req.environ['cinder.context']
 
102
        new_metadata = self._update_volume_metadata(context,
 
103
                                                    volume_id,
 
104
                                                    metadata,
 
105
                                                    delete=True)
 
106
 
 
107
        return {'metadata': new_metadata}
 
108
 
 
109
    def _update_volume_metadata(self, context,
 
110
                                volume_id, metadata,
 
111
                                delete=False):
 
112
        try:
 
113
            volume = self.volume_api.get(context, volume_id)
 
114
            return self.volume_api.update_volume_metadata(context,
 
115
                                                          volume,
 
116
                                                          metadata,
 
117
                                                          delete)
 
118
        except exception.VolumeNotFound:
 
119
            msg = _('volume does not exist')
 
120
            raise exc.HTTPNotFound(explanation=msg)
 
121
 
 
122
        except (ValueError, AttributeError):
 
123
            msg = _("Malformed request body")
 
124
            raise exc.HTTPBadRequest(explanation=msg)
 
125
 
 
126
        except exception.InvalidVolumeMetadata as error:
 
127
            raise exc.HTTPBadRequest(explanation=unicode(error))
 
128
 
 
129
        except exception.InvalidVolumeMetadataSize as error:
 
130
            raise exc.HTTPRequestEntityTooLarge(explanation=unicode(error))
 
131
 
 
132
    @wsgi.serializers(xml=common.MetaItemTemplate)
 
133
    def show(self, req, volume_id, id):
 
134
        """ Return a single metadata item """
 
135
        context = req.environ['cinder.context']
 
136
        data = self._get_metadata(context, volume_id)
 
137
 
 
138
        try:
 
139
            return {'meta': {id: data[id]}}
 
140
        except KeyError:
 
141
            msg = _("Metadata item was not found")
 
142
            raise exc.HTTPNotFound(explanation=msg)
 
143
 
 
144
    def delete(self, req, volume_id, id):
 
145
        """ Deletes an existing metadata """
 
146
        context = req.environ['cinder.context']
 
147
 
 
148
        metadata = self._get_metadata(context, volume_id)
 
149
 
 
150
        if id not in metadata:
 
151
            msg = _("Metadata item was not found")
 
152
            raise exc.HTTPNotFound(explanation=msg)
 
153
 
 
154
        try:
 
155
            volume = self.volume_api.get(context, volume_id)
 
156
            self.volume_api.delete_volume_metadata(context, volume, id)
 
157
        except exception.VolumeNotFound:
 
158
            msg = _('volume does not exist')
 
159
            raise exc.HTTPNotFound(explanation=msg)
 
160
        return webob.Response(status_int=200)
 
161
 
 
162
 
 
163
def create_resource():
 
164
    return wsgi.Resource(Controller())