Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / versions.py @ 6dd70a5c

History | View | Annotate | Download (4.2 kB)

1 adee02b8 Giorgos Verigakis
# Copyright 2011 GRNET S.A. All rights reserved.
2 adee02b8 Giorgos Verigakis
# 
3 adee02b8 Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 adee02b8 Giorgos Verigakis
# without modification, are permitted provided that the following
5 adee02b8 Giorgos Verigakis
# conditions are met:
6 adee02b8 Giorgos Verigakis
# 
7 adee02b8 Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 adee02b8 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 adee02b8 Giorgos Verigakis
#      disclaimer.
10 adee02b8 Giorgos Verigakis
# 
11 adee02b8 Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 adee02b8 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 adee02b8 Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 adee02b8 Giorgos Verigakis
#      provided with the distribution.
15 adee02b8 Giorgos Verigakis
# 
16 adee02b8 Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 adee02b8 Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 adee02b8 Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 adee02b8 Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 adee02b8 Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 adee02b8 Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 adee02b8 Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 adee02b8 Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 adee02b8 Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 adee02b8 Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 adee02b8 Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 adee02b8 Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 adee02b8 Giorgos Verigakis
# 
29 adee02b8 Giorgos Verigakis
# The views and conclusions contained in the software and
30 adee02b8 Giorgos Verigakis
# documentation are those of the authors and should not be
31 adee02b8 Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 adee02b8 Giorgos Verigakis
# or implied, of GRNET S.A.
33 d8e50a39 Giorgos Verigakis
34 d8e50a39 Giorgos Verigakis
from datetime import datetime
35 2035039b Giorgos Verigakis
from logging import getLogger
36 d8e50a39 Giorgos Verigakis
37 d8e50a39 Giorgos Verigakis
from django.conf import settings
38 d8e50a39 Giorgos Verigakis
from django.http import HttpResponse
39 d8e50a39 Giorgos Verigakis
from django.template.loader import render_to_string
40 d8e50a39 Giorgos Verigakis
from django.utils import simplejson as json
41 d8e50a39 Giorgos Verigakis
42 d8e50a39 Giorgos Verigakis
from synnefo.api.util import api_method, isoformat
43 9e98ba3c Giorgos Verigakis
44 9e98ba3c Giorgos Verigakis
45 9e98ba3c Giorgos Verigakis
log = getLogger('synnefo.api')
46 d8e50a39 Giorgos Verigakis
47 d8e50a39 Giorgos Verigakis
48 d8e50a39 Giorgos Verigakis
VERSION_1_1 = {
49 d8e50a39 Giorgos Verigakis
    'id': 'v1.1',
50 d8e50a39 Giorgos Verigakis
    'status': 'CURRENT',
51 d8e50a39 Giorgos Verigakis
    'updated': '2011-04-01',
52 d8e50a39 Giorgos Verigakis
    'links': [
53 d8e50a39 Giorgos Verigakis
        {
54 d8e50a39 Giorgos Verigakis
            'rel': 'self',
55 d8e50a39 Giorgos Verigakis
            'href': settings.API_ROOT_URL,
56 d8e50a39 Giorgos Verigakis
        }
57 d8e50a39 Giorgos Verigakis
    ]
58 d8e50a39 Giorgos Verigakis
}
59 d8e50a39 Giorgos Verigakis
60 d8e50a39 Giorgos Verigakis
VERSIONS = [VERSION_1_1]
61 d8e50a39 Giorgos Verigakis
62 d8e50a39 Giorgos Verigakis
MEDIA_TYPES = [
63 d8e50a39 Giorgos Verigakis
    {'base': 'application/xml', 'type': 'application/vnd.openstack.compute-v1.1+xml'},
64 d8e50a39 Giorgos Verigakis
    {'base': 'application/json', 'type': 'application/vnd.openstack.compute-v1.1+json'}
65 d8e50a39 Giorgos Verigakis
]
66 d8e50a39 Giorgos Verigakis
67 c4c07cab Giorgos Verigakis
DESCRIBED_BY = [
68 c4c07cab Giorgos Verigakis
    {
69 c4c07cab Giorgos Verigakis
        'rel' : 'describedby',
70 c4c07cab Giorgos Verigakis
        'type' : 'application/pdf',
71 c4c07cab Giorgos Verigakis
        'href' : 'http://docs.rackspacecloud.com/servers/api/v1.1/cs-devguide-20110125.pdf'
72 c4c07cab Giorgos Verigakis
    },
73 c4c07cab Giorgos Verigakis
    {
74 c4c07cab Giorgos Verigakis
        'rel' : 'describedby',
75 c4c07cab Giorgos Verigakis
        'type' : 'application/vnd.sun.wadl+xml',
76 c4c07cab Giorgos Verigakis
        'href' : 'http://docs.rackspacecloud.com/servers/api/v1.1/application.wadl'
77 c4c07cab Giorgos Verigakis
    }
78 c4c07cab Giorgos Verigakis
]
79 d8e50a39 Giorgos Verigakis
80 d8e50a39 Giorgos Verigakis
@api_method('GET', atom_allowed=True)
81 d8e50a39 Giorgos Verigakis
def versions_list(request):
82 d8e50a39 Giorgos Verigakis
    # Normal Response Codes: 200, 203
