Removes f5-sdk code from bigip modules (#44595)

This patch is part of an ongoing effort to fully remove the f5sdk
from the f5 modules.
This commit is contained in:
Tim Rupp 2018-08-23 16:53:34 -04:00 committed by GitHub
parent 4bea004a4e
commit 9c12d845ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 149 additions and 203 deletions

View file

@ -267,6 +267,12 @@ try:
from library.module_utils.network.f5.common import cleanup_tokens
from library.module_utils.network.f5.common import compare_dictionary
from library.module_utils.network.f5.common import f5_argument_spec
from library.module_utils.network.f5.ipaddress import is_valid_ip
from library.module_utils.network.f5.ipaddress import is_valid_ip_network
from library.module_utils.network.f5.ipaddress import is_valid_ip_interface
from library.module_utils.compat.ipaddress import ip_address
from library.module_utils.compat.ipaddress import ip_network
from library.module_utils.compat.ipaddress import ip_interface
try:
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError:
@ -279,17 +285,17 @@ except ImportError:
from ansible.module_utils.network.f5.common import cleanup_tokens
from ansible.module_utils.network.f5.common import compare_dictionary
from ansible.module_utils.network.f5.common import f5_argument_spec
from ansible.module_utils.network.f5.ipaddress import is_valid_ip
from ansible.module_utils.network.f5.ipaddress import is_valid_ip_network
from ansible.module_utils.network.f5.ipaddress import is_valid_ip_interface
from ansible.module_utils.compat.ipaddress import ip_address
from ansible.module_utils.compat.ipaddress import ip_network
from ansible.module_utils.compat.ipaddress import ip_interface
try:
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError:
HAS_F5SDK = False
try:
import netaddr
HAS_NETADDR = True
except ImportError:
HAS_NETADDR = False
LINE_LIMIT = 65000
SIZE_LIMIT_BYTES = 4000000
@ -345,22 +351,33 @@ class RecordsEncoder(object):
return self.encode_string_from_dict(record)
def encode_address_from_dict(self, record):
try:
key = netaddr.IPNetwork(record['key'])
except netaddr.core.AddrFormatError:
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:
if key.prefixlen in [32, 128]:
return self.encode_host(key.ip, record['value'])
else:
return self.encode_network(key.network, key.prefixlen, record['value'])
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:
if key.prefixlen in [32, 128]:
return self.encode_host(key.ip, key.ip)
else:
return self.encode_network(key.network, key.prefixlen, key.network)
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_integer_from_dict(self, record):
try:
@ -401,25 +418,38 @@ class RecordsEncoder(object):
else:
# 192.168.0.0/16 := "Network3",
# 2402:9400:1000:0::/64 := "Network4",
try:
parts = record.split(self._separator)
if len(parts) == 2:
key = netaddr.IPNetwork(parts[0])
if key.prefixlen in [32, 128]:
return self.encode_host(key.ip, parts[1])
if is_valid_ip_network(parts[0]):
key = ip_network(u"{0}".format(str(parts[0])))
elif is_valid_ip(parts[0]):
key = ip_address(u"{0}".format(str(parts[0])))
elif is_valid_ip_interface(parts[0]):
key = ip_interface(u"{0}".format(str(parts[0])))
elif parts[0] == '':
pass
else:
return self.encode_network(key.network, key.prefixlen, parts[1])
elif len(parts) == 1 and parts[0] != '':
key = netaddr.IPNetwork(parts[0])
if key.prefixlen in [32, 128]:
return self.encode_host(key.ip, key.ip)
else:
return self.encode_network(key.network, key.prefixlen, key.network)
except netaddr.core.AddrFormatError:
raise F5ModuleError(
"When specifying an 'address' type, the value to the left of the separator must be an IP."
)
if len(parts) == 2:
try:
# Only ip_address's have max_prefixlen
if key.max_prefixlen in [32, 128]:
return self.encode_host(str(key), parts[1])
except ValueError:
return self.encode_network(
str(key.network_address), key.prefixlen, parts[1])
elif len(parts) == 1 and parts[0] != '':
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_host(self, key, value):
return 'host {0} {1} {2}'.format(str(key), self._separator, str(value))
@ -466,13 +496,12 @@ class RecordsDecoder(object):
return self.decode_from_string(record)
def decode_address_from_string(self, record):
try:
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 = "{0}/{1}".format(matches.group('addr'), matches.group('prefix'))
addr = netaddr.IPNetwork(key)
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
@ -481,11 +510,10 @@ class RecordsDecoder(object):
# host 172.16.1.1/32 := "Host3"
# host 2001:0db8:85a3:0000:0000:8a2e:0370:7334 := "Host4"
key = matches.group('addr')
addr = netaddr.IPNetwork(key)
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
except netaddr.core.AddrFormatError:
raise F5ModuleError(
'The value "{0}" is not an address'.format(record)
)
@ -589,7 +617,7 @@ class ModuleParameters(Parameters):
data = records.read(4096)
if not data:
break
result.update(data)
result.update(data.encode('utf-8'))
result = result.hexdigest()
self._values['checksum'] = result
return result
@ -600,7 +628,7 @@ class ModuleParameters(Parameters):
name = self.name
else:
name = self._values['external_file_name']
if re.search(r'[^a-z0-9-_.]', name):
if re.search(r'[^a-zA-Z0-9-_.]', name):
raise F5ModuleError(
"'external_file_name' may only contain letters, numbers, underscores, dashes, or a period."
)
@ -1051,8 +1079,6 @@ def main():
)
if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required")
if not HAS_NETADDR:
module.fail_json(msg="The python netaddr module is required")
try:
client = F5Client(**module.params)

