1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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
This commit is contained in:
Tim Rupp 2017-12-02 21:03:19 -08:00 committed by GitHub
parent a4aa00f556
commit 798de98b0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 7 deletions

View file

@ -43,6 +43,7 @@ notes:
extends_documentation_fragment: f5
requirements:
- f5-sdk >= 2.2.0
- netaddr
author:
- Tim Rupp (@caphrim007)
'''
@ -81,6 +82,8 @@ local_ip:
sample: 10.10.10.10
'''
import re
try:
import netaddr
HAS_NETADDR = True
@ -163,13 +166,40 @@ class Parameters(AnsibleF5Parameters):
@property
def remote_host(self):
try:
ip = netaddr.IPAddress(self._values['remote_host'])
return str(ip)
# Check for valid IPv4 or IPv6 entries
netaddr.IPAddress(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']):
return str(self._values['remote_host'])
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
def remote_port(self):
if self._values['remote_port'] is None:

View file

@ -15,11 +15,9 @@ if sys.version_info < (2, 7):
raise SkipTest("F5 Ansible modules require Python >= 2.7")
from ansible.compat.tests import unittest
from ansible.compat.tests.mock import patch, Mock
from ansible.module_utils import basic
from ansible.module_utils._text import to_bytes
from ansible.compat.tests.mock import Mock
from ansible.compat.tests.mock import patch
from ansible.module_utils.f5_utils import AnsibleF5Client
from units.modules.utils import set_module_args
try:
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_NETADDR
from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError
from test.unit.modules.utils import set_module_args
except ImportError:
try:
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_NETADDR
from ansible.module_utils.f5_utils import iControlUnexpectedHTTPError
from units.modules.utils import set_module_args
except ImportError:
raise SkipTest("F5 Ansible modules require the f5-sdk Python library")