From 92e68ad89590363376edd543c69a34c3e4fb4829 Mon Sep 17 00:00:00 2001 From: Tong Liu Date: Thu, 3 Aug 2017 14:52:26 -0700 Subject: [PATCH] NSXv3: Delete lb binding after pool deletion LBaaS allows user to delete pool that still has members. In that way, it deletes the pool members first and then delete the pool. So, in backend we need to to delete lb service and lb binding if user delete the pool with all members. Change-Id: If967be52afbbd0e35ca502f79a99cafad7a49088 --- vmware_nsx/services/lbaas/nsx_v3/pool_mgr.py | 25 +++++++++++++++++++ .../unit/services/lbaas/test_nsxv3_driver.py | 7 ++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/vmware_nsx/services/lbaas/nsx_v3/pool_mgr.py b/vmware_nsx/services/lbaas/nsx_v3/pool_mgr.py index cf11d50ca6..7e1ab54fb6 100644 --- a/vmware_nsx/services/lbaas/nsx_v3/pool_mgr.py +++ b/vmware_nsx/services/lbaas/nsx_v3/pool_mgr.py @@ -89,6 +89,7 @@ class EdgePoolManager(base_mgr.Nsxv3LoadbalancerBaseManager): lb_id = pool.loadbalancer_id pool_client = self.core_plugin.nsxlib.load_balancer.pool vs_client = self.core_plugin.nsxlib.load_balancer.virtual_server + service_client = self.core_plugin.nsxlib.load_balancer.service binding = nsx_db.get_nsx_lbaas_pool_binding( context.session, lb_id, pool.id) @@ -112,6 +113,30 @@ class EdgePoolManager(base_mgr.Nsxv3LoadbalancerBaseManager): raise n_exc.BadRequest(resource='lbaas-pool', msg=msg) nsx_db.delete_nsx_lbaas_pool_binding(context.session, lb_id, pool.id) + lb_binding = nsx_db.get_nsx_lbaas_loadbalancer_binding( + context.session, lb_id) + if lb_binding: + lb_service_id = lb_binding['lb_service_id'] + try: + lb_service = service_client.get(lb_service_id) + vs_list = lb_service.get('virtual_server_ids') + if vs_list and vs_id in vs_list: + vs_list.remove(vs_id) + else: + LOG.debug('virtual server id %s is not in the lb ' + 'service virtual server list %s', + vs_id, vs_list) + service_client.update(lb_service_id, + virtual_server_ids=vs_list) + if not vs_list: + service_client.delete(lb_service_id) + nsx_db.delete_nsx_lbaas_loadbalancer_binding( + context.session, lb_id) + except nsxlib_exc.ManagerError: + self.lbv2_driver.pool.failed_completion(context, pool) + msg = (_('Failed to delete lb pool from nsx: %(pool)s') % + {'pool': lb_pool_id}) + raise n_exc.BadRequest(resource='lbaas-pool', msg=msg) self.lbv2_driver.pool.successful_completion( context, pool, delete=True) diff --git a/vmware_nsx/tests/unit/services/lbaas/test_nsxv3_driver.py b/vmware_nsx/tests/unit/services/lbaas/test_nsxv3_driver.py index 0c987ddfb0..dea6e14587 100644 --- a/vmware_nsx/tests/unit/services/lbaas/test_nsxv3_driver.py +++ b/vmware_nsx/tests/unit/services/lbaas/test_nsxv3_driver.py @@ -349,8 +349,11 @@ class TestEdgeLbaasV2Pool(BaseTestEdgeLbaasV2): mock.patch.object(self.pool_client, 'delete' ) as mock_delete_pool, \ mock.patch.object(nsx_db, 'delete_nsx_lbaas_pool_binding' - ) as mock_delete_pool_binding: + ) as mock_delete_pool_binding, \ + mock.patch.object(nsx_db, 'get_nsx_lbaas_loadbalancer_binding' + ) as mock_get_lb_binding: mock_get_pool_binding.return_value = POOL_BINDING + mock_get_lb_binding.return_value = None self.edge_driver.pool.delete(self.context, self.pool) @@ -632,7 +635,7 @@ class TestEdgeLbaasV2L7Rule(BaseTestEdgeLbaasV2): mock_successful_completion.assert_called_with( self.context, new_l7rule) - def test_delete(self): + def test_delete_pool_without_members(self): with mock.patch.object(nsx_db, 'get_nsx_lbaas_l7rule_binding', ) as mock_get_l7rule_binding, \ mock.patch.object(self.rule_client, 'delete',