From a769b4d8ed5bc8d4bb9f4c940dac9c83c7afae3c Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 28 Sep 2013 22:04:52 +0200 Subject: [PATCH] cleanup settings access --- nsupdate/main/_tests/test_dnstools.py | 20 ++++++++++---------- nsupdate/main/dnstools.py | 24 +++++------------------- 2 files changed, 15 insertions(+), 29 deletions(-) diff --git a/nsupdate/main/_tests/test_dnstools.py b/nsupdate/main/_tests/test_dnstools.py index c128ded..4ef7d91 100644 --- a/nsupdate/main/_tests/test_dnstools.py +++ b/nsupdate/main/_tests/test_dnstools.py @@ -4,16 +4,16 @@ Tests for dnstools module. import pytest -from ..dnstools import (update, query_ns, parse_name, update_ns, SameIpError, - BASEDOMAIN, NONEXISTING_HOST, - WWW_HOST, WWW_IPV4_HOST, WWW_IPV4_IP, WWW_IPV6_HOST, WWW_IPV6_IP, ) +from ..dnstools import update, query_ns, parse_name, update_ns, SameIpError +from django.conf import settings +BASEDOMAIN = settings.BASEDOMAIN from dns.resolver import NXDOMAIN class TestIntelligentUpdater(object): def test_double_update(self): - host, ip = 'test0.' + BASEDOMAIN, '1.2.3.4' + host, ip = 'test0.' + settings.BASEDOMAIN, '1.2.3.4' # make sure the host is not there try: update_ns(host, 'A', action='del') @@ -29,16 +29,16 @@ class TestIntelligentUpdater(object): class TestQuery(object): def test_queries_ok(self): - assert query_ns(WWW_IPV4_HOST, 'A') == WWW_IPV4_IP # v4 ONLY - assert query_ns(WWW_IPV6_HOST, 'AAAA') == WWW_IPV6_IP # v6 ONLY - assert query_ns(WWW_HOST, 'A') == WWW_IPV4_IP # v4 and v6, query v4 - assert query_ns(WWW_HOST, 'AAAA') == WWW_IPV6_IP # v4 and v6, query v6 + assert query_ns(settings.WWW_IPV4_HOST, 'A') == settings.WWW_IPV4_IP # v4 ONLY + assert query_ns(settings.WWW_IPV6_HOST, 'AAAA') == settings.WWW_IPV6_IP # v6 ONLY + assert query_ns(settings.WWW_HOST, 'A') == settings.WWW_IPV4_IP # v4 and v6, query v4 + assert query_ns(settings.WWW_HOST, 'AAAA') == settings.WWW_IPV6_IP # v4 and v6, query v6 def test_queries_failing(self): with pytest.raises(NXDOMAIN): - query_ns(NONEXISTING_HOST, 'A') + query_ns(settings.NONEXISTING_HOST, 'A') with pytest.raises(NXDOMAIN): - query_ns(NONEXISTING_HOST, 'AAAA') + query_ns(settings.NONEXISTING_HOST, 'AAAA') class TestUpdate(object): diff --git a/nsupdate/main/dnstools.py b/nsupdate/main/dnstools.py index 78ff67e..8c2f74d 100644 --- a/nsupdate/main/dnstools.py +++ b/nsupdate/main/dnstools.py @@ -13,20 +13,6 @@ import dns.tsig import dns.tsigkeyring -SERVER = settings.SERVER -BASEDOMAIN = settings.BASEDOMAIN - -NONEXISTING_HOST = settings.NONEXISTING_HOST -WWW_HOST = settings.WWW_HOST -WWW_IPV4_HOST = settings.WWW_IPV4_HOST -WWW_IPV6_HOST = settings.WWW_IPV6_HOST -WWW_IPV4_IP = settings.WWW_IPV4_IP -WWW_IPV6_IP = settings.WWW_IPV6_IP - -UPDATE_ALGO = settings.UPDATE_ALGO -UPDATE_KEY = settings.UPDATE_KEY - - class SameIpError(ValueError): """ raised if an IP address is already present in DNS and and update was @@ -66,8 +52,8 @@ def query_ns(qname, rdtype): resolver = dns.resolver.Resolver(configure=False) # we do not configure it from resolv.conf, but patch in the values we # want into the documented attributes: - resolver.nameservers = [SERVER, ] - resolver.search = [dns.name.from_text(BASEDOMAIN), ] + resolver.nameservers = [settings.SERVER, ] + resolver.search = [dns.name.from_text(settings.BASEDOMAIN), ] answer = resolver.query(qname, rdtype) return str(list(answer)[0]) @@ -107,8 +93,8 @@ def update_ns(fqdn, rdtype='A', ipaddr=None, origin=None, action='upd', ttl=60): assert action in ['add', 'del', 'upd', ] origin, name = parse_name(fqdn, origin) upd = dns.update.Update(origin, - keyring=dns.tsigkeyring.from_text({BASEDOMAIN+'.': UPDATE_KEY}), - keyalgorithm=UPDATE_ALGO) + keyring=dns.tsigkeyring.from_text({settings.BASEDOMAIN+'.': settings.UPDATE_KEY}), + keyalgorithm=settings.UPDATE_ALGO) if action == 'add': assert ipaddr is not None upd.add(name, ttl, rdtype, ipaddr) @@ -117,5 +103,5 @@ def update_ns(fqdn, rdtype='A', ipaddr=None, origin=None, action='upd', ttl=60): elif action == 'upd': assert ipaddr is not None upd.replace(name, ttl, rdtype, ipaddr) - response = dns.query.tcp(upd, SERVER) + response = dns.query.tcp(upd, settings.SERVER) return response