Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[openwrt] Allow configure DNS resolvers without the need of defining #129

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion netjsonconfig/backends/openwrt/converters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .default import Default
from .interfaces import Interfaces
from .interfaces import DnsServer
from .interfaces import DnsSearch
from .general import General
from .led import Led
from .ntp import Ntp
Expand All @@ -10,7 +12,7 @@
from .switch import Switch
from .wireless import Wireless

__all__ = ['Default', 'Interfaces', 'General',
__all__ = ['Default', 'Interfaces', 'DnsServer', 'DnsSearch', 'General',
'Led', 'Ntp', 'OpenVpn', 'Radios',
'Routes', 'Rules', 'Switch',
'Wireless']
63 changes: 63 additions & 0 deletions netjsonconfig/backends/openwrt/converters/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,66 @@ def __netjson_dns(self, interface, result):
items = items.split()
result.setdefault(netjson_key, [])
result[netjson_key] += items


class DnsServer(OpenWrtConverter):
netjson_key = 'dns_servers'
intermediate_key = 'network'
_uci_types = ['dns']

def to_intermediate_loop(self, block, result, index=None):
"""
Convert dns_servers NetJson to intermediate data structure if
no interface was provided.
"""
if 'interfaces' not in self.backend.config:
if index > 1:
result['network'][0]['dns'] += ' ' + block
return result
options = {
'.type': 'dns',
'.name': 'dns',
'dns': block
}
result.setdefault('network', [])
result['network'].append(self.sorted_dict(options))
return result

def to_netjson_loop(self, block, result, index=None):
if 'dns' in block:
items = block.pop('dns')
if isinstance(items, six.string_types):
items = items.split()
result.setdefault('dns_servers', [])
result['dns_servers'] += items
if 'dns_search' in block:
items = block.pop('dns_search')
if isinstance(items, six.string_types):
items = items.split()
result.setdefault('dns_search', [])
result['dns_search'] += items
return result


class DnsSearch(OpenWrtConverter):
netjson_key = 'dns_search'
intermediate_key = 'network'
_uci_types = ['dns']

def to_intermediate_loop(self, block, result, index=None):
"""
Convert dns_search NetJson to intermediate data structure if
no interface was provided.
"""
if 'interfaces' not in self.backend.config:
if index > 1:
result['network'][0]['dns_search'] += ' ' + block
return result
options = {
'.type': 'dns',
'.name': 'dns',
'dns_search': block
}
result.setdefault('network', [])
result['network'].append(self.sorted_dict(options))
return result
2 changes: 2 additions & 0 deletions netjsonconfig/backends/openwrt/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class OpenWrt(BaseBackend):
converters.Ntp,
converters.Led,
converters.Interfaces,
converters.DnsServer,
converters.DnsSearch,
converters.Routes,
converters.Rules,
converters.Switch,
Expand Down
27 changes: 27 additions & 0 deletions tests/openwrt/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,33 @@ def test_parse_dns_list(self):
o = OpenWrt(native=native)
self.assertEqual(o.config, self._dns_netjson)

def test_render_dns_without_interface(self):
o = OpenWrt({
"dns_servers": ["10.11.12.13", "8.8.8.8"],
"dns_search": ["netjson.org", "openwisp.org"],
})
expected = self._tabs("""package network

config dns 'dns'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't look like something that would work on OpenWRT, can you provide the piece of documentation of OpenWRT which explains that this would work? Have you tested this on an actual OpenWRT device?

Copy link
Contributor Author

@NoumbissiValere NoumbissiValere Mar 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmm, @nemesisdesign . I just followed standards and conversion used in other tests. I don't have an actual OpenWRT device. will love to have one but can't afford it now. Please can you tell me what is wrong with the code so that i try to correct it? thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NoumbissiValere You can make an OpenWRT VM on your laptop in the meanwhile for testing.
This should help: http://openwisp.io/docs/user/configure-device.html#install-openwrt-on-virtualbox

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the help @atb00ker

option dns '10.11.12.13 8.8.8.8'
option dns_search 'netjson.org openwisp.org'
""")
self.assertEqual(o.render(), expected)

def test_parse_dns_without_interface(self):
native = self._tabs("""package network

config dns 'dns'
option dns '8.8.8.8 4.4.4.4'
option dns_search 'netjson.org openwisp.org'
""")
_dns_json = {
"dns_servers": ["8.8.8.8", "4.4.4.4"],
"dns_search": ["netjson.org", "openwisp.org"],
}
o = OpenWrt(native=native)
self.assertEqual(o.config, _dns_json)

def test_dns_dhcpv4_ignored(self):
o = OpenWrt({
"interfaces": [
Expand Down