Beispiel #1
0
 def _format_address(self, type):
     if self._values[type] is None:
         return None
     pattern = r'(?P<addr>[^%/]+)(%(?P<rd>\d+))?(/(?P<cidr>\d+))?'
     if '%' in self._values[type]:
         # Handle route domains
         matches = re.match(pattern, self._values[type])
         if not matches:
             return None
         addr = matches.group('addr')
         if addr is None:
             return -1
         cidr = matches.group('cidr')
         rd = matches.group('rd')
         if cidr is not None:
             ip = ip_interface(u'{0}/{1}'.format(addr, cidr))
         else:
             ip = ip_interface(u'{0}'.format(addr))
         if rd:
             result = '{0}%{1}/{2}'.format(str(ip.ip), rd,
                                           ip.network.prefixlen)
         else:
             result = '{0}/{1}'.format(str(ip.ip), ip.network.prefixlen)
         return result
     return str(ip_interface(u'{0}'.format(self._values[type])))
Beispiel #2
0
 def encode_rd_address(self, record, match, host=False):
     if host:
         if is_valid_ip_interface(match.group('addr')):
             key = ip_interface(u"{0}".format(match.group('addr')))
         else:
             raise F5ModuleError(
                 "When specifying an 'address' type, the value to the left of the separator must be an IP."
             )
     else:
         if is_valid_ip_interface(match.group('addr')):
             key = ip_interface(u"{0}/{1}".format(match.group('addr'), match.group('prefix')))
         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:
         if key.network.prefixlen in [32, 128]:
             return self.encode_host(str(key.ip) + '%' + match.group('rd'), record['value'])
         return self.encode_network(
             str(key.network.network_address) + '%' + match.group('rd'), key.network.prefixlen, record['value']
         )
     elif key:
         if key.network.prefixlen in [32, 128]:
             return self.encode_host(str(key.ip) + '%' + match.group('rd'), str(key.ip) + '%' + match.group('rd'))
         return self.encode_network(
             str(key.network.network_address) + '%' + match.group('rd'), key.network.prefixlen,
             str(key.network.network_address) + '%' + match.group('rd')
         )
 def gateway_address(self):
     if self._values['gateway_address'] is None:
         return None
     try:
         if '%' in self._values['gateway_address']:
             addr = self._values['gateway_address'].split('%')[0]
         else:
             addr = self._values['gateway_address']
         ip_interface(u'%s' % str(addr))
         return str(self._values['gateway_address'])
     except ValueError:
         raise F5ModuleError(
             "The provided gateway_address is not an IP address")
 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 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 parts[0] == '':
                return
            if not is_valid_ip_interface(parts[0]):
                raise F5ModuleError(
                    "When specifying an 'address' type, the value to the left of the separator must be an IP."
                )
            key = ip_interface(u"{0}".format(str(parts[0])))

            if len(parts) == 2:
                if key.network.prefixlen in [32, 128]:
                    return self.encode_host(str(key.ip), parts[1])
                return self.encode_network(str(key.network.network_address),
                                           key.network.prefixlen, parts[1])
            elif len(parts) == 1 and parts[0] != '':
                if key.network.prefixlen in [32, 128]:
                    return self.encode_host(str(key.ip), str(key.ip))
                return self.encode_network(str(key.network.network_address),
                                           key.network.prefixlen,
                                           str(key.network.network_address))
Beispiel #6
0
 def encode_address_from_dict(self, record):
     rd_match = re.match(self._rd_net_pattern, record['key'])
     if rd_match:
         return self.encode_rd_address(record, rd_match)
     rd_match = re.match(self._rd_host_pattern, record['key'])
     if rd_match:
         return self.encode_rd_address(record, rd_match, host=True)
     if 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:
         if key.network.prefixlen in [32, 128]:
             return self.encode_host(str(key.ip), record['value'])
         return self.encode_network(
             str(key.network.network_address), key.network.prefixlen, record['value']
         )
     elif key:
         if key.network.prefixlen in [32, 128]:
             return self.encode_host(str(key.ip), str(key.ip))
         return self.encode_network(
             str(key.network.network_address), key.network.prefixlen, str(key.network.network_address)
         )
