Skip to content

Commit

Permalink
fix usage over IPv6 (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
ansibleguy committed Jul 29, 2024
1 parent d58864f commit e0c8a2e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 6 additions & 1 deletion plugins/module_utils/base/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.helper.api import \
check_host, ssl_verification, check_response, get_params_path, debug_api, \
check_or_load_credentials, api_pretty_exception
from ansible_collections.ansibleguy.opnsense.plugins.module_utils.helper.main import is_ip6

DEFAULT_TIMEOUT = 20.0
HTTPX_EXCEPTIONS = (
Expand All @@ -29,8 +30,12 @@ def _start(self, timeout: float) -> httpx.Client:

setdefaulttimeout(timeout)

fw = self.m.params['firewall']
if is_ip6(fw, strip_enclosure=False):
fw = f"[{fw}]"

return httpx.Client(
base_url=f"https://{self.m.params['firewall']}:{self.m.params['api_port']}/api",
base_url=f"https://{fw}:{self.m.params['api_port']}/api",
auth=(self.m.params['api_key'], self.m.params['api_secret']),
timeout=httpx.Timeout(timeout=timeout, connect=2.0),
transport=httpx.HTTPTransport(
Expand Down
8 changes: 7 additions & 1 deletion plugins/module_utils/helper/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def is_ip(host: str, ignore_empty: bool = False) -> bool:
if ignore_empty and is_unset(host):
return True

if host.find('[') != -1:
host = host[1:-1]

try:
ip_address(host)
return True
Expand All @@ -54,10 +57,13 @@ def is_ip4(host: str, ignore_empty: bool = False) -> bool:
return False


def is_ip6(host: str, ignore_empty: bool = False) -> bool:
def is_ip6(host: str, ignore_empty: bool = False, strip_enclosure: bool = True) -> bool:
if ignore_empty and is_unset(host):
return True

if strip_enclosure and host.find('[') != -1:
host = host[1:-1]

try:
IPv6Address(host)
return True
Expand Down

0 comments on commit e0c8a2e

Please sign in to comment.