Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / versions.py @ 1102774c

History | View | Annotate | Download (3.9 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.http import HttpResponse
37 d8e50a39 Giorgos Verigakis
from django.template.loader import render_to_string
38 d8e50a39 Giorgos Verigakis
from django.utils import simplejson as json
39 e3ff6830 Georgios D. Tsoukalas
from synnefo.cyclades_settings import COMPUTE_ROOT_URL
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 2af5dc96 Georgios D. Tsoukalas
VERSION_2_0 = {
48 32e4e343 Christos Stavrakakis
    "id": "v2.0",
49 32e4e343 Christos Stavrakakis
    "status": "CURRENT",
50 32e4e343 Christos Stavrakakis
    "updated": "2011-01-21T11:33:21-06:00",
51 2af5dc96 Georgios D. Tsoukalas
    "links": [
52 d8e50a39 Giorgos Verigakis
        {
53 32e4e343 Christos Stavrakakis
            "rel": "self",
54 32e4e343 Christos Stavrakakis
            "href": COMPUTE_ROOT_URL,
55 2af5dc96 Georgios D. Tsoukalas
        },
56 2af5dc96 Georgios D. Tsoukalas
    ],
57 d8e50a39 Giorgos Verigakis
}
58 d8e50a39 Giorgos Verigakis
59 2af5dc96 Georgios D. Tsoukalas
VERSIONS = [VERSION_2_0]
60 d8e50a39 Giorgos Verigakis
61 d8e50a39 Giorgos Verigakis
MEDIA_TYPES = [
62 2af5dc96 Georgios D. Tsoukalas
    {
63 32e4e343 Christos Stavrakakis
        "base": "application/xml",
64 32e4e343 Christos Stavrakakis
        "type": "application/vnd.openstack.compute.v2+xml"
65 2af5dc96 Georgios D. Tsoukalas
    },
66 2af5dc96 Georgios D. Tsoukalas
    {
67 32e4e343 Christos Stavrakakis
        "base": "application/json",
68 32e4e343 Christos Stavrakakis
        "type": "application/vnd.openstack.compute.v2+json"
69 2af5dc96 Georgios D. Tsoukalas
    }
70 d8e50a39 Giorgos Verigakis
]
71 d8e50a39 Giorgos Verigakis
72 c4c07cab Giorgos Verigakis
DESCRIBED_BY = [
73 2af5dc96 Georgios D. Tsoukalas
    {
74 32e4e343 Christos Stavrakakis
        "rel": "describedby",
75 32e4e343 Christos Stavrakakis
        "type": "application/pdf",
76 32e4e343 Christos Stavrakakis
        "href": "http://docs.rackspacecloud.com/servers/api/v2/"
77 32e4e343 Christos Stavrakakis
                "cs-devguide-20110125.pdf"
78 2af5dc96 Georgios D. Tsoukalas
    },
79 2af5dc96 Georgios D. Tsoukalas
    {
80 32e4e343 Christos Stavrakakis
        "rel": "describedby",
81 32e4e343 Christos Stavrakakis
        "type": "application/vnd.sun.wadl+xml",
82 32e4e343 Christos Stavrakakis
        "href": "http://docs.rackspacecloud.com/servers/api/v2/"
83 32e4e343 Christos Stavrakakis
                "application.wadl"
84 2af5dc96 Georgios D. Tsoukalas
    }
85 c4c07cab Giorgos Verigakis
]
86 d8e50a39 Giorgos Verigakis
87 b3fd98ae Christos Stavrakakis
88 b3fd98ae Christos Stavrakakis
@api.api_method(http_method='GET', user_required=True, logger=log)
89 d8e50a39 Giorgos Verigakis
def versions_list(request):
90 d8e50a39 Giorgos Verigakis
    # Normal Response Codes: 200, 203
91 d8e50a39 Giorgos Verigakis
    # Error Response Codes: 400, 413, 500, 503
92 aa197ee4 Vangelis Koukis
93 d8e50a39 Giorgos Verigakis
    if request.serialization == 'xml':
94 d8e50a39 Giorgos Verigakis
        data = render_to_string('versions_list.xml', {'versions': VERSIONS})
95 d8e50a39 Giorgos Verigakis
    else:
96 5029ff36 Christos Stavrakakis
        data = json.dumps({'versions': VERSIONS})
97 aa197ee4 Vangelis Koukis
98 d8e50a39 Giorgos Verigakis
    return HttpResponse(data)
99 d8e50a39 Giorgos Verigakis
100 b3fd98ae Christos Stavrakakis
101 b3fd98ae Christos Stavrakakis
@api.api_method('GET', user_required=True, logger=log)
102 d8e50a39 Giorgos Verigakis
def version_details(request, api_version):
103 d8e50a39 Giorgos Verigakis
    # Normal Response Codes: 200, 203
104 d8e50a39 Giorgos Verigakis
    # Error Response Codes: computeFault (400, 500),
105 d8e50a39 Giorgos Verigakis
    #                       serviceUnavailable (503),
106 d8e50a39 Giorgos Verigakis
    #                       unauthorized (401),
107 d8e50a39 Giorgos Verigakis
    #                       badRequest (400),
108 d8e50a39 Giorgos Verigakis
    #                       overLimit(413)
109 e440e835 Christos Stavrakakis
110 9e98ba3c Giorgos Verigakis
    log.debug('version_details %s', api_version)
111 2af5dc96 Georgios D. Tsoukalas
    # We hardcode to v2.0 since it is the only one we support
112 2af5dc96 Georgios D. Tsoukalas
    version = VERSION_2_0.copy()
113 c4c07cab Giorgos Verigakis
    version['links'] = version['links'] + DESCRIBED_BY
114 aa197ee4 Vangelis Koukis
115 d8e50a39 Giorgos Verigakis
    if request.serialization == 'xml':
116 d8e50a39 Giorgos Verigakis
        version['media_types'] = MEDIA_TYPES
117 d8e50a39 Giorgos Verigakis
        data = render_to_string('version_details.xml', {'version': version})
118 d8e50a39 Giorgos Verigakis
    else:
119 d8e50a39 Giorgos Verigakis
        version['media-types'] = MEDIA_TYPES
120 d8e50a39 Giorgos Verigakis
        data = json.dumps({'version': version})
121 d8e50a39 Giorgos Verigakis
    return HttpResponse(data)