add FQDN type to use instead of str to keep host and domain separate

This commit is contained in:
Thomas Waldmann 2014-08-30 16:08:10 +02:00
parent 435204da87
commit f13d985f4a
2 changed files with 34 additions and 1 deletions

View File

@ -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:

View File

@ -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