"""Bind the snat router to the chosen l3 service agent."""
chosen_snat_agent = random.choice(snat_candidates)
self.bind_snat_router(context, router_id, chosen_snat_agent)
+ return chosen_snat_agent
def unbind_snat_servicenode(self, context, router_id):
"""Unbind the snat router to the chosen l3 service agent."""
def schedule_snat_router(self, context, router_id, sync_router):
"""Schedule the snat router on l3 service agent."""
- binding = (context.session.
- query(CentralizedSnatL3AgentBinding).
- filter_by(router_id=router_id).first())
- if binding:
- l3_agent_id = binding.l3_agent_id
- l3_agent = binding.l3_agent
- LOG.debug('SNAT Router %(router_id)s has already been '
- 'hosted by L3 agent '
- '%(l3_agent_id)s', {'router_id': router_id,
- 'l3_agent_id': l3_agent_id})
- self.bind_dvr_router_servicenode(context, router_id, l3_agent)
- return
active_l3_agents = self.get_l3_agents(context, active=True)
if not active_l3_agents:
LOG.warn(_('No active L3 agents found for SNAT'))
snat_candidates = self.get_snat_candidates(sync_router,
active_l3_agents)
if snat_candidates:
- self.bind_snat_servicenode(context, router_id, snat_candidates)
+ chosen_agent = self.bind_snat_servicenode(
+ context, router_id, snat_candidates)
+ self.bind_dvr_router_servicenode(
+ context, router_id, chosen_agent)
}
return agent, router
- def test_schedule_snat_router_with_gateway_and_nobinding(self):
- agent, router = self._prepare_schedule_snat_tests()
- with contextlib.nested(
- mock.patch.object(query.Query, 'first'),
- mock.patch.object(self.dut, 'get_l3_agents'),
- mock.patch.object(self.dut, 'get_snat_candidates'),
- mock.patch.object(self.dut, 'get_router'),
- mock.patch.object(self.dut, 'bind_dvr_router_servicenode'),
- mock.patch.object(self.dut, 'bind_snat_servicenode')) as (
- mock_query, mock_agents,
- mock_candidates, mock_rd, mock_dvr, mock_bind):
- mock_rd.return_value = router
- mock_query.return_value = []
- mock_agents.return_value = [agent]
- mock_candidates.return_value = [agent]
- self.dut.schedule_snat_router(
- self.adminContext, 'foo_router_id', router)
- self.assertFalse(mock_dvr.called)
-
def test_schedule_router_unbind_snat_servicenode_negativetest(self):
router = {
'id': 'foo_router_id',