Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrects downstream bugs in static-route module #44775

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 35 additions & 13 deletions lib/ansible/modules/network/f5/bigip_static_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@
from library.module_utils.network.f5.common import transform_name
from library.module_utils.network.f5.common import exit_json
from library.module_utils.network.f5.common import fail_json
from library.module_utils.network.f5.ipaddress import is_valid_ip
from library.module_utils.network.f5.ipaddress import ipv6_netmask_to_cidr
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
except ImportError:
from ansible.module_utils.network.f5.bigip import F5RestClient
from ansible.module_utils.network.f5.common import F5ModuleError
Expand All @@ -173,7 +177,11 @@
from ansible.module_utils.network.f5.common import transform_name
from ansible.module_utils.network.f5.common import exit_json
from ansible.module_utils.network.f5.common import fail_json
from ansible.module_utils.network.f5.ipaddress import is_valid_ip
from ansible.module_utils.network.f5.ipaddress import ipv6_netmask_to_cidr
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


class Parameters(AnsibleF5Parameters):
Expand Down Expand Up @@ -264,7 +272,7 @@ def destination(self):
if self._values['destination'].startswith('default'):
self._values['destination'] = '0.0.0.0/0'
if self._values['destination'].startswith('default-inet6'):
self._values['destination'] = '::/::'
self._values['destination'] = '::/0'
try:
ip = ip_network(u'%s' % str(self.destination_ip))
if self.route_domain:
Expand All @@ -286,22 +294,36 @@ def destination_ip(self):
def netmask(self):
if self._values['netmask'] is None:
return None
# Check if numeric
try:
result = int(self._values['netmask'])
if 0 <= result < 256:

# CIDRs between 0 and 128 are allowed
if 0 <= result <= 128:
return result
else:
raise F5ModuleError(
"The provided netmask must be between 0 and 32 for IPv4, or "
"0 and 128 for IPv6."
)
except ValueError:
# not a number, but that's ok. Further processing necessary
pass

if not is_valid_ip(self._values['netmask']):
raise F5ModuleError(
'The provided netmask {0} is neither in IP or CIDR format'.format(result)
)
except ValueError:
try:
ip = ip_network(u'%s' % str(self._values['netmask']))
except ValueError:
raise F5ModuleError(
'The provided netmask {0} is neither in IP or CIDR format'.format(self._values['netmask'])
)
result = int(ip.prefixlen)

# Create a temporary address to check if the netmask IP is v4 or v6
addr = ip_address(u'{0}'.format(str(self._values['netmask'])))
if addr.version == 4:
# Create a more real v4 address using a wildcard, so that we can determine
# the CIDR value from it.
ip = ip_network(u'0.0.0.0/%s' % str(self._values['netmask']))
result = ip.prefixlen
else:
result = ipv6_netmask_to_cidr(self._values['netmask'])

return result


Expand Down Expand Up @@ -343,9 +365,9 @@ def destination_to_network(self):
if destination.startswith('default%'):
destination = '0.0.0.0%{0}/0'.format(destination.split('%')[1])
elif destination.startswith('default-inet6%'):
destination = '::%{0}/::'.format(destination.split('%')[1])
destination = '::%{0}/0'.format(destination.split('%')[1])
elif destination.startswith('default-inet6'):
destination = '::/::'
destination = '::/0'
elif destination.startswith('default'):
destination = '0.0.0.0/0'
return destination
Expand Down