Coverage for watcher/api/controllers/root.py: 100%
65 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-17 12:22 +0000
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-17 12:22 +0000
1# -*- encoding: utf-8 -*-
2#
3# Copyright © 2012 New Dream Network, LLC (DreamHost)
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
17import importlib
19import pecan
20from pecan import rest
21from wsme import types as wtypes
22import wsmeext.pecan as wsme_pecan
24from watcher.api.controllers import base
25from watcher.api.controllers import link
26from watcher.api.controllers import v1
29class APIStatus(object):
30 CURRENT = "CURRENT"
31 SUPPORTED = "SUPPORTED"
32 DEPRECATED = "DEPRECATED"
33 EXPERIMENTAL = "EXPERIMENTAL"
36class Version(base.APIBase):
37 """An API version representation."""
39 id = wtypes.text
40 """The ID of the version, also acts as the release number"""
42 status = wtypes.text
43 """The state of this API version"""
45 max_version = wtypes.text
46 """The maximum version supported"""
48 min_version = wtypes.text
49 """The minimum version supported"""
51 links = [link.Link]
52 """A Link that point to a specific version of the API"""
54 @staticmethod
55 def convert(id, status=APIStatus.CURRENT):
56 v = importlib.import_module('watcher.api.controllers.%s.versions' % id)
57 version = Version()
58 version.id = id
59 version.status = status
60 version.max_version = v.max_version_string()
61 version.min_version = v.min_version_string()
62 version.links = [link.Link.make_link('self',
63 pecan.request.application_url,
64 id, '', bookmark=True)]
65 return version
68class Root(base.APIBase):
70 name = wtypes.text
71 """The name of the API"""
73 description = wtypes.text
74 """Some information about this API"""
76 versions = [Version]
77 """Links to all the versions available in this API"""
79 default_version = Version
80 """A link to the default version of the API"""
82 @staticmethod
83 def convert():
84 root = Root()
85 root.name = "OpenStack Watcher API"
86 root.description = ("Watcher is an OpenStack project which aims to "
87 "improve physical resources usage through "
88 "better VM placement.")
89 root.versions = [Version.convert('v1')]
90 root.default_version = Version.convert('v1')
91 return root
94class RootController(rest.RestController):
96 _versions = ['v1']
97 """All supported API versions"""
99 _default_version = 'v1'
100 """The default API version"""
102 v1 = v1.Controller()
104 @wsme_pecan.wsexpose(Root)
105 def get(self):
106 # NOTE: The reason why convert() it's being called for every
107 # request is because we need to get the host url from
108 # the request object to make the links.
109 return Root.convert()
111 @pecan.expose()
112 def _route(self, args):
113 """Overrides the default routing behavior.
115 It redirects the request to the default version of the watcher API
116 if the version number is not specified in the url.
117 """
119 if args[0] and args[0] not in self._versions:
120 args = [self._default_version] + args
121 return super(RootController, self)._route(args)