work on issue 1, handle errors when querying server for list of zones

handles errors such as connection refused, no host found, etc.
This commit is contained in:
Jeffrey Forman 2011-04-23 11:19:01 -04:00
parent a69baae308
commit 27a121ca0a
6 changed files with 48 additions and 38 deletions

26
binder/bcommon/helpers.py Normal file
View File

@ -0,0 +1,26 @@
import urllib2
from BeautifulSoup import BeautifulStoneSoup as BS
import re
def list_server_zones(dns_hostname):
# I should take the dns_hostname here, get the object from the DB,
# and use the status port attribute for the urllib2 query.
myreq = urllib2.Request("http://%s:853" % dns_hostname)
try:
http_request = urllib2.urlopen(myreq)
except urllib2.URLError, err_reason: # Error retrieving zone list.
return { 'errors' : err_reason, 'error_context' : "Trying to retrieve zone list from %s" % dns_hostname }
return_array = []
xmloutput = http_request.read()
mysoup = BS(xmloutput)
zones = mysoup.findAll('zone')
for current_zone in zones: # Interate over found zones
zone_name = current_zone.find('name').contents[0]
try: # Is this zone of 'IN' type?
in_zone = re.search(r"(.*)\/IN", zone_name).group(1)
return_array.append(in_zone)
except:
pass
return return_array

View File

@ -3,10 +3,8 @@
from bcommon.models import BindServer
from django.template import Context
from django.shortcuts import render_to_response, redirect
from bcommon.helpers import list_server_zones
import urllib2
from BeautifulSoup import BeautifulStoneSoup as BS
import re
import dns.query
import dns.zone
@ -21,30 +19,12 @@ def list_servers(request):
{ 'server_list' : server_list })
def list_server_zones(request, dns_hostname):
# I should take the dns_hostname here, get the object from the DB,
# and use the status port attribute for the urllib2 query.
myreq = urllib2.Request("http://%s:853" % dns_hostname)
try:
http_request = urllib2.urlopen(myreq)
except urllib2.URLError, err_reason: # Error retrieving zone list.
server_list = BindServer.objects.all().order_by('hostname')
return render_to_response('bcommon/list_servers.htm',
{ 'server_list' : server_list,
'errors' : err_reason,
'error_context' : "Trying to retrieve zone list from %s" % dns_hostname})
xmloutput = http_request.read()
mysoup = BS(xmloutput)
zones = mysoup.findAll('zone')
zone_array = []
for current_zone in zones: # Interate over found zones
zone_name = current_zone.find('name').contents[0]
try: # Is this zone of 'IN' type?
in_zone = re.search(r"(.*)\/IN", zone_name).group(1)
zone_array.append(in_zone)
except:
pass
def view_server_zones(request, dns_hostname):
zone_array = list_server_zones(dns_hostname)
if 'errors' in zone_array:
return render_to_response('bcommon/list_server_zones.htm',
{ 'errors' : zone_array['errors'],
'error_context' : zone_array['error_context'] })
return render_to_response('bcommon/list_server_zones.htm',
{ 'zone_array' : zone_array,

View File

@ -1,6 +1,7 @@
{% extends "base.htm" %}
{% block body %}
{% if not errors %}
DNS Server Zone List:
<ul>
@ -8,5 +9,6 @@ DNS Server Zone List:
<li> <a href="/info/{{ dns_hostname }}/{{ current_zone }}/"> {{ current_zone }} </li>
{% endfor %}
</ul>
{% endif %}
{% endblock body %}

View File

@ -1,13 +1,14 @@
{% extends "base.htm" %}
{% block pageheader %}
Server List:
{% endblock pageheader %}
{% block body %}
<ul>
{% for current_server in server_list %}
<li> <a href="/info/{{ current_server }}/"> {{ current_server }} </li>
{% endfor %}
</ul>
{% endblock body %}

View File

@ -3,7 +3,8 @@
{% block body %}
<ul>
<a href="/info/">Server List</a>
<li><a href="/info/">Server List</a></li>
<li><a href="/modify/add_record">Add Resource Record</a></li>
</ul>
{% endblock body %}

View File

@ -9,7 +9,7 @@ urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^$', 'bcommon.views.home_index'),
(r'^info/$', 'bcommon.views.list_servers'),
(r'^info/(?P<dns_hostname>[a-zA-Z0-9.-]+)/$', 'bcommon.views.list_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.list_zone'),
)