コード例 #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])))
コード例 #2
0
 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")
コード例 #3
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.")
コード例 #4
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")
コード例 #5
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
コード例 #6
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
コード例 #7
0
 def extract_primary_ip6(self, host):
     try:
         address = host["primary_ip6"]["address"]
         return str(ip_interface(address).ip)
     except Exception:
         return
コード例 #8
0
 def ip(self):
     result = ip_interface(self.destination_ip)
     return str(result.ip)
コード例 #9
0
 def netmask(self):
     ip = ip_interface(self.destination_ip)
     return int(ip.network.prefixlen)
コード例 #10
0
def is_valid_ip_interface(address):
    try:
        ip_interface(u'{0}'.format(address))
        return True
    except ValueError:
        return False