Statistics
| Branch: | Tag: | Revision:

root / api / versions.py @ aa197ee4

History | View | Annotate | Download (2.6 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
DESCRIBED_BY = [
35
    {
36
        'rel' : 'describedby',
37
        'type' : 'application/pdf',
38
        'href' : 'http://docs.rackspacecloud.com/servers/api/v1.1/cs-devguide-20110125.pdf'
39
    },
40
    {
41
        'rel' : 'describedby',
42
        'type' : 'application/vnd.sun.wadl+xml',
43
        'href' : 'http://docs.rackspacecloud.com/servers/api/v1.1/application.wadl'
44
    }
45
]
46

    
47
@api_method('GET', atom_allowed=True)
48
def versions_list(request):
49
    # Normal Response Codes: 200, 203
50
    # Error Response Codes: 400, 413, 500, 503
51

    
52
    if request.serialization == 'xml':
53
        data = render_to_string('versions_list.xml', {'versions': VERSIONS})
54
    elif request.serialization == 'atom':
55
        now = isoformat(datetime.now())
56
        data = render_to_string('versions_list.atom', {'now': now,'versions': VERSIONS})
57
    else:
58
        data = json.dumps({'versions': {'values': VERSIONS}})
59

    
60
    return HttpResponse(data)
61

    
62
@api_method('GET', atom_allowed=True)
63
def version_details(request, api_version):
64
    # Normal Response Codes: 200, 203
65
    # Error Response Codes: computeFault (400, 500),
66
    #                       serviceUnavailable (503),
67
    #                       unauthorized (401),
68
    #                       badRequest (400),
69
    #                       overLimit(413)
70

    
71
    # We hardcode to v1.1 since it is the only one we support
72
    version = VERSION_1_1.copy()
73
    version['links'] = version['links'] + DESCRIBED_BY
74

    
75
    if request.serialization == 'xml':
76
        version['media_types'] = MEDIA_TYPES
77
        data = render_to_string('version_details.xml', {'version': version})
78
    elif request.serialization == 'atom':
79
        version['media_types'] = MEDIA_TYPES
80
        now = isoformat(datetime.now())
81
        data = render_to_string('version_details.atom', {'now': now,'version': version})
82
    else:
83
        version['media-types'] = MEDIA_TYPES
84
        data = json.dumps({'version': version})
85
    return HttpResponse(data)