Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / versions.py @ 07419e58

History | View | Annotate | Download (3.8 kB)

1 adee02b8 Giorgos Verigakis
# Copyright 2011 GRNET S.A. All rights reserved.
2 e440e835 Christos Stavrakakis
#
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 e440e835 Christos Stavrakakis
#
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 e440e835 Christos Stavrakakis
#
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 e440e835 Christos Stavrakakis
#
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 e440e835 Christos Stavrakakis
#
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 2035039b Giorgos Verigakis
from logging import getLogger
35 d8e50a39 Giorgos Verigakis
36 d8e50a39 Giorgos Verigakis
from django.conf import settings
37 d8e50a39 Giorgos Verigakis
from django.http import HttpResponse
38 d8e50a39 Giorgos Verigakis
from django.template.loader import render_to_string
39 d8e50a39 Giorgos Verigakis
from django.utils import simplejson as json
40 d8e50a39 Giorgos Verigakis
41 b3fd98ae Christos Stavrakakis
from snf_django.lib import api
42 9e98ba3c Giorgos Verigakis
43 9e98ba3c Giorgos Verigakis
44 9e98ba3c Giorgos Verigakis
log = getLogger('synnefo.api')
45 d8e50a39 Giorgos Verigakis
46 d8e50a39 Giorgos Verigakis
47 d8e50a39 Giorgos Verigakis
VERSION_1_1 = {
48 d8e50a39 Giorgos Verigakis
    'id': 'v1.1',
49 d8e50a39 Giorgos Verigakis
    'status': 'CURRENT',
50 d8e50a39 Giorgos Verigakis
    'updated': '2011-04-01',
51 d8e50a39 Giorgos Verigakis
    'links': [
52 d8e50a39 Giorgos Verigakis
        {
53 d8e50a39 Giorgos Verigakis
            'rel': 'self',
54 d8e50a39 Giorgos Verigakis
            'href': settings.API_ROOT_URL,
55 d8e50a39 Giorgos Verigakis
        }
56 d8e50a39 Giorgos Verigakis
    ]
57 d8e50a39 Giorgos Verigakis
}
58 d8e50a39 Giorgos Verigakis
59 d8e50a39 Giorgos Verigakis
VERSIONS = [VERSION_1_1]
60 d8e50a39 Giorgos Verigakis
61 d8e50a39 Giorgos Verigakis
MEDIA_TYPES = [
62 b3fd98ae Christos Stavrakakis
    {'base': 'application/xml',
63 b3fd98ae Christos Stavrakakis
     'type': 'application/vnd.openstack.compute-v1.1+xml'},
64 b3fd98ae Christos Stavrakakis
    {'base': 'application/json',
65 b3fd98ae Christos Stavrakakis
     'type': 'application/vnd.openstack.compute-v1.1+json'}
66 d8e50a39 Giorgos Verigakis
]
67 d8e50a39 Giorgos Verigakis
68 c4c07cab Giorgos Verigakis
DESCRIBED_BY = [
69 b3fd98ae Christos Stavrakakis
    {'rel': 'describedby',
70 b3fd98ae Christos Stavrakakis
     'type': 'application/pdf',
71 b3fd98ae Christos Stavrakakis
     'href': "http://docs.rackspacecloud.com/servers/api/"
72 b3fd98ae Christos Stavrakakis
             "v1.1/cs-devguide-20110125.pdf"},
73 b3fd98ae Christos Stavrakakis
    {'rel': 'describedby',
74 b3fd98ae Christos Stavrakakis
     'type': 'application/vnd.sun.wadl+xml',
75 b3fd98ae Christos Stavrakakis
     'href': "http://docs.rackspacecloud.com/servers/api/v1.1/"
76 b3fd98ae Christos Stavrakakis
             "application.wadl"}
77 c4c07cab Giorgos Verigakis
]
78 d8e50a39 Giorgos Verigakis
79 b3fd98ae Christos Stavrakakis
80 b3fd98ae Christos Stavrakakis
@api.api_method(http_method='GET', user_required=True, logger=log)
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
    else:
88 d8e50a39 Giorgos Verigakis
        data = json.dumps({'versions': {'values': VERSIONS}})
89 aa197ee4 Vangelis Koukis
90 d8e50a39 Giorgos Verigakis
    return HttpResponse(data)
91 d8e50a39 Giorgos Verigakis
92 b3fd98ae Christos Stavrakakis
93 b3fd98ae Christos Stavrakakis
@api.api_method('GET', user_required=True, logger=log)
94 d8e50a39 Giorgos Verigakis
def version_details(request, api_version):
95 d8e50a39 Giorgos Verigakis
    # Normal Response Codes: 200, 203
96 d8e50a39 Giorgos Verigakis
    # Error Response Codes: computeFault (400, 500),
97 d8e50a39 Giorgos Verigakis
    #                       serviceUnavailable (503),
98 d8e50a39 Giorgos Verigakis
    #                       unauthorized (401),
99 d8e50a39 Giorgos Verigakis
    #                       badRequest (400),
100 d8e50a39 Giorgos Verigakis
    #                       overLimit(413)
101 e440e835 Christos Stavrakakis
102 9e98ba3c Giorgos Verigakis
    log.debug('version_details %s', api_version)
103 d8e50a39 Giorgos Verigakis
    # We hardcode to v1.1 since it is the only one we support
104 d8e50a39 Giorgos Verigakis
    version = VERSION_1_1.copy()
105 c4c07cab Giorgos Verigakis
    version['links'] = version['links'] + DESCRIBED_BY
106 aa197ee4 Vangelis Koukis
107 d8e50a39 Giorgos Verigakis
    if request.serialization == 'xml':
108 d8e50a39 Giorgos Verigakis
        version['media_types'] = MEDIA_TYPES
109 d8e50a39 Giorgos Verigakis
        data = render_to_string('version_details.xml', {'version': version})
110 d8e50a39 Giorgos Verigakis
    else:
111 d8e50a39 Giorgos Verigakis
        version['media-types'] = MEDIA_TYPES
112 d8e50a39 Giorgos Verigakis
        data = json.dumps({'version': version})
113 d8e50a39 Giorgos Verigakis
    return HttpResponse(data)