versions.py 2.63 KB
Newer Older
Giorgos Verigakis's avatar
Giorgos Verigakis committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#
# Copyright (c) 2010 Greek Research and Technology Network
#

from datetime import datetime

from django.conf import settings
from django.http import HttpResponse
from django.template.loader import render_to_string
from django.utils import simplejson as json

from synnefo.api.util import api_method, isoformat


VERSION_1_1 = {
    'id': 'v1.1',
    'status': 'CURRENT',
    'updated': '2011-04-01',
    'links': [
        {
            'rel': 'self',
            'href': settings.API_ROOT_URL,
        }
    ]
}

VERSIONS = [VERSION_1_1]

MEDIA_TYPES = [
    {'base': 'application/xml', 'type': 'application/vnd.openstack.compute-v1.1+xml'},
    {'base': 'application/json', 'type': 'application/vnd.openstack.compute-v1.1+json'}
]

34
35
36
37
38
39
40
41
42
43
44
45
DESCRIBED_BY = [
    {
        'rel' : 'describedby',
        'type' : 'application/pdf',
        'href' : 'http://docs.rackspacecloud.com/servers/api/v1.1/cs-devguide-20110125.pdf'
    },
    {
        'rel' : 'describedby',
        'type' : 'application/vnd.sun.wadl+xml',
        'href' : 'http://docs.rackspacecloud.com/servers/api/v1.1/application.wadl'
    }
]
Giorgos Verigakis's avatar
Giorgos Verigakis committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

@api_method('GET', atom_allowed=True)
def versions_list(request):
    # Normal Response Codes: 200, 203
    # Error Response Codes: 400, 413, 500, 503
    
    if request.serialization == 'xml':
        data = render_to_string('versions_list.xml', {'versions': VERSIONS})
    elif request.serialization == 'atom':
        now = isoformat(datetime.now())
        data = render_to_string('versions_list.atom', {'now': now,'versions': VERSIONS})
    else:
        data = json.dumps({'versions': {'values': VERSIONS}})
        
    return HttpResponse(data)

@api_method('GET', atom_allowed=True)
def version_details(request, api_version):
    # Normal Response Codes: 200, 203
    # Error Response Codes: computeFault (400, 500),
    #                       serviceUnavailable (503),
    #                       unauthorized (401),
    #                       badRequest (400),
    #                       overLimit(413)

    # We hardcode to v1.1 since it is the only one we support
    version = VERSION_1_1.copy()
73
    version['links'] = version['links'] + DESCRIBED_BY
Giorgos Verigakis's avatar
Giorgos Verigakis committed
74
75
76
77
78
79
80
81
82
83
84
85
    
    if request.serialization == 'xml':
        version['media_types'] = MEDIA_TYPES
        data = render_to_string('version_details.xml', {'version': version})
    elif request.serialization == 'atom':
        version['media_types'] = MEDIA_TYPES
        now = isoformat(datetime.now())
        data = render_to_string('version_details.atom', {'now': now,'version': version})
    else:
        version['media-types'] = MEDIA_TYPES
        data = json.dumps({'version': version})
    return HttpResponse(data)