diff --git a/binder/bcommon/helpers.py b/binder/bcommon/helpers.py new file mode 100644 index 0000000..7b52dd1 --- /dev/null +++ b/binder/bcommon/helpers.py @@ -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 diff --git a/binder/bcommon/views.py b/binder/bcommon/views.py index 9f93769..bed16d1 100644 --- a/binder/bcommon/views.py +++ b/binder/bcommon/views.py @@ -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, diff --git a/binder/templates/bcommon/list_server_zones.htm b/binder/templates/bcommon/list_server_zones.htm index 017e41f..6b177b0 100644 --- a/binder/templates/bcommon/list_server_zones.htm +++ b/binder/templates/bcommon/list_server_zones.htm @@ -1,12 +1,14 @@ {% extends "base.htm" %} {% block body %} +{% if not errors %} DNS Server Zone List: - + +{% endif %} {% endblock body %} diff --git a/binder/templates/bcommon/list_servers.htm b/binder/templates/bcommon/list_servers.htm index f3ccc8a..b43d121 100644 --- a/binder/templates/bcommon/list_servers.htm +++ b/binder/templates/bcommon/list_servers.htm @@ -1,13 +1,14 @@ {% extends "base.htm" %} + {% block pageheader %} Server List: {% endblock pageheader %} + {% block body %} - {% endblock body %} diff --git a/binder/templates/index.htm b/binder/templates/index.htm index e7fda9f..9863d31 100644 --- a/binder/templates/index.htm +++ b/binder/templates/index.htm @@ -3,7 +3,8 @@ {% block body %} {% endblock body %} diff --git a/binder/urls.py b/binder/urls.py index 88fdf4a..ba954fb 100644 --- a/binder/urls.py +++ b/binder/urls.py @@ -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[a-zA-Z0-9.-]+)/$', 'bcommon.views.list_server_zones'), + (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.list_zone'), )