Ejemplo n.º 1
0
 def address(self):
     if self._values['address'] is None:
         return None
     if is_valid_ip(self._values['address']):
         ip = str(ip_address(u'{0}'.format(self._values['address'])))
         return ip
     raise F5ModuleError("Specified 'address' is not an IP address.")
Ejemplo n.º 2
0
 def encode_address_from_dict(self, record):
     if is_valid_ip_network(record['key']):
         key = ip_network(u"{0}".format(str(record['key'])))
     elif is_valid_ip(record['key']):
         key = ip_address(u"{0}".format(str(record['key'])))
     elif is_valid_ip_interface(record['key']):
         key = ip_interface(u"{0}".format(str(record['key'])))
     else:
         raise F5ModuleError(
             "When specifying an 'address' type, the value to the left of the separator must be an IP."
         )
     if key and 'value' in record:
         try:
             # Only ip_address's have max_prefixlen
             if key.max_prefixlen in [32, 128]:
                 return self.encode_host(str(key), record['value'])
         except ValueError:
             return self.encode_network(str(key.network_address),
                                        key.prefixlen, record['value'])
     elif key:
         try:
             # Only ip_address's have max_prefixlen
             if key.max_prefixlen in [32, 128]:
                 return self.encode_host(str(key), str(key))
         except ValueError:
             return self.encode_network(str(key.network_address),
                                        key.prefixlen,
                                        str(key.network_address))
    def netmask(self):
        if self._values['netmask'] is None:
            return None
        try:
            result = int(self._values['netmask'])

            # CIDRs between 0 and 128 are allowed
            if 0 <= result <= 128:
                return result
            else:
                raise F5ModuleError(
                    "The provided netmask must be between 0 and 32 for IPv4, or "
                    "0 and 128 for IPv6.")
        except ValueError:
            # not a number, but that's ok. Further processing necessary
            pass

        if not is_valid_ip(self._values['netmask']):
            raise F5ModuleError(
                'The provided netmask {0} is neither in IP or CIDR format'.
                format(result))

        # Create a temporary address to check if the netmask IP is v4 or v6
        addr = ip_address(u'{0}'.format(str(self._values['netmask'])))
        if addr.version == 4:
            # Create a more real v4 address using a wildcard, so that we can determine
            # the CIDR value from it.
            ip = ip_network(u'0.0.0.0/%s' % str(self._values['netmask']))
            result = ip.prefixlen
        else:
            result = ipv6_netmask_to_cidr(self._values['netmask'])

        return result
Ejemplo n.º 4
0
    def address_ranges(self):
        result = []
        for item in self._values['addresses']:
            if '-' not in item['name']:
                continue
            start, stop = item['name'].split('-')
            start = start.strip()
            stop = stop.strip()

            start = ip_address(u'{0}'.format(start))
            stop = ip_address(u'{0}'.format(stop))
            if start.version != stop.version:
                raise F5ModuleError(
                    "When specifying a range, IP addresses must be of the same type; IPv4 or IPv6."
                )
            if int(start) > int(stop):
                stop, start = start, stop
            item = '{0}-{1}'.format(str(start), str(stop))
            result.append(item)
        result = sorted(result)
        return result
Ejemplo n.º 5
0
    def address_ranges(self):
        if self._values['address_ranges'] is None:
            return None
        result = []
        for address_range in self._values['address_ranges']:
            start, stop = address_range.split('-')
            start = start.strip()
            stop = stop.strip()

            start = ip_address(start)
            stop = ip_address(stop)
            if start.version != stop.version:
                raise F5ModuleError(
                    "When specifying a range, IP addresses must be of the same type; IPv4 or IPv6."
                )
            if start > stop:
                stop, start = start, stop
            item = '{0}-{1}'.format(str(start), str(stop))
            result.append(item)
        result = sorted(result)
        return result
Ejemplo n.º 6
0
 def addresses(self):
     if self._values['addresses'] is None:
         return None
     result = []
     for x in self._values['addresses']:
         if is_valid_ip(x):
             result.append(str(ip_address(u'{0}'.format(x))))
         elif is_valid_ip_interface(x):
             result.append(str(ip_interface(u'{0}'.format(x))))
         else:
             raise F5ModuleError(
                 "Address {0} must be either an IPv4 or IPv6 address or network.".format(x)
             )
     result = sorted(result)
     return result
Ejemplo n.º 7
0
    def encode_address_from_string(self, record):
        if self._network_pattern.match(record):
            # network 192.168.0.0 prefixlen 16 := "Network3",
            # network 2402:9400:1000:0:: prefixlen 64 := "Network4",
            return record
        elif self._host_pattern.match(record):
            # host 172.16.1.1/32 := "Host3"
            # host 2001:0db8:85a3:0000:0000:8a2e:0370:7334 := "Host4"
            return record
        else:
            # 192.168.0.0/16 := "Network3",
            # 2402:9400:1000:0::/64 := "Network4",
            parts = record.split(self._separator)
            if is_valid_ip_network(parts[0]):
                key = ip_network(u"{0}".format(str(parts[0])))
            elif is_valid_ip(parts[0]):
                key = ip_address(u"{0}".format(str(parts[0])))
            elif is_valid_ip_interface(parts[0]):
                key = ip_interface(u"{0}".format(str(parts[0])))
            elif parts[0] == '':
                pass
            else:
                raise F5ModuleError(
                    "When specifying an 'address' type, the value to the left of the separator must be an IP."
                )

            if len(parts) == 2:
                try:
                    # Only ip_address's have max_prefixlen
                    if key.max_prefixlen in [32, 128]:
                        return self.encode_host(str(key), parts[1])
                except ValueError:
                    return self.encode_network(str(key.network_address),
                                               key.prefixlen, parts[1])
            elif len(parts) == 1 and parts[0] != '':
                try:
                    # Only ip_address's have max_prefixlen
                    if key.max_prefixlen in [32, 128]:
                        return self.encode_host(str(key), str(key))
                except ValueError:
                    return self.encode_network(str(key.network_address),
                                               key.prefixlen,
                                               str(key.network_address))
Ejemplo n.º 8
0
 def netmask(self):
     if self._values['netmask'] is None:
         return None
     result = -1
     try:
         result = int(self._values['netmask'])
         if 0 < result < 256:
             pass
     except ValueError:
         if is_valid_ip(self._values['netmask']):
             addr = ip_address(u'{0}'.format(str(self._values['netmask'])))
             if addr.version == 4:
                 ip = ip_network(u'0.0.0.0/%s' % str(self._values['netmask']))
                 result = ip.prefixlen
             else:
                 result = ipv6_netmask_to_cidr(self._values['netmask'])
     if result < 0:
         raise F5ModuleError(
             'The provided netmask {0} is neither in IP or CIDR format'.format(result)
         )
     return result