Ejemplo n.º 1
0
    def _ipam_get_subnets(self, context, network_id, host, service_type=None,
                          fixed_configured=False, fixed_ips=None,
                          distributed_service=False):
        """Return eligible subnets

        If no eligible subnets are found, determine why and potentially raise
        an appropriate error.
        """
        subnets = subnet_obj.Subnet.find_candidate_subnets(
            context, network_id, host, service_type, fixed_configured,
            fixed_ips, distributed_service=distributed_service)
        if subnets:
            msg = ('This subnet is being modified by another concurrent '
                   'operation')
            for subnet in subnets:
                subnet.lock_register(
                    context, exc.SubnetInUse(subnet_id=subnet.id, reason=msg),
                    id=subnet.id)
            subnet_dicts = [self._make_subnet_dict(subnet, context=context)
                            for subnet in subnets]
            # Give priority to subnets with service_types
            return sorted(
                subnet_dicts,
                key=lambda subnet: not subnet.get('service_types'))

        if subnet_obj.Subnet.network_has_no_subnet(
                context, network_id, host, service_type):
            return []

        raise ipam_exceptions.IpAddressGenerationFailureNoMatchingSubnet(
            network_id=network_id, service_type=service_type)
Ejemplo n.º 2
0
    def _validate_auto_address_subnet_delete(self, resource, event, trigger,
                                             payload):
        context = payload.context
        subnet = subnet_obj.Subnet.get_object(context, id=payload.resource_id)
        is_auto_addr_subnet = ipv6_utils.is_auto_address_subnet(subnet)
        if not is_auto_addr_subnet or subnet.segment_id is None:
            return

        net_allocs = (context.session.query(
            models_v2.IPAllocation.port_id).filter_by(subnet_id=subnet.id))
        port_ids_on_net = [ipalloc.port_id for ipalloc in net_allocs]
        for port_id in port_ids_on_net:
            try:
                port = ports_obj.Port.get_object(context, id=port_id)
                fixed_ips = [
                    f for f in port['fixed_ips'] if f['subnet_id'] != subnet.id
                ]
                if len(fixed_ips) != 0:
                    continue

                LOG.info(
                    "Found port %(port_id)s, with IP auto-allocation "
                    "only on subnet %(subnet)s which is associated with "
                    "segment %(segment_id)s, cannot delete", {
                        'port_id': port_id,
                        'subnet': subnet.id,
                        'segment_id': subnet.segment_id
                    })
                raise n_exc.SubnetInUse(subnet_id=subnet.id)
            except n_exc.PortNotFound:
                # port is gone
                continue
Ejemplo n.º 3
0
    def _validate_auto_address_subnet_delete(self, resource, event, trigger,
                                             payload):
        context = payload.context
        subnet = subnet_obj.Subnet.get_object(context, id=payload.resource_id)
        is_auto_addr_subnet = ipv6_utils.is_auto_address_subnet(subnet)
        if not is_auto_addr_subnet or subnet.segment_id is None:
            return

        ports = ports_obj.Port.get_ports_allocated_by_subnet_id(
            context, subnet.id)
        for port in ports:
            fixed_ips = [f for f in port.fixed_ips if f.subnet_id != subnet.id]
            if len(fixed_ips) != 0:
                continue

            LOG.info(
                "Found port %(port_id)s, with IP auto-allocation "
                "only on subnet %(subnet)s which is associated with "
                "segment %(segment_id)s, cannot delete", {
                    'port_id': port.id,
                    'subnet': subnet.id,
                    'segment_id': subnet.segment_id
                })
            raise n_exc.SubnetInUse(subnet_id=subnet.id)
Ejemplo n.º 4
0
def _delete_subnet(context, subnet):
    if subnet.allocated_ips:
        raise n_exc.SubnetInUse(subnet_id=subnet["id"])
    db_api.subnet_delete(context, subnet)
Ejemplo n.º 5
0
 def check_subnet_in_use(self, context, subnet_id):
     query = context.session.query(Pool).filter_by(subnet_id=subnet_id)
     if query.count():
         pool_id = query.one().id
         raise n_exc.SubnetInUse(
             reason=_LE("Subnet is used by loadbalancer pool %s") % pool_id)