clean up some string casting for adding cname and forward/reverse records.

This commit is contained in:
Jeffrey Forman 2012-11-21 22:08:37 -05:00
parent 751dae3356
commit a5af78d18d
2 changed files with 71 additions and 25 deletions

View File

@ -12,8 +12,20 @@ from binder import exceptions, models
re_IPADDRESS = re.compile(r"\d+.\d+.\d+.\d+") re_IPADDRESS = re.compile(r"\d+.\d+.\d+.\d+")
def add_forward_record(dns_server, zone_name, record_name, record_type, record_data, ttl, keyring): def add_forward_record(dns_server, zone_name, record_name, record_type, record_data, ttl, keyring):
"""Take in data from FormAddRecord and a keyring object, """ Create a forward DNS record given passed arguments.
return a response from the DNS server about adding the record."""
Args:
String dns_server
String zone_name
String record_name (just record name, not FQDN)
String record_type (A, AAAA, etc)
String record_data (IP address)
Int ttl
Dict keyring object
Return:
String representation of DNS update output from record creation.
"""
dns_update = dns.update.Update(zone_name, keyring = keyring) dns_update = dns.update.Update(zone_name, keyring = keyring)
dns_update.replace(record_name, ttl, record_type, record_data) dns_update.replace(record_name, ttl, record_type, record_data)
@ -22,7 +34,21 @@ def add_forward_record(dns_server, zone_name, record_name, record_type, record_d
return output return output
def add_reverse_record(dns_server, zone_name, record_name, record_data, ttl, keyring): def add_reverse_record(dns_server, zone_name, record_name, record_data, ttl, keyring):
""" Given passed arguments, add/update a reverse PTR record.""" """ Create a reverse DNS record (PTR) given passed arguments.
Args:
String dns_server
String zone_name
String record_name (just record name, not FQDN)
String record_type (A, AAAA, etc)
String record_data (IP address)
Int ttl
Dict keyring object
Return:
String representation of DNS update output from record creation.
"""
reverse_ip_fqdn = str(dns.reversename.from_address(record_data)) reverse_ip_fqdn = str(dns.reversename.from_address(record_data))
reverse_ip = re.search(r"([0-9]+).(.*).$", reverse_ip_fqdn).group(1) reverse_ip = re.search(r"([0-9]+).(.*).$", reverse_ip_fqdn).group(1)
reverse_domain = re.search(r"([0-9]+).(.*).$", reverse_ip_fqdn).group(2) reverse_domain = re.search(r"([0-9]+).(.*).$", reverse_ip_fqdn).group(2)
@ -33,33 +59,46 @@ def add_reverse_record(dns_server, zone_name, record_name, record_data, ttl, key
return output return output
def add_record(form_data): def add_record(dns_server, zone_name, record_name, record_type, record_data, ttl, key_name, create_reverse):
"""Add a DNS record with data from a FormAddRecord dict. """ Create DNS record(s) given passed arguments.
If a reverse PTR record is requested, this will be added too."""
if form_data["key_name"]: Args:
this_key = models.Key.objects.get(name=form_data["key_name"]) String dns_server
keyring = keyutils.create_keyring(this_key.name, this_key.data) String zone_name
else: String record_name (just record name, not FQDN)
String record_type (A, AAAA, etc)
String record_data (IP address)
Int ttl
String key_name (from Key model)
Boolean create_reverse
Return:
Dict containing {description, output} from record creation
"""
if key_name is None:
keyring = None keyring = None
else:
this_key = models.Key.objects.get(name=key_name)
keyring = keyutils.create_keyring(this_key.name, this_key.data)
response = [] response = []
response.append({ "description" : "Forward Record Added: %(record_name)s.%(zone_name)s" % form_data, response.append({ "description" : "Forward Record Added: %s.%s" % (record_name, zone_name),
"output" : add_forward_record(str(form_data["dns_server"]), "output" : add_forward_record(dns_server,
str(form_data["zone_name"]), zone_name,
str(form_data["record_name"]), record_name,
str(form_data["record_type"]), record_type,
str(form_data["record_data"]), record_data,
form_data["ttl"], ttl,
keyring)}) keyring)})
if form_data["create_reverse"]: if create_reverse:
response.append({ "description" : "Reverse Record Added: %(record_data)s" % form_data, response.append({ "description" : "Reverse Record Added: %s" % record_data,
"output" : add_reverse_record(str(form_data["dns_server"]), "output" : add_reverse_record(dns_server,
str(form_data["zone_name"]), zone_name,
str(form_data["record_name"]), record_name,
str(form_data["record_data"]), record_data,
form_data["ttl"], ttl,
keyring)}) keyring)})
return response return response

View File

@ -66,7 +66,14 @@ def view_add_record_result(request):
if form.is_valid(): if form.is_valid():
form_cleaned = form.cleaned_data form_cleaned = form.cleaned_data
try: try:
add_record_response = helpers.add_record(form_cleaned) add_record_response = helpers.add_record(form_cleaned["dns_server"],
str(form_cleaned["zone_name"]),
str(form_cleaned["record_name"]),
str(form_cleaned["record_type"]),
str(form_cleaned["record_data"]),
form_cleaned["ttl"],
form_cleaned["key_name"],
form_cleaned["create_reverse"])
except exceptions.RecordException, err: except exceptions.RecordException, err:
# TODO: Start using this exception. # TODO: Start using this exception.
errors = err errors = err