new add_to_zone function which is more dnspythonic, add DNAME records

This commit is contained in:
otthorn 2021-04-14 15:34:06 +02:00
parent 2fa0aece48
commit d8d67379e0

40
lib.py
View file

@ -34,7 +34,7 @@ def format_rname(mail: str):
return rname return rname
def format_mname(name: str): def format_re2o_domain(name: str):
""" """
Format a zone name given by the re2o API to a mname dnspython object. Format a zone name given by the re2o API to a mname dnspython object.
@ -51,29 +51,17 @@ def format_mname(name: str):
return mname return mname
#def add_to_zone(zone, name, rdata):
# """Add a rdata object to a zone object."""
#
# zone.nodes.setdefault(name, dns.node.Node())
# rdataset = zone.nodes[name].find_rdataset(
# rdata.rdclass,
# rdata.rdtype,
# create=True
# )
# rdataset.add(rdata)
def add_to_zone(zone, name, rdata): def add_to_zone(zone, name, rdata):
"""Add a rdata object to a zone object.""" """Add a rdata object to a zone object."""
rdataset = zone.find_rdataset( node = zone.find_node(name, create=True)
name, rdataset = node.find_rdataset(
rdata.rdclass,
rdata.rdtype, rdata.rdtype,
create=True create=True
) )
rdataset.add(rdata) rdataset.add(rdata)
def soa_handler(re2o_zone, dns_zone): def soa_handler(re2o_zone, dns_zone):
"""Handler for SOA record""" """Handler for SOA record"""
@ -307,6 +295,24 @@ def cname_records_handler(re2o_zone, dns_zone):
add_to_zone(dns_zone, name, CNAME_obj) add_to_zone(dns_zone, name, CNAME_obj)
def dname_records_handler(re2o_zone, dns_zone):
"""Handler for DNAME records"""
for record in re2o_zone["dname_records"]:
logging.debug(f"DNAME = {record}")
alias = format_re2o_domain(record["alias"])
zone = format_re2o_domain(record["zone"])
DNAME_obj = DNAME.DNAME(
dns.rdataclass.IN,
dns.rdatatype.DNAME,
alias
)
add_to_zone(dns_zone, zone, DNAME_obj)
def pass_handler(zone, records): def pass_handler(zone, records):
pass pass
@ -323,7 +329,7 @@ HANDLERS = {
"srv_records": srv_records_handler, "srv_records": srv_records_handler,
"a_records": a_records_handler, "a_records": a_records_handler,
"aaaa_records": aaaa_records_handler, "aaaa_records": aaaa_records_handler,
"name": pass_handler,
"cname_records": cname_records_handler, "cname_records": cname_records_handler,
"dname_records": pass_handler, "dname_records": pass_handler,
"name": pass_handler,
} }