Better handle when we try to list a zone that does not exist.

This commit is contained in:
Jeffrey Forman 2011-11-25 17:43:26 -05:00
parent 81f8865dcd
commit 380657d5f5
5 changed files with 55 additions and 23 deletions

View File

@ -17,12 +17,12 @@ def list_zone_records(dns_hostname, zone_name):
# There was an error querying the server for the specific zone. # There was an error querying the server for the specific zone.
# Example: a zone that does not exist on the server. # Example: a zone that does not exist on the server.
return { 'errors' : 'Encountered a FormError when querying %s on %s' % (zone_name, dns_hostname) } 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. # 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 = zone.nodes.keys()
names.sort() # Sort the array alphabetically names.sort()
record_array = [] record_array = []
for current_name in names: for current_name in names:
current_record = zone[current_name].to_text(current_name) 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"])) dns_update.replace(hostname, int(form_data["ttl"]), str(form_data["record_type"]), str(form_data["data"]))
try: try:
print "in try in add_forward_record"
response = dns.query.tcp(dns_update, form_data["dns_server"]) response = dns.query.tcp(dns_update, form_data["dns_server"])
except dns.tsig.BadPeerKey: 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 return response
def add_reverse_record(form_data, zone_keyring): 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. """Add a DNS record with data from a FormAddRecord object.
If a reverse PTR record is requested, this will be added too.""" 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 = {} response = {}
forward_response = add_forward_record(form_data, keyring) forward_response = add_forward_record(form_data, keyring)
response["forward_response"] = forward_response response["forward_response"] = forward_response

View File

@ -40,6 +40,26 @@ class BindServer(models.Model):
return return_array 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): class Key(models.Model):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)

View File

@ -3,7 +3,7 @@
from bcommon.models import BindServer, Key from bcommon.models import BindServer, Key
from django.template import Context from django.template import Context
from django.shortcuts import render_to_response, redirect 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 bcommon.forms import FormAddRecord
from django.template import RequestContext from django.template import RequestContext
@ -16,14 +16,13 @@ RE_UNICODEARRAY = re.compile(r"u'(.*?)'")
def home_index(request): def home_index(request):
return render_to_response('index.htm') return render_to_response('index.htm')
def list_servers(request): def view_server_list(request):
""" List the DNS servers configured in the Django DB. """ """ List the DNS servers configured in the Django DB. """
server_list = BindServer.objects.all().order_by('hostname') server_list = BindServer.objects.all().order_by('hostname')
return render_to_response('bcommon/list_servers.htm', return render_to_response('bcommon/list_servers.htm',
{ 'server_list' : server_list }, { "server_list" : server_list},
context_instance=RequestContext(request)) context_instance=RequestContext(request))
def view_server_zones(request, dns_hostname): def view_server_zones(request, dns_hostname):
""" Display the list of DNS zones a particular DNS host provides. """ """ Display the list of DNS zones a particular DNS host provides. """
try: try:
@ -45,18 +44,18 @@ def view_server_zones(request, dns_hostname):
context_instance=RequestContext(request)) context_instance=RequestContext(request))
def view_zone_records(request, dns_hostname, zone_name): def view_zone_records(request, dns_hostname, zone_name):
""" Display the list of records a particular zone on a DNS host provides. """ """ Display the list of records for a a particular zone."""
record_array = list_zone_records(dns_hostname, zone_name) try:
if 'errors' in record_array: this_server = BindServer.objects.get(hostname=dns_hostname)
return render_to_response('bcommon/list_server_zones.htm', zone_array = this_server.list_zone_records(zone_name)
{ 'errors' : record_array['errors'], except Exception, err:
'error_context' : record_array['error_context']}, return render_to_response('bcommon/list_zone.htm',
{ 'errors' : err},
context_instance=RequestContext(request)) context_instance=RequestContext(request))
return render_to_response('bcommon/list_zone.htm', return render_to_response('bcommon/list_zone.htm',
{ 'record_array' : record_array, { 'zone_array' : zone_array,
'dns_hostname' : dns_hostname, 'dns_hostname' : dns_hostname,
'rr_server' : dns_hostname,
'rr_domain' : zone_name}, 'rr_domain' : zone_name},
context_instance=RequestContext(request)) context_instance=RequestContext(request))
@ -83,8 +82,12 @@ def view_add_record_result(request):
{ 'form' : form }, { 'form' : form },
context_instance=RequestContext(request)) context_instance=RequestContext(request))
try:
add_record_response = add_record(cd) 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', return render_to_response('bcommon/add_record_result.htm',
{ 'response' : add_record_response }, { 'response' : add_record_response },

View File

@ -2,6 +2,7 @@
{% block body %} {% block body %}
{% if not errors %}
<table> <table>
<tr> <tr>
<th></th> <th></th>
@ -13,9 +14,9 @@
</tr> </tr>
<form action="/delete_record/" method="post">{% csrf_token %} <form action="/delete_record/" method="post">{% csrf_token %}
<input type="hidden" name="rr_server" value="{{ rr_server }}"> <input type="hidden" name="dns_server" value="{{ rr_server }}">
<input type="hidden" name="rr_domain" value="{{ rr_domain }}"> <input type="hidden" name="zone_name" value="{{ rr_domain }}">
{% for current_record in record_array %} {% for current_record in zone_array %}
<tr> <tr>
<td><input type="checkbox" name="rr_array" value="{{ current_record.rr_name }}.{{ rr_domain }}"></td> <td><input type="checkbox" name="rr_array" value="{{ current_record.rr_name }}.{{ rr_domain }}"></td>
<td>{{ current_record.rr_name }}</td> <td>{{ current_record.rr_name }}</td>
@ -30,4 +31,5 @@
<input type="submit" value="Delete Selected"/> <input type="submit" value="Delete Selected"/>
</form> </form>
{% endif %}
{% endblock body %} {% endblock body %}

View File

@ -8,7 +8,7 @@ admin.autodiscover()
urlpatterns = patterns('', urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)), (r'^admin/', include(admin.site.urls)),
(r'^$', 'bcommon.views.home_index'), (r'^$', 'bcommon.views.home_index'),
(r'^info/$', 'bcommon.views.list_servers'), (r'^info/$', 'bcommon.views.view_server_list'),
(r'^info/(?P<dns_hostname>[a-zA-Z0-9.-]+)/$', 'bcommon.views.view_server_zones'), (r'^info/(?P<dns_hostname>[a-zA-Z0-9.-]+)/$', 'bcommon.views.view_server_zones'),
(r'^info/(?P<dns_hostname>[a-zA-Z0-9.-]+)/(?P<zone_name>[a-zA-Z0-9.-]+)/$', 'bcommon.views.view_zone_records'), (r'^info/(?P<dns_hostname>[a-zA-Z0-9.-]+)/(?P<zone_name>[a-zA-Z0-9.-]+)/$', 'bcommon.views.view_zone_records'),