LB: Add methods to add/remove monitor from pool

Add two methods in load_balancer module to add/remove LB monitor
from monitor list of a LB pool. With these function, we can easily
add/remove monitor with just monitor_id without operating on the
monitor list explicitly.

Change-Id: I146c1af8558eb59e31c684b5d9720e13ce1694f7
This commit is contained in:
Tong Liu 2017-06-16 09:30:58 -07:00
parent 346135adea
commit a89c605a9d
2 changed files with 76 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import mock
from vmware_nsxlib.tests.unit.v3 import nsxlib_testcase
from vmware_nsxlib.tests.unit.v3 import test_constants as consts
from vmware_nsxlib.v3 import exceptions as nsxlib_exc
from vmware_nsxlib.v3 import load_balancer
@ -277,6 +278,48 @@ class TestPool(nsxlib_testcase.NsxClientTestCase):
delete.assert_called_with(
'loadbalancer/pools/%s' % fake_profile['id'])
def test_remove_monitor_from_pool(self):
fake_pool = consts.FAKE_POOL.copy()
fake_pool['active_monitor_ids'] = [consts.FAKE_MONITOR_UUID]
body = {'display_name': fake_pool['display_name'],
'description': fake_pool['description'],
'id': fake_pool['id'],
'algorithm': fake_pool['algorithm'],
'active_monitor_ids': []}
with mock.patch.object(self.nsxlib.client, 'get',
return_value=fake_pool):
with mock.patch.object(self.nsxlib.client, 'update') as update:
self.nsxlib.load_balancer.pool.remove_monitor_from_pool(
fake_pool['id'], consts.FAKE_MONITOR_UUID)
resource = 'loadbalancer/pools/%s' % fake_pool['id']
update.assert_called_with(resource, body)
def test_remove_non_exist_monitor_from_pool(self):
fake_pool = consts.FAKE_POOL.copy()
fake_pool['active_monitor_ids'] = [consts.FAKE_MONITOR_UUID]
with mock.patch.object(self.nsxlib.client, 'get',
return_value=fake_pool):
self.assertRaises(
nsxlib_exc.ResourceNotFound,
self.nsxlib.load_balancer.pool.remove_monitor_from_pool,
fake_pool['id'],
'xxx-yyy')
def test_add_monitor_to_pool(self):
fake_pool = consts.FAKE_POOL.copy()
body = {'display_name': fake_pool['display_name'],
'description': fake_pool['description'],
'id': fake_pool['id'],
'algorithm': fake_pool['algorithm'],
'active_monitor_ids': [consts.FAKE_MONITOR_UUID]}
with mock.patch.object(self.nsxlib.client, 'get',
return_value=fake_pool):
with mock.patch.object(self.nsxlib.client, 'update') as update:
self.nsxlib.load_balancer.pool.add_monitor_to_pool(
fake_pool['id'], consts.FAKE_MONITOR_UUID)
resource = 'loadbalancer/pools/%s' % fake_pool['id']
update.assert_called_with(resource, body)
class TestVirtualServer(nsxlib_testcase.NsxClientTestCase):

View File

@ -13,9 +13,13 @@
# License for the specific language governing permissions and limitations
# under the License.
from oslo_log import log as logging
from vmware_nsxlib.v3 import exceptions as nsxlib_exc
from vmware_nsxlib.v3 import utils
LOG = logging.getLogger(__name__)
class ApplicationProfileTypes(object):
"""LoadBalancer Application Profile types"""
@ -214,6 +218,35 @@ class Pool(LoadBalancerBase):
body['members'] = members
return self.client.update(object_url, body)
def add_monitor_to_pool(self, pool_id, monitor_id):
object_url = self.resource + '/' + pool_id
body = self.client.get(object_url)
if 'active_monitor_ids' in body:
monitor_list = body['active_monitor_ids']
if monitor_id not in monitor_list:
monitor_list.append(monitor_id)
else:
LOG.error('Monitor %s is already in pool', monitor_id)
return body
else:
monitor_list = [monitor_id]
body['active_monitor_ids'] = monitor_list
return self.client.update(object_url, body)
def remove_monitor_from_pool(self, pool_id, monitor_id):
object_url = self.resource + '/' + pool_id
body = self.client.get(object_url)
monitor_list = body.get('active_monitor_ids')
if monitor_list and monitor_id in monitor_list:
monitor_list.remove(monitor_id)
body['active_monitor_ids'] = monitor_list
return self.client.update(object_url, body)
else:
ops = ('removing monitor %s from pool active_monitor_ids %s'
'as it is not in the list', monitor_id, monitor_list)
raise nsxlib_exc.ResourceNotFound(
manager=self.client.nsx_api_managers, operation=ops)
class VirtualServer(LoadBalancerBase):
resource = 'loadbalancer/virtual-servers'