Skip to content

Commit 2ffe2de

Browse files
committed
[openwrt] Added "vid" option in "switch" #85
- automatically infer "vid" attribute from "vlan" - allow manual override of "vid" - allow disabling override by supplying an empty value Fixes #85
1 parent 80f18a8 commit 2ffe2de

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

docs/source/backends/openwrt.rst

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,62 @@ Will be rendered as follows::
15761576
config switch_vlan 'switch0_vlan1'
15771577
option device 'switch0'
15781578
option ports '0t 2 3 4 5'
1579+
option vid '1'
1580+
option vlan '1'
1581+
1582+
config switch_vlan 'switch0_vlan2'
1583+
option device 'switch0'
1584+
option ports '0t 1'
1585+
option vid '2'
1586+
option vlan '2'
1587+
1588+
Overriding or disabling ``vid`` UCI option
1589+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1590+
1591+
The OpenWRT/LEDE UCI ``vid`` option of ``switch_vlan`` sections is automatically inferred
1592+
from the ``vlan`` number, although it's possible to override it or disable it if needed:
1593+
1594+
.. code-block:: python
1595+
1596+
{
1597+
"switch": [
1598+
{
1599+
"name": "switch0",
1600+
"reset": True,
1601+
"enable_vlan": True,
1602+
"vlan": [
1603+
{
1604+
"device": "switch0",
1605+
"vlan": 1,
1606+
"vid": 110, # manual override
1607+
"ports": "0t 2 3 4 5"
1608+
},
1609+
{
1610+
"device": "switch0",
1611+
"vlan": 2,
1612+
# ``None`` or empty string will remove
1613+
# ``vid`` output from the UCI result
1614+
"vid": None,
1615+
"ports": "0t 1"
1616+
}
1617+
]
1618+
}
1619+
]
1620+
}
1621+
1622+
Will be rendered as follows::
1623+
1624+
package network
1625+
1626+
config switch 'switch0'
1627+
option enable_vlan '1'
1628+
option name 'switch0'
1629+
option reset '1'
1630+
1631+
config switch_vlan 'switch0_vlan1'
1632+
option device 'switch0'
1633+
option ports '0t 2 3 4 5'
1634+
option vid '110'
15791635
option vlan '1'
15801636

15811637
config switch_vlan 'switch0_vlan2'

netjsonconfig/backends/openwrt/converters.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ def to_intermediate(self):
326326
'.type': 'switch_vlan',
327327
'.name': '{0}_vlan{1}'.format(switch['name'], i)
328328
})
329+
if 'vid' not in vlan:
330+
vlan['vid'] = vlan['vlan']
329331
vlans.append(sorted_dict(vlan))
330332
i += 1
331333
del switch['vlan']

tests/openwrt/test_network.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ def test_switch(self):
750750
{
751751
"device": "switch0",
752752
"vlan": 2,
753+
"vid": None, # ``None`` or empty string disable ``vid``
753754
"ports": "0t 1"
754755
}
755756
]
@@ -762,6 +763,7 @@ def test_switch(self):
762763
{
763764
"device": "switch1",
764765
"vlan": 3,
766+
"vid": 130,
765767
"ports": "0t 6 7"
766768
}
767769
]
@@ -778,6 +780,7 @@ def test_switch(self):
778780
config switch_vlan 'switch0_vlan1'
779781
option device 'switch0'
780782
option ports '0t 2 3 4 5'
783+
option vid '1'
781784
option vlan '1'
782785
783786
config switch_vlan 'switch0_vlan2'
@@ -793,6 +796,7 @@ def test_switch(self):
793796
config switch_vlan 'switch1_vlan1'
794797
option device 'switch1'
795798
option ports '0t 6 7'
799+
option vid '130'
796800
option vlan '3'
797801
""")
798802
self.assertEqual(o.render(), expected)

0 commit comments

Comments
 (0)