root / snf-cyclades-app / synnefo / api / flavors.py @ be1dca04
History | View | Annotate | Download (3.9 kB)
1 |
# Copyright 2011, 2012, 2013 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 logging import getLogger |
35 |
|
36 |
from django.conf.urls.defaults import patterns |
37 |
from django.http import HttpResponse |
38 |
from django.template.loader import render_to_string |
39 |
from django.utils import simplejson as json |
40 |
|
41 |
from synnefo.api import util |
42 |
from synnefo.db.models import Flavor |
43 |
|
44 |
|
45 |
log = getLogger('synnefo.api')
|
46 |
|
47 |
|
48 |
urlpatterns = patterns( |
49 |
'synnefo.api.flavors',
|
50 |
(r'^(?:/|.json|.xml)?$', 'list_flavors'), |
51 |
(r'^/detail(?:.json|.xml)?$', 'list_flavors', {'detail': True}), |
52 |
(r'^/(\d+)(?:.json|.xml)?$', 'get_flavor_details'), |
53 |
) |
54 |
|
55 |
|
56 |
def flavor_to_dict(flavor, detail=True): |
57 |
d = {'id': flavor.id, 'name': flavor.name} |
58 |
if detail:
|
59 |
d['ram'] = flavor.ram
|
60 |
d['disk'] = flavor.disk
|
61 |
d['cpu'] = flavor.cpu
|
62 |
d['SNF:disk_template'] = flavor.disk_template
|
63 |
return d
|
64 |
|
65 |
|
66 |
@util.api_method('GET') |
67 |
def list_flavors(request, detail=False): |
68 |
# Normal Response Codes: 200, 203
|
69 |
# Error Response Codes: computeFault (400, 500),
|
70 |
# serviceUnavailable (503),
|
71 |
# unauthorized (401),
|
72 |
# badRequest (400),
|
73 |
# overLimit (413)
|
74 |
|
75 |
log.debug('list_flavors detail=%s', detail)
|
76 |
active_flavors = Flavor.objects.exclude(deleted=True)
|
77 |
flavors = [flavor_to_dict(flavor, detail) |
78 |
for flavor in active_flavors.order_by('id')] |
79 |
|
80 |
if request.serialization == 'xml': |
81 |
data = render_to_string('list_flavors.xml', {
|
82 |
'flavors': flavors,
|
83 |
'detail': detail})
|
84 |
else:
|
85 |
data = json.dumps({'flavors': {'values': flavors}}) |
86 |
|
87 |
return HttpResponse(data, status=200) |
88 |
|
89 |
|
90 |
@util.api_method('GET') |
91 |
def get_flavor_details(request, flavor_id): |
92 |
# Normal Response Codes: 200, 203
|
93 |
# Error Response Codes: computeFault (400, 500),
|
94 |
# serviceUnavailable (503),
|
95 |
# unauthorized (401),
|
96 |
# badRequest (400),
|
97 |
# itemNotFound (404),
|
98 |
# overLimit (413)
|
99 |
|
100 |
log.debug('get_flavor_details %s', flavor_id)
|
101 |
flavor = util.get_flavor(flavor_id, include_deleted=True)
|
102 |
flavordict = flavor_to_dict(flavor, detail=True)
|
103 |
|
104 |
if request.serialization == 'xml': |
105 |
data = render_to_string('flavor.xml', {'flavor': flavordict}) |
106 |
else:
|
107 |
data = json.dumps({'flavor': flavordict})
|
108 |
|
109 |
return HttpResponse(data, status=200) |