Revision cd2b0bf5 snf-cyclades-app/synnefo/api/images.py

b/snf-cyclades-app/synnefo/api/images.py
1 1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2
# 
2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
5 5
# conditions are met:
6
# 
6
#
7 7
#   1. Redistributions of source code must retain the above
8 8
#      copyright notice, this list of conditions and the following
9 9
#      disclaimer.
10
# 
10
#
11 11
#   2. Redistributions in binary form must reproduce the above
12 12
#      copyright notice, this list of conditions and the following
13 13
#      disclaimer in the documentation and/or other materials
14 14
#      provided with the distribution.
15
# 
15
#
16 16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......
25 25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 27
# POSSIBILITY OF SUCH DAMAGE.
28
# 
28
#
29 29
# The views and conclusions contained in the software and
30 30
# documentation are those of the authors and should not be
31 31
# interpreted as representing official policies, either expressed
......
112 112
    #                       unauthorized (401),
113 113
    #                       badRequest (400),
114 114
    #                       overLimit (413)
115
    
115

  
116 116
    log.debug('list_images detail=%s', detail)
117 117
    backend = ImageBackend(request.user_uniq)
118
    
118

  
119 119
    since = isoparse(request.GET.get('changes-since'))
120 120
    if since:
121 121
        images = []
......
127 127
            return HttpResponse(status=304)
128 128
    else:
129 129
        images = backend.list()
130
    
130

  
131
    images = sorted(images, key=lambda x: x['id'])
131 132
    reply = [image_to_dict(image, detail) for image in images]
132
    
133

  
133 134
    if request.serialization == 'xml':
134 135
        data = render_to_string('list_images.xml',
135 136
                                dict(images=reply, detail=detail))
136 137
    else:
137 138
        data = json.dumps(dict(images={'values': reply}))
138
    
139

  
139 140
    return HttpResponse(data, status=200)
140 141

  
141 142

  
......
153 154
    #                       resizeNotAllowed (403),
154 155
    #                       backupOrResizeInProgress (409),
155 156
    #                       overLimit (413)
156
    
157

  
157 158
    raise ServiceUnavailable('Not supported.')
158 159

  
159 160

  
......
166 167
    #                       badRequest (400),
167 168
    #                       itemNotFound (404),
168 169
    #                       overLimit (413)
169
    
170

  
170 171
    log.debug('get_image_details %s', image_id)
171 172
    image = util.get_image(image_id, request.user_uniq)
172 173
    reply = image_to_dict(image)
173
    
174

  
174 175
    if request.serialization == 'xml':
175 176
        data = render_to_string('image.xml', dict(image=reply))
176 177
    else:
177 178
        data = json.dumps(dict(image=reply))
178
    
179

  
179 180
    return HttpResponse(data, status=200)
180 181

  
181 182

  
......
187 188
    #                       unauthorized (401),
188 189
    #                       itemNotFound (404),
189 190
    #                       overLimit (413)
190
    
191

  
191 192
    log.info('delete_image %s', image_id)
192 193
    backend = ImageBackend(request.user_uniq)
193 194
    backend.delete(image_id)
......
204 205
    #                       unauthorized (401),
205 206
    #                       badRequest (400),
206 207
    #                       overLimit (413)
207
    
208

  
208 209
    log.debug('list_image_metadata %s', image_id)
209 210
    image = util.get_image(image_id, request.user_uniq)
210 211
    metadata = image['properties']
......
221 222
    #                       buildInProgress (409),
222 223
    #                       badMediaType(415),
223 224
    #                       overLimit (413)
224
    
225

  
225 226
    req = util.get_request_dict(request)
226 227
    log.info('update_image_metadata %s %s', image_id, req)
227 228
    image = util.get_image(image_id, request.user_uniq)
......
230 231
        assert isinstance(metadata, dict)
231 232
    except (KeyError, AssertionError):
232 233
        raise BadRequest('Malformed request.')
233
    
234

  
234 235
    properties = image['properties']
235 236
    properties.update(metadata)
236
    
237

  
237 238
    backend = ImageBackend(request.user_uniq)
238 239
    backend.update(image_id, dict(properties=properties))
239 240
    backend.close()
240
    
241

  
241 242
    return util.render_metadata(request, properties, status=201)
242 243

  
243 244

  
......
250 251
    #                       itemNotFound (404),
251 252
    #                       badRequest (400),
252 253
    #                       overLimit (413)
253
    
254

  
254 255
    log.debug('get_image_metadata_item %s %s', image_id, key)
255 256
    image = util.get_image(image_id, request.user_uniq)
256 257
    val = image['properties'].get(key)
......
270 271
    #                       buildInProgress (409),
271 272
    #                       badMediaType(415),
272 273
    #                       overLimit (413)
273
    
274

  
274 275
    req = util.get_request_dict(request)
275 276
    log.info('create_image_metadata_item %s %s %s', image_id, key, req)
276 277
    try:
......
285 286
    image = util.get_image(image_id, request.user_uniq)
286 287
    properties = image['properties']
287 288
    properties[key] = val
288
    
289

  
289 290
    backend = ImageBackend(request.user_uniq)
290 291
    backend.update(image_id, dict(properties=properties))
291 292
    backend.close()
292
    
293

  
293 294
    return util.render_meta(request, {key: val}, status=201)
294 295

  
295 296

  
......
304 305
    #                       buildInProgress (409),
305 306
    #                       badMediaType(415),
306 307
    #                       overLimit (413),
307
    
308

  
308 309
    log.info('delete_image_metadata_item %s %s', image_id, key)
309 310
    image = util.get_image(image_id, request.user_uniq)
310 311
    properties = image['properties']
311 312
    properties.pop(key, None)
312
    
313

  
313 314
    backend = ImageBackend(request.user_uniq)
314 315
    backend.update(image_id, dict(properties=properties))
315 316
    backend.close()
316
    
317

  
317 318
    return HttpResponse(status=204)

Also available in: Unified diff