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

« back to all changes in this revision

Viewing changes to cinder/api/openstack/volume/snapshots.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-22 09:57:46 UTC
  • Revision ID: package-import@ubuntu.com-20120522095746-9lm71yvzltjybk4b
Tags: upstream-2012.2~f1~20120503.2
ImportĀ upstreamĀ versionĀ 2012.2~f1~20120503.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2011 Justin Santa Barbara
 
2
# All Rights Reserved.
 
3
#
 
4
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
5
#    not use this file except in compliance with the License. You may obtain
 
6
#    a copy of the License at
 
7
#
 
8
#         http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
10
#    Unless required by applicable law or agreed to in writing, software
 
11
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
12
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
13
#    License for the specific language governing permissions and limitations
 
14
#    under the License.
 
15
 
 
16
"""The volumes snapshots api."""
 
17
 
 
18
from webob import exc
 
19
import webob
 
20
 
 
21
from cinder.api.openstack import common
 
22
from cinder.api.openstack import wsgi
 
23
from cinder.api.openstack import xmlutil
 
24
from cinder import exception
 
25
from cinder import flags
 
26
from cinder import log as logging
 
27
from cinder import volume
 
28
 
 
29
 
 
30
LOG = logging.getLogger(__name__)
 
31
 
 
32
 
 
33
FLAGS = flags.FLAGS
 
34
 
 
35
 
 
36
def _translate_snapshot_detail_view(context, vol):
 
37
    """Maps keys for snapshots details view."""
 
38
 
 
39
    d = _translate_snapshot_summary_view(context, vol)
 
40
 
 
41
    # NOTE(gagupta): No additional data / lookups at the moment
 
42
    return d
 
43
 
 
44
 
 
45
def _translate_snapshot_summary_view(context, vol):
 
46
    """Maps keys for snapshots summary view."""
 
47
    d = {}
 
48
 
 
49
    # TODO(bcwaldon): remove str cast once we use uuids
 
50
    d['id'] = str(vol['id'])
 
51
    d['volume_id'] = str(vol['volume_id'])
 
52
    d['status'] = vol['status']
 
53
    # NOTE(gagupta): We map volume_size as the snapshot size
 
54
    d['size'] = vol['volume_size']
 
55
    d['created_at'] = vol['created_at']
 
56
    d['display_name'] = vol['display_name']
 
57
    d['display_description'] = vol['display_description']
 
58
    return d
 
59
 
 
60
 
 
61
def make_snapshot(elem):
 
62
    elem.set('id')
 
63
    elem.set('status')
 
64
    elem.set('size')
 
65
    elem.set('created_at')
 
66
    elem.set('display_name')
 
67
    elem.set('display_description')
 
68
    elem.set('volume_id')
 
69
 
 
70
 
 
71
class SnapshotTemplate(xmlutil.TemplateBuilder):
 
72
    def construct(self):
 
73
        root = xmlutil.TemplateElement('snapshot', selector='snapshot')
 
74
        make_snapshot(root)
 
75
        return xmlutil.MasterTemplate(root, 1)
 
76
 
 
77
 
 
78
class SnapshotsTemplate(xmlutil.TemplateBuilder):
 
79
    def construct(self):
 
80
        root = xmlutil.TemplateElement('snapshots')
 
81
        elem = xmlutil.SubTemplateElement(root, 'snapshot',
 
82
                                          selector='snapshots')
 
83
        make_snapshot(elem)
 
84
        return xmlutil.MasterTemplate(root, 1)
 
85
 
 
86
 
 
87
class SnapshotsController(object):
 
88
    """The Volumes API controller for the OpenStack API."""
 
89
 
 
90
    def __init__(self):
 
91
        self.volume_api = volume.API()
 
92
        super(SnapshotsController, self).__init__()
 
93
 
 
94
    @wsgi.serializers(xml=SnapshotTemplate)
 
95
    def show(self, req, id):
 
96
        """Return data about the given snapshot."""
 
97
        context = req.environ['cinder.context']
 
98
 
 
99
        try:
 
100
            vol = self.volume_api.get_snapshot(context, id)
 
101
        except exception.NotFound:
 
102
            raise exc.HTTPNotFound()
 
103
 
 
104
        return {'snapshot': _translate_snapshot_detail_view(context, vol)}
 
105
 
 
106
    def delete(self, req, id):
 
107
        """Delete a snapshot."""
 
108
        context = req.environ['cinder.context']
 
109
 
 
110
        LOG.audit(_("Delete snapshot with id: %s"), id, context=context)
 
111
 
 
112
        try:
 
113
            snapshot = self.volume_api.get_snapshot(context, id)
 
114
            self.volume_api.delete_snapshot(context, snapshot)
 
115
        except exception.NotFound:
 
116
            raise exc.HTTPNotFound()
 
117
        return webob.Response(status_int=202)
 
118
 
 
119
    @wsgi.serializers(xml=SnapshotsTemplate)
 
120
    def index(self, req):
 
121
        """Returns a summary list of snapshots."""
 
122
        return self._items(req, entity_maker=_translate_snapshot_summary_view)
 
123
 
 
124
    @wsgi.serializers(xml=SnapshotsTemplate)
 
125
    def detail(self, req):
 
126
        """Returns a detailed list of snapshots."""
 
127
        return self._items(req, entity_maker=_translate_snapshot_detail_view)
 
128
 
 
129
    def _items(self, req, entity_maker):
 
130
        """Returns a list of snapshots, transformed through entity_maker."""
 
131
        context = req.environ['cinder.context']
 
132
 
 
133
        snapshots = self.volume_api.get_all_snapshots(context)
 
134
        limited_list = common.limited(snapshots, req)
 
135
        res = [entity_maker(context, snapshot) for snapshot in limited_list]
 
136
        return {'snapshots': res}
 
137
 
 
138
    @wsgi.serializers(xml=SnapshotTemplate)
 
139
    def create(self, req, body):
 
140
        """Creates a new snapshot."""
 
141
        context = req.environ['cinder.context']
 
142
 
 
143
        if not body:
 
144
            return exc.HTTPUnprocessableEntity()
 
145
 
 
146
        snapshot = body['snapshot']
 
147
        volume_id = snapshot['volume_id']
 
148
        volume = self.volume_api.get(context, volume_id)
 
149
        force = snapshot.get('force', False)
 
150
        msg = _("Create snapshot from volume %s")
 
151
        LOG.audit(msg, volume_id, context=context)
 
152
 
 
153
        if force:
 
154
            new_snapshot = self.volume_api.create_snapshot_force(context,
 
155
                                        volume,
 
156
                                        snapshot.get('display_name'),
 
157
                                        snapshot.get('display_description'))
 
158
        else:
 
159
            new_snapshot = self.volume_api.create_snapshot(context,
 
160
                                        volume,
 
161
                                        snapshot.get('display_name'),
 
162
                                        snapshot.get('display_description'))
 
163
 
 
164
        retval = _translate_snapshot_detail_view(context, new_snapshot)
 
165
 
 
166
        return {'snapshot': retval}
 
167
 
 
168
 
 
169
def create_resource():
 
170
    return wsgi.Resource(SnapshotsController())