Skip to content

Commit

Permalink
Added support for logical interface names #25
Browse files Browse the repository at this point in the history
Added a custom attribute: "network"; closes #25.
  • Loading branch information
nemesifier committed Nov 3, 2015
1 parent 76c093c commit 77d8cf1
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 6 deletions.
17 changes: 13 additions & 4 deletions netjsonconfig/backends/openwrt/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ def _get_interfaces(self):
for interface in interfaces:
counter = 1
is_bridge = False
# ensure uci interface name is valid
uci_name = interface['name'].replace('.', '_')\
.replace('-', '_')
# determine uci logical interface name
network = interface.get('network')
if network:
uci_name = network
# default to ifname
else:
uci_name = interface['name'].replace('.', '_')\
.replace('-', '_')
# determine if must be type bridge
if interface.get('type') == 'bridge':
is_bridge = True
Expand All @@ -36,6 +41,8 @@ def _get_interfaces(self):
for address in interface.get('addresses', default_addresses):
# prepare new UCI interface directive
uci_interface = deepcopy(interface)
if network:
del uci_interface['network']
if uci_interface.get('autostart'):
uci_interface['auto'] = interface['autostart']
del uci_interface['autostart']
Expand Down Expand Up @@ -297,7 +304,9 @@ def _get_wifi_interfaces(self):
# to its defining interface
# but this behaviour can be overridden
if not uci_wifi.get('network'):
uci_wifi['network'] = [wifi_interface['name']]
# get network, default to ifname
network = wifi_interface.get('network', wifi_interface['name'])
uci_wifi['network'] = [network]
uci_wifi['network'] = ' '.join(uci_wifi['network'])\
.replace('.', '_')
uci_wifi_ifaces.append(sorted_dict(uci_wifi))
Expand Down
5 changes: 5 additions & 0 deletions netjsonconfig/backends/openwrt/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"definitions": {
"interface_settings": {
"properties": {
"network": {
"type": "string",
"maxLength": 9,
"pattern": "^[a-zA-z0-9_]*$"
},
"addresses": {
"items": {
"properties": {
Expand Down
78 changes: 76 additions & 2 deletions tests/openwrt/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ def test_simple_bridge(self):
]
},
{
"name": "br-eth0",
"network": "lan",
"name": "br-lan",
"type": "bridge",
"bridge_members": [
"eth0",
Expand All @@ -342,7 +343,7 @@ def test_simple_bridge(self):
option ifname 'eth1'
option proto 'dhcp'
config interface 'br_eth0'
config interface 'lan'
option ifname 'eth0 eth1'
option proto 'none'
option type 'bridge'
Expand Down Expand Up @@ -638,3 +639,76 @@ def test_ifname_pattern(self):
# ensure fix works
o.config['interfaces'][0]['name'] = 'e-t_h@=0.1'
o.validate()

def test_network_maxlength(self):
o = OpenWrt({
"interfaces": [
{
"name": "eth0",
"network": "lan0123456789",
"type": "ethernet"
}
]
})
with self.assertRaises(ValidationError):
o.validate()
# ensure fix works
o.config['interfaces'][0]['network'] = 'lan'
o.validate()

def test_network_pattern(self):
o = OpenWrt({
"interfaces": [
{
"name": "eth0",
"network": "lan 0",
"type": "ethernet"
}
]
})
with self.assertRaises(ValidationError):
o.validate()
o.config['interfaces'][0]['network'] = 'lan-0'
with self.assertRaises(ValidationError):
o.validate()
# ensure fix works
o.config['interfaces'][0]['network'] = 'lan'
o.validate()

def test_network_attribute(self):
o = OpenWrt({
"interfaces": [
{
"name": "eth0",
"type": "ethernet",
"network": "lan",
"addresses": [
{
"address": "192.168.1.1",
"mask": 24,
"proto": "static",
"family": "ipv4"
},
{
"address": "192.168.2.1",
"mask": 24,
"proto": "static",
"family": "ipv4"
}
]
}
]
})
expected = self._tabs("""package network
config interface 'lan'
option ifname 'eth0'
option ipaddr '192.168.1.1/24'
option proto 'static'
config interface 'lan_2'
option ifname 'eth0'
option ipaddr '192.168.2.1/24'
option proto 'static'
""")
self.assertEqual(o.render(), expected)
33 changes: 33 additions & 0 deletions tests/openwrt/test_wireless.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,36 @@ def test_network_schema_attribute(self):
})
with self.assertRaises(ValidationError):
o.validate()

def test_network_attribute(self):
o = OpenWrt({
"interfaces": [
{
"name": "wlan0",
"type": "wireless",
"network": "guests",
"wireless": [
{
"radio": "radio0",
"mode": "access_point",
"ssid": "open"
}
]
}
]
})
expected = self._tabs("""package network
config interface 'guests'
option ifname 'wlan0'
option proto 'none'
package wireless
config wifi-iface
option device 'radio0'
option mode 'ap'
option network 'guests'
option ssid 'open'
""")
self.assertEqual(o.render(), expected)

0 comments on commit 77d8cf1

Please sign in to comment.