Clean up tier0 link port if failed to create peer tier1 link port

When creating a pair of link ports for a t1 router, if failed to
create the tier1 link port, we need to cleanup the already-created
peer tier0 link port to avoid leaving any orphaned link port.

Change-Id: Ice7c25d362b54024634f311c2df1465089bad1b4
This commit is contained in:
Shih-Hao Li 2020-06-25 19:49:31 -07:00
parent 028feaa65b
commit 5b29902ae9
2 changed files with 32 additions and 6 deletions

View File

@ -83,6 +83,24 @@ class TestRouter(nsxlib_testcase.NsxClientTestCase):
tier1_uuid, tier0_uuid, tags)) tier1_uuid, tier0_uuid, tags))
self.assertEqual(port_create.call_count, 2) self.assertEqual(port_create.call_count, 2)
def test_add_router_link_port_fail(self):
tags = [{'scope': 'a', 'tag': 'b'}]
tier0_uuid = uuidutils.generate_uuid()
tier1_uuid = uuidutils.generate_uuid()
tier0_link_port_id = uuidutils.generate_uuid()
with mock.patch.object(self.nsxlib.router._router_port_client,
'create') as port_create,\
mock.patch.object(self.nsxlib.router._router_port_client,
'delete') as port_delete:
tier0_link_port = {'id': tier0_link_port_id}
port_create.side_effect = [tier0_link_port,
nsxlib_exc.ManagerError]
self.assertRaises(
nsxlib_exc.ManagerError,
self.nsxlib.router.add_router_link_port,
tier1_uuid, tier0_uuid, tags)
port_delete.assert_called_once_with(tier0_link_port_id)
def test_remove_router_link_port(self): def test_remove_router_link_port(self):
tier1_uuid = uuidutils.generate_uuid() tier1_uuid = uuidutils.generate_uuid()
with mock.patch.object( with mock.patch.object(

View File

@ -94,12 +94,20 @@ class RouterLib(object):
# Create Tier1 logical router link port # Create Tier1 logical router link port
t1_tags = copy.copy(tags) t1_tags = copy.copy(tags)
t1_tags = utils.add_v3_tag(t1_tags, 'os-tier1-uuid', tier1_uuid) t1_tags = utils.add_v3_tag(t1_tags, 'os-tier1-uuid', tier1_uuid)
tier1_link_port = self._router_port_client.create( try:
tier1_uuid, display_name=TIER1_ROUTER_LINK_PORT_NAME, tags=t1_tags, tier1_link_port = self._router_port_client.create(
resource_type=nsx_constants.LROUTERPORT_LINKONTIER1, tier1_uuid, display_name=TIER1_ROUTER_LINK_PORT_NAME,
logical_port_id=linked_logical_port_id, tags=t1_tags,
address_groups=None) resource_type=nsx_constants.LROUTERPORT_LINKONTIER1,
return tier0_link_port, tier1_link_port logical_port_id=linked_logical_port_id,
address_groups=None)
return tier0_link_port, tier1_link_port
except Exception as e:
LOG.warning("Failed to create tier1 link port for tier1 router "
"%s, deleting peer tier0 link port %s: %s",
tier1_uuid, linked_logical_port_id, e)
self._router_port_client.delete(linked_logical_port_id)
raise e
def remove_router_link_port(self, tier1_uuid, tier0_uuid=None): def remove_router_link_port(self, tier1_uuid, tier0_uuid=None):
# Note(asarfaty): tier0_uuid is not used by this method and can # Note(asarfaty): tier0_uuid is not used by this method and can