Adds hostname to remote-syslog (#33496)

* Adds hostname to remote-syslog

Previously, only an IP address wa allowed. This removes that restriction

* Fixes upstream errors
pull/33497/head
Tim Rupp 7 years ago committed by GitHub
parent a4aa00f556
commit 798de98b0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -43,6 +43,7 @@ notes:
extends_documentation_fragment: f5 extends_documentation_fragment: f5
requirements: requirements:
- f5-sdk >= 2.2.0 - f5-sdk >= 2.2.0
- netaddr
author: author:
- Tim Rupp (@caphrim007) - Tim Rupp (@caphrim007)
''' '''
@ -81,6 +82,8 @@ local_ip:
sample: 10.10.10.10 sample: 10.10.10.10
''' '''
import re
try: try:
import netaddr import netaddr
HAS_NETADDR = True HAS_NETADDR = True
@ -163,13 +166,40 @@ class Parameters(AnsibleF5Parameters):
@property @property
def remote_host(self): def remote_host(self):
try: try:
ip = netaddr.IPAddress(self._values['remote_host']) # Check for valid IPv4 or IPv6 entries
return str(ip) netaddr.IPAddress(self._values['remote_host'])
return self._values['remote_host']
except netaddr.core.AddrFormatError: except netaddr.core.AddrFormatError:
# else fallback to checking reasonably well formatted hostnames
if self.is_valid_hostname(self._values['remote_host']):
return str(self._values['remote_host'])
raise F5ModuleError( raise F5ModuleError(
"The provided 'remote_host' is not a valid IP address" "The provided 'remote_host' is not a valid IP or hostname"
) )
def is_valid_hostname(self, host):
"""Reasonable attempt at validating a hostname
Compiled from various paragraphs outlined here
https://tools.ietf.org/html/rfc3696#section-2
https://tools.ietf.org/html/rfc1123
Notably,
* Host software MUST handle host names of up to 63 characters and
SHOULD handle host names of up to 255 characters.
* The "LDH rule", after the characters that it permits. (letters, digits, hyphen)
* If the hyphen is used, it is not permitted to appear at
either the beginning or end of a label
:param host:
:return:
"""
if len(host) > 255:
return False
host = host.rstrip(".")
allowed = re.compile(r'(?!-)[A-Z0-9-]{1,63}(?<!-)$', re.IGNORECASE)
return all(allowed.match(x) for x in host.split("."))
@property @property
def remote_port(self): def remote_port(self):
if self._values['remote_port'] is None: if self._values['remote_port'] is None:

@ -15,11 +15,9 @@ if sys.version_info < (2, 7):
raise SkipTest("F5 Ansible modules require Python >= 2.7") raise SkipTest("F5 Ansible modules require Python >= 2.7")
from ansible.compat.tests import unittest from ansible.compat.tests import unittest
from ansible.compat.tests.mock import patch, Mock from ansible.compat.tests.mock import Mock
from ansible.module_utils import basic from ansible.compat.tests.mock import patch
from ansible.module_utils._text import to_bytes
from ansible.module_utils.f5_utils import AnsibleF5Client from ansible.module_utils.f5_utils import AnsibleF5Client
from units.modules.utils import set_module_args
try: try:
from library.bigip_remote_syslog import Parameters from library.bigip_remote_syslog import Parameters
@ -28,6 +26,7 @@ try:
from library.bigip_remote_syslog import HAS_F5SDK from library.bigip_remote_syslog import HAS_F5SDK
from library.bigip_remote_syslog import HAS_NETADDR from library.bigip_remote_syslog import HAS_NETADDR
from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError
from test.unit.modules.utils import set_module_args
except ImportError: except ImportError:
try: try:
from ansible.modules.network.f5.bigip_remote_syslog import Parameters from ansible.modules.network.f5.bigip_remote_syslog import Parameters
@ -36,6 +35,7 @@ except ImportError:
from ansible.modules.network.f5.bigip_remote_syslog import HAS_F5SDK from ansible.modules.network.f5.bigip_remote_syslog import HAS_F5SDK
from ansible.modules.network.f5.bigip_remote_syslog import HAS_NETADDR from ansible.modules.network.f5.bigip_remote_syslog import HAS_NETADDR
from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError
from units.modules.utils import set_module_args
except ImportError: except ImportError:
raise SkipTest("F5 Ansible modules require the f5-sdk Python library") raise SkipTest("F5 Ansible modules require the f5-sdk Python library")

Loading…
Cancel
Save