83 d8e50a39 Giorgos Verigakis
    # Error Response Codes: 400, 413, 500, 503
84 aa197ee4 Vangelis Koukis
85 d8e50a39 Giorgos Verigakis
    if request.serialization == 'xml':
86 d8e50a39 Giorgos Verigakis
        data = render_to_string('versions_list.xml', {'versions': VERSIONS})
87 d8e50a39 Giorgos Verigakis
    elif request.serialization == 'atom':
88 d8e50a39 Giorgos Verigakis
        now = isoformat(datetime.now())
89 d8e50a39 Giorgos Verigakis
        data = render_to_string('versions_list.atom', {'now': now,'versions': VERSIONS})
90 d8e50a39 Giorgos Verigakis
    else:
91 d8e50a39 Giorgos Verigakis
        data = json.dumps({'versions': {'values': VERSIONS}})
92 aa197ee4 Vangelis Koukis
93 d8e50a39 Giorgos Verigakis
    return HttpResponse(data)
94 d8e50a39 Giorgos Verigakis
95 d8e50a39 Giorgos Verigakis
@api_method('GET', atom_allowed=True)
96 d8e50a39 Giorgos Verigakis
def version_details(request, api_version):
97 d8e50a39 Giorgos Verigakis
    # Normal Response Codes: 200, 203
98 d8e50a39 Giorgos Verigakis
    # Error Response Codes: computeFault (400, 500),
99 d8e50a39 Giorgos Verigakis
    #                       serviceUnavailable (503),
100 d8e50a39 Giorgos Verigakis
    #                       unauthorized (401),
101 d8e50a39 Giorgos Verigakis
    #                       badRequest (400),
102 d8e50a39 Giorgos Verigakis
    #                       overLimit(413)
103 9e98ba3c Giorgos Verigakis
    
104 9e98ba3c Giorgos Verigakis
    log.debug('version_details %s', api_version)
105 d8e50a39 Giorgos Verigakis
    # We hardcode to v1.1 since it is the only one we support
106 d8e50a39 Giorgos Verigakis
    version = VERSION_1_1.copy()
107 c4c07cab Giorgos Verigakis
    version['links'] = version['links'] + DESCRIBED_BY
108 aa197ee4 Vangelis Koukis
109 d8e50a39 Giorgos Verigakis
    if request.serialization == 'xml':
110 d8e50a39 Giorgos Verigakis
        version['media_types'] = MEDIA_TYPES
111 d8e50a39 Giorgos Verigakis
        data = render_to_string('version_details.xml', {'version': version})
112 d8e50a39 Giorgos Verigakis
    elif request.serialization == 'atom':
113 d8e50a39 Giorgos Verigakis
        version['media_types'] = MEDIA_TYPES
114 d8e50a39 Giorgos Verigakis
        now = isoformat(datetime.now())
115 d8e50a39 Giorgos Verigakis
        data = render_to_string('version_details.atom', {'now': now,'version': version})
116 d8e50a39 Giorgos Verigakis
    else:
117 d8e50a39 Giorgos Verigakis
        version['media-types'] = MEDIA_TYPES
118 d8e50a39 Giorgos Verigakis
        data = json.dumps({'version': version})
119 d8e50a39 Giorgos Verigakis
    return HttpResponse(data)