root / api / flavors.py @ adee02b8
History | View | Annotate | Download (3.6 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.http import HttpResponse |
36 |
from django.template.loader import render_to_string |
37 |
from django.utils import simplejson as json |
38 |
|
39 |
from synnefo.api import util |
40 |
from synnefo.db.models import Flavor |
41 |
|
42 |
|
43 |
urlpatterns = patterns('synnefo.api.flavors',
|
44 |
(r'^(?:/|.json|.xml)?$', 'list_flavors'), |
45 |
(r'^/detail(?:.json|.xml)?$', 'list_flavors', {'detail': True}), |
46 |
(r'^/(\d+)(?:.json|.xml)?$', 'get_flavor_details'), |
47 |
) |
48 |
|
49 |
|
50 |
def flavor_to_dict(flavor, detail=True): |
51 |
d = {'id': flavor.id, 'name': flavor.name} |
52 |
if detail:
|
53 |
d['ram'] = flavor.ram
|
54 |
d['disk'] = flavor.disk
|
55 |
d['cpu'] = flavor.cpu
|
56 |
return d
|
57 |
|
58 |
|
59 |
@util.api_method('GET') |
60 |
def list_flavors(request, detail=False): |
61 |
# Normal Response Codes: 200, 203
|
62 |
# Error Response Codes: computeFault (400, 500),
|
63 |
# serviceUnavailable (503),
|
64 |
# unauthorized (401),
|
65 |
# badRequest (400),
|
66 |
# overLimit (413)
|
67 |
|
68 |
all_flavors = Flavor.objects.all() |
69 |
flavors = [flavor_to_dict(flavor, detail) for flavor in all_flavors] |
70 |
|
71 |
if request.serialization == 'xml': |
72 |
data = render_to_string('list_flavors.xml', {
|
73 |
'flavors': flavors,
|
74 |
'detail': detail})
|
75 |
else:
|
76 |
data = json.dumps({'flavors': {'values': flavors}}) |
77 |
|
78 |
return HttpResponse(data, status=200) |
79 |
|
80 |
@util.api_method('GET') |
81 |
def get_flavor_details(request, flavor_id): |
82 |
# Normal Response Codes: 200, 203
|
83 |
# Error Response Codes: computeFault (400, 500),
|
84 |
# serviceUnavailable (503),
|
85 |
# unauthorized (401),
|
86 |
# badRequest (400),
|
87 |
# itemNotFound (404),
|
88 |
# overLimit (413)
|
89 |
|
90 |
flavor = util.get_flavor(flavor_id) |
91 |
flavordict = flavor_to_dict(flavor, detail=True)
|
92 |
|
93 |
if request.serialization == 'xml': |
94 |
data = render_to_string('flavor.xml', {'flavor': flavordict}) |
95 |
else:
|
96 |
data = json.dumps({'flavor': flavordict})
|
97 |
|
98 |
return HttpResponse(data, status=200) |