Remove unnecessary code from create_pool_health_monitor method

fixes bug 1175481

Remove incorrect database object fetch and update.
Get rid of unnecessary query for pool object.
Make code that creates monitor list more pythonic

Change-Id: I6d2e9dfafe9ce9f0c089408b2e6e719a46285ba6
This commit is contained in:
Eugene Nikanorov 2013-05-02 18:49:41 +04:00
parent c22b181756
commit ffa04d3e83
2 changed files with 33 additions and 16 deletions

View File

@ -602,12 +602,6 @@ class LoadBalancerPluginDb(LoadBalancerPluginBase):
def create_pool_health_monitor(self, context, health_monitor, pool_id):
monitor_id = health_monitor['health_monitor']['id']
with context.session.begin(subtransactions=True):
monitor_qry = context.session.query(HealthMonitor)
try:
monitor = monitor_qry.filter_by(id=monitor_id).one()
monitor.update({'pool_id': pool_id})
except exc.NoResultFound:
raise loadbalancer.HealthMonitorNotFound(monitor_id=monitor_id)
try:
qry = context.session.query(Pool)
pool = qry.filter_by(id=pool_id).one()
@ -616,17 +610,8 @@ class LoadBalancerPluginDb(LoadBalancerPluginBase):
assoc = PoolMonitorAssociation(pool_id=pool_id,
monitor_id=monitor_id)
assoc.healthmonitor = monitor
pool.monitors.append(assoc)
monitors = []
try:
qry = context.session.query(Pool)
pool = qry.filter_by(id=pool_id).one()
for monitor in pool['monitors']:
monitors.append(monitor['monitor_id'])
except exc.NoResultFound:
pass
monitors = [monitor['monitor_id'] for monitor in pool['monitors']]
res = {"health_monitor": monitors}
return res

View File

@ -1097,6 +1097,38 @@ class TestLoadBalancer(LoadBalancerPluginDbTestCase):
self._delete('members', member1['member']['id'])
self._delete('members', member2['member']['id'])
def test_create_pool_health_monitor(self):
with contextlib.nested(
self.pool(name="pool"),
self.health_monitor(),
self.health_monitor()
) as (pool, health_mon1, health_mon2):
res = self.plugin.create_pool_health_monitor(
context.get_admin_context(),
health_mon1, pool['pool']['id']
)
self.assertEqual({'health_monitor':
[health_mon1['health_monitor']['id']]},
res)
res = self.plugin.create_pool_health_monitor(
context.get_admin_context(),
health_mon2, pool['pool']['id']
)
self.assertEqual({'health_monitor':
[health_mon1['health_monitor']['id'],
health_mon2['health_monitor']['id']]},
res)
def test_create_pool_healthmon_invalid_pool_id(self):
with self.health_monitor() as healthmon:
self.assertRaises(loadbalancer.PoolNotFound,
self.plugin.create_pool_health_monitor,
context.get_admin_context(),
healthmon,
"123-456-789"
)
class TestLoadBalancerXML(TestLoadBalancer):
fmt = 'xml'