diff --git a/nsupdate/main/_tests/test_dnstools.py b/nsupdate/main/_tests/test_dnstools.py index 3ac5d79..a7b0afc 100644 --- a/nsupdate/main/_tests/test_dnstools.py +++ b/nsupdate/main/_tests/test_dnstools.py @@ -10,13 +10,22 @@ pytestmark = pytest.mark.django_db from dns.resolver import NXDOMAIN, NoAnswer -from ..dnstools import add, delete, update, query_ns, rev_lookup, parse_name, update_ns, SameIpError, DnsUpdateError +from ..dnstools import (add, delete, update, query_ns, rev_lookup, parse_name, update_ns, + SameIpError, DnsUpdateError, FQDN) # see also conftest.py BASEDOMAIN = 'nsupdate.info' INVALID_HOST = 'test999.' + BASEDOMAIN # this can't get updated +class TestFQDN(object): + def test_create(self): + fqdn = FQDN('test', 'example.org') + assert fqdn.host == 'test' + assert fqdn.domain == 'example.org' + assert str(fqdn) == 'test.example.org' + + def remove_records(host, records=('A', 'AAAA', )): # make sure the records are not there for record in records: diff --git a/nsupdate/main/dnstools.py b/nsupdate/main/dnstools.py index f253477..480fdcc 100644 --- a/nsupdate/main/dnstools.py +++ b/nsupdate/main/dnstools.py @@ -18,6 +18,7 @@ UNAVAILABLE_RETRY = 300.0 import time from datetime import timedelta +from collections import namedtuple import logging logger = logging.getLogger(__name__) @@ -35,6 +36,29 @@ import dns.tsigkeyring from django.utils.timezone import now +class FQDN(namedtuple('FQDN', ['host', 'domain'])): + """ + named tuple to represent a fully qualified domain name: + + * a host in a zone/domain + * just a zone/domain (give host=None when creating) + + use this instead of str so that the information is not lost + what the host part is vs. what the domain part is. + + e.g. foo.bar.example.org could be a host foo in domain bar.example.org + or a host foo.bar in domain example.org. + """ + def __str__(self): + """ + when transforming this into a str, just give the fqdn + """ + if self.host: + return self.host + '.' + self.domain + else: + return self.domain + + Timeout = dns.resolver.Timeout NoNameservers = dns.resolver.NoNameservers