From 5ee7524cc5769154e842e321fab7023fe212a7e8 Mon Sep 17 00:00:00 2001 From: asarfaty Date: Tue, 13 Oct 2020 08:55:17 +0200 Subject: [PATCH] Check realization status when getting id by search Change-Id: I912b51933f2ad1e9fa2b1750c42301b1a1a70c25 --- .../tests/unit/v3/nsxlib_testcase.py | 4 +- .../tests/unit/v3/policy/test_resources.py | 48 +++++++++++++++++++ vmware_nsxlib/v3/policy/core_resources.py | 5 +- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py b/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py index bea9cd03..6dd7a748 100644 --- a/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py +++ b/vmware_nsxlib/tests/unit/v3/nsxlib_testcase.py @@ -114,7 +114,9 @@ def get_default_nsxlib_config(allow_passthrough=True): plugin_ver=PLUGIN_VER, dns_nameservers=DNS_NAMESERVERS, dns_domain=DNS_DOMAIN, - allow_passthrough=allow_passthrough + allow_passthrough=allow_passthrough, + realization_max_attempts=3, + realization_wait_sec=0.2 ) diff --git a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py index 884301fd..32d75af3 100644 --- a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py @@ -3089,6 +3089,30 @@ class TestPolicyTier1(NsxPolicyLibTestCase): tier1_id, tenant=TEST_TENANT, max_attempts=5, sleep=0.1) + def test_get_realized_id(self): + # Get realized ID using search + tier1_id = '111' + logical_router_id = 'realized_111' + info = {'results': [{'status': {'state': 'success'}, + 'id': logical_router_id}]} + with mock.patch.object(self.resourceApi.nsx_api, "search_by_tags", + return_value=info): + realized_id = self.resourceApi.get_realized_id(tier1_id) + self.assertEqual(logical_router_id, realized_id) + + def test_get_realized_id_failed(self): + # Get realized ID using search + tier1_id = '111' + logical_router_id = 'realized_111' + info = {'results': [{'status': {'state': 'in_progress'}, + 'id': logical_router_id}]} + with mock.patch.object(self.resourceApi.nsx_api, "search_by_tags", + return_value=info),\ + mock.patch.object(self.resourceApi.policy_api, + "get_realized_entities"): + self.assertRaises(nsxlib_exc.RealizationTimeoutError, + self.resourceApi.get_realized_id, tier1_id) + def test_get_realized_downlink_port(self): tier1_id = '111' segment_id = '222' @@ -3510,6 +3534,30 @@ class TestPolicyTier1NoPassthrough(TestPolicyTier1): self.resourceApi.set_dhcp_relay(tier1_id, segment_id, relay_id) realization.assert_not_called() + def test_get_realized_id(self): + # Get realized ID using policy api + tier1_id = '111' + logical_router_id = 'realized_111' + result = [{'state': constants.STATE_REALIZED, + 'entity_type': 'RealizedLogicalRouter', + 'realization_specific_identifier': logical_router_id}] + with mock.patch.object(self.resourceApi.policy_api, + "get_realized_entities", + return_value=result): + realized_id = self.resourceApi.get_realized_id(tier1_id) + self.assertEqual(logical_router_id, realized_id) + + def test_get_realized_id_failed(self): + # Get realized ID using policy api + tier1_id = '111' + result = [{'state': constants.STATE_UNREALIZED, + 'entity_type': 'RealizedLogicalRouter'}] + with mock.patch.object(self.resourceApi.policy_api, + "get_realized_entities", + return_value=result): + realized_id = self.resourceApi.get_realized_id(tier1_id) + self.assertEqual(None, realized_id) + class TestPolicyTier0NatRule(NsxPolicyLibTestCase): diff --git a/vmware_nsxlib/v3/policy/core_resources.py b/vmware_nsxlib/v3/policy/core_resources.py index 7743c469..d4143d14 100644 --- a/vmware_nsxlib/v3/policy/core_resources.py +++ b/vmware_nsxlib/v3/policy/core_resources.py @@ -316,7 +316,10 @@ class NsxPolicyResourceBase(object, metaclass=abc.ABCMeta): resources = self.nsx_api.search_by_tags( tags=tag, resource_type=mp_resource_type)['results'] if resources: - return resources[0]['id'] + # If status exists, make sure the state is successful + if (not resources[0].get('status') or + resources[0]['status'].get('state') == 'success'): + return resources[0]['id'] # From time to time also check the Policy realization state, # as if it is in ERROR waiting should be avoided.