From 4f8d105108bbd7ede4ae12360fb7380e4b0dafbb Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Thu, 15 Jun 2017 13:11:44 +0200 Subject: [PATCH] [openwrt] Added support for "ip6gw" option #86 - updated tests - updated docs Closes #86 --- docs/source/backends/openwrt.rst | 14 ++++++++---- netjsonconfig/backends/openwrt/converters.py | 5 +++- tests/openwrt/test_network.py | 24 ++++++++++++-------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/docs/source/backends/openwrt.rst b/docs/source/backends/openwrt.rst index 654ab912d..cfe54aa8c 100644 --- a/docs/source/backends/openwrt.rst +++ b/docs/source/backends/openwrt.rst @@ -55,7 +55,8 @@ Code example: "type": "ethernet", "addresses": [ { - "address": "192.168.1.1", + "address": "192.168.1.2", + "gateway": "192.168.1.1", "mask": 24, "proto": "static", "family": "ipv4" @@ -67,8 +68,9 @@ Code example: "family": "ipv4" }, { - "address": "fd87::1", - "mask": 128, + "address": "fd87::2", + "gateway": "fd87::1", + "mask": 64, "proto": "static", "family": "ipv6" } @@ -83,9 +85,11 @@ Will return the following output:: package network config interface 'eth0_1' + option gateway '192.168.1.1' option ifname 'eth0.1' - option ip6addr 'fd87::1/128' - list ipaddr '192.168.1.1/24' + option ip6addr 'fd87::2/64' + option ip6gw 'fd87::1' + list ipaddr '192.168.1.2/24' list ipaddr '192.168.2.1/24' option proto 'static' diff --git a/netjsonconfig/backends/openwrt/converters.py b/netjsonconfig/backends/openwrt/converters.py index a9ccce308..c932e4be8 100644 --- a/netjsonconfig/backends/openwrt/converters.py +++ b/netjsonconfig/backends/openwrt/converters.py @@ -126,6 +126,9 @@ def __get_addresses(self, interface): address['proto'] = 'dhcp' if family == 'ipv4' else 'dhcpv6' dhcp.append(self.__del_address_keys(address)) continue + if 'gateway' in address: + uci_key = 'gateway' if family == 'ipv4' else 'ip6gw' + interface[uci_key] = address['gateway'] # static address_key = 'ipaddr' if family == 'ipv4' else 'ip6addr' static.setdefault(address_key, []) @@ -178,7 +181,7 @@ def __get_interface(self, interface, uci_name): del interface['addresses'] return interface - _address_keys = ['address', 'mask', 'family'] + _address_keys = ['address', 'mask', 'family', 'gateway'] def __del_address_keys(self, address): """ diff --git a/tests/openwrt/test_network.py b/tests/openwrt/test_network.py index 394d2f97f..861fe7c1c 100644 --- a/tests/openwrt/test_network.py +++ b/tests/openwrt/test_network.py @@ -44,26 +44,28 @@ def test_multiple_ip(self): "autostart": True, "addresses": [ { - "address": "192.168.1.1", + "address": "192.168.1.2", + "gateway": "192.168.1.1", "mask": 24, "proto": "static", "family": "ipv4" }, { - "address": "192.168.2.1", + "address": "192.168.2.3", "mask": 24, "proto": "static", "family": "ipv4" }, { - "address": "fd87::1", - "mask": 128, + "address": "fd87::2", + "gateway": "fd87::1", + "mask": 64, "proto": "static", "family": "ipv6" }, { - "address": "fd87::2", - "mask": 128, + "address": "fd87::3", + "mask": 64, "proto": "static", "family": "ipv6" } @@ -75,11 +77,13 @@ def test_multiple_ip(self): config interface 'eth0_1' option auto '1' + option gateway '192.168.1.1' option ifname 'eth0.1' - list ip6addr 'fd87::1/128' - list ip6addr 'fd87::2/128' - list ipaddr '192.168.1.1/24' - list ipaddr '192.168.2.1/24' + list ip6addr 'fd87::2/64' + list ip6addr 'fd87::3/64' + option ip6gw 'fd87::1' + list ipaddr '192.168.1.2/24' + list ipaddr '192.168.2.3/24' option proto 'static' """) self.assertEqual(o.render(), expected)