From 740bf12dfa6850c84a3ce07c1a840ae113045f71 Mon Sep 17 00:00:00 2001 From: Bodo Petermann Date: Tue, 14 Jun 2022 15:44:32 +0200 Subject: [PATCH] Fix creating ipsec site connection Fixes a RuntimeError "Method remove_reservation cannot be called within a transaction" while creating an ipsec site connection. To avoid the error the service driver now gets the vpnservice (to get the router_id) inside a CONTEXT_READER wrapper. Closes-Bug: #1978571 Change-Id: I6552816db8a596a933365dc9de481d8c32371f1c --- neutron_vpnaas/db/vpn/vpn_db.py | 5 +++++ .../services/vpn/service_drivers/base_ipsec.py | 12 ++++++------ .../unit/services/vpn/service_drivers/test_ipsec.py | 5 ++--- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/neutron_vpnaas/db/vpn/vpn_db.py b/neutron_vpnaas/db/vpn/vpn_db.py index c4fae60d4..12c39c73a 100644 --- a/neutron_vpnaas/db/vpn/vpn_db.py +++ b/neutron_vpnaas/db/vpn/vpn_db.py @@ -668,6 +668,11 @@ class VPNPluginDb(vpnaas.VPNPluginBase, if query.first(): raise vpn_exception.EndpointGroupInUse(group_id=group_id) + def get_vpnservice_router_id(self, context, vpnservice_id): + with db_api.CONTEXT_READER.using(context): + vpnservice = self._get_vpnservice(context, vpnservice_id) + return vpnservice['router_id'] + class VPNPluginRpcDbMixin(object): def _build_local_subnet_cidr_map(self, context): diff --git a/neutron_vpnaas/services/vpn/service_drivers/base_ipsec.py b/neutron_vpnaas/services/vpn/service_drivers/base_ipsec.py index c7c3e883a..7f45b9a83 100644 --- a/neutron_vpnaas/services/vpn/service_drivers/base_ipsec.py +++ b/neutron_vpnaas/services/vpn/service_drivers/base_ipsec.py @@ -112,20 +112,20 @@ class BaseIPsecVPNDriver(service_drivers.VpnDriver, metaclass=abc.ABCMeta): pass def create_ipsec_site_connection(self, context, ipsec_site_connection): - vpnservice = self.service_plugin._get_vpnservice( + router_id = self.service_plugin.get_vpnservice_router_id( context, ipsec_site_connection['vpnservice_id']) - self.agent_rpc.vpnservice_updated(context, vpnservice['router_id']) + self.agent_rpc.vpnservice_updated(context, router_id) def update_ipsec_site_connection( self, context, old_ipsec_site_connection, ipsec_site_connection): - vpnservice = self.service_plugin._get_vpnservice( + router_id = self.service_plugin.get_vpnservice_router_id( context, ipsec_site_connection['vpnservice_id']) - self.agent_rpc.vpnservice_updated(context, vpnservice['router_id']) + self.agent_rpc.vpnservice_updated(context, router_id) def delete_ipsec_site_connection(self, context, ipsec_site_connection): - vpnservice = self.service_plugin._get_vpnservice( + router_id = self.service_plugin.get_vpnservice_router_id( context, ipsec_site_connection['vpnservice_id']) - self.agent_rpc.vpnservice_updated(context, vpnservice['router_id']) + self.agent_rpc.vpnservice_updated(context, router_id) def create_ikepolicy(self, context, ikepolicy): pass diff --git a/neutron_vpnaas/tests/unit/services/vpn/service_drivers/test_ipsec.py b/neutron_vpnaas/tests/unit/services/vpn/service_drivers/test_ipsec.py index fdc114da9..9c26db98c 100644 --- a/neutron_vpnaas/tests/unit/services/vpn/service_drivers/test_ipsec.py +++ b/neutron_vpnaas/tests/unit/services/vpn/service_drivers/test_ipsec.py @@ -98,9 +98,8 @@ class TestIPsecDriver(base.BaseTestCase): self.svc_plugin = mock.Mock() self.svc_plugin.get_l3_agents_hosting_routers.return_value = [l3_agent] self._fake_vpn_router_id = _uuid() - self.svc_plugin._get_vpnservice.return_value = { - 'router_id': self._fake_vpn_router_id - } + self.svc_plugin.get_vpnservice_router_id.return_value = \ + self._fake_vpn_router_id self.driver = ipsec_driver.IPsecVPNDriver(self.svc_plugin) self.validator = ipsec_validator.IpsecVpnValidator(self.driver) self.context = n_ctx.get_admin_context()