View file

@ -69,8 +69,6 @@ options:
choices:
- absent
- present
requirements:
- netaddr
extends_documentation_fragment: f5
author:
- Tim Rupp (@caphrim007)
@ -115,6 +113,7 @@ try:
from library.module_utils.network.f5.common import AnsibleF5Parameters
from library.module_utils.network.f5.common import cleanup_tokens
from library.module_utils.network.f5.common import f5_argument_spec
from library.module_utils.network.f5.ipaddress import is_valid_ip
try:
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError:
@ -126,17 +125,12 @@ except ImportError:
from ansible.module_utils.network.f5.common import AnsibleF5Parameters
from ansible.module_utils.network.f5.common import cleanup_tokens
from ansible.module_utils.network.f5.common import f5_argument_spec
from ansible.module_utils.network.f5.ipaddress import is_valid_ip
try:
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError:
HAS_F5SDK = False
try:
import netaddr
HAS_NETADDR = True
except ImportError:
HAS_NETADDR = False
class Parameters(AnsibleF5Parameters):
api_map = {
@ -171,10 +165,8 @@ class Parameters(AnsibleF5Parameters):
def peer_server(self):
if self._values['peer_server'] is None:
return None
try:
result = str(netaddr.IPAddress(self._values['peer_server']))
return result
except netaddr.core.AddrFormatError:
if is_valid_ip(self._values['peer_server']):
return self._values['peer_server']
raise F5ModuleError(
"The provided 'peer_server' parameter is not an IP address."
)
@ -334,8 +326,6 @@ def main():
)
if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required")
if not HAS_NETADDR:
module.fail_json(msg="The python netaddr module is required")
try:
client = F5Client(**module.params)

View file

