From 44269524204fcfd9ffafd2cb7b9c087ef1236541 Mon Sep 17 00:00:00 2001 From: BPplays Date: Sat, 15 Nov 2025 17:54:16 -0800 Subject: [PATCH 1/5] refactor host formatting and added option for just string --- .../modules/addons/template_helpers.py | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/opnsense/service/modules/addons/template_helpers.py b/src/opnsense/service/modules/addons/template_helpers.py index 8edfab39774..1260ce868bd 100644 --- a/src/opnsense/service/modules/addons/template_helpers.py +++ b/src/opnsense/service/modules/addons/template_helpers.py @@ -151,14 +151,20 @@ def physical_interfaces(self, names): result.append(self.getNodeByTag('interfaces.'+name+'.if')) return list(filter(None, result)) - def host_for_port(self, host_tag: str): - return self.host_with_port( - host_tag=host_tag, - port_tag='', - brackets_on_bare_ipv6=True - ) - - def host_with_port(self, host_tag: str, port_tag: str, brackets_on_bare_ipv6: bool = False): + def format_host_str_for_port(self, host: str) -> str: + """ returns a formatting host (bracketed if IPv6) from a string + :param host: string + :return: string + """ + if self.is_ipv6(host): + host = "[" + host + "]" + return host + + def host_for_port(self, host_tag: str) -> str: + host = self.getNodeByTag(host_tag) + return self.format_host_str_for_port(host) + + def host_with_port(self, host_tag: str, port_tag: str) -> str: """ returns a formatting host and port and bracketed if IPv6 from tags :param host_tag: string :param port_tag: setting this < 0 will disable it's output and just output the ip formatted for inclusion with a separate formatted port @@ -179,15 +185,11 @@ def host_with_port(self, host_tag: str, port_tag: str, brackets_on_bare_ipv6: bo if host is None or host == "": return "" - if skip_port and not brackets_on_bare_ipv6: - return host - - if self.is_ipv6(host): - host = "[" + host + "]" - if skip_port: return host + host = self.format_host_str_for_port(host) + return '{}:{}'.format(host, port) @staticmethod From 4cda811c63c00b7603bb066c115d5bd27e928ab1 Mon Sep 17 00:00:00 2001 From: BPplays Date: Wed, 26 Nov 2025 10:38:46 -0800 Subject: [PATCH 2/5] renamed func --- src/opnsense/service/modules/addons/template_helpers.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/opnsense/service/modules/addons/template_helpers.py b/src/opnsense/service/modules/addons/template_helpers.py index 1260ce868bd..ad1b4d8644e 100644 --- a/src/opnsense/service/modules/addons/template_helpers.py +++ b/src/opnsense/service/modules/addons/template_helpers.py @@ -151,7 +151,7 @@ def physical_interfaces(self, names): result.append(self.getNodeByTag('interfaces.'+name+'.if')) return list(filter(None, result)) - def format_host_str_for_port(self, host: str) -> str: + def host_str_for_port(self, host: str) -> str: """ returns a formatting host (bracketed if IPv6) from a string :param host: string :return: string @@ -162,7 +162,7 @@ def format_host_str_for_port(self, host: str) -> str: def host_for_port(self, host_tag: str) -> str: host = self.getNodeByTag(host_tag) - return self.format_host_str_for_port(host) + return self.host_str_for_port(host) def host_with_port(self, host_tag: str, port_tag: str) -> str: """ returns a formatting host and port and bracketed if IPv6 from tags @@ -188,7 +188,7 @@ def host_with_port(self, host_tag: str, port_tag: str) -> str: if skip_port: return host - host = self.format_host_str_for_port(host) + host = self.host_str_for_port(host) return '{}:{}'.format(host, port) From 8e0eadf51ad2ff2cf987278681bb6780cbfe25ac Mon Sep 17 00:00:00 2001 From: BPplays Date: Thu, 27 Nov 2025 04:24:09 -0800 Subject: [PATCH 3/5] refactored funcs to be simpler and fixed doc comments --- .../service/modules/addons/template_helpers.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/opnsense/service/modules/addons/template_helpers.py b/src/opnsense/service/modules/addons/template_helpers.py index ad1b4d8644e..dd7d704f05f 100644 --- a/src/opnsense/service/modules/addons/template_helpers.py +++ b/src/opnsense/service/modules/addons/template_helpers.py @@ -157,7 +157,7 @@ def host_str_for_port(self, host: str) -> str: :return: string """ if self.is_ipv6(host): - host = "[" + host + "]" + return "[" + host + "]" return host def host_for_port(self, host_tag: str) -> str: @@ -168,23 +168,20 @@ def host_with_port(self, host_tag: str, port_tag: str) -> str: """ returns a formatting host and port and bracketed if IPv6 from tags :param host_tag: string :param port_tag: setting this < 0 will disable it's output and just output the ip formatted for inclusion with a separate formatted port - :param no_brackets_on_bare_ip: setting this to True means that there will be brackets on IPv6 even if it just returns an address with no port :return: string """ host = self.getNodeByTag(host_tag) port = self.getNodeByTag(port_tag) + skip_port = False + + if host is None or host == "": + return "" if port is None or port == "": - port = -1 + skip_port = True else: port = int(port) - - skip_port = port < 0 - - if host is None or host == "": - return "" - if skip_port: return host From bcff72017c53a78f7b2fde088cd9fdc88da603e6 Mon Sep 17 00:00:00 2001 From: BPplays Date: Thu, 4 Dec 2025 09:52:45 -0800 Subject: [PATCH 4/5] added str func for host and port; fixed func docs --- .../modules/addons/template_helpers.py | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/opnsense/service/modules/addons/template_helpers.py b/src/opnsense/service/modules/addons/template_helpers.py index dd7d704f05f..bce86479dd8 100644 --- a/src/opnsense/service/modules/addons/template_helpers.py +++ b/src/opnsense/service/modules/addons/template_helpers.py @@ -160,18 +160,12 @@ def host_str_for_port(self, host: str) -> str: return "[" + host + "]" return host - def host_for_port(self, host_tag: str) -> str: - host = self.getNodeByTag(host_tag) - return self.host_str_for_port(host) - - def host_with_port(self, host_tag: str, port_tag: str) -> str: - """ returns a formatting host and port and bracketed if IPv6 from tags - :param host_tag: string - :param port_tag: setting this < 0 will disable it's output and just output the ip formatted for inclusion with a separate formatted port + def host_str_with_port(self, host: str, port: str) -> str: + """ returns a formatting host (bracketed if IPv6) from a string + :param host: string + :param port: setting this to none or "" will disable it's output and just output the ip formatted for inclusion with a separate formatted port :return: string """ - host = self.getNodeByTag(host_tag) - port = self.getNodeByTag(port_tag) skip_port = False if host is None or host == "": @@ -189,6 +183,21 @@ def host_with_port(self, host_tag: str, port_tag: str) -> str: return '{}:{}'.format(host, port) + def host_for_port(self, host_tag: str) -> str: + host = self.getNodeByTag(host_tag) + return self.host_str_for_port(host) + + def host_with_port(self, host_tag: str, port_tag: str) -> str: + """ returns a formatting host and port and bracketed if IPv6 from tags + :param host_tag: string + :param port_tag: setting this to none or "" will disable it's output and just output the ip formatted for inclusion with a separate formatted port + :return: string + """ + host = self.getNodeByTag(host_tag) + port = self.getNodeByTag(port_tag) + + return self.host_str_with_port(host, port) + @staticmethod def is_ipv6(ip: str) -> bool: return ":" in ip From f42ba55a541c983deaaeb9e5a95b7854c1ec44b3 Mon Sep 17 00:00:00 2001 From: BPplays Date: Thu, 4 Dec 2025 10:00:54 -0800 Subject: [PATCH 5/5] better docs --- .../modules/addons/template_helpers.py | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/opnsense/service/modules/addons/template_helpers.py b/src/opnsense/service/modules/addons/template_helpers.py index bce86479dd8..54043d68134 100644 --- a/src/opnsense/service/modules/addons/template_helpers.py +++ b/src/opnsense/service/modules/addons/template_helpers.py @@ -152,19 +152,19 @@ def physical_interfaces(self, names): return list(filter(None, result)) def host_str_for_port(self, host: str) -> str: - """ returns a formatting host (bracketed if IPv6) from a string - :param host: string - :return: string + """ + :param host: network host + :return: formatted host, bracketed if IPv6 """ if self.is_ipv6(host): return "[" + host + "]" return host def host_str_with_port(self, host: str, port: str) -> str: - """ returns a formatting host (bracketed if IPv6) from a string - :param host: string - :param port: setting this to none or "" will disable it's output and just output the ip formatted for inclusion with a separate formatted port - :return: string + """ + :param host: network host + :param port: network port; setting this to none or "" will make it so no port is included + :return: formatted host, bracketed if IPv6 and there is a port """ skip_port = False @@ -184,14 +184,18 @@ def host_str_with_port(self, host: str, port: str) -> str: return '{}:{}'.format(host, port) def host_for_port(self, host_tag: str) -> str: + """ + :param host_tag: tag of a network host + :return: formatted host and port, bracketed if IPv6 + """ host = self.getNodeByTag(host_tag) return self.host_str_for_port(host) def host_with_port(self, host_tag: str, port_tag: str) -> str: - """ returns a formatting host and port and bracketed if IPv6 from tags - :param host_tag: string - :param port_tag: setting this to none or "" will disable it's output and just output the ip formatted for inclusion with a separate formatted port - :return: string + """ + :param host_tag: tag of a network host + :param port_tag: tag of a network port; setting this to none or "" will make it so no port is included + :return: formatted host and port, bracketed if IPv6 and there is a port """ host = self.getNodeByTag(host_tag) port = self.getNodeByTag(port_tag)