Coverage for nova/api/openstack/compute/views/versions.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-24 11:16 +0000

1# Copyright 2010-2011 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 

16import copy 

17 

18from nova.api.openstack import common 

19 

20 

21def get_view_builder(req): 

22 base_url = req.application_url 

23 return ViewBuilder(base_url) 

24 

25 

26class ViewBuilder(common.ViewBuilder): 

27 

28 def __init__(self, base_url): 

29 """:param base_url: url of the root wsgi application.""" 

30 self.prefix = self._update_compute_link_prefix(base_url) 

31 self.base_url = base_url 

32 

33 def build_choices(self, VERSIONS, req): 

34 version_objs = [] 

35 for version in sorted(VERSIONS): 

36 version = VERSIONS[version] 

37 version_objs.append({ 

38 "id": version['id'], 

39 "status": version['status'], 

40 "links": [ 

41 { 

42 "rel": "self", 

43 "href": self.generate_href(version['id'], req.path), 

44 }, 

45 ], 

46 "media-types": version['media-types'], 

47 }) 

48 

49 return dict(choices=version_objs) 

50 

51 def build_versions(self, versions): 

52 version_objs = [] 

53 for version in sorted(versions.keys()): 

54 version = versions[version] 

55 version_objs.append({ 

56 "id": version['id'], 

57 "status": version['status'], 

58 "version": version['version'], 

59 "min_version": version['min_version'], 

60 "updated": version['updated'], 

61 "links": self._build_links(version), 

62 }) 

63 

64 return dict(versions=version_objs) 

65 

66 def build_version(self, version): 

67 reval = copy.deepcopy(version) 

68 reval['links'].insert(0, { 

69 "rel": "self", 

70 "href": self.prefix.rstrip('/') + '/', 

71 }) 

72 return dict(version=reval) 

73 

74 def _build_links(self, version_data): 

75 """Generate a container of links that refer to the provided version.""" 

76 href = self.generate_href(version_data['id']) 

77 

78 links = [ 

79 { 

80 "rel": "self", 

81 "href": href, 

82 }, 

83 ] 

84 

85 return links 

86 

87 def generate_href(self, version, path=None): 

88 """Create an url that refers to a specific version_number.""" 

89 if version.find('v2.1') == 0: 

90 version_number = 'v2.1' 

91 else: 

92 version_number = 'v2' 

93 

94 path = path or '' 

95 return common.url_join(self.prefix, version_number, path)