From bf31413c737182b1cf73c8f1cde58004c3faa9a2 Mon Sep 17 00:00:00 2001 From: Anna Khmelnitsky Date: Fri, 2 Jun 2017 12:26:33 -0700 Subject: [PATCH] Avoid version API call for policy lib Policy API does not expose version yet In addition, this solves client cert generation issue (no backend connection is yet available when certificate generation flow is invoked, so get_version will fail on init) Change-Id: Ibb1ea9b64bb92f3cae488db41da6bec849ce042e --- .../tests/unit/v3/nsxlib_testcase.py | 4 --- .../tests/unit/v3/test_policy_resources.py | 2 -- vmware_nsxlib/v3/__init__.py | 27 +++++++++---------- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py b/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py index d79aaed1..fb89b806 100644 --- a/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py +++ b/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py @@ -92,10 +92,6 @@ def _mock_nsxlib(): "NsxLibTransportZone.get_id_by_name_or_id"), side_effect=_return_id_key).start() - mock.patch( - "vmware_nsxlib.v3.NsxLib.get_version", - return_value='1.1.0').start() - def get_default_nsxlib_config(): return config.NsxLibConfig( diff --git a/vmware_nsxlib/tests/unit/v3/test_policy_resources.py b/vmware_nsxlib/tests/unit/v3/test_policy_resources.py index d0148ba8..641b079c 100644 --- a/vmware_nsxlib/tests/unit/v3/test_policy_resources.py +++ b/vmware_nsxlib/tests/unit/v3/test_policy_resources.py @@ -29,8 +29,6 @@ class NsxPolicyLibTestCase(unittest.TestCase): def setUp(self, *args, **kwargs): super(NsxPolicyLibTestCase, self).setUp() - mock.patch.object(v3.NsxPolicyLib, "get_version", - return_value='2.0.0').start() nsxlib_config = nsxlib_testcase.get_default_nsxlib_config() self.policy_lib = v3.NsxPolicyLib(nsxlib_config) self.policy_api = self.policy_lib.policy_api diff --git a/vmware_nsxlib/v3/__init__.py b/vmware_nsxlib/v3/__init__.py index 2b0b4865..5eed387c 100644 --- a/vmware_nsxlib/v3/__init__.py +++ b/vmware_nsxlib/v3/__init__.py @@ -59,7 +59,7 @@ class NsxLibBase(object): super(NsxLibBase, self).__init__() - self.nsx_version = self.get_version() + self.nsx_version = None def set_config(self, nsxlib_config): """Set config user provided and extend it according to application""" @@ -79,11 +79,6 @@ class NsxLibBase(object): def init_api(self): pass - def get_version(self): - node = self.client.get("node") - version = node.get('node_version') - return version - @abc.abstractmethod def feature_supported(self, feature): pass @@ -190,8 +185,16 @@ class NsxLib(NsxLibBase): def keepalive_section(self): return 'transport-zones' + def get_version(self): + if self.nsx_version: + return self.nsx_version + + node = self.client.get("node") + self.nsx_version = node.get('node_version') + return self.nsx_version + def feature_supported(self, feature): - if (version.LooseVersion(self.nsx_version) >= + if (version.LooseVersion(self.get_version()) >= version.LooseVersion(nsx_constants.NSX_VERSION_2_0_0)): # Features available since 2.0 if (feature == nsx_constants.FEATURE_EXCLUDE_PORT_BY_TAG or @@ -199,7 +202,7 @@ class NsxLib(NsxLibBase): feature == nsx_constants.FEATURE_LOAD_BALANCER): return True - if (version.LooseVersion(self.nsx_version) >= + if (version.LooseVersion(self.get_version()) >= version.LooseVersion(nsx_constants.NSX_VERSION_1_1_0)): # Features available since 1.1 if (feature == nsx_constants.FEATURE_MAC_LEARNING or @@ -234,13 +237,7 @@ class NsxPolicyLib(NsxLibBase): return 'infra' def feature_supported(self, feature): - if (version.LooseVersion(self.nsx_version) >= - version.LooseVersion(nsx_constants.NSX_VERSION_2_0_0)): - # NSX policy is available since 2.0 - if feature == nsx_constants.FEATURE_NSX_POLICY: - return True - - return False + return (feature == nsx_constants.FEATURE_NSX_POLICY) @property def client_url_prefix(self):