예제 #1
0
    def _ipam_get_subnets(self,
                          context,
                          network_id,
                          host,
                          service_type=None,
                          fixed_configured=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)
        if subnets:
            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)
예제 #2
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)
예제 #3
0
 def _check_service_subnets(self, query, service_type):
     """Raise an exception if empty subnet list is caused by service type"""
     if not query.limit(1).count():
         return
     query = self._query_filter_service_subnets(query, service_type)
     if query.limit(1).count():
         return
     raise ipam_exceptions.IpAddressGenerationFailureNoMatchingSubnet()
예제 #4
0
    def _ipam_get_subnets(self,
                          context,
                          network_id,
                          host,
                          service_type=None,
                          fixed_configured=False):
        """Return eligible subnets

        If no eligible subnets are found, determine why and potentially raise
        an appropriate error.
        """
        subnets = self._find_candidate_subnets(context, network_id, host,
                                               service_type, fixed_configured)
        if subnets:
            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'))

        # Determine why we found no subnets to raise the right error
        query = self._query_subnets_on_network(context, network_id)

        if self.is_host_set(host):
            # Empty because host isn't mapped to a segment with a subnet?
            s_query = query.filter(models_v2.Subnet.segment_id.isnot(None))
            if s_query.limit(1).count() != 0:
                # It is a routed network but no subnets found for host
                raise segment_exc.HostNotConnectedToAnySegment(
                    host=host, network_id=network_id)

        if not query.limit(1).count():
            # Network has *no* subnets of any kind. This isn't an error.
            return []

        # Does filtering ineligible service subnets makes the list empty?
        query = self._query_filter_service_subnets(query, service_type)
        if query.limit(1).count():
            # No, must be a deferred IP port because there are matching
            # subnets. Happens on routed networks when host isn't known.
            raise ipam_exceptions.DeferIpam()

        raise ipam_exceptions.IpAddressGenerationFailureNoMatchingSubnet(
            network_id=network_id, service_type=service_type)