From 417f4085a52f15bed11b21e04f490a9ab5a98136 Mon Sep 17 00:00:00 2001 From: lxiaopei Date: Wed, 15 Feb 2023 17:05:19 +0800 Subject: [PATCH] Add version check to use POST API when restore vif Since the new POST API to restore vif only exists on NSX version >=4.1.0, add version check before invoking the new POST API. Otherwise, still use the old patch API. Change-Id: Ic0047cba6ccaf275830b3c24a73f59ca28883de6 --- .../tests/unit/v3/policy/test_resources.py | 46 +++++++++++++++++-- vmware_nsxlib/v3/policy/core_resources.py | 10 ++-- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py index ad09381b..eaa6486d 100644 --- a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py @@ -5451,6 +5451,46 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): with mock.patch.object( self.policy_api.client, "url_post") as api_post, \ + mock.patch.object(self.resourceApi, 'version', + nsx_constants.NSX_VERSION_4_1_0): + result = self.resourceApi.create_or_overwrite( + name, segment_id, port_id=port_id, description=description, + address_bindings=address_bindings, + attachment_type=attachment_type, vif_id=vif_id, app_id=app_id, + context_id=context_id, traffic_tag=traffic_tag, + allocate_addresses=allocate_addresses, + hyperbus_mode=hyperbus_mode, admin_state=admin_state, + tags=tags, + tenant=TEST_TENANT, + init_state=init_state) + + expected_def = core_defs.SegmentPortDef( + nsx_version=nsx_constants.NSX_VERSION_4_1_0, + segment_id=segment_id, + port_id=port_id, + name=name, + description=description, + address_bindings=address_bindings, + attachment_type=attachment_type, + vif_id=vif_id, + app_id=app_id, + context_id=context_id, + traffic_tag=traffic_tag, + allocate_addresses=allocate_addresses, + admin_state=admin_state, + tags=tags, + tenant=TEST_TENANT, + hyperbus_mode=hyperbus_mode, + init_state=init_state) + + api_post.assert_called_once_with( + expected_def.get_resource_path(), + expected_def.get_obj_dict(), headers=None, + expected_results=None, retry_confirm=False) + self.assertIsNotNone(result) + + with mock.patch.object( + self.policy_api, "create_or_update") as api_call, \ mock.patch.object(self.resourceApi, 'version', nsxlib_testcase.LATEST_VERSION): result = self.resourceApi.create_or_overwrite( @@ -5482,11 +5522,7 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): tenant=TEST_TENANT, hyperbus_mode=hyperbus_mode, init_state=init_state) - - api_post.assert_called_once_with( - expected_def.get_resource_path(), - expected_def.get_obj_dict(), headers=None, - expected_results=None, retry_confirm=False) + self.assert_called_with_def(api_call, expected_def) self.assertIsNotNone(result) def test_create_with_unsupported_attribute(self): diff --git a/vmware_nsxlib/v3/policy/core_resources.py b/vmware_nsxlib/v3/policy/core_resources.py index 964b6258..5f475669 100644 --- a/vmware_nsxlib/v3/policy/core_resources.py +++ b/vmware_nsxlib/v3/policy/core_resources.py @@ -2547,9 +2547,13 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): tags=tags, tenant=tenant) if init_state == nsx_constants.INIT_STATE_RESTORE_VIF: - path = port_def.get_resource_path() - body = port_def.get_obj_dict() - self.policy_api.client.create(path, body=body) + if (version.LooseVersion(self.version) >= + version.LooseVersion(nsx_constants.NSX_VERSION_4_1_0)): + path = port_def.get_resource_path() + body = port_def.get_obj_dict() + self.policy_api.client.create(path, body=body) + else: + self._create_or_store(port_def) else: self._create_or_store(port_def) return port_id