Statistics
| Branch: | Tag: | Revision:

root / api / flavors.py @ 96b635d9

History | View | Annotate | Download (3.6 kB)

1 adee02b8 Giorgos Verigakis
# Copyright 2011 GRNET S.A. All rights reserved.
2 adee02b8 Giorgos Verigakis
# 
3 adee02b8 Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 adee02b8 Giorgos Verigakis
# without modification, are permitted provided that the following
5 adee02b8 Giorgos Verigakis
# conditions are met:
6 adee02b8 Giorgos Verigakis
# 
7 adee02b8 Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 adee02b8 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 adee02b8 Giorgos Verigakis
#      disclaimer.
10 adee02b8 Giorgos Verigakis
# 
11 adee02b8 Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 adee02b8 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 adee02b8 Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 adee02b8 Giorgos Verigakis
#      provided with the distribution.
15 adee02b8 Giorgos Verigakis
# 
16 adee02b8 Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 adee02b8 Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 adee02b8 Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 adee02b8 Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 adee02b8 Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 adee02b8 Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 adee02b8 Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 adee02b8 Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 adee02b8 Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 adee02b8 Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 adee02b8 Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 adee02b8 Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 adee02b8 Giorgos Verigakis
# 
29 adee02b8 Giorgos Verigakis
# The views and conclusions contained in the software and
30 adee02b8 Giorgos Verigakis
# documentation are those of the authors and should not be
31 adee02b8 Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 adee02b8 Giorgos Verigakis
# or implied, of GRNET S.A.
33 c36934a7 Giorgos Verigakis
34 d8e50a39 Giorgos Verigakis
from django.conf.urls.defaults import patterns
35 c36934a7 Giorgos Verigakis
from django.http import HttpResponse
36 c36934a7 Giorgos Verigakis
from django.template.loader import render_to_string
37 29a59bc1 Giorgos Verigakis
from django.utils import simplejson as json
38 c36934a7 Giorgos Verigakis
39 b36f78fa Giorgos Verigakis
from synnefo.api import util
40 d8e50a39 Giorgos Verigakis
from synnefo.db.models import Flavor
41 d8e50a39 Giorgos Verigakis
42 c36934a7 Giorgos Verigakis
43 c36934a7 Giorgos Verigakis
urlpatterns = patterns('synnefo.api.flavors',
44 c36934a7 Giorgos Verigakis
    (r'^(?:/|.json|.xml)?$', 'list_flavors'),
45 c36934a7 Giorgos Verigakis
    (r'^/detail(?:.json|.xml)?$', 'list_flavors', {'detail': True}),
46 c36934a7 Giorgos Verigakis
    (r'^/(\d+)(?:.json|.xml)?$', 'get_flavor_details'),
47 c36934a7 Giorgos Verigakis
)
48 c36934a7 Giorgos Verigakis
49 c36934a7 Giorgos Verigakis
50 c36934a7 Giorgos Verigakis
def flavor_to_dict(flavor, detail=True):
51 c36934a7 Giorgos Verigakis
    d = {'id': flavor.id, 'name': flavor.name}
52 c36934a7 Giorgos Verigakis
    if detail:
53 c36934a7 Giorgos Verigakis
        d['ram'] = flavor.ram
54 c36934a7 Giorgos Verigakis
        d['disk'] = flavor.disk
55 f1641e4d Markos Gogoulos
        d['cpu'] = flavor.cpu
56 c36934a7 Giorgos Verigakis
    return d
57 c36934a7 Giorgos Verigakis
58 c36934a7 Giorgos Verigakis
59 b36f78fa Giorgos Verigakis
@util.api_method('GET')
60 c36934a7 Giorgos Verigakis
def list_flavors(request, detail=False):
61 c36934a7 Giorgos Verigakis
    # Normal Response Codes: 200, 203
62 c36934a7 Giorgos Verigakis
    # Error Response Codes: computeFault (400, 500),
63 c36934a7 Giorgos Verigakis
    #                       serviceUnavailable (503),
64 c36934a7 Giorgos Verigakis
    #                       unauthorized (401),
65 c36934a7 Giorgos Verigakis
    #                       badRequest (400),
66 c36934a7 Giorgos Verigakis
    #                       overLimit (413)
67 aa197ee4 Vangelis Koukis
68 c36934a7 Giorgos Verigakis
    all_flavors = Flavor.objects.all()
69 c36934a7 Giorgos Verigakis
    flavors = [flavor_to_dict(flavor, detail) for flavor in all_flavors]
70 aa197ee4 Vangelis Koukis
71 d8e50a39 Giorgos Verigakis
    if request.serialization == 'xml':
72 b36f78fa Giorgos Verigakis
        data = render_to_string('list_flavors.xml', {
73 b36f78fa Giorgos Verigakis
            'flavors': flavors,
74 b36f78fa Giorgos Verigakis
            'detail': detail})
75 c36934a7 Giorgos Verigakis
    else:
76 c36934a7 Giorgos Verigakis
        data = json.dumps({'flavors': {'values': flavors}})
77 aa197ee4 Vangelis Koukis
78 d8e50a39 Giorgos Verigakis
    return HttpResponse(data, status=200)
79 c36934a7 Giorgos Verigakis
80 b36f78fa Giorgos Verigakis
@util.api_method('GET')
81 c36934a7 Giorgos Verigakis
def get_flavor_details(request, flavor_id):
82 c36934a7 Giorgos Verigakis
    # Normal Response Codes: 200, 203
83 c36934a7 Giorgos Verigakis
    # Error Response Codes: computeFault (400, 500),
84 c36934a7 Giorgos Verigakis
    #                       serviceUnavailable (503),
85 c36934a7 Giorgos Verigakis
    #                       unauthorized (401),
86 c36934a7 Giorgos Verigakis
    #                       badRequest (400),
87 c36934a7 Giorgos Verigakis
    #                       itemNotFound (404),
88 c36934a7 Giorgos Verigakis
    #                       overLimit (413)
89 aa197ee4 Vangelis Koukis
90 b36f78fa Giorgos Verigakis
    flavor = util.get_flavor(flavor_id)
91 529178b1 Giorgos Verigakis
    flavordict = flavor_to_dict(flavor, detail=True)
92 aa197ee4 Vangelis Koukis
93 d8e50a39 Giorgos Verigakis
    if request.serialization == 'xml':
94 529178b1 Giorgos Verigakis
        data = render_to_string('flavor.xml', {'flavor': flavordict})
95 c36934a7 Giorgos Verigakis
    else:
96 529178b1 Giorgos Verigakis
        data = json.dumps({'flavor': flavordict})
97 aa197ee4 Vangelis Koukis
98 c36934a7 Giorgos Verigakis
    return HttpResponse(data, status=200)