Statistics
| Branch: | Tag: | Revision:

root / api / images.py @ f4fe8796

History | View | Annotate | Download (11.3 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
# 
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
# 
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
# 
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
# 
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
# 
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from django.conf.urls.defaults import patterns
35
from django.db.models import Q
36
from django.http import HttpResponse
37
from django.template.loader import render_to_string
38
from django.utils import simplejson as json
39

    
40
from synnefo.api import util
41
from synnefo.api.common import method_not_allowed
42
from synnefo.api.faults import BadRequest
43
from synnefo.db.models import Image, ImageMetadata
44

    
45

    
46
urlpatterns = patterns('synnefo.api.images',
47
    (r'^(?:/|.json|.xml)?$', 'demux'),
48
    (r'^/detail(?:.json|.xml)?$', 'list_images', {'detail': True}),
49
    (r'^/(\d+)(?:.json|.xml)?$', 'image_demux'),
50
    (r'^/(\d+)/meta(?:.json|.xml)?$', 'metadata_demux'),
51
    (r'^/(\d+)/meta/(.+?)(?:.json|.xml)?$', 'metadata_item_demux'),
52
)
53

    
54
def demux(request):
55
    if request.method == 'GET':
56
        return list_images(request)
57
    elif request.method == 'POST':
58
        return create_image(request)
59
    else:
60
        return method_not_allowed(request)
61

    
62
def image_demux(request, image_id):
63
    if request.method == 'GET':
64
        return get_image_details(request, image_id)
65
    elif request.method == 'DELETE':
66
        return delete_image(request, image_id)
67
    else:
68
        return method_not_allowed(request)
69

    
70
def metadata_demux(request, image_id):
71
    if request.method == 'GET':
72
        return list_metadata(request, image_id)
73
    elif request.method == 'POST':
74
        return update_metadata(request, image_id)
75
    else:
76
        return method_not_allowed(request)
77

    
78
def metadata_item_demux(request, image_id, key):
79
    if request.method == 'GET':
80
        return get_metadata_item(request, image_id, key)
81
    elif request.method == 'PUT':
82
        return create_metadata_item(request, image_id, key)
83
    elif request.method == 'DELETE':
84
        return delete_metadata_item(request, image_id, key)
85
    else:
86
        return method_not_allowed(request)
87

    
88

    
89
def image_to_dict(image, detail=True):
90
    d = {'id': image.id, 'name': image.name}
91
    if detail:
92
        d['updated'] = util.isoformat(image.updated)
93
        d['created'] = util.isoformat(image.created)
94
        d['status'] = image.state
95
        d['progress'] = 100 if image.state == 'ACTIVE' else 0
96
        if image.sourcevm:
97
            d['serverRef'] = image.sourcevm.id
98

    
99
        metadata = {}
100
        for meta in ImageMetadata.objects.filter(image=image):
101
            metadata[meta.meta_key] = meta.meta_value
102

    
103
        if metadata:
104
            d['metadata'] = {'values': metadata}
105

    
106
    return d
107

    
108
def metadata_to_dict(image):
109
    image_meta = image.imagemetadata_set.all()
110
    return dict((meta.meta_key, meta.meta_value) for meta in image_meta)
111

    
112

    
113
@util.api_method('GET')
114
def list_images(request, detail=False):
115
    # Normal Response Codes: 200, 203
116
    # Error Response Codes: computeFault (400, 500),
117
    #                       serviceUnavailable (503),
118
    #                       unauthorized (401),
119
    #                       badRequest (400),
120
    #                       overLimit (413)
121
    
122
    user_images = Image.objects.filter(Q(owner=request.user) | Q(public=True))
123
    since = util.isoparse(request.GET.get('changes-since'))
124
    
125
    if since:
126
        user_images = user_images.filter(updated__gte=since)
127
        if not user_images:
128
            return HttpResponse(status=304)
129
    else:
130
        user_images = user_images.exclude(state='DELETED')
131
    
132
    images = [image_to_dict(image, detail) for image in user_images]
133
    
134
    if request.serialization == 'xml':
135
        data = render_to_string('list_images.xml', {
136
            'images': images,
137
            'detail': detail})
138
    else:
139
        data = json.dumps({'images': {'values': images}})
140

    
141
    return HttpResponse(data, status=200)
142

    
143
@util.api_method('POST')
144
def create_image(request):
145
    # Normal Response Code: 202
146
    # Error Response Codes: computeFault (400, 500),
147
    #                       serviceUnavailable (503),
148
    #                       unauthorized (401),
149
    #                       badMediaType(415),
150
    #                       itemNotFound (404),
151
    #                       badRequest (400),
152
    #                       serverCapacityUnavailable (503),
153
    #                       buildInProgress (409),
154
    #                       resizeNotAllowed (403),
155
    #                       backupOrResizeInProgress (409),
156
    #                       overLimit (413)
157

    
158
    req = util.get_request_dict(request)
159

    
160
    try:
161
        d = req['image']
162
        server_id = d['serverRef']
163
        name = d['name']
164
    except (KeyError, ValueError):
165
        raise BadRequest('Malformed request.')
166

    
167
    owner = request.user
168
    vm = util.get_vm(server_id, owner)
169
    image = Image.objects.create(name=name, owner=owner, sourcevm=vm)
170

    
171
    imagedict = image_to_dict(image)
172
    if request.serialization == 'xml':
173
        data = render_to_string('image.xml', {'image': imagedict})
174
    else:
175
        data = json.dumps({'image': imagedict})
176

    
177
    return HttpResponse(data, status=202)
178

    
179
@util.api_method('GET')
180
def get_image_details(request, image_id):
181
    # Normal Response Codes: 200, 203
182
    # Error Response Codes: computeFault (400, 500),
183
    #                       serviceUnavailable (503),
184
    #                       unauthorized (401),
185
    #                       badRequest (400),
186
    #                       itemNotFound (404),
187
    #                       overLimit (413)
188

    
189
    image = util.get_image(image_id, request.user)
190
    imagedict = image_to_dict(image)
191

    
192
    if request.serialization == 'xml':
193
        data = render_to_string('image.xml', {'image': imagedict})
194
    else:
195
        data = json.dumps({'image': imagedict})
196

    
197
    return HttpResponse(data, status=200)
198

    
199
@util.api_method('DELETE')
200
def delete_image(request, image_id):
201
    # Normal Response Code: 204
202
    # Error Response Codes: computeFault (400, 500),
203
    #                       serviceUnavailable (503),
204
    #                       unauthorized (401),
205
    #                       itemNotFound (404),
206
    #                       overLimit (413)
207

    
208
    image = util.get_image(image_id, request.user)
209
    image.state = 'DELETED'
210
    image.save()
211
    return HttpResponse(status=204)
212

    
213
@util.api_method('GET')
214
def list_metadata(request, image_id):
215
    # Normal Response Codes: 200, 203
216
    # Error Response Codes: computeFault (400, 500),
217
    #                       serviceUnavailable (503),
218
    #                       unauthorized (401),
219
    #                       badRequest (400),
220
    #                       overLimit (413)
221

    
222
    image = util.get_image(image_id, request.user)
223
    metadata = metadata_to_dict(image)
224
    return util.render_metadata(request, metadata, use_values=True, status=200)
225

    
226
@util.api_method('POST')
227
def update_metadata(request, image_id):
228
    # Normal Response Code: 201
229
    # Error Response Codes: computeFault (400, 500),
230
    #                       serviceUnavailable (503),
231
    #                       unauthorized (401),
232
    #                       badRequest (400),
233
    #                       buildInProgress (409),
234
    #                       badMediaType(415),
235
    #                       overLimit (413)
236

    
237
    image = util.get_image(image_id, request.user)
238
    req = util.get_request_dict(request)
239
    try:
240
        metadata = req['metadata']
241
        assert isinstance(metadata, dict)
242
    except (KeyError, AssertionError):
243
        raise BadRequest('Malformed request.')
244

    
245
    updated = {}
246

    
247
    for key, val in metadata.items():
248
        try:
249
            meta = ImageMetadata.objects.get(meta_key=key, image=image)
250
            meta.meta_value = val
251
            meta.save()
252
            updated[key] = val
253
        except ImageMetadata.DoesNotExist:
254
            pass    # Ignore non-existent metadata
255
    
256
    if updated:
257
        image.save()
258
    
259
    return util.render_metadata(request, updated, status=201)
260

    
261
@util.api_method('GET')
262
def get_metadata_item(request, image_id, key):
263
    # Normal Response Codes: 200, 203
264
    # Error Response Codes: computeFault (400, 500),
265
    #                       serviceUnavailable (503),
266
    #                       unauthorized (401),
267
    #                       itemNotFound (404),
268
    #                       badRequest (400),
269
    #                       overLimit (413)
270

    
271
    image = util.get_image(image_id, request.user)
272
    meta = util.get_image_meta(image, key)
273
    return util.render_meta(request, meta, status=200)
274

    
275
@util.api_method('PUT')
276
def create_metadata_item(request, image_id, key):
277
    # Normal Response Code: 201
278
    # Error Response Codes: computeFault (400, 500),
279
    #                       serviceUnavailable (503),
280
    #                       unauthorized (401),
281
    #                       itemNotFound (404),
282
    #                       badRequest (400),
283
    #                       buildInProgress (409),
284
    #                       badMediaType(415),
285
    #                       overLimit (413)
286

    
287
    image = util.get_image(image_id, request.user)
288
    req = util.get_request_dict(request)
289
    try:
290
        metadict = req['meta']
291
        assert isinstance(metadict, dict)
292
        assert len(metadict) == 1
293
        assert key in metadict
294
    except (KeyError, AssertionError):
295
        raise BadRequest('Malformed request.')
296
    
297
    meta, created = ImageMetadata.objects.get_or_create(
298
        meta_key=key,
299
        image=image)
300
    
301
    meta.meta_value = metadict[key]
302
    meta.save()
303
    image.save()
304
    return util.render_meta(request, meta, status=201)
305

    
306
@util.api_method('DELETE')
307
def delete_metadata_item(request, image_id, key):
308
    # Normal Response Code: 204
309
    # Error Response Codes: computeFault (400, 500),
310
    #                       serviceUnavailable (503),
311
    #                       unauthorized (401),
312
    #                       itemNotFound (404),
313
    #                       badRequest (400),
314
    #                       buildInProgress (409),
315
    #                       badMediaType(415),
316
    #                       overLimit (413),
317

    
318
    image = util.get_image(image_id, request.user)
319
    meta = util.get_image_meta(image, key)
320
    meta.delete()
321
    image.save()
322
    return HttpResponse(status=204)