| 1 |
# This code is part of Ansible, but is an independent component. |
| 2 |
# This particular file snippet, and this file snippet only, is BSD licensed. |
| 3 |
# Modules you write using this snippet, which is embedded dynamically by Ansible |
| 4 |
# still belong to the author of the module, and may assign their own license |
| 5 |
# to the complete work. |
| 6 |
# |
| 7 |
# Copyright (c) 2020-2022, Laurent Nicolas <[email protected]> |
| 8 |
# All rights reserved. |
| 9 |
# |
| 10 |
# Redistribution and use in source and binary forms, with or without modification, |
| 11 |
# are permitted provided that the following conditions are met: |
| 12 |
# |
| 13 |
# * Redistributions of source code must retain the above copyright |
| 14 |
# notice, this list of conditions and the following disclaimer. |
| 15 |
# * Redistributions in binary form must reproduce the above copyright notice, |
| 16 |
# this list of conditions and the following disclaimer in the documentation |
| 17 |
# and/or other materials provided with the distribution. |
| 18 |
# |
| 19 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 |
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 27 |
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
|
| 29 |
""" Support class for NetApp ansible modules |
| 30 |
|
| 31 |
Provides accesss to ipaddress - mediating unicode issues with python2.7 |
| 32 |
""" |
| 33 |
|
| 34 |
from __future__ import (absolute_import, division, print_function) |
| 35 |
__metaclass__ = type |
| 36 |
|
| 37 |
from ansible.module_utils._text import to_native |
| 38 |
|
| 39 |
try: |
| 40 |
import ipaddress |
| 41 |
HAS_IPADDRESS_LIB = True |
| 42 |
IMPORT_ERROR = None |
| 43 |
except ImportError as exc: |
| 44 |
HAS_IPADDRESS_LIB = False |
| 45 |
IMPORT_ERROR = to_native(exc) |
| 46 |
|
| 47 |
|
| 48 |
def _check_ipaddress_is_present(module): |
| 49 |
''' |
| 50 |
report error at runtime rather than when attempting to load the module |
| 51 |
''' |
| 52 |
if HAS_IPADDRESS_LIB: |
| 53 |
return None |
| 54 |
module.fail_json(msg="Error: the python ipaddress package is required for this module. Import error: %s" % IMPORT_ERROR) |
| 55 |
|
| 56 |
|
| 57 |
def _get_ipv4orv6_address(ip_address, module): |
| 58 |
''' |
| 59 |
return IPV4Adress or IPV6Address object |
| 60 |
''' |
| 61 |
_check_ipaddress_is_present(module) |
| 62 |
# python 2.7 requires unicode format |
| 63 |
ip_addr = u'%s' % ip_address |
| 64 |
try: |
| 65 |
return ipaddress.ip_address(ip_addr) |
| 66 |
except ValueError as exc: |
| 67 |
error = 'Error: Invalid IP address value %s - %s' % (ip_address, to_native(exc)) |
| 68 |
module.fail_json(msg=error) |
| 69 |
|
| 70 |
|
| 71 |
def _get_ipv4orv6_network(ip_address, netmask, strict, module): |
| 72 |
''' |
| 73 |
return IPV4Network or IPV6Network object |
| 74 |
''' |
| 75 |
_check_ipaddress_is_present(module) |
| 76 |
# python 2.7 requires unicode format |
| 77 |
ip_addr = u'%s/%s' % (ip_address, netmask) if netmask is not None else u'%s' % ip_address |
| 78 |
try: |
| 79 |
return ipaddress.ip_network(ip_addr, strict) |
| 80 |
except ValueError as exc: |
| 81 |
error = 'Error: Invalid IP network value %s' % ip_addr |
| 82 |
if 'has host bits set' in to_native(exc): |
| 83 |
error += '. Please specify a network address without host bits set' |
| 84 |
elif netmask is not None: |
| 85 |
error += '. Check address and netmask values' |
| 86 |
error += ': %s.' % to_native(exc) |
| 87 |
module.fail_json(msg=error) |
| 88 |
|
| 89 |
|
| 90 |
def _check_ipv6_has_prefix_length(ip_address, netmask, module): |
| 91 |
ip_address = _get_ipv4orv6_address(ip_address, module) |
| 92 |
if not isinstance(ip_address, ipaddress.IPv6Address) or isinstance(netmask, int): |
| 93 |
return |
| 94 |
if ':' in netmask: |
| 95 |
module.fail_json(msg='Error: only prefix_len is supported for IPv6 addresses, got %s' % netmask) |
| 96 |
|
| 97 |
|
| 98 |
def validate_ip_address_is_network_address(ip_address, module): |
| 99 |
''' |
| 100 |
Validate if the given IP address is a network address (i.e. it's host bits are set to 0) |
| 101 |
ONTAP doesn't validate if the host bits are set, |
| 102 |
and hence doesn't add a new address unless the IP is from a different network. |
| 103 |
So this validation allows the module to be idempotent. |
| 104 |
:return: None |
| 105 |
''' |
| 106 |
dummy = _get_ipv4orv6_network(ip_address, None, True, module) |
| 107 |
|
| 108 |
|
| 109 |
def validate_and_compress_ip_address(ip_address, module): |
| 110 |
''' |
| 111 |
0's in IPv6 addresses can be compressed to save space |
| 112 |
This will be a noop for IPv4 address |
| 113 |
In addition, it makes sure the address is in a valid format |
| 114 |
''' |
| 115 |
# return compressed value for IPv6 and value in . notation for IPv4 |
| 116 |
return str(_get_ipv4orv6_address(ip_address, module)) |
| 117 |
|
| 118 |
|
| 119 |
def netmask_length_to_netmask(ip_address, length, module): |
| 120 |
''' |
| 121 |
input: ip_address and netmask length |
| 122 |
output: netmask in dot notation |
| 123 |
''' |
| 124 |
return str(_get_ipv4orv6_network(ip_address, length, False, module).netmask) |
| 125 |
|
| 126 |
|
| 127 |
def netmask_to_netmask_length(ip_address, netmask, module): |
| 128 |
''' |
| 129 |
input: ip_address and netmask in dot notation for IPv4, expanded netmask is not supported for IPv6 |
| 130 |
netmask as int or a str representaiton of int is also accepted |
| 131 |
output: netmask length as int |
| 132 |
''' |
| 133 |
_check_ipv6_has_prefix_length(ip_address, netmask, module) |
| 134 |
return _get_ipv4orv6_network(ip_address, netmask, False, module).prefixlen |