def test_stop_vpn_in_subnet(self, delete_vpnservice, delete_subnet_vpn): context = base.create_context() cleaner = common.OnCrashCleaner() mock_manager = mock.Mock() mock_manager.attach_mock(delete_vpnservice, 'delete_vpnservice') mock_manager.attach_mock(delete_subnet_vpn, 'delete_subnet_vpn') self.set_mock_db_items(fakes.DB_VPN_CONNECTION_1, fakes.DB_VPN_CONNECTION_2) vpn_gateway_api._stop_vpn_in_subnet( context, self.neutron, cleaner, copy.deepcopy(fakes.DB_SUBNET_1)) mock_manager.has_calls([ mock.call.delete_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_1, fakes.DB_VPN_CONNECTION_1), mock.call.delete_subnet_vpn( context, self.neutron, cleaner, fakes.DB_SUBNET_1, fakes.DB_VPN_CONNECTION_2), mock.call.delete_vpnservice( self.neutron, fakes.ID_OS_VPNSERVICE_1, fakes.ID_EC2_SUBNET_1)]) delete_subnet_vpn.reset_mock() delete_vpnservice.reset_mock() vpn_gateway_api._stop_vpn_in_subnet( context, self.neutron, cleaner, self.DB_SUBNET_1_NO_VPN) self.assertFalse(delete_subnet_vpn.called) self.assertFalse(delete_vpnservice.called)
def delete_subnet(context, subnet_id): subnet = ec2utils.get_db_item(context, subnet_id) vpc = db_api.get_item_by_id(context, subnet['vpc_id']) network_interfaces = network_interface_api.describe_network_interfaces( context, filter=[{ 'name': 'subnet-id', 'value': [subnet_id] }])['networkInterfaceSet'] if network_interfaces: msg = _("The subnet '%(subnet_id)s' has dependencies and " "cannot be deleted.") % { 'subnet_id': subnet_id } raise exception.DependencyViolation(msg) neutron = clients.neutron(context) with common.OnCrashCleaner() as cleaner: db_api.delete_item(context, subnet['id']) cleaner.addCleanup(db_api.restore_item, context, 'subnet', subnet) vpn_gateway_api._stop_vpn_in_subnet(context, neutron, cleaner, subnet) try: neutron.remove_interface_router(vpc['os_id'], {'subnet_id': subnet['os_id']}) except neutron_exception.NotFound: pass cleaner.addCleanup(neutron.add_interface_router, vpc['os_id'], {'subnet_id': subnet['os_id']}) try: os_subnet = neutron.show_subnet(subnet['os_id'])['subnet'] except neutron_exception.NotFound: pass else: try: neutron.delete_network(os_subnet['network_id']) except neutron_exception.NetworkInUseClient as ex: LOG.warning( _('Failed to delete network %(os_id)s during ' 'deleting Subnet %(id)s. Reason: %(reason)s'), { 'id': subnet['id'], 'os_id': os_subnet['network_id'], 'reason': ex.message }) return True
def delete_subnet(context, subnet_id): subnet = ec2utils.get_db_item(context, subnet_id) vpc = db_api.get_item_by_id(context, subnet['vpc_id']) network_interfaces = network_interface_api.describe_network_interfaces( context, filter=[{'name': 'subnet-id', 'value': [subnet_id]}])['networkInterfaceSet'] if network_interfaces: msg = _("The subnet '%(subnet_id)s' has dependencies and " "cannot be deleted.") % {'subnet_id': subnet_id} raise exception.DependencyViolation(msg) neutron = clients.neutron(context) with common.OnCrashCleaner() as cleaner: db_api.delete_item(context, subnet['id']) cleaner.addCleanup(db_api.restore_item, context, 'subnet', subnet) vpn_gateway_api._stop_vpn_in_subnet(context, neutron, cleaner, subnet) try: neutron.remove_interface_router(vpc['os_id'], {'subnet_id': subnet['os_id']}) except neutron_exception.NotFound: pass cleaner.addCleanup(neutron.add_interface_router, vpc['os_id'], {'subnet_id': subnet['os_id']}) try: os_subnet = neutron.show_subnet(subnet['os_id'])['subnet'] except neutron_exception.NotFound: pass else: try: neutron.delete_network(os_subnet['network_id']) except neutron_exception.NetworkInUseClient as ex: LOG.warning(_('Failed to delete network %(os_id)s during ' 'deleting Subnet %(id)s. Reason: %(reason)s'), {'id': subnet['id'], 'os_id': os_subnet['network_id'], 'reason': ex.message}) return True