Beispiel #7
0
 def mgmt_address(self):
     if self._values['mgmt_address'] is None:
         return None
     try:
         addr = ip_interface(u'%s' % str(self._values['mgmt_address']))
         return str(addr.with_prefixlen)
     except ValueError:
         raise F5ModuleError(
             "The specified 'mgmt_address' is not a valid IP address.")
Beispiel #8
0
    def decode_address_from_string(self, record):
        matches = self._rd_net_ptrn.match(record)
        if matches:
            # network 192.168.0.0%11 prefixlen 16 := "Network3",
            # network 2402:9400:1000:0::%11 prefixlen 64 := "Network4",
            value = record.split(self._separator)[1].strip().strip('"')
            addr = "{0}%{1}/{2}".format(matches.group('addr'),
                                        matches.group('rd'),
                                        matches.group('prefix'))
            result = dict(name=addr, data=value)
            return result
        matches = self._network_pattern.match(record)
        if matches:
            # network 192.168.0.0 prefixlen 16 := "Network3",
            # network 2402:9400:1000:0:: prefixlen 64 := "Network4",
            key = u"{0}/{1}".format(matches.group('addr'),
                                    matches.group('prefix'))
            addr = ip_network(key)
            value = record.split(self._separator)[1].strip().strip('"')
            result = dict(name=str(addr), data=value)
            return result
        matches = self._rd_host_ptrn.match(record)
        if matches:
            # host 172.16.1.1%11/32 := "Host3"
            # host 2001:0db8:85a3:0000:0000:8a2e:0370:7334%11 := "Host4"
            host = ip_interface(u"{0}".format(matches.group('addr')))
            addr = "{0}%{1}/{2}".format(matches.group('addr'),
                                        matches.group('rd'),
                                        str(host.network.prefixlen))
            value = record.split(self._separator)[1].strip().strip('"')
            result = dict(name=addr, data=value)
            return result
        matches = self._host_pattern.match(record)
        if matches:
            # host 172.16.1.1/32 := "Host3"
            # host 2001:0db8:85a3:0000:0000:8a2e:0370:7334 := "Host4"
            key = matches.group('addr')
            addr = ip_interface(u"{0}".format(str(key)))
            value = record.split(self._separator)[1].strip().strip('"')
            result = dict(name=str(addr), data=value)
            return result

        raise F5ModuleError('The value "{0}" is not an address'.format(record))
Beispiel #9
0
 def destination_ip(self):
     if self._values['address'] is None:
         return None
     try:
         pattern = r'(?P<rd>%[0-9]+)'
         addr = re.sub(pattern, '', self._values['address'])
         ip = ip_interface(u'{0}'.format(addr))
         return ip.with_prefixlen
     except ValueError:
         raise F5ModuleError(
             "The provided destination is not an IP address")
Beispiel #10
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
Beispiel #11
0
 def _convert_address(self, item, mask=None):
     if item == 'any':
         return '0.0.0.0/0'
     if not is_valid_ip(item):
         raise F5ModuleError('The provided IP address is not a valid IP address.')
     if mask:
         msk = self._convert_netmask(mask)
         network = '{0}/{1}'.format(item, msk)
         if is_valid_ip_network(u'{0}'.format(network)):
             return network
         else:
             raise F5ModuleError(
                 'The provided IP and Mask are not a valid IP network.'
             )
     host = ip_interface(u'{0}'.format(item))
     return host.with_prefixlen
Beispiel #12
0
 def ip(self):
     result = ip_interface(self.destination_ip)
     return str(result.ip)
Beispiel #13
0
 def netmask(self):
     ip = ip_interface(self.destination_ip)
     return int(ip.network.prefixlen)
def is_valid_ip_interface(address):
    try:
        ip_interface(u'{0}'.format(address))
        return True
    except ValueError:
        return False
Beispiel #15
0
def is_valid_ip_interface(address):
    try:
        ip_interface(address)
        return True
    except ValueError:
        return False