Skip to content

Commit

Permalink
[OpenWrt] Simplified definition of custom interface "proto" options
Browse files Browse the repository at this point in the history
Backward incompatible change.
  • Loading branch information
nemesifier committed Mar 31, 2016
1 parent d448af4 commit 9f93776
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 62 deletions.
13 changes: 11 additions & 2 deletions netjsonconfig/backends/openwrt/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ def _get_interfaces(self):
# default values
address_key = None
address_value = None
# proto defaults to static
proto = address.get('proto', 'static')
proto = self.__get_proto(uci_interface, address)
# add suffix if there is more than one config block
if counter > 1:
name = '{name}_{counter}'.format(name=uci_name, counter=counter)
Expand Down Expand Up @@ -117,6 +116,16 @@ def _get_interfaces(self):
counter += 1
return uci_interfaces

def __get_proto(self, interface, address):
"""
determines interface "proto" option
"""
if 'proto' not in interface:
# proto defaults to static
return address.get('proto', 'static')
else:
# allow override
return interface['proto']
def _get_routes(self):
routes = self.config.get('routes', [])
# results container
Expand Down
42 changes: 0 additions & 42 deletions netjsonconfig/backends/openwrt/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,13 @@

schema = merge_config(default_schema, {
"definitions": {
"other_address": {
"type": "object",
"title": "other",
"allOf": [
{"$ref": "#/definitions/base_address"},
{
"type": "object",
"properties": {
"family": {"enum": ["ipv4", "ipv6"]},
"proto": {
"enum": [
'ppp',
'pppoe',
'pppoa',
'3g',
'qmi',
'ncm',
'hnet',
'pptp',
'6in4',
'aiccu',
'6to4',
'6rd',
'dslite',
'l2tp',
'relay',
'gre',
'gretap',
'grev6',
'grev6tap',
'none'
]
}
}
}
]
},
"interface_settings": {
"properties": {
"network": {
"type": "string",
"maxLength": 15,
"pattern": "^[a-zA-z0-9_\\.\\-]*$",
"propertyOrder": 7
},
"addresses": {
"items": {
"oneOf": [{"$ref": "#/definitions/other_address"}]
}
}
}
},
Expand Down
36 changes: 18 additions & 18 deletions tests/openwrt/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,26 +261,31 @@ def test_ipv6_routes(self):
""")
self.assertEqual(o.render(), expected)

def test_additional_proto(self):
def test_custom_proto(self):
o = OpenWrt({
"interfaces": [
{
"name": "mobile0",
"type": "wireless",
"addresses": [
{
"proto": "3g",
"family": "ipv4"
}
]
"name": "ppp0",
"type": "other",
"proto": "ppp",
"device": "/dev/usb/modem1",
"username": "user1",
"password": "pwd0123",
"keepalive": 3,
"ipv6": True
}
]
})
expected = self._tabs("""package network
config interface 'mobile0'
option ifname 'mobile0'
option proto '3g'
config interface 'ppp0'
option device '/dev/usb/modem1'
option ifname 'ppp0'
option ipv6 '1'
option keepalive '3'
option password 'pwd0123'
option proto 'ppp'
option username 'user1'
""")
self.assertEqual(o.render(), expected)

Expand Down Expand Up @@ -312,12 +317,7 @@ def test_interface_custom_attrs(self):
"mtu": 1400,
"custom_attr": "yes",
"empty": "",
"addresses": [
{
"proto": "3g",
"family": "ipv4"
}
]
"proto": "3g",
}
]
})
Expand Down

0 comments on commit 9f93776

Please sign in to comment.