root / api / versions.py @ d8e50a39
History | View | Annotate | Download (2.2 kB)
1 |
#
|
---|---|
2 |
# Copyright (c) 2010 Greek Research and Technology Network
|
3 |
#
|
4 |
|
5 |
from datetime import datetime |
6 |
|
7 |
from django.conf import settings |
8 |
from django.http import HttpResponse |
9 |
from django.template.loader import render_to_string |
10 |
from django.utils import simplejson as json |
11 |
|
12 |
from synnefo.api.util import api_method, isoformat |
13 |
|
14 |
|
15 |
VERSION_1_1 = { |
16 |
'id': 'v1.1', |
17 |
'status': 'CURRENT', |
18 |
'updated': '2011-04-01', |
19 |
'links': [
|
20 |
{ |
21 |
'rel': 'self', |
22 |
'href': settings.API_ROOT_URL,
|
23 |
} |
24 |
] |
25 |
} |
26 |
|
27 |
VERSIONS = [VERSION_1_1] |
28 |
|
29 |
MEDIA_TYPES = [ |
30 |
{'base': 'application/xml', 'type': 'application/vnd.openstack.compute-v1.1+xml'}, |
31 |
{'base': 'application/json', 'type': 'application/vnd.openstack.compute-v1.1+json'} |
32 |
] |
33 |
|
34 |
|
35 |
@api_method('GET', atom_allowed=True) |
36 |
def versions_list(request): |
37 |
# Normal Response Codes: 200, 203
|
38 |
# Error Response Codes: 400, 413, 500, 503
|
39 |
|
40 |
if request.serialization == 'xml': |
41 |
data = render_to_string('versions_list.xml', {'versions': VERSIONS}) |
42 |
elif request.serialization == 'atom': |
43 |
now = isoformat(datetime.now()) |
44 |
data = render_to_string('versions_list.atom', {'now': now,'versions': VERSIONS}) |
45 |
else:
|
46 |
data = json.dumps({'versions': {'values': VERSIONS}}) |
47 |
|
48 |
return HttpResponse(data)
|
49 |
|
50 |
@api_method('GET', atom_allowed=True) |
51 |
def version_details(request, api_version): |
52 |
# Normal Response Codes: 200, 203
|
53 |
# Error Response Codes: computeFault (400, 500),
|
54 |
# serviceUnavailable (503),
|
55 |
# unauthorized (401),
|
56 |
# badRequest (400),
|
57 |
# overLimit(413)
|
58 |
|
59 |
# We hardcode to v1.1 since it is the only one we support
|
60 |
version = VERSION_1_1.copy() |
61 |
|
62 |
if request.serialization == 'xml': |
63 |
version['media_types'] = MEDIA_TYPES
|
64 |
data = render_to_string('version_details.xml', {'version': version}) |
65 |
elif request.serialization == 'atom': |
66 |
version['media_types'] = MEDIA_TYPES
|
67 |
now = isoformat(datetime.now()) |
68 |
data = render_to_string('version_details.atom', {'now': now,'version': version}) |
69 |
else:
|
70 |
version['media-types'] = MEDIA_TYPES
|
71 |
data = json.dumps({'version': version})
|
72 |
return HttpResponse(data)
|