Debian Codegraph @

infoblox_client/feature.py

raw symbols SCIP
1 # Copyright 2015 Infoblox Inc.
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 six
17
18 from infoblox_client import exceptions as ib_ex
19
20 FEATURE_VERSIONS = {
21 'create_ea_def': '2.2',
22 'cloud_api': '2.0',
23 'member_ipv6_setting': '2.2',
24 'member_licenses': '2.0',
25 'enable_member_dns': '2.2.1',
26 'enable_member_dhcp': '2.2.1'}
27
28
29 class Feature(object):
30 """Class representing available NIOS features
31
32 Based on the following:
33 - Infoblox WAPI Version
34 - Known features and corresponding WAPI version requirement
35
36 the Feature class represents available NIOS features as attributes.
37 """
38
39 def __init__(self, version, feature_versions=None):
40 self._wapi_version = None
41
42 if feature_versions is None:
43 feature_versions = FEATURE_VERSIONS
44
45 if isinstance(version, six.string_types):
46 wapi_version = version
47 elif hasattr(version, 'wapi_version'):
48 wapi_version = getattr(version, 'wapi_version')
49 else:
50 msg = "WAPI version cannot be determined from '%s'" % version
51 raise ib_ex.InfobloxConfigException(msg=msg)
52
53 wapi_util = WapiVersionUtil(wapi_version)
54 for f, v in feature_versions.items():
55 setattr(self,
56 f,
57 wapi_util.is_version_supported(v))
58
59
60 class WapiVersionUtil(object):
61 """Provide utility functions for manipulating WAPI version
62
63 Provide methods that manipulate and get information from
64 WAPI version string.
65 """
66
67 def __init__(self, version):
68 self._version_parts = self._get_wapi_version_parts(version)
69
70 @property
71 def version_parts(self):
72 return self._version_parts
73
74 @property
75 def major_version(self):
76 return self.version_parts[0]
77
78 @property
79 def minor_version(self):
80 return self.version_parts[1]
81
82 @property
83 def patch_version(self):
84 return self.version_parts[2]
85
86 def is_version_supported(self, req_ver):
87 req_parts = WapiVersionUtil(req_ver).version_parts
88
89 for a, b in zip(self.version_parts, req_parts):
90 if a is None:
91 return True if b is None else False
92 elif b is None:
93 return True
94 elif not a == b:
95 return a > b
96 return True
97
98 @staticmethod
99 def _get_wapi_version_parts(version):
100 parts = version.split('.')
101 if not parts or len(parts) > 3 or len(parts) < 2:
102 raise ValueError("Invalid argument was passed")
103 for p in parts:
104 if not len(p) or not p.isdigit():
105 raise ValueError("Invalid argument was passed")
106 parts = [int(x) for x in parts]
107 if len(parts) == 2:
108 parts.append(0)
109 return parts