from oslo_log import log as logging
import sqlalchemy as sa
from sqlalchemy import orm
-from sqlalchemy.orm import exc
from sqlalchemy.orm import joinedload
from neutron.callbacks import events
def unbind_snat(self, context, router_id, agent_id=None):
"""Unbind snat from the chosen l3 service agent.
- Unbinds from any L3 agent hosting SNAT if passed agent_id is None
+ Unbinds from all L3 agents hosting SNAT if passed agent_id is None
"""
with context.session.begin(subtransactions=True):
query = (context.session.
filter_by(router_id=router_id))
if agent_id:
query = query.filter_by(l3_agent_id=agent_id)
- try:
- binding = query.one()
- except exc.NoResultFound:
- LOG.debug('no snat router binding found for router: %('
- 'router)s, agent: %(agent)s',
+ binding = query.first()
+ if not binding:
+ LOG.debug('no SNAT router binding found for router: '
+ '%(router)s, agent: %(agent)s',
{'router': router_id, 'agent': agent_id or 'any'})
return
- agent_id = binding.l3_agent_id
- LOG.debug('Delete binding of the SNAT router %(router_id)s '
- 'from agent %(id)s', {'router_id': router_id,
- 'id': agent_id})
- context.session.delete(binding)
+ query.delete()
+ LOG.debug('Deleted binding of the SNAT router %s', router_id)
return binding
binding = l3_dvrscheduler_db.CentralizedSnatL3AgentBinding(
router_id=router_id, l3_agent_id='foo_l3_agent_id',
l3_agent=agents_db.Agent())
- with mock.patch.object(query.Query, 'one') as mock_query,\
- mock.patch.object(self.adminContext.session,
- 'delete') as mock_session,\
+ with mock.patch.object(query.Query, 'first') as mock_first,\
mock.patch.object(query.Query, 'delete') as mock_delete,\
mock.patch.object(
self.dut,
'get_subnet_ids_on_router') as mock_get_subnets:
- mock_query.return_value = binding
+ mock_first.return_value = binding
mock_get_subnets.return_value = ['foo_subnet_id']
self.dut.unbind_snat_servicenode(self.adminContext, router_id)
mock_get_subnets.assert_called_with(self.adminContext, router_id)
- self.assertTrue(mock_session.call_count)
self.assertTrue(mock_delete.call_count)
core_plugin.assert_called_once_with()
l3_notifier.assert_called_once_with()