From 012348829ba0da262952426a9dc6b5b0601310e6 Mon Sep 17 00:00:00 2001 From: Oleg Bondarev Date: Tue, 6 Aug 2013 17:44:30 +0400 Subject: [PATCH] LBaaS: throw proper exception on duplicating monitor association Fixes bug 1208728 Change-Id: I6cf27766eedff34a6ed5062a50b049ab0a5bf96a --- neutron/db/loadbalancer/loadbalancer_db.py | 7 +++++++ neutron/extensions/loadbalancer.py | 5 +++++ .../db/loadbalancer/test_db_loadbalancer.py | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/neutron/db/loadbalancer/loadbalancer_db.py b/neutron/db/loadbalancer/loadbalancer_db.py index 4c9fd7195b..8b890fc00c 100644 --- a/neutron/db/loadbalancer/loadbalancer_db.py +++ b/neutron/db/loadbalancer/loadbalancer_db.py @@ -565,6 +565,13 @@ 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): + assoc_qry = context.session.query(PoolMonitorAssociation) + assoc = assoc_qry.filter_by(pool_id=pool_id, + monitor_id=monitor_id).first() + if assoc: + raise loadbalancer.PoolMonitorAssociationExists( + monitor_id=monitor_id, pool_id=pool_id) + pool = self._get_resource(context, Pool, pool_id) assoc = PoolMonitorAssociation(pool_id=pool_id, diff --git a/neutron/extensions/loadbalancer.py b/neutron/extensions/loadbalancer.py index 82b4a8e665..084a7c4555 100644 --- a/neutron/extensions/loadbalancer.py +++ b/neutron/extensions/loadbalancer.py @@ -54,6 +54,11 @@ class PoolMonitorAssociationNotFound(qexception.NotFound): "with Pool %(pool_id)s") +class PoolMonitorAssociationExists(qexception.Conflict): + message = _('health_monitor %(monitor_id)s is already associated ' + 'with pool %(pool_id)s') + + class StateInvalid(qexception.NeutronException): message = _("Invalid state %(state)s of Loadbalancer resource %(id)s") diff --git a/neutron/tests/unit/db/loadbalancer/test_db_loadbalancer.py b/neutron/tests/unit/db/loadbalancer/test_db_loadbalancer.py index 697b1cd464..8ba25aea22 100644 --- a/neutron/tests/unit/db/loadbalancer/test_db_loadbalancer.py +++ b/neutron/tests/unit/db/loadbalancer/test_db_loadbalancer.py @@ -1210,6 +1210,24 @@ class TestLoadBalancer(LoadBalancerPluginDbTestCase): driver_call.assert_called_once_with( mock.ANY, hm['health_monitor'], pool['pool']['id']) + def test_create_pool_health_monitor_already_associated(self): + with contextlib.nested( + self.pool(name="pool"), + self.health_monitor(), + ) as (pool, hm): + res = self.plugin.create_pool_health_monitor( + context.get_admin_context(), + hm, pool['pool']['id'] + ) + self.assertEqual({'health_monitor': + [hm['health_monitor']['id']]}, + res) + self.assertRaises(loadbalancer.PoolMonitorAssociationExists, + self.plugin.create_pool_health_monitor, + context.get_admin_context(), + hm, + pool['pool']['id']) + def test_create_pool_healthmon_invalid_pool_id(self): with self.health_monitor() as healthmon: self.assertRaises(loadbalancer.PoolNotFound,