From 380657d5f5091307b11c200644c5981d1c42d014 Mon Sep 17 00:00:00 2001 From: Jeffrey Forman Date: Fri, 25 Nov 2011 17:43:26 -0500 Subject: [PATCH] Better handle when we try to list a zone that does not exist. --- binder/bcommon/helpers.py | 17 +++++++++----- binder/bcommon/models.py | 20 +++++++++++++++++ binder/bcommon/views.py | 31 ++++++++++++++------------ binder/templates/bcommon/list_zone.htm | 8 ++++--- binder/urls.py | 2 +- 5 files changed, 55 insertions(+), 23 deletions(-) diff --git a/binder/bcommon/helpers.py b/binder/bcommon/helpers.py index 2cee2e1..dc1ae65 100644 --- a/binder/bcommon/helpers.py +++ b/binder/bcommon/helpers.py @@ -17,12 +17,12 @@ def list_zone_records(dns_hostname, zone_name): # There was an error querying the server for the specific zone. # Example: a zone that does not exist on the server. return { 'errors' : 'Encountered a FormError when querying %s on %s' % (zone_name, dns_hostname) } - except socket.gaierror, e: + except socket.gaierror, err: # TODO: Need to better handle errors here. - return { 'errors' : "Problems querying DNS server %s: %s" % (dns_hostname, e) } + return { 'errors' : "Problems querying DNS server %s: %s" % (dns_hostname, err) } names = zone.nodes.keys() - names.sort() # Sort the array alphabetically + names.sort() record_array = [] for current_name in names: current_record = zone[current_name].to_text(current_name) @@ -46,10 +46,13 @@ def add_forward_record(form_data, zone_keyring): dns_update.replace(hostname, int(form_data["ttl"]), str(form_data["record_type"]), str(form_data["data"])) try: + print "in try in add_forward_record" response = dns.query.tcp(dns_update, form_data["dns_server"]) except dns.tsig.BadPeerKey: - response = "There was a problem adding your forward record due to a TSIG key issue." + print "in except dns.tsig.badpeerkey" + raise Exception("There was a problem adding your forward record due to a TSIG key issue.") + print "add forward record response: %s" % response return response def add_reverse_record(form_data, zone_keyring): @@ -69,7 +72,11 @@ def add_record(form_data): """Add a DNS record with data from a FormAddRecord object. If a reverse PTR record is requested, this will be added too.""" - keyring = create_keyring(form_data["key_name"]) + try: + keyring = create_keyring(form_data["key_name"]) + except Exception, err: + raise Exception("Error creating keyring in add_record: %s" % err) + response = {} forward_response = add_forward_record(form_data, keyring) response["forward_response"] = forward_response diff --git a/binder/bcommon/models.py b/binder/bcommon/models.py index 5d92a56..4128bad 100644 --- a/binder/bcommon/models.py +++ b/binder/bcommon/models.py @@ -40,6 +40,26 @@ class BindServer(models.Model): return return_array + def list_zone_records(self, zone): + """Given a zone, produce an array of dicts containing + each RR record and its attributes.""" + try: + zone = dns.zone.from_xfr(dns.query.xfr(self.hostname, zone)) + except dns.exception.FormError, err: + raise Exception("The zone requested %s is not found on %s." % (zone, self.hostname)) + + names = zone.nodes.keys() + names.sort() + record_array = [] + for current_name in names: + current_record = zone[current_name].to_text(current_name) + for split_record in current_record.split("\n"): # Split the records on the newline + record_array.append({'rr_name' : split_record.split(" ")[0], + 'rr_ttl' : split_record.split(" ")[1], + 'rr_class' : split_record.split(" ")[2], + 'rr_type' : split_record.split(" ")[3], + 'rr_data' : split_record.split(" ")[4]}) + return record_array class Key(models.Model): name = models.CharField(max_length=50) diff --git a/binder/bcommon/views.py b/binder/bcommon/views.py index ca80c91..40a4a60 100644 --- a/binder/bcommon/views.py +++ b/binder/bcommon/views.py @@ -3,7 +3,7 @@ from bcommon.models import BindServer, Key from django.template import Context from django.shortcuts import render_to_response, redirect -from bcommon.helpers import list_zone_records, add_record, delete_record +from bcommon.helpers import add_record, delete_record from bcommon.forms import FormAddRecord from django.template import RequestContext @@ -16,14 +16,13 @@ RE_UNICODEARRAY = re.compile(r"u'(.*?)'") def home_index(request): return render_to_response('index.htm') -def list_servers(request): +def view_server_list(request): """ List the DNS servers configured in the Django DB. """ server_list = BindServer.objects.all().order_by('hostname') return render_to_response('bcommon/list_servers.htm', - { 'server_list' : server_list }, + { "server_list" : server_list}, context_instance=RequestContext(request)) - def view_server_zones(request, dns_hostname): """ Display the list of DNS zones a particular DNS host provides. """ try: @@ -45,18 +44,18 @@ def view_server_zones(request, dns_hostname): context_instance=RequestContext(request)) def view_zone_records(request, dns_hostname, zone_name): - """ Display the list of records a particular zone on a DNS host provides. """ - record_array = list_zone_records(dns_hostname, zone_name) - if 'errors' in record_array: - return render_to_response('bcommon/list_server_zones.htm', - { 'errors' : record_array['errors'], - 'error_context' : record_array['error_context']}, + """ Display the list of records for a a particular zone.""" + try: + this_server = BindServer.objects.get(hostname=dns_hostname) + zone_array = this_server.list_zone_records(zone_name) + except Exception, err: + return render_to_response('bcommon/list_zone.htm', + { 'errors' : err}, context_instance=RequestContext(request)) return render_to_response('bcommon/list_zone.htm', - { 'record_array' : record_array, + { 'zone_array' : zone_array, 'dns_hostname' : dns_hostname, - 'rr_server' : dns_hostname, 'rr_domain' : zone_name}, context_instance=RequestContext(request)) @@ -83,8 +82,12 @@ def view_add_record_result(request): { 'form' : form }, context_instance=RequestContext(request)) - - add_record_response = add_record(cd) + try: + add_record_response = add_record(cd) + except Exception, err: + return render_to_response('bcommon/add_record_result.htm', + { "errors" : err }, + context_instance=RequestContext(request)) return render_to_response('bcommon/add_record_result.htm', { 'response' : add_record_response }, diff --git a/binder/templates/bcommon/list_zone.htm b/binder/templates/bcommon/list_zone.htm index 27073ee..69eb1bb 100644 --- a/binder/templates/bcommon/list_zone.htm +++ b/binder/templates/bcommon/list_zone.htm @@ -2,6 +2,7 @@ {% block body %} +{% if not errors %} @@ -13,9 +14,9 @@ {% csrf_token %} - - -{% for current_record in record_array %} + + +{% for current_record in zone_array %} @@ -30,4 +31,5 @@ +{% endif %} {% endblock body %} diff --git a/binder/urls.py b/binder/urls.py index 3a87ef5..4536fc3 100644 --- a/binder/urls.py +++ b/binder/urls.py @@ -8,7 +8,7 @@ admin.autodiscover() urlpatterns = patterns('', (r'^admin/', include(admin.site.urls)), (r'^$', 'bcommon.views.home_index'), - (r'^info/$', 'bcommon.views.list_servers'), + (r'^info/$', 'bcommon.views.view_server_list'), (r'^info/(?P[a-zA-Z0-9.-]+)/$', 'bcommon.views.view_server_zones'), (r'^info/(?P[a-zA-Z0-9.-]+)/(?P[a-zA-Z0-9.-]+)/$', 'bcommon.views.view_zone_records'),
{{ current_record.rr_name }}