Coverage for manila/api/views/versions.py: 100%
29 statements
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
1# Copyright 2010-2011 OpenStack LLC.
2# Copyright 2015 Clinton Knight
3# All Rights Reserved.
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 copy
18import re
19import urllib
22def get_view_builder(req):
23 return ViewBuilder(req.application_url)
26_URL_SUFFIX = {'v1.0': 'v1', 'v2.0': 'v2'}
29class ViewBuilder(object):
30 def __init__(self, base_url):
31 """Initialize ViewBuilder.
33 :param base_url: url of the root wsgi application
34 """
35 self.base_url = base_url
37 def build_versions(self, versions):
38 views = [self._build_version(versions[key])
39 for key in sorted(list(versions.keys()))]
40 return dict(versions=views)
42 def _build_version(self, version):
43 view = copy.deepcopy(version)
44 view['links'] = self._build_links(version)
45 return view
47 def _build_links(self, version_data):
48 """Generate a container of links that refer to the provided version."""
49 links = copy.deepcopy(version_data.get('links', {}))
50 version = _URL_SUFFIX.get(version_data['id'])
51 links.append({'rel': 'self',
52 'href': self._generate_href(version=version)})
53 return links
55 def _generate_href(self, version='v1', path=None):
56 """Create a URL that refers to a specific version_number."""
57 base_url = self._get_base_url_without_version()
58 href = urllib.parse.urljoin(base_url, version).rstrip('/') + '/'
59 if path:
60 href += path.lstrip('/')
61 return href
63 def _get_base_url_without_version(self):
64 """Get the base URL with out the /v1 suffix."""
65 return re.sub('v[1-9]+/?$', '', self.base_url)