From 586e4bc4bd2469767d73239a3275213b5a57e67a Mon Sep 17 00:00:00 2001 From: Gagan Deep Date: Thu, 27 Jul 2023 03:30:12 +0530 Subject: [PATCH] [feature] Allowed defining VXLAN interface in VXLAN over WireGuard backend --- netjsonconfig/backends/openwrt/openwrt.py | 2 +- netjsonconfig/backends/vxlan/schema.py | 54 +++++++++++++++++++ .../backends/vxlan/vxlan_wireguard.py | 7 ++- tests/vxlan/test_vxlan_wireguard.py | 15 +++--- 4 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 netjsonconfig/backends/vxlan/schema.py diff --git a/netjsonconfig/backends/openwrt/openwrt.py b/netjsonconfig/backends/openwrt/openwrt.py index d8cd2f5c4..b6ae8c1ec 100644 --- a/netjsonconfig/backends/openwrt/openwrt.py +++ b/netjsonconfig/backends/openwrt/openwrt.py @@ -124,7 +124,7 @@ def vxlan_wireguard_auto_client(cls, **kwargs): config = cls.wireguard_auto_client(**kwargs) vxlan_config = VxlanWireguard.auto_client(**kwargs) vxlan_interface = { - 'name': 'vxlan', + 'name': vxlan_config['name'], 'type': 'vxlan', 'vtep': vxlan_config['server_ip_address'], 'port': 4789, diff --git a/netjsonconfig/backends/vxlan/schema.py b/netjsonconfig/backends/vxlan/schema.py new file mode 100644 index 000000000..6af606912 --- /dev/null +++ b/netjsonconfig/backends/vxlan/schema.py @@ -0,0 +1,54 @@ +from copy import deepcopy + +from ..wireguard.schema import schema as base_schema + +base_vxlan_properties = { + "vxlan": { + "type": "array", + "title": "VXLAN", + "uniqueItems": True, + "additionalItems": True, + "propertyOrder": 13, + "items": { + "type": "object", + "title": "VXLAN tunnel", + "additionalProperties": True, + "properties": { + "name": { + "title": "interface name", + "description": "VXLAN interface name", + "type": "string", + "minLength": 2, + "maxLength": 15, + "pattern": "^[^\\s]*$", + "propertyOrder": 1, + }, + "vni": { + "propertyOrder": 2, + "title": "VNI", + "oneOf": [ + { + "title": "VNI (auto)", + "description": "Auto-generate (different for each tunnel)", + "type": "string", + "enum": [""], + "options": {"enum_titles": ["auto"]}, + "readonly": True, + }, + { + "title": "VNI (manual)", + "type": "integer", + "default": 1, + "minimum": 0, + "maximum": 16777216, + }, + ], + }, + }, + }, + } +} + + +schema = deepcopy(base_schema) +schema['properties'].update(base_vxlan_properties) diff --git a/netjsonconfig/backends/vxlan/vxlan_wireguard.py b/netjsonconfig/backends/vxlan/vxlan_wireguard.py index 080d2697e..0cb868893 100644 --- a/netjsonconfig/backends/vxlan/vxlan_wireguard.py +++ b/netjsonconfig/backends/vxlan/vxlan_wireguard.py @@ -1,9 +1,12 @@ from ..wireguard.wireguard import Wireguard +from .schema import schema class VxlanWireguard(Wireguard): + schema = schema + @classmethod - def auto_client(cls, vni=0, server_ip_address='', **kwargs): + def auto_client(cls, vni=0, server_ip_address='', vxlan=None, **kwargs): """ Returns a configuration dictionary representing VXLAN configuration that is compatible with the passed server configuration. @@ -12,8 +15,10 @@ def auto_client(cls, vni=0, server_ip_address='', **kwargs): :param server_ip_address: server internal tunnel address :returns: dictionary representing VXLAN properties """ + vxlan = vxlan or {} config = { 'server_ip_address': server_ip_address, 'vni': vni, + 'name': vxlan.get('name', 'vxlan'), } return config diff --git a/tests/vxlan/test_vxlan_wireguard.py b/tests/vxlan/test_vxlan_wireguard.py index 078689a6f..979530fc7 100644 --- a/tests/vxlan/test_vxlan_wireguard.py +++ b/tests/vxlan/test_vxlan_wireguard.py @@ -6,20 +6,17 @@ class TestBackend(unittest.TestCase): def test_auto_client(self): with self.subTest('No arguments are provided'): - expected = { - 'server_ip_address': '', - 'vni': 0, - } + expected = {'server_ip_address': '', 'vni': 0, 'name': 'vxlan'} self.assertDictEqual(VxlanWireguard.auto_client(), expected) with self.subTest('All arguments are provided'): - expected = { - 'server_ip_address': '10.0.0.1', - 'vni': 1, - } + expected = {'server_ip_address': '10.0.0.1', 'vni': 1, 'name': 'vxlan1'} self.assertDictEqual( VxlanWireguard.auto_client( - vni=1, server_ip_address='10.0.0.1', server={} + vni=1, + server_ip_address='10.0.0.1', + server={}, + vxlan={'name': 'vxlan1'}, ), expected, )