From df1d4ca3d5c9158468866f7145a18ca46cd92e18 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Wed, 11 Sep 2013 16:24:13 -0700 Subject: [PATCH] Fix error code for deletion of router which is in use by vpnservice In this commit, we check router is in use by a vpnservice or not when we delete router. Fixes bug 1224174 Change-Id: I55a4b9b93715fbb36816c69729d88eb494bcf15e --- neutron/db/l3_db.py | 7 +++++++ neutron/db/vpn/vpn_db.py | 8 ++++++++ neutron/extensions/vpnaas.py | 4 ++++ neutron/tests/unit/db/vpn/test_db_vpnaas.py | 9 +++++++++ 4 files changed, 28 insertions(+) diff --git a/neutron/db/l3_db.py b/neutron/db/l3_db.py index 454691e5b..ab6017f5c 100644 --- a/neutron/db/l3_db.py +++ b/neutron/db/l3_db.py @@ -33,6 +33,7 @@ from neutron import manager from neutron.openstack.common import log as logging from neutron.openstack.common.notifier import api as notifier_api from neutron.openstack.common import uuidutils +from neutron.plugins.common import constants LOG = logging.getLogger(__name__) @@ -230,6 +231,12 @@ class L3_NAT_db_mixin(l3.RouterPluginBase): if ports: raise l3.RouterInUse(router_id=id) + #TODO(nati) Refactor here when we have router insertion model + vpnservice = manager.NeutronManager.get_service_plugins().get( + constants.VPN) + if vpnservice: + vpnservice.check_router_in_use(context, id) + # delete any gw port device_filter = {'device_id': [id], 'device_owner': [DEVICE_OWNER_ROUTER_GW]} diff --git a/neutron/db/vpn/vpn_db.py b/neutron/db/vpn/vpn_db.py index f82f4ee5f..1daaa64f1 100644 --- a/neutron/db/vpn/vpn_db.py +++ b/neutron/db/vpn/vpn_db.py @@ -583,6 +583,14 @@ class VPNPluginDb(VPNPluginBase, base_db.CommonDbMixin): self._make_vpnservice_dict, filters=filters, fields=fields) + def check_router_in_use(self, context, router_id): + vpnservices = self.get_vpnservices( + context, filters={'router_id': [router_id]}) + if vpnservices: + raise vpnaas.RouterInUseByVPNService( + router_id=router_id, + vpnservice_id=vpnservices[0]['id']) + class VPNPluginRpcDbMixin(): def _get_agent_hosting_vpn_services(self, context, host): diff --git a/neutron/extensions/vpnaas.py b/neutron/extensions/vpnaas.py index e85174bbd..dc89752a4 100644 --- a/neutron/extensions/vpnaas.py +++ b/neutron/extensions/vpnaas.py @@ -65,6 +65,10 @@ class VPNServiceInUse(qexception.InUse): message = _("VPNService %(vpnservice_id)s is still in use") +class RouterInUseByVPNService(qexception.InUse): + message = _("Router %(router_id)s is used by VPNService %(vpnservice_id)s") + + class VPNStateInvalid(qexception.BadRequest): message = _("Invalid state %(state)s of vpnaas resource %(id)s") diff --git a/neutron/tests/unit/db/vpn/test_db_vpnaas.py b/neutron/tests/unit/db/vpn/test_db_vpnaas.py index ced5cf2c7..f4f4b525b 100644 --- a/neutron/tests/unit/db/vpn/test_db_vpnaas.py +++ b/neutron/tests/unit/db/vpn/test_db_vpnaas.py @@ -770,6 +770,15 @@ class TestVpnaas(VPNPluginDbTestCase): expected) return vpnservice + def test_delete_router_in_use_by_vpnservice(self): + """Test delete router in use by vpn service.""" + with self.subnet(cidr='10.2.0.0/24') as subnet: + with self.router() as router: + with self.vpnservice(subnet=subnet, + router=router): + self._delete('routers', router['router']['id'], + expected_code=webob.exc.HTTPConflict.code) + def _set_active(self, model, resource_id): service_plugin = manager.NeutronManager.get_service_plugins()[ constants.VPN] -- 2.45.2