From bb76575715682594d4d6d73d8b9e87692bdc6841 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sun, 1 Nov 2020 10:46:46 +0100 Subject: openvpn: T2994: remove workarounds for individual ipv4 and ipv6 keys Remove workaround which split (local|remote)_address and also subnet keys into individual keys for the assigned IP address family (4/6). During template rendering check IP version by introducing new ipv4 and ipv6 Jinja2 filters {% if foo | ipv4 %} or {% if bar | ipv6 %} options. --- python/vyos/template.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'python') diff --git a/python/vyos/template.py b/python/vyos/template.py index 1f1ddc000..621be0695 100644 --- a/python/vyos/template.py +++ b/python/vyos/template.py @@ -16,7 +16,6 @@ import functools import os -from ipaddress import ip_network from jinja2 import Environment from jinja2 import FileSystemLoader from vyos.defaults import directories @@ -131,6 +130,7 @@ def vyos_address_from_cidr(text): Example: 192.0.2.0/24 -> 192.0.2.0, 2001:db8::/48 -> 2001:db8:: """ + from ipaddress import ip_network return str(ip_network(text).network_address) @@ -140,4 +140,17 @@ def vyos_netmask_from_cidr(text): Example: 192.0.2.0/24 -> 255.255.255.0, 2001:db8::/48 -> ffff:ffff:ffff:: """ + from ipaddress import ip_network return str(ip_network(text).netmask) + +@register_filter("ipv4") +def vyos_ipv4(text): + """ Filter IP address, return True on IPv4 address, False otherwise """ + from ipaddress import ip_interface + return ip_interface(text).version == 4 + +@register_filter("ipv6") +def vyos_ipv6(text): + """ Filter IP address, return True on IPv6 address, False otherwise """ + from ipaddress import ip_interface + return ip_interface(text).version == 6 -- cgit v1.2.3