diff --git a/vmware_nsxlib/tests/unit/v3/test_constants.py b/vmware_nsxlib/tests/unit/v3/test_constants.py index 049a112c..b840c7dd 100644 --- a/vmware_nsxlib/tests/unit/v3/test_constants.py +++ b/vmware_nsxlib/tests/unit/v3/test_constants.py @@ -363,12 +363,32 @@ FAKE_TZ = { } FAKE_TN_UUID = uuidutils.generate_uuid() +FAKE_TZ_EP_UUID = uuidutils.generate_uuid() +FAKE_TZ_EP_UUID2 = uuidutils.generate_uuid() FAKE_TN = { "resource_type": "TransportNode", "revision": 0, "id": FAKE_TZ_UUID, "display_name": FAKE_NAME, - "transport_zone_endpoints": [{"transport_zone_id": FAKE_TZ_UUID}] + "transport_zone_endpoints": [{"transport_zone_id": FAKE_TZ_UUID}], + "host_switch_spec": { + "host_switches": [ + { + 'transport_zone_endpoints': [ + { + 'transport_zone_id': FAKE_TZ_EP_UUID + } + ] + }, + { + 'transport_zone_endpoints': [ + { + 'transport_zone_id': FAKE_TZ_EP_UUID2 + } + ] + } + ] + } } FAKE_MD_UUID = uuidutils.generate_uuid() diff --git a/vmware_nsxlib/tests/unit/v3/test_resources.py b/vmware_nsxlib/tests/unit/v3/test_resources.py index 2bef5953..e372a4bf 100644 --- a/vmware_nsxlib/tests/unit/v3/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/test_resources.py @@ -1877,9 +1877,13 @@ class TransportNode(BaseTestResource): def test_get_transport_zones(self): fake_tn = test_constants.FAKE_TN.copy() tn = self.get_mocked_resource() + self.nsxlib.feature_supported = mock.MagicMock() with mock.patch.object(tn.client, 'url_get', return_value=fake_tn): + self.nsxlib.feature_supported.side_effect = [False, True] tzs = tn.get_transport_zones(fake_tn['id']) self.assertEqual([test_constants.FAKE_TZ_UUID], tzs) + tzs = tn.get_transport_zones(fake_tn['id']) + self.assertEqual([test_constants.FAKE_TZ_EP_UUID], tzs) class MetadataProxy(BaseTestResource): diff --git a/vmware_nsxlib/tests/unit/v3/test_router.py b/vmware_nsxlib/tests/unit/v3/test_router.py index 238e6784..0e455e7b 100644 --- a/vmware_nsxlib/tests/unit/v3/test_router.py +++ b/vmware_nsxlib/tests/unit/v3/test_router.py @@ -139,6 +139,8 @@ class TestRouter(nsxlib_testcase.NsxClientTestCase): def test_get_tier0_router_tz(self): tier0_uuid = uuidutils.generate_uuid() + self.nsxlib.feature_supported = mock.MagicMock() + self.nsxlib.feature_supported.return_value = False with mock.patch.object(self.nsxlib.router._router_client, 'get', return_value=test_constants.FAKE_TIERO_ROUTER),\ mock.patch.object(self.nsxlib.edge_cluster, 'get', @@ -150,6 +152,8 @@ class TestRouter(nsxlib_testcase.NsxClientTestCase): def test_get_tier0_router_overlay_tz(self): tier0_uuid = uuidutils.generate_uuid() + self.nsxlib.feature_supported = mock.MagicMock() + self.nsxlib.feature_supported.return_value = False with mock.patch.object(self.nsxlib.router._router_client, 'get', return_value=test_constants.FAKE_TIERO_ROUTER),\ mock.patch.object(self.nsxlib.edge_cluster, 'get', diff --git a/vmware_nsxlib/v3/__init__.py b/vmware_nsxlib/v3/__init__.py index be41e0b4..811a3481 100644 --- a/vmware_nsxlib/v3/__init__.py +++ b/vmware_nsxlib/v3/__init__.py @@ -167,6 +167,12 @@ class NsxLib(lib.NsxLibBase): return node.get('export_type') is 'RESTRICTED' def feature_supported(self, feature): + if (version.LooseVersion(self.get_version()) >= + version.LooseVersion(nsx_constants.NSX_VERSION_3_0_0)): + # features available since 3.0.0 + if (feature == nsx_constants.FEATURE_GET_TZ_FROM_SWITCH): + return True + if (version.LooseVersion(self.get_version()) >= version.LooseVersion(nsx_constants.NSX_VERSION_2_5_1)): # features available since 2.5.1 diff --git a/vmware_nsxlib/v3/core_resources.py b/vmware_nsxlib/v3/core_resources.py index 3db8d164..591a5270 100644 --- a/vmware_nsxlib/v3/core_resources.py +++ b/vmware_nsxlib/v3/core_resources.py @@ -907,9 +907,21 @@ class NsxLibTransportNode(utils.NsxLibApiBase): return True def get_transport_zones(self, uuid): - tz = self.get(uuid) - return [ep.get('transport_zone_id') for ep in - tz.get('transport_zone_endpoints', [])] + tn = self.get(uuid) + + if (self.nsxlib and self.nsxlib.feature_supported( + nsx_constants.FEATURE_GET_TZ_FROM_SWITCH)): + if (not tn.get('host_switch_spec') or not + tn['host_switch_spec'].get('host_switches')): + return [] + + host_switches = tn.get('host_switch_spec').get('host_switches', []) + + return [ep.get('transport_zone_id') for ep in + host_switches[0].get('transport_zone_endpoints', [])] + else: + return [ep.get('transport_zone_id') for ep in + tn.get('transport_zone_endpoints', [])] class NsxLibDhcpProfile(utils.NsxLibApiBase): diff --git a/vmware_nsxlib/v3/nsx_constants.py b/vmware_nsxlib/v3/nsx_constants.py index 29b97465..b91b695c 100644 --- a/vmware_nsxlib/v3/nsx_constants.py +++ b/vmware_nsxlib/v3/nsx_constants.py @@ -172,6 +172,7 @@ FEATURE_ENABLE_STANDBY_RELOCATION = 'Router Enable standby relocation' FEATURE_PARTIAL_UPDATES = 'Partial Update with PATCH' FEATURE_RELAX_SCALE_VALIDATION = 'Relax Scale Validation for LbService' FEATURE_SWITCH_HYPERBUS_MODE = 'Switch hyperbus mode with policy API' +FEATURE_GET_TZ_FROM_SWITCH = 'Get TZ endpoints from host switch' # Features available depending on the Policy Manager backend version FEATURE_NSX_POLICY = 'NSX Policy'