@@ 1,6 1,6 @@
''' Copyright 2009 Peter Sanchez <petersanchez@gmail.com>.
See BSD-LICENSE for license information.
'''
-from api import ZerigoError, ZerigoNotFound, NSZone, NSHost, \
- NSZoneTemplate, NSHostTemplate
-__version__ = '0.3'
+from api import ZerigoError, ZerigoNotFound, NSZone, ZoneHostList, \
+ NSHost, NSZoneTemplate, NSHostTemplate
+__version__ = '0.4'
@@ 118,11 118,6 @@ class ZerigoDNS(object):
req_headers.update(headers)
if method in ('POST', 'PUT'):
req_headers['Content-Type'] = 'application/xml'
- # XXX This is a hack. For some reason letting the Python
- # HTTPConnection.request() method set the Content-Length
- # header (default) was setting the length 2 bytes short.
- # Manually set the length for now until we can look into it
- #req_headers['Content-Length'] = (len(request) + 2)
if self.is_debug:
print 'DEBUG (method): ', method
@@ 249,6 244,79 @@ class ZerigoDNS(object):
return (not self.has_errors())
+class ZoneHostList(list):
+ ''' Wrapper around list to paginate through a Zerigo zone's
+ host list.
+ '''
+ _paginate_by = 100
+
+ def __init__(self, zone, *args, **kwargs):
+ self._zone = zone
+ self._host_paginator = NSHost(
+ zone.user,
+ zone.key,
+ is_secure=zone.is_secure,
+ is_debug=zone.is_debug,
+ )
+ super(ZoneHostList, self).__init__(*args, **kwargs)
+ if self.cur_count == 0 and self.host_count > 0:
+ self.load()
+
+ @property
+ def host_count(self):
+ return self._zone.hosts_count
+
+ @property
+ def cur_count(self):
+ return len(self)
+
+ @property
+ def paginate_by(self):
+ if self._paginate_by <= 0:
+ self._paginate_by = 100
+ elif self._paginate_by > 1000:
+ self._paginate_by = 1000
+ return self._paginate_by
+
+ @property
+ def cur_page(self):
+ return (self.cur_count / self.paginate_by) + 1
+
+ @property
+ def next_page(self):
+ if self.cur_count < self.host_count:
+ if self.cur_count == 0:
+ return self.cur_page
+ return self.cur_page + 1
+ return -1
+
+ @property
+ def num_pages(self):
+ return (self.host_count / self.paginate_by) + 1
+
+ @property
+ def all_pages(self):
+ return [(x + 1) for x in range(self.num_pages)]
+
+ def load(self):
+ if self.next_page > 0:
+ hosts = self._host_paginator.find_all(
+ self._zone, {
+ 'page': self.next_page,
+ 'per_page': self.paginate_by,
+ },
+ )
+ self.extend(hosts)
+ return hosts
+ return []
+
+ def load_all(self):
+ host_tmp = self.load()
+ while host_tmp:
+ host_tmp = self.load()
+ return True
+
+
class NSZone(ZerigoDNS):
''' Class to manage zone entries
'''
@@ 262,17 330,24 @@ class NSZone(ZerigoDNS):
def __setattr__(self, name, val):
if name == 'hosts' and isinstance(val, (list, tuple)):
# Add hosts as NSHost objects
- self.vals[name] = [
- NSHost(
+ self.vals[name] = ZoneHostList(
+ self,
+ [NSHost(
self.user,
self.key,
data=zdata['host'],
is_secure=self.is_secure,
is_debug=self.is_debug,
- ) for zdata in val
- ]
+ ) for zdata in val],
+ )
else:
super(NSZone, self).__setattr__(name, val)
+
+ def load(self, data, reset=True):
+ super(NSZone, self).load(data, reset=reset)
+ host_count = getattr(self, 'host_count', 0)
+ if 'hosts' not in self.vals and host_count > 0:
+ setattr(self, 'hosts', ZoneHostList(self))
def to_xml(self):
return self._to_xml({'zone': self.get_xml_data()})