Fixes get_realization_info, added API to get router port

Currently in get_realizaiton_info in Tier1 API, the entity_type
is ignored. This patch fixes this issue to use entity_type to
filter for realized entity returned by this API

Also to easily get router port, an API is added for Tier1 API
to return a list of RouterPort realized associated with the tier1

Issue: #
Jira: #
Signed-off-by: Rongrong_Miao <rmiao@vmware.com>
Change-Id: Ife3f3652255db4ffc72872e4aef84418bf1a3211
(cherry picked from commit a953b1df2f)
This commit is contained in:
Rongrong_Miao 2021-04-04 22:49:14 -07:00 committed by Salvatore Orlando
parent 3c8fee6a47
commit dd76e05bf3
2 changed files with 49 additions and 4 deletions

View File

@ -3145,6 +3145,34 @@ class TestPolicyTier1(NsxPolicyLibTestCase):
tier1_id, segment_id) tier1_id, segment_id)
self.assertEqual(lrp_id, actual_id) self.assertEqual(lrp_id, actual_id)
def test_get_realization_info(self):
tier1_id = 'id-rtr'
with mock.patch.object(self.resourceApi, "_get_realization_info") as\
mock_call:
self.resourceApi.get_realization_info(
tier1_id, entity_type='RealizedLogicalRouterPort', tenant=None)
expected_def = core_defs.Tier1Def(tier1_id=tier1_id, tenant=None)
self.assert_called_with_def_and_kwargs(
mock_call,
expected_def,
entity_type='RealizedLogicalRouterPort',
silent=False)
def test_get_realized_router_port(self):
tier1_id = 'id-rtr'
with mock.patch.object(self.resourceApi, "_get_realization_info") as \
mock_call:
self.resourceApi.get_realized_router_port(
tier1_id, tenant=None)
expected_def = core_defs.Tier1Def(tier1_id=tier1_id, tenant=None)
self.assert_called_with_def_and_kwargs(
mock_call,
expected_def,
entity_type='RealizedLogicalRouterPort',
all_result=True,
silent=False
)
def test_set_dhcp_relay(self): def test_set_dhcp_relay(self):
tier1_id = '111' tier1_id = '111'
segment_id = '222' segment_id = '222'

View File

@ -191,8 +191,9 @@ class NsxPolicyResourceBase(object, metaclass=abc.ABCMeta):
return obj return obj
def _get_realization_info(self, resource_def, entity_type=None, def _get_realization_info(self, resource_def, entity_type=None,
silent=False): silent=False, all_results=False):
entities = [] entities = []
results = []
try: try:
path = resource_def.get_resource_full_path() path = resource_def.get_resource_full_path()
entities = self.policy_api.get_realized_entities( entities = self.policy_api.get_realized_entities(
@ -202,11 +203,18 @@ class NsxPolicyResourceBase(object, metaclass=abc.ABCMeta):
# look for the entry with the right entity_type # look for the entry with the right entity_type
for entity in entities: for entity in entities:
if entity.get('entity_type') == entity_type: if entity.get('entity_type') == entity_type:
return entity if all_results:
results.append(entity)
else:
return entity
return results
else: else:
# return the first realization entry # return the first realization entry
# (Useful for resources with single realization entity) # (Useful for resources with single realization entity)
return entities[0] if not all_results:
return entities[0]
else:
return entities
except exceptions.ResourceNotFound: except exceptions.ResourceNotFound:
pass pass
@ -1397,7 +1405,16 @@ class NsxPolicyTier1Api(NsxPolicyResourceBase):
silent=False, silent=False,
tenant=constants.POLICY_INFRA_TENANT): tenant=constants.POLICY_INFRA_TENANT):
tier1_def = self.entry_def(tier1_id=tier1_id, tenant=tenant) tier1_def = self.entry_def(tier1_id=tier1_id, tenant=tenant)
return self._get_realization_info(tier1_def, silent=silent) return self._get_realization_info(tier1_def, silent=silent,
entity_type=entity_type)
def get_realized_router_port(self, tier1_id, silent=False,
tenant=constants.POLICY_INFRA_TENANT):
tier1_def = self.entry_def(tier1_id=tier1_id, tenant=tenant)
ports = self._get_realization_info(
tier1_def, entity_type='RealizedLogicalRouterPort',
all_result=True, silent=silent)
return ports
def wait_until_realized(self, tier1_id, entity_type=None, def wait_until_realized(self, tier1_id, entity_type=None,
tenant=constants.POLICY_INFRA_TENANT, tenant=constants.POLICY_INFRA_TENANT,