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 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))
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) )
def addresses(self): if self._values['addresses'] is None: return None for x in self._values['addresses']: if is_valid_ip(x) or is_valid_ip_network( x) or is_valid_ip_interface(x): continue else: raise F5ModuleError( "Address {0} must be either an IPv4 or IPv6 address or network." .format(x)) result = [str(x) for x in self._values['addresses']] result = sorted(result) return result
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