summaryrefslogtreecommitdiff
path: root/trove/versions.py
blob: 48ed94d90bdbf7fcceda4a762be62a6f34e7ef31 (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Copyright 2011 OpenStack Foundation
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import os
import routes

from trove.common import cfg
from trove.common import wsgi

CONF = cfg.CONF
VERSIONS = {
    "1.0": {
        "id": "v1.0",
        "status": "CURRENT",
        "updated": "2012-08-01T00:00:00Z",
        "links": [],
    },
}


class VersionsController(wsgi.Controller):

    def index(self, request):
        """Respond to a request for API versions."""
        versions = []
        for key, data in VERSIONS.items():
            v = BaseVersion(
                data["id"],
                data["status"],
                request.application_url,
                data["updated"])
            versions.append(v)
        return wsgi.Result(VersionsDataView(versions))

    def show(self, request):
        """Respond to a request for a specific API version."""
        data = VERSIONS[request.url_version]
        v = Version(data["id"], data["status"],
                    request.application_url, data["updated"])
        return wsgi.Result(VersionDataView(v))


class BaseVersion(object):

    def __init__(self, id, status, base_url, updated):
        self.id = id
        self.status = status
        self.base_url = CONF.public_endpoint or base_url
        self.updated = updated

    def data(self):
        return {
            "id": self.id,
            "status": self.status,
            "updated": self.updated,
            "links": [{"rel": "self", "href": self.url()}],
        }

    def url(self):
        url = os.path.join(self.base_url, self.id)
        if not url.endswith("/"):
            return url + "/"
        return url


class Version(BaseVersion):

    def url(self):
        if not self.base_url.endswith("/"):
            return self.base_url + "/"
        return self.base_url


class VersionDataView(object):

    def __init__(self, version):
        self.version = version

    def data_for_json(self):
        return {'version': self.version.data()}


class VersionsDataView(object):

    def __init__(self, versions):
        self.versions = versions

    def data_for_json(self):
        return {'versions': [version.data() for version in self.versions]}


class VersionsAPI(wsgi.Router):
    def __init__(self):
        mapper = routes.Mapper()
        versions_resource = VersionsController().create_resource()
        mapper.connect("/", controller=versions_resource, action="index")
        super(VersionsAPI, self).__init__(mapper)


def app_factory(global_conf, **local_conf):
    conf = global_conf.copy()
    conf.update(local_conf)
    return VersionsAPI()