mirror of https://github.com/ansible/ansible.git
Updates F5 module utils (#43047)
New functions and fixes/refactorings for existing functions for the 2.7 workpull/43062/head
parent
1e2b332001
commit
867dedc787
@ -0,0 +1,77 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (c) 2018 F5 Networks Inc.
|
||||
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.module_utils.network.common.utils import validate_ip_address
|
||||
from ansible.module_utils.network.common.utils import validate_ip_v6_address
|
||||
|
||||
try:
|
||||
from library.module_utils.compat.ipaddress import ip_interface
|
||||
from library.module_utils.compat.ipaddress import ip_network
|
||||
except ImportError:
|
||||
from ansible.module_utils.compat.ipaddress import ip_interface
|
||||
from ansible.module_utils.compat.ipaddress import ip_network
|
||||
|
||||
|
||||
def is_valid_ip(addr, type='all'):
|
||||
if type in ['all', 'ipv4']:
|
||||
if validate_ip_address(addr):
|
||||
return True
|
||||
if type in ['all', 'ipv6']:
|
||||
if validate_ip_v6_address(addr):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def ipv6_netmask_to_cidr(mask):
|
||||
"""converts an IPv6 netmask to CIDR form
|
||||
|
||||
According to the link below, CIDR is the only official way to specify
|
||||
a subset of IPv6. With that said, the same link provides a way to
|
||||
loosely convert an netmask to a CIDR.
|
||||
|
||||
Arguments:
|
||||
mask (string): The IPv6 netmask to convert to CIDR
|
||||
|
||||
Returns:
|
||||
int: The CIDR representation of the netmask
|
||||
|
||||
References:
|
||||
https://stackoverflow.com/a/33533007
|
||||
http://v6decode.com/
|
||||
"""
|
||||
bit_masks = [
|
||||
0, 0x8000, 0xc000, 0xe000, 0xf000, 0xf800,
|
||||
0xfc00, 0xfe00, 0xff00, 0xff80, 0xffc0,
|
||||
0xffe0, 0xfff0, 0xfff8, 0xfffc, 0xfffe,
|
||||
0xffff
|
||||
]
|
||||
count = 0
|
||||
try:
|
||||
for w in mask.split(':'):
|
||||
if not w or int(w, 16) == 0:
|
||||
break
|
||||
count += bit_masks.index(int(w, 16))
|
||||
return count
|
||||
except:
|
||||
return -1
|
||||
|
||||
|
||||
def is_valid_ip_network(address):
|
||||
try:
|
||||
ip_network(address)
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
|
||||
def is_valid_ip_interface(address):
|
||||
try:
|
||||
ip_interface(address)
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
Loading…
Reference in New Issue