@ -177,12 +177,7 @@ options:
- This parameter is only relevant when a C(type) of C(require) is used.
- This parameter will be ignored if a type of either C(all) or C(at_least) is used.
version_added: 2.6
notes:
- Requires the netaddr Python package on the host. This is as easy as
pip install netaddr.
extends_documentation_fragment: f5
requirements:
- netaddr
author:
- Tim Rupp (@caphrim007)
'''
@ -262,6 +257,7 @@ try:
from library.module_utils.network.f5.common import cleanup_tokens
from library.module_utils.network.f5.common import fq_name
from library.module_utils.network.f5.common import f5_argument_spec
from library.module_utils.network.f5.ipaddress import is_valid_ip
try:
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
from f5.sdk_exception import LazyAttributesRequired
@ -275,18 +271,13 @@ except ImportError:
from ansible.module_utils.network.f5.common import cleanup_tokens
from ansible.module_utils.network.f5.common import fq_name
from ansible.module_utils.network.f5.common import f5_argument_spec
from ansible.module_utils.network.f5.ipaddress import is_valid_ip
try:
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
from f5.sdk_exception import LazyAttributesRequired
except ImportError:
HAS_F5SDK = False
try:
from netaddr import IPAddress, AddrFormatError
HAS_NETADDR = True
except ImportError:
HAS_NETADDR = False
class Parameters(AnsibleF5Parameters):
api_map = {
@ -384,14 +375,9 @@ class Parameters(AnsibleF5Parameters):
return 'any'
if self._values['fallback_ip'] == 'any6':
return 'any6'
try:
address = IPAddress(self._values['fallback_ip'])
if address.version == 4:
return str(address.ip)
elif address.version == 6:
return str(address.ip)
return None
except AddrFormatError:
if is_valid_ip(self._values['fallback_ip']):
return self._values['fallback_ip']
else:
raise F5ModuleError(
'The provided fallback address is not a valid IPv4 address'
)
@ -1123,8 +1109,6 @@ def main():
)
if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required")
if not HAS_NETADDR:
module.fail_json(msg="The python netaddr module is required")
try:
client = F5Client(**module.params)

View file

@ -252,6 +252,8 @@ try:
from library.module_utils.network.f5.common import fq_name
from library.module_utils.network.f5.common import compare_dictionary
from library.module_utils.network.f5.common import f5_argument_spec
from library.module_utils.network.f5.ipaddress import is_valid_ip
from library.module_utils.network.f5.ipaddress import validate_ip_v6_address
try:
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
from f5.sdk_exception import LazyAttributesRequired
@ -266,18 +268,14 @@ except ImportError:
from ansible.module_utils.network.f5.common import fq_name
from ansible.module_utils.network.f5.common import compare_dictionary
from ansible.module_utils.network.f5.common import f5_argument_spec
from ansible.module_utils.network.f5.ipaddress import is_valid_ip
from ansible.module_utils.network.f5.ipaddress import validate_ip_v6_address
try:
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
from f5.sdk_exception import LazyAttributesRequired
except ImportError:
HAS_F5SDK = False
try:
import netaddr
HAS_NETADDR = True
except ImportError:
HAS_NETADDR = False
class Parameters(AnsibleF5Parameters):
api_map = {
@ -357,8 +355,11 @@ class ApiParameters(Parameters):
else:
# IPv4
parts = self._values['destination'].split(':')
addr = netaddr.IPAddress(parts[0])
return str(addr)
if is_valid_ip(parts[0]):
return str(parts[0])
raise F5ModuleError(
"'address' parameter from API was not an IP address."
)
@property
def port(self):
@ -532,8 +533,11 @@ class ModuleParameters(Parameters):
def address(self):
if self._values['address'] is None:
return None
addr = netaddr.IPAddress(self._values['address'])
return str(addr)
if is_valid_ip(self._values['address']):
return self._values['address']
raise F5ModuleError(
"Specified 'address' is not an IP address."
)
@property
def port(self):
@ -549,11 +553,10 @@ class ModuleParameters(Parameters):
return None
if self.port is None:
return None
addr = netaddr.IPAddress(self.address)
if addr.version == 4:
result = '{0}:{1}'.format(self.address, self.port)
else:
if validate_ip_v6_address(self.address):
result = '{0}.{1}'.format(self.address, self.port)
else:
result = '{0}:{1}'.format(self.address, self.port)
return result
@property
@ -1061,8 +1064,6 @@ def main():
)
if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required")
if not HAS_NETADDR:
module.fail_json(msg="The python netaddr module is required")
try:
client = F5Client(**module.params)

View file

@ -99,6 +99,8 @@ try:
from library.module_utils.network.f5.common import cleanup_tokens
from library.module_utils.network.f5.common import fq_name
from library.module_utils.network.f5.common import f5_argument_spec
from library.module_utils.network.f5.ipaddress import is_valid_ip
from library.module_utils.compat.ipaddress import ip_network
try:
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError:
@ -111,17 +113,13 @@ except ImportError:
from ansible.module_utils.network.f5.common import cleanup_tokens
from ansible.module_utils.network.f5.common import fq_name
from ansible.module_utils.network.f5.common import f5_argument_spec
from ansible.module_utils.network.f5.ipaddress import is_valid_ip
from ansible.module_utils.compat.ipaddress import ip_network
try:
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError:
HAS_F5SDK = False
try:
import netaddr
HAS_NETADDR = True
except ImportError:
HAS_NETADDR = False
class Parameters(AnsibleF5Parameters):
api_map = {
@ -159,9 +157,9 @@ class ModuleParameters(Parameters):
if self._values['network'] == 'default':
return 'default'
try:
addr = netaddr.IPNetwork(self._values['network'])
addr = ip_network(u"{0}".format(str(self._values['network'])))
return str(addr)
except netaddr.core.AddrFormatError:
except ValueError:
raise F5ModuleError(
"The 'network' must either be a network address (with CIDR) or the word 'default'."
)
@ -170,12 +168,11 @@ class ModuleParameters(Parameters):
def gateway(self):
if self._values['gateway'] is None:
return None
try:
addr = netaddr.IPNetwork(self._values['gateway'])
return str(addr.ip)
except netaddr.core.AddrFormatError:
if is_valid_ip(self._values['gateway']):
return self._values['gateway']
else:
raise F5ModuleError(
"The 'gateway' must either be an IP address."
"The 'gateway' must an IP address."
)
@ -402,8 +399,6 @@ def main():
)
if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required")
if not HAS_NETADDR:
module.fail_json(msg="The python netaddr module is required")
try:
client = F5Client(**module.params)

View file

@ -144,7 +144,6 @@ options:
- minimum_active_members
version_added: 2.6
notes:
- Requires BIG-IP software version >= 12.
- To add members do a pool, use the C(bigip_pool_member) module. Previously, the
C(bigip_pool) module allowed the management of users, but this has been removed
in version 2.5 of Ansible.
@ -364,12 +363,6 @@ except ImportError:
except ImportError:
HAS_F5SDK = False
try:
from netaddr import IPAddress, AddrFormatError
HAS_NETADDR = True
except ImportError:
HAS_NETADDR = False
class Parameters(AnsibleF5Parameters):
api_map = {
@ -947,8 +940,6 @@ def main():
)
if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required")
if not HAS_NETADDR:
module.fail_json(msg="The python netaddr module is required")
try:
client = F5Client(**module.params)

View file

@ -45,12 +45,7 @@ options:
choices:
- absent
- present
notes:
- Requires the netaddr Python package on the host. This is as easy as pip
install netaddr.
extends_documentation_fragment: f5
requirements:
- netaddr
author:
- Tim Rupp (@caphrim007)
'''
@ -101,6 +96,7 @@ try:
from library.module_utils.network.f5.common import AnsibleF5Parameters
from library.module_utils.network.f5.common import cleanup_tokens
from library.module_utils.network.f5.common import f5_argument_spec
from library.module_utils.network.f5.ipaddress import is_valid_ip
try:
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError:
@ -112,17 +108,12 @@ except ImportError:
from ansible.module_utils.network.f5.common import AnsibleF5Parameters
from ansible.module_utils.network.f5.common import cleanup_tokens
from ansible.module_utils.network.f5.common import f5_argument_spec
from ansible.module_utils.network.f5.ipaddress import is_valid_ip
try:
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError:
HAS_F5SDK = False
try:
import netaddr
HAS_NETADDR = True
except ImportError:
HAS_NETADDR = False
class Parameters(AnsibleF5Parameters):
updatables = [
@ -146,13 +137,9 @@ class Parameters(AnsibleF5Parameters):
@property
def remote_host(self):
try:
# Check for valid IPv4 or IPv6 entries
netaddr.IPAddress(self._values['remote_host'])
if is_valid_ip(self._values['remote_host']):
return self._values['remote_host']
except netaddr.core.AddrFormatError:
# else fallback to checking reasonably well formatted hostnames
if self.is_valid_hostname(self._values['remote_host']):
elif self.is_valid_hostname(self._values['remote_host']):
return str(self._values['remote_host'])
raise F5ModuleError(
"The provided 'remote_host' is not a valid IP or hostname"
@ -195,10 +182,9 @@ class Parameters(AnsibleF5Parameters):
def local_ip(self):
if self._values['local_ip'] in [None, 'none']:
return None
try:
ip = netaddr.IPAddress(self._values['local_ip'])
return str(ip)
except netaddr.core.AddrFormatError:
if is_valid_ip(self._values['local_ip']):
return self._values['local_ip']
else:
raise F5ModuleError(
"The provided 'local_ip' is not a valid IP address"
)
@ -422,7 +408,7 @@ class ModuleManager(object):
def read_current_from_device(self):
resource = self.client.api.tm.sys.syslog.load()
attrs = resource.attrs
result = Parameters(attrs)
result = Parameters(params=attrs)
return result
def absent(self):
@ -473,8 +459,6 @@ def main():
)
if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required")
if not HAS_NETADDR:
module.fail_json(msg="The python netaddr module is required")
try:
client = F5Client(**module.params)

View file

@ -93,11 +93,6 @@ options:
- always
- on_create
extends_documentation_fragment: f5
notes:
- Requires the netaddr Python package on the host. This is as easy as
C(pip install netaddr).
requirements:
- netaddr
author:
- Tim Rupp (@caphrim007)
'''
@ -162,6 +157,7 @@ try:
from library.module_utils.network.f5.common import cleanup_tokens
from library.module_utils.network.f5.common import is_valid_hostname
from library.module_utils.network.f5.common import f5_argument_spec
from library.module_utils.network.f5.ipaddress import is_valid_ip
try:
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError:
@ -174,17 +170,12 @@ except ImportError:
from ansible.module_utils.network.f5.common import cleanup_tokens
from ansible.module_utils.network.f5.common import is_valid_hostname
from ansible.module_utils.network.f5.common import f5_argument_spec
from ansible.module_utils.network.f5.ipaddress import is_valid_ip
try:
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError:
HAS_F5SDK = False
try:
import netaddr
HAS_NETADDR = True
except ImportError:
HAS_NETADDR = False
class Parameters(AnsibleF5Parameters):
api_map = {
@ -227,13 +218,10 @@ class ModuleParameters(Parameters):
def local_host_name(self):
if self._values['local_host_name'] is None:
return None
try:
# Check for valid IPv4 or IPv6 entries
netaddr.IPNetwork(self._values['local_host_name'])
if is_valid_ip(self._values['local_host_name']):
return self._values['local_host_name']
except netaddr.core.AddrFormatError:
elif is_valid_hostname(self._values['local_host_name']):
# else fallback to checking reasonably well formatted hostnames
if is_valid_hostname(self._values['local_host_name']):
return str(self._values['local_host_name'])
raise F5ModuleError(
"The provided 'local_host_name' value {0} is not a valid IP or hostname".format(
@ -512,8 +500,6 @@ def main():
)
if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required")
if not HAS_NETADDR:
module.fail_json(msg="The python netaddr module is required")
try:
client = F5Client(**module.params)

View file

@ -61,11 +61,6 @@ options:
description:
- Specifies the description of this system's physical location.
extends_documentation_fragment: f5
notes:
- Requires the netaddr Python package on the host. This is as easy as
C(pip install netaddr).
requirements:
- netaddr
author:
- Tim Rupp (@caphrim007)
'''
@ -134,6 +129,7 @@ try:
from library.module_utils.network.f5.common import cleanup_tokens
from library.module_utils.network.f5.common import is_valid_hostname
from library.module_utils.network.f5.common import f5_argument_spec
from library.module_utils.compat.ipaddress import ip_network
try:
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
@ -147,18 +143,13 @@ except ImportError:
from ansible.module_utils.network.f5.common import cleanup_tokens
from ansible.module_utils.network.f5.common import is_valid_hostname
from ansible.module_utils.network.f5.common import f5_argument_spec
from ansible.module_utils.compat.ipaddress import ip_network
try:
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError:
HAS_F5SDK = False
try:
import netaddr
HAS_NETADDR = True
except ImportError:
HAS_NETADDR = False
class Parameters(AnsibleF5Parameters):
api_map = {
@ -221,9 +212,9 @@ class ModuleParameters(Parameters):
for address in addresses:
try:
# Check for valid IPv4 or IPv6 entries
netaddr.IPNetwork(address)
ip_network(u'%s' % str(address))
result.append(address)
except netaddr.core.AddrFormatError:
except ValueError:
# else fallback to checking reasonably well formatted hostnames
if is_valid_hostname(address):
result.append(str(address))
@ -392,8 +383,6 @@ def main():
)
if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required")
if not HAS_NETADDR:
module.fail_json(msg="The python netaddr module is required")
try:
client = F5Client(**module.params)