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

View file

@ -69,8 +69,6 @@ options:
choices: choices:
- absent - absent
- present - present
requirements:
- netaddr
extends_documentation_fragment: f5 extends_documentation_fragment: f5
author: author:
- Tim Rupp (@caphrim007) - 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 AnsibleF5Parameters
from library.module_utils.network.f5.common import cleanup_tokens 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.common import f5_argument_spec
from library.module_utils.network.f5.ipaddress import is_valid_ip
try: try:
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError: 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 AnsibleF5Parameters
from ansible.module_utils.network.f5.common import cleanup_tokens 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.common import f5_argument_spec
from ansible.module_utils.network.f5.ipaddress import is_valid_ip
try: try:
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError: except ImportError:
HAS_F5SDK = False HAS_F5SDK = False
try:
import netaddr
HAS_NETADDR = True
except ImportError:
HAS_NETADDR = False
class Parameters(AnsibleF5Parameters): class Parameters(AnsibleF5Parameters):
api_map = { api_map = {
@ -171,13 +165,11 @@ class Parameters(AnsibleF5Parameters):
def peer_server(self): def peer_server(self):
if self._values['peer_server'] is None: if self._values['peer_server'] is None:
return None return None
try: if is_valid_ip(self._values['peer_server']):
result = str(netaddr.IPAddress(self._values['peer_server'])) return self._values['peer_server']
return result raise F5ModuleError(
except netaddr.core.AddrFormatError: "The provided 'peer_server' parameter is not an IP address."
raise F5ModuleError( )
"The provided 'peer_server' parameter is not an IP address."
)
@property @property
def peer_hostname(self): def peer_hostname(self):
@ -334,8 +326,6 @@ def main():
) )
if not HAS_F5SDK: if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required") 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: try:
client = F5Client(**module.params) 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 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. - This parameter will be ignored if a type of either C(all) or C(at_least) is used.
version_added: 2.6 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 extends_documentation_fragment: f5
requirements:
- netaddr
author: author:
- Tim Rupp (@caphrim007) - 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 cleanup_tokens
from library.module_utils.network.f5.common import fq_name 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.common import f5_argument_spec
from library.module_utils.network.f5.ipaddress import is_valid_ip
try: try:
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
from f5.sdk_exception import LazyAttributesRequired 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 cleanup_tokens
from ansible.module_utils.network.f5.common import fq_name 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.common import f5_argument_spec
from ansible.module_utils.network.f5.ipaddress import is_valid_ip
try: try:
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
from f5.sdk_exception import LazyAttributesRequired from f5.sdk_exception import LazyAttributesRequired
except ImportError: except ImportError:
HAS_F5SDK = False HAS_F5SDK = False
try:
from netaddr import IPAddress, AddrFormatError
HAS_NETADDR = True
except ImportError:
HAS_NETADDR = False
class Parameters(AnsibleF5Parameters): class Parameters(AnsibleF5Parameters):
api_map = { api_map = {
@ -384,14 +375,9 @@ class Parameters(AnsibleF5Parameters):
return 'any' return 'any'
if self._values['fallback_ip'] == 'any6': if self._values['fallback_ip'] == 'any6':
return 'any6' return 'any6'
try: if is_valid_ip(self._values['fallback_ip']):
address = IPAddress(self._values['fallback_ip']) return self._values['fallback_ip']
if address.version == 4: else:
return str(address.ip)
elif address.version == 6:
return str(address.ip)
return None
except AddrFormatError:
raise F5ModuleError( raise F5ModuleError(
'The provided fallback address is not a valid IPv4 address' 'The provided fallback address is not a valid IPv4 address'
) )
@ -1123,8 +1109,6 @@ def main():
) )
if not HAS_F5SDK: if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required") 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: try:
client = F5Client(**module.params) 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 fq_name
from library.module_utils.network.f5.common import compare_dictionary 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.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: try:
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
from f5.sdk_exception import LazyAttributesRequired 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 fq_name
from ansible.module_utils.network.f5.common import compare_dictionary 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.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: try:
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
from f5.sdk_exception import LazyAttributesRequired from f5.sdk_exception import LazyAttributesRequired
except ImportError: except ImportError:
HAS_F5SDK = False HAS_F5SDK = False
try:
import netaddr
HAS_NETADDR = True
except ImportError:
HAS_NETADDR = False
class Parameters(AnsibleF5Parameters): class Parameters(AnsibleF5Parameters):
api_map = { api_map = {
@ -357,8 +355,11 @@ class ApiParameters(Parameters):
else: else:
# IPv4 # IPv4
parts = self._values['destination'].split(':') parts = self._values['destination'].split(':')
addr = netaddr.IPAddress(parts[0]) if is_valid_ip(parts[0]):
return str(addr) return str(parts[0])
raise F5ModuleError(
"'address' parameter from API was not an IP address."
)
@property @property
def port(self): def port(self):
@ -532,8 +533,11 @@ class ModuleParameters(Parameters):
def address(self): def address(self):
if self._values['address'] is None: if self._values['address'] is None:
return None return None
addr = netaddr.IPAddress(self._values['address']) if is_valid_ip(self._values['address']):
return str(addr) return self._values['address']
raise F5ModuleError(
"Specified 'address' is not an IP address."
)
@property @property
def port(self): def port(self):
@ -549,11 +553,10 @@ class ModuleParameters(Parameters):
return None return None
if self.port is None: if self.port is None:
return None return None
addr = netaddr.IPAddress(self.address) if validate_ip_v6_address(self.address):
if addr.version == 4:
result = '{0}:{1}'.format(self.address, self.port)
else:
result = '{0}.{1}'.format(self.address, self.port) result = '{0}.{1}'.format(self.address, self.port)
else:
result = '{0}:{1}'.format(self.address, self.port)
return result return result
@property @property
@ -1061,8 +1064,6 @@ def main():
) )
if not HAS_F5SDK: if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required") 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: try:
client = F5Client(**module.params) 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 cleanup_tokens
from library.module_utils.network.f5.common import fq_name 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.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: try:
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError: 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 cleanup_tokens
from ansible.module_utils.network.f5.common import fq_name 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.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: try:
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError: except ImportError:
HAS_F5SDK = False HAS_F5SDK = False
try:
import netaddr
HAS_NETADDR = True
except ImportError:
HAS_NETADDR = False
class Parameters(AnsibleF5Parameters): class Parameters(AnsibleF5Parameters):
api_map = { api_map = {
@ -159,9 +157,9 @@ class ModuleParameters(Parameters):
if self._values['network'] == 'default': if self._values['network'] == 'default':
return 'default' return 'default'
try: try:
addr = netaddr.IPNetwork(self._values['network']) addr = ip_network(u"{0}".format(str(self._values['network'])))
return str(addr) return str(addr)
except netaddr.core.AddrFormatError: except ValueError:
raise F5ModuleError( raise F5ModuleError(
"The 'network' must either be a network address (with CIDR) or the word 'default'." "The 'network' must either be a network address (with CIDR) or the word 'default'."
) )
@ -170,12 +168,11 @@ class ModuleParameters(Parameters):
def gateway(self): def gateway(self):
if self._values['gateway'] is None: if self._values['gateway'] is None:
return None return None
try: if is_valid_ip(self._values['gateway']):
addr = netaddr.IPNetwork(self._values['gateway']) return self._values['gateway']
return str(addr.ip) else:
except netaddr.core.AddrFormatError:
raise F5ModuleError( 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: if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required") 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: try:
client = F5Client(**module.params) client = F5Client(**module.params)

View file

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

View file

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

View file

@ -93,11 +93,6 @@ options:
- always - always
- on_create - on_create
extends_documentation_fragment: f5 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: author:
- Tim Rupp (@caphrim007) - 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 cleanup_tokens
from library.module_utils.network.f5.common import is_valid_hostname 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.common import f5_argument_spec
from library.module_utils.network.f5.ipaddress import is_valid_ip
try: try:
from library.module_utils.network.f5.common import iControlUnexpectedHTTPError from library.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError: 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 cleanup_tokens
from ansible.module_utils.network.f5.common import is_valid_hostname 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.common import f5_argument_spec
from ansible.module_utils.network.f5.ipaddress import is_valid_ip
try: try:
from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError from ansible.module_utils.network.f5.common import iControlUnexpectedHTTPError
except ImportError: except ImportError:
HAS_F5SDK = False HAS_F5SDK = False
try:
import netaddr
HAS_NETADDR = True
except ImportError:
HAS_NETADDR = False
class Parameters(AnsibleF5Parameters): class Parameters(AnsibleF5Parameters):
api_map = { api_map = {
@ -227,19 +218,16 @@ class ModuleParameters(Parameters):
def local_host_name(self): def local_host_name(self):
if self._values['local_host_name'] is None: if self._values['local_host_name'] is None:
return None return None
try: if is_valid_ip(self._values['local_host_name']):
# Check for valid IPv4 or IPv6 entries
netaddr.IPNetwork(self._values['local_host_name'])
return 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 # else fallback to checking reasonably well formatted hostnames
if is_valid_hostname(self._values['local_host_name']): return str(self._values['local_host_name'])
return str(self._values['local_host_name']) raise F5ModuleError(
raise F5ModuleError( "The provided 'local_host_name' value {0} is not a valid IP or hostname".format(
"The provided 'local_host_name' value {0} is not a valid IP or hostname".format( str(self._values['local_host_name'])
str(self._values['local_host_name'])
)
) )
)
@property @property
def authentication_enabled(self): def authentication_enabled(self):
@ -512,8 +500,6 @@ def main():
) )
if not HAS_F5SDK: if not HAS_F5SDK:
module.fail_json(msg="The python f5-sdk module is required") 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: try:
client = F5Client(**module.params) client = F5Client(**module.params)

View file

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