add escalator frame
[escalator.git] / api / escalator / api / versions.py
1 # Copyright 2012 OpenStack Foundation.
2 # All Rights Reserved.
3 #
4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
5 #    not use this file except in compliance with the License. You may obtain
6 #    a copy of the License at
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #    Unless required by applicable law or agreed to in writing, software
11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 #    License for the specific language governing permissions and limitations
14 #    under the License.
15
16 import httplib
17
18 from oslo_serialization import jsonutils
19 from oslo_config import cfg
20 import webob.dec
21
22 from escalator.common import wsgi
23 from escalator import i18n
24
25 _ = i18n._
26
27 versions_opts = [
28     cfg.StrOpt('public_endpoint', default=None,
29                help=_('Public url to use for versions endpoint. The default '
30                       'is None, which will use the request\'s host_url '
31                       'attribute to populate the URL base. If Escalator is '
32                       'operating behind a proxy, you will want to change '
33                       'this to represent the proxy\'s URL.')),
34 ]
35
36 CONF = cfg.CONF
37 CONF.register_opts(versions_opts)
38
39
40 class Controller(object):
41
42     """A wsgi controller that reports which API versions are supported."""
43
44     def index(self, req):
45         """Respond to a request for all OpenStack API versions."""
46         def build_version_object(version, path, status):
47             url = CONF.public_endpoint or req.host_url
48             return {
49                 'id': 'v%s' % version,
50                 'status': status,
51                 'links': [
52                     {
53                         'rel': 'self',
54                         'href': '%s/%s/' % (url, path),
55                     },
56                 ],
57             }
58
59         version_objs = []
60         if CONF.enable_v1_api:
61             version_objs.extend([
62                 build_version_object(1.1, 'v1', 'SUPPORTED'),
63                 build_version_object(1.0, 'v1', 'SUPPORTED'),
64             ])
65
66         response = webob.Response(request=req,
67                                   status=httplib.MULTIPLE_CHOICES,
68                                   content_type='application/json')
69         response.body = jsonutils.dumps(dict(versions=version_objs))
70         return response
71
72     @webob.dec.wsgify(RequestClass=wsgi.Request)
73     def __call__(self, req):
74         return self.index(req)
75
76
77 def create_resource(conf):
78     return wsgi.Resource(Controller())