Skip to content

Commit 772cec1

Browse files
committed
[bug] Fix switch vlan ordering #104
Closes #104
1 parent 90ab88b commit 772cec1

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

netjsonconfig/backends/base/converter.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ def to_netjson(self, remove_block=True):
9292
to a NetJSON configuration dictionary (``self.config``)
9393
"""
9494
result = OrderedDict()
95-
# copy list
96-
intermediate_data = list(self.intermediate_data[self.intermediate_key])
95+
# Clean intermediate data
96+
intermediate_data = self.clean_intermediate_data(list(self.intermediate_data[self.intermediate_key]))
9797
# iterate over copied intermediate data structure
9898
for index, block in enumerate(intermediate_data):
9999
if self.should_skip_block(block):
@@ -109,6 +109,19 @@ def to_netjson(self, remove_block=True):
109109
# return result, expects dict
110110
return result
111111

112+
def clean_intermediate_data(self, intermediate_data):
113+
"""
114+
Utility method called to clean data for backend in ``to_netjson``
115+
"""
116+
clean_intermediate_data, appendto_clean_intermediate_data = [], []
117+
for block in intermediate_data:
118+
if '.type' in block and block['.type'] == 'switch_vlan':
119+
appendto_clean_intermediate_data.append(block)
120+
else:
121+
clean_intermediate_data.append(block)
122+
clean_intermediate_data.extend(appendto_clean_intermediate_data)
123+
return clean_intermediate_data
124+
112125
def to_netjson_loop(self, block, result, index=None): # pragma: nocover
113126
"""
114127
Utility method called in the loop of ``to_netjson``

tests/openwrt/test_network.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,37 @@ def test_rules_no_src_dest(self):
284284
option vlan '3'
285285
"""
286286

287+
_switch_uci_reorder = """package network
288+
289+
config switch_vlan 'switch0_vlan1'
290+
option device 'switch0'
291+
option ports '0t 2 3 4 5'
292+
option vid '1'
293+
option vlan '1'
294+
295+
config switch 'switch0'
296+
option enable_vlan '1'
297+
option name 'switch0'
298+
option reset '1'
299+
"""
300+
301+
_switch_netjson_reorder = {
302+
"switch": [
303+
{
304+
"name": "switch0",
305+
"reset": True,
306+
"enable_vlan": True,
307+
"vlan": [
308+
{
309+
"device": "switch0",
310+
"vlan": 1,
311+
"ports": "0t 2 3 4 5"
312+
}
313+
]
314+
}
315+
]
316+
}
317+
287318
def test_render_switch(self):
288319
o = OpenWrt(self._switch_netjson)
289320
expected = self._tabs(self._switch_uci)
@@ -292,3 +323,7 @@ def test_render_switch(self):
292323
def test_parse_switch(self):
293324
o = OpenWrt(native=self._switch_uci)
294325
self.assertEqual(o.config, self._switch_netjson)
326+
327+
def test_parse_switch_reorder(self):
328+
o = OpenWrt(native=self._switch_uci_reorder)
329+
self.assertEqual(o.config, self._switch_netjson_reorder)

0 commit comments

Comments
 (0)