コード例 #1
0
def build_ip_qsets(ip_str):
    ip_type, Klass = resolve_ip_type(ip_str)
    try:
        ip = Klass(ip_str)
        ip_upper, ip_lower = one_to_two(int(ip))
    except (ipaddr.AddressValueError):
        raise BadDirective("{0} isn't a valid "
                           "IP address.".format(ip_str))
    return build_ipf_qsets(Q(ip_upper=ip_upper, ip_lower=ip_lower))
コード例 #2
0
ファイル: models.py プロジェクト: rrsc/inventory-1
 def update_network(self):
     """This function will look at the value of network_str to update other
     fields in the network object. This function will also set the 'network'
     attribute to either an ipaddr.IPv4Network or ipaddr.IPv6Network object.
     """
     if not isinstance(self.network_str, basestring):
         raise ValidationError("ERROR: No network str.")
     try:
         if self.ip_type == "4":
             self.network = ipaddr.IPv4Network(self.network_str)
         elif self.ip_type == "6":
             self.network = ipaddr.IPv6Network(self.network_str)
         else:
             raise ValidationError("Could not determine IP type of network" " %s" % (self.network_str))
     except (ipaddr.AddressValueError, ipaddr.NetmaskValueError):
         raise ValidationError("Invalid network for ip type of " "'{0}'.".format(self, self.ip_type))
     # Update fields
     self.ip_upper, self.ip_lower = one_to_two(int(self.network))
     self.prefixlen = self.network.prefixlen
def ip_to_range(ip):
    """Attempt to map an IP into a range"""
    ip_type = resolve_ip_type(ip)

    ip_upper, ip_lower = one_to_two(ip_to_int(ip, ip_type[0]))
    # If it's within the uppers, it's definitely within the lowers
    upper_q = Q(
        ~Q(start_upper=F('end_upper')),
        start_upper__lte=ip_upper, end_upper__gte=ip_upper
    )

    # If the uppers match, look in the lowers
    lower_q = Q(
        Q(start_upper=F('end_upper')),
        start_lower__lte=ip_lower, end_lower__gte=ip_lower
    )
    try:
        return Range.objects.get(upper_q | lower_q)
    except Range.DoesNotExist:
        pass
 def update_network(self):
     """This function will look at the value of network_str to update other
     fields in the network object. This function will also set the 'network'
     attribute to either an ipaddr.IPv4Network or ipaddr.IPv6Network object.
     """
     if not isinstance(self.network_str, basestring):
         raise ValidationError("ERROR: No network str.")
     try:
         if self.ip_type == '4':
             self.network = ipaddr.IPv4Network(self.network_str)
         elif self.ip_type == '6':
             self.network = ipaddr.IPv6Network(self.network_str)
         else:
             raise ValidationError("Could not determine IP type of network"
                                   " %s" % (self.network_str))
     except (ipaddr.AddressValueError, ipaddr.NetmaskValueError):
         raise ValidationError("Invalid network for ip type of "
                               "'{0}'.".format(self, self.ip_type))
     # Update fields
     self.ip_upper, self.ip_lower = one_to_two(int(self.network))
     self.prefixlen = self.network.prefixlen
コード例 #5
0
ファイル: invfilter.py プロジェクト: jlin/inventory
        site = None
        vlan = None
        for parent in reversed(parents):
            if parent.site:
                site = parent.site
                vlan = parent.vlan
                break

        if not site:
            for child in children:
                if child.site:
                    site = child.site
                    vlan = child.vlan
                    break

    ip_upper, ip_lower = one_to_two(int(ip))

    ipf_qs = build_ipf_qsets(Q(ip_upper=ip_upper, ip_lower=ip_lower))
    q_sets = []

    for q, (name, Klass) in izip(ipf_qs, searchables):
        if name == "NET":
            q_sets.append(network_q)
        elif name == "SITE" and site:
            q_sets.append(Q(pk=site.pk))
        elif name == "VLAN" and vlan:
            q_sets.append(Q(pk=vlan.pk))
        else:
            q_sets.append(q)

    return q_sets
コード例 #6
0
        site = None
        vlan = None
        for parent in reversed(parents):
            if parent.site:
                site = parent.site
                vlan = parent.vlan
                break

        if not site:
            for child in children:
                if child.site:
                    site = child.site
                    vlan = child.vlan
                    break

    ip_upper, ip_lower = one_to_two(int(ip))

    ipf_qs = build_ipf_qsets(Q(ip_upper=ip_upper, ip_lower=ip_lower))
    q_sets = []

    for q, (name, Klass) in izip(ipf_qs, searchables):
        if name == 'NET':
            q_sets.append(network_q)
        elif name == 'SITE' and site:
            q_sets.append(Q(pk=site.pk))
        elif name == 'VLAN' and vlan:
            q_sets.append(Q(pk=vlan.pk))
        else:
            q_sets.append(q)

    return q_sets