New api for getting router port transit cidr

Change-Id: I82bb71186ccba599ff81c24d624b51167c58c521
This commit is contained in:
Adit Sarfaty 2018-07-19 16:21:39 +03:00
parent 18b03b993f
commit d21bee4ed5
2 changed files with 41 additions and 0 deletions

View File

@ -136,3 +136,23 @@ class TestRouter(nsxlib_testcase.NsxClientTestCase):
return_value="OVERLAY"):
tz = self.nsxlib.router.get_tier0_router_overlay_tz(tier0_uuid)
self.assertEqual(tz, test_constants.FAKE_TZ_UUID)
def test_get_connected_t0_transit_net(self):
t1_uuid = uuidutils.generate_uuid()
link_port_uuid = uuidutils.generate_uuid()
link_port = {
'linked_logical_router_port_id': {
'target_id': link_port_uuid}}
transit_net = '1.1.1.0'
transit_prefix = '31'
t0_port = {'subnets': [{'ip_addresses': [transit_net],
'prefix_length': transit_prefix}]}
with mock.patch.object(self.nsxlib.router._router_port_client,
'get_tier1_link_port',
return_value=link_port) as get_link,\
mock.patch.object(self.nsxlib.router._router_port_client,
'get', return_value=t0_port) as get_port:
cidr = self.nsxlib.router.get_connected_t0_transit_net(t1_uuid)
get_link.assert_called_with(t1_uuid)
get_port.assert_called_with(link_port_uuid)
self.assertEqual('%s/%s' % (transit_net, transit_prefix), cidr)

View File

@ -277,3 +277,24 @@ class RouterLib(object):
if (backend_type ==
self.nsxlib.transport_zone.TRANSPORT_TYPE_OVERLAY):
return tz_uuid
def get_connected_t0_transit_net(self, tier1_uuid):
"""Return the cidr of the tier1->tier0 link port
return None if the router is not connected to a tier0 router
"""
try:
tier1_link_port = (
self._router_port_client.get_tier1_link_port(tier1_uuid))
except exceptions.ResourceNotFound:
# No GW
return
tier0_link_port_id = tier1_link_port.get(
'linked_logical_router_port_id', {}).get('target_id')
if not tier0_link_port_id:
return
tier0_link_port = self._router_port_client.get(tier0_link_port_id)
for subnet in tier0_link_port.get('subnets', []):
for ip_address in subnet.get('ip_addresses'):
# Expecting only 1 cidr here. Return it.
return "%s/%s" % (ip_address, subnet.get('prefix_length', '0'))