/
version.py
157 lines (124 loc) · 4.2 KB
/
version.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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.
from distutils.version import StrictVersion
import operator
NEGOTIATE_VERSIONS = [
'1', # Added for auto choice for appropriate version
]
SUPPORTED_MICROVERSIONS = [
'1.0',
'1.1',
'1.2',
'1.3',
'1.4',
'1.5',
'1.6',
'1.7',
'1.8',
'1.9',
'1.10',
'1.11',
'1.12',
'1.13', # unused
'1.14',
'1.15', # unused
'1.16',
'1.17',
'1.18',
'1.19',
'1.20', # unused
'1.21',
'1.22',
'1.23', # unused
'1.24',
'1.25',
'1.26', # unused
'1.27', # unused
'1.28', # Added for provider allocation (un)set (Ussuri)
'1.29',
]
SUPPORTED_VERSIONS = SUPPORTED_MICROVERSIONS + NEGOTIATE_VERSIONS
# The max microversion lower than which are all supported by this client.
# This is used to automatically pick up the microversion to use. Change this
# when you add a microversion to the `_SUPPORTED_VERSIONS` without a gap.
# TestVersion.test_max_version_consistency checks its consistency.
MAX_VERSION_NO_GAP = '1.29'
def _op(func, b, msg):
return lambda a: func(StrictVersion(a), StrictVersion(b)) or msg
def lt(b):
msg = 'requires version less than %s' % b
return _op(operator.lt, b, msg)
def le(b):
msg = 'requires at most version %s' % b
return _op(operator.le, b, msg)
def eq(b):
msg = 'requires version %s' % b
return _op(operator.eq, b, msg)
def ne(b):
msg = 'can not use version %s' % b
return _op(operator.ne, b, msg)
def ge(b):
msg = 'requires at least version %s' % b
return _op(operator.ge, b, msg)
def gt(b):
msg = 'requires version greater than %s' % b
return _op(operator.gt, b, msg)
def _compare(ver, *predicates, **kwargs):
func = kwargs.get('op', all)
if func(p(ver) is True for p in predicates):
return True
# construct an error message if the requirement not satisfied
err_msg = 'Operation or argument is not supported with version %s; ' % ver
err_detail = [p(ver) for p in predicates if p(ver) is not True]
logic = ', and ' if func is all else ', or '
return err_msg + logic.join(err_detail)
def compare(ver, *predicates, **kwargs):
"""Validate version satisfies provided predicates.
kwargs['exc'] - boolean whether exception should be raised
kwargs['op'] - (all, any) how predicates should be checked
Examples:
compare('1.1', version.gt('1.2'), exc=False) - False
compare('1.1', version.eq('1.0'), version.eq('1.1'), op=any) - True
"""
exc = kwargs.get('exc', True)
result = _compare(ver, *predicates, **kwargs)
if result is not True:
if exc:
raise ValueError(result)
return False
return True
def check(*predicates, **check_kwargs):
"""Decorator for command object method.
See `compare`
"""
def wrapped(func):
def inner(self, *args, **kwargs):
compare(get_version(self), *predicates, **check_kwargs)
return func(self, *args, **kwargs)
return inner
return wrapped
def get_version(obj):
"""Extract version from a command object."""
try:
if obj.app.client_manager.session is None:
return MAX_VERSION_NO_GAP
version = obj.app.client_manager.placement.api_version
except AttributeError:
# resource does not have api_version attr when docs are generated
# so let's use the minimal one
version = SUPPORTED_VERSIONS[0]
return version
class CheckerMixin(object):
def check_version(self, *predicates, **kwargs):
return compare(get_version(self), *predicates, **kwargs)
def compare_version(self, *predicates, **kwargs):
return compare(get_version(self), *predicates, exc=False, **kwargs)