def delete_network(context, id): """Delete a network. : param context: neutron api request context : param id: UUID representing the network to delete. """ LOG.info("delete_network %s for tenant %s" % (id, context.tenant_id)) with context.session.begin(): net = db_api.network_find(context=context, limit=None, sorts=['id'], marker=None, page_reverse=False, id=id, scope=db_api.ONE) if not net: raise n_exc.NetworkNotFound(net_id=id) if not context.is_admin: if STRATEGY.is_provider_network(net.id): raise n_exc.NotAuthorized(net_id=id) if net.ports: raise n_exc.NetworkInUse(net_id=id) net_driver = registry.DRIVER_REGISTRY.get_driver(net["network_plugin"]) net_driver.delete_network(context, id) for subnet in net["subnets"]: subnets._delete_subnet(context, subnet) db_api.network_delete(context, net)
def check_network_not_in_use(self, context, t_ctx, network_id): # use a different name to avoid override _ensure_entwork_not_in_use subnets = self._get_subnets_by_network(context, network_id) auto_delete_port_names = [] for subnet in subnets: subnet_id = subnet['id'] region_names = [ e[0] for e in t_ctx.session.query(sql.distinct(models.Pod.region_name)). join(models.ResourceRouting, models.Pod.pod_id == models.ResourceRouting.pod_id).filter( models.ResourceRouting.top_id == subnet_id) ] auto_delete_port_names.extend([ t_constants.interface_port_name % (region_name, subnet_id) for region_name in region_names ]) dhcp_port_name = t_constants.dhcp_port_name % subnet_id snat_port_name = t_constants.snat_port_name % subnet_id auto_delete_port_names.append(dhcp_port_name) auto_delete_port_names.append(snat_port_name) if not auto_delete_port_names: # pre-created port not found, any ports left need to be deleted # before deleting network non_auto_delete_ports = context.session.query( models_v2.Port.id).filter_by(network_id=network_id) if non_auto_delete_ports.count(): raise exceptions.NetworkInUse(net_id=network_id) return t_pod = db_api.get_top_pod(t_ctx) auto_delete_port_ids = [ e[0] for e in t_ctx.session.query(models.ResourceRouting.bottom_id). filter_by(pod_id=t_pod['pod_id'], resource_type=t_constants.RT_PORT). filter(models.ResourceRouting.top_id.in_(auto_delete_port_names)) ] non_auto_delete_ports = context.session.query( models_v2.Port.id).filter_by(network_id=network_id).filter( ~models_v2.Port.id.in_(auto_delete_port_ids)) if non_auto_delete_ports.count(): raise exceptions.NetworkInUse(net_id=network_id)
def test_convert_exception_to_http_exc_multiple_different_codes(self): e = exceptions.MultipleExceptions([ exceptions.NetworkInUse(net_id='nid'), exceptions.PortNotFound(port_id='pid') ]) conv = common.convert_exception_to_http_exc(e, base_v2.FAULT_MAP, None) self.assertIsInstance(conv, exc.HTTPConflict) self.assertEqual( ("HTTP 409 NetworkInUse: Unable to complete operation on network " "nid. There are one or more ports still in use on the network.\n" "HTTP 404 PortNotFound: Port pid could not be found."), jsonutils.loads(conv.body)['NeutronError']['message'])
def delete_network(context, id): """Delete a network. : param context: neutron api request context : param id: UUID representing the network to delete. """ LOG.info("delete_network %s for tenant %s" % (id, context.tenant_id)) with context.session.begin(): net = db_api.network_find(context, None, None, None, False, id=id, scope=db_api.ONE) if not net: raise n_exc.NetworkNotFound(net_id=id) if net.ports: raise n_exc.NetworkInUse(net_id=id) net_driver = registry.DRIVER_REGISTRY.get_driver(net["network_plugin"]) net_driver.delete_network(context, id) for subnet in net["subnets"]: subnets._delete_subnet(context, subnet) db_api.network_delete(context, net)
def test_ha_network_is_not_deleted_if_network_in_use(self): self._test_ha_network_is_not_deleted_raise_exception( n_exc.NetworkInUse(net_id="foo_net_id"))