Skip to content

Commit

Permalink
[feature] Allowed defining VXLAN interface in VXLAN over WireGuard ba…
Browse files Browse the repository at this point in the history
…ckend
  • Loading branch information
pandafy authored Jul 26, 2023
1 parent 9dbbbf0 commit 586e4bc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion netjsonconfig/backends/openwrt/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
54 changes: 54 additions & 0 deletions netjsonconfig/backends/vxlan/schema.py
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 6 additions & 1 deletion netjsonconfig/backends/vxlan/vxlan_wireguard.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
15 changes: 6 additions & 9 deletions tests/vxlan/test_vxlan_wireguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

0 comments on commit 586e4bc

Please sign in to comment.