Statistics
| Branch: | Tag: | Revision:

root / api / flavors.py @ b016b476

History | View | Annotate | Download (2.2 kB)

1 c36934a7 Giorgos Verigakis
#
2 c36934a7 Giorgos Verigakis
# Copyright (c) 2010 Greek Research and Technology Network
3 c36934a7 Giorgos Verigakis
#
4 c36934a7 Giorgos Verigakis
5 c36934a7 Giorgos Verigakis
from synnefo.api.util import *
6 c36934a7 Giorgos Verigakis
from synnefo.db.models import Flavor
7 c36934a7 Giorgos Verigakis
8 c36934a7 Giorgos Verigakis
from django.conf.urls.defaults import *
9 c36934a7 Giorgos Verigakis
from django.http import HttpResponse
10 c36934a7 Giorgos Verigakis
from django.template.loader import render_to_string
11 c36934a7 Giorgos Verigakis
12 c36934a7 Giorgos Verigakis
13 c36934a7 Giorgos Verigakis
urlpatterns = patterns('synnefo.api.flavors',
14 c36934a7 Giorgos Verigakis
    (r'^(?:/|.json|.xml)?$', 'list_flavors'),
15 c36934a7 Giorgos Verigakis
    (r'^/detail(?:.json|.xml)?$', 'list_flavors', {'detail': True}),
16 c36934a7 Giorgos Verigakis
    (r'^/(\d+)(?:.json|.xml)?$', 'get_flavor_details'),
17 c36934a7 Giorgos Verigakis
)
18 c36934a7 Giorgos Verigakis
19 c36934a7 Giorgos Verigakis
20 c36934a7 Giorgos Verigakis
def flavor_to_dict(flavor, detail=True):
21 c36934a7 Giorgos Verigakis
    d = {'id': flavor.id, 'name': flavor.name}
22 c36934a7 Giorgos Verigakis
    if detail:
23 c36934a7 Giorgos Verigakis
        d['ram'] = flavor.ram
24 c36934a7 Giorgos Verigakis
        d['disk'] = flavor.disk
25 c36934a7 Giorgos Verigakis
    return d
26 c36934a7 Giorgos Verigakis
27 c36934a7 Giorgos Verigakis
28 c36934a7 Giorgos Verigakis
@api_method('GET')
29 c36934a7 Giorgos Verigakis
def list_flavors(request, detail=False):
30 c36934a7 Giorgos Verigakis
    # Normal Response Codes: 200, 203
31 c36934a7 Giorgos Verigakis
    # Error Response Codes: computeFault (400, 500),
32 c36934a7 Giorgos Verigakis
    #                       serviceUnavailable (503),
33 c36934a7 Giorgos Verigakis
    #                       unauthorized (401),
34 c36934a7 Giorgos Verigakis
    #                       badRequest (400),
35 c36934a7 Giorgos Verigakis
    #                       overLimit (413)
36 c36934a7 Giorgos Verigakis
    
37 c36934a7 Giorgos Verigakis
    all_flavors = Flavor.objects.all()
38 c36934a7 Giorgos Verigakis
    flavors = [flavor_to_dict(flavor, detail) for flavor in all_flavors]
39 c36934a7 Giorgos Verigakis
    
40 c36934a7 Giorgos Verigakis
    if request.type == 'xml':
41 c36934a7 Giorgos Verigakis
        mimetype = 'application/xml'
42 c36934a7 Giorgos Verigakis
        data = render_to_string('list_flavors.xml', {'flavors': flavors, 'detail': detail})
43 c36934a7 Giorgos Verigakis
    else:
44 c36934a7 Giorgos Verigakis
        mimetype = 'application/json'
45 c36934a7 Giorgos Verigakis
        data = json.dumps({'flavors': {'values': flavors}})
46 c36934a7 Giorgos Verigakis
    
47 c36934a7 Giorgos Verigakis
    return HttpResponse(data, mimetype=mimetype, status=200)
48 c36934a7 Giorgos Verigakis
49 c36934a7 Giorgos Verigakis
@api_method('GET')
50 c36934a7 Giorgos Verigakis
def get_flavor_details(request, flavor_id):
51 c36934a7 Giorgos Verigakis
    # Normal Response Codes: 200, 203
52 c36934a7 Giorgos Verigakis
    # Error Response Codes: computeFault (400, 500),
53 c36934a7 Giorgos Verigakis
    #                       serviceUnavailable (503),
54 c36934a7 Giorgos Verigakis
    #                       unauthorized (401),
55 c36934a7 Giorgos Verigakis
    #                       badRequest (400),
56 c36934a7 Giorgos Verigakis
    #                       itemNotFound (404),
57 c36934a7 Giorgos Verigakis
    #                       overLimit (413)
58 c36934a7 Giorgos Verigakis
59 c36934a7 Giorgos Verigakis
    try:
60 c36934a7 Giorgos Verigakis
        falvor_id = int(flavor_id)
61 c36934a7 Giorgos Verigakis
        flavor = flavor_to_dict(Flavor.objects.get(id=flavor_id))
62 c36934a7 Giorgos Verigakis
    except Flavor.DoesNotExist:
63 c36934a7 Giorgos Verigakis
        raise ItemNotFound
64 c36934a7 Giorgos Verigakis
    
65 c36934a7 Giorgos Verigakis
    if request.type == 'xml':
66 c36934a7 Giorgos Verigakis
        data = render_to_string('flavor.xml', {'flavor': flavor})
67 c36934a7 Giorgos Verigakis
    else:
68 c36934a7 Giorgos Verigakis
        data = json.dumps({'flavor': flavor})
69 c36934a7 Giorgos Verigakis
    
70 c36934a7 Giorgos Verigakis
    return HttpResponse(data, status=200)