pvl.hosts-reverse: move options out of pvl.hosts.zone
authorTero Marttila <tero.marttila@aalto.fi>
Thu, 26 Feb 2015 15:29:03 +0200
changeset 489 7f1bd12e0d54
parent 488 502ee5fcf91c
child 490 805645dbb9bb
pvl.hosts-reverse: move options out of pvl.hosts.zone
bin/pvl.hosts-forward
bin/pvl.hosts-reverse
pvl/hosts/config.py
pvl/hosts/tests.py
pvl/hosts/zone.py
--- a/bin/pvl.hosts-forward	Thu Feb 26 15:26:26 2015 +0200
+++ b/bin/pvl.hosts-forward	Thu Feb 26 15:29:03 2015 +0200
@@ -32,7 +32,9 @@
 
     # process
     try:
-        for rr in pvl.hosts.zone.apply_hosts_forward(options, hosts, options.forward_zone):
+        for rr in pvl.hosts.zone.apply_hosts_forward(hosts, options.forward_zone,
+                add_origin  = options.add_origin,
+        ):
             print unicode(rr)
     except pvl.hosts.HostError as error:
         log.error("%s", error)
--- a/bin/pvl.hosts-reverse	Thu Feb 26 15:26:26 2015 +0200
+++ b/bin/pvl.hosts-reverse	Thu Feb 26 15:29:03 2015 +0200
@@ -28,14 +28,22 @@
         log.fatal("required --reverse-zone")
         return 1
 
+    if options.unknown_host and not options.hosts_domain:
+        log.fatal("--unknown-host requires --hosts-domain=")
+        return 1
+
     hosts = pvl.hosts.apply(options, args)
 
     # process
     prefix = pvl.dns.parse_prefix(options.reverse_zone)
 
     try:
-        for rr in pvl.hosts.zone.apply_hosts_reverse(options, hosts, prefix):
+        for rr in pvl.hosts.zone.apply_hosts_reverse(hosts, prefix,
+                unknown_host    = options.unknown_host,
+                unknown_domain  = options.hosts_domain,
+        ):
             print unicode(rr)
+
     except pvl.hosts.HostError as error:
         log.error("%s", error)
         return 3
--- a/pvl/hosts/config.py	Thu Feb 26 15:26:26 2015 +0200
+++ b/pvl/hosts/config.py	Thu Feb 26 15:29:03 2015 +0200
@@ -313,6 +313,8 @@
 def apply (options, args):
     """
         Load Hosts from arguments.
+
+        Exits with status=2 if loading the confs fails.
     """
     
     try:
@@ -328,6 +330,4 @@
         sys.exit(2)
 
     # stable ordering
-    hosts = sorted(hosts, key=Host.sort_key)
-    
-    return hosts
+    return sorted(hosts, key=Host.sort_key)
--- a/pvl/hosts/tests.py	Thu Feb 26 15:26:26 2015 +0200
+++ b/pvl/hosts/tests.py	Thu Feb 26 15:29:03 2015 +0200
@@ -246,7 +246,7 @@
                 )
         ]
 
-        self.assertZoneEquals(zone.apply_hosts_forward(self.options, hosts, 'domain'), {
+        self.assertZoneEquals(zone.apply_hosts_forward(hosts, 'domain'), {
             ('foo', 'A'): ['192.0.2.1'],
             ('foo', 'AAAA'): ['2001:db8::c000:201'],
             ('test', 'CNAME'): ['foo'],
@@ -264,7 +264,7 @@
         ]
         
         with self.assertRaises(zone.HostZoneError):
-            self.assertZoneEquals(zone.apply_hosts_forward(self.options, hosts, 'domain'), { })
+            self.assertZoneEquals(zone.apply_hosts_forward(hosts, 'domain'), { })
 
     def testHostsAliasConflict(self):
         hosts = [
@@ -279,11 +279,11 @@
         
         # with A first
         with self.assertRaises(zone.HostZoneError):
-            self.assertZoneEquals(zone.apply_hosts_forward(self.options, hosts, 'domain'), { })
+            self.assertZoneEquals(zone.apply_hosts_forward(hosts, 'domain'), { })
     
         # also with CNAME first
         with self.assertRaises(zone.HostZoneError):
-            self.assertZoneEquals(zone.apply_hosts_forward(self.options, reversed(hosts), 'domain'), { })
+            self.assertZoneEquals(zone.apply_hosts_forward(reversed(hosts), 'domain'), { })
 
     def testHostsAlias4Conflict(self):
         hosts = [
@@ -297,7 +297,7 @@
         ]
         
         with self.assertRaises(zone.HostZoneError):
-            self.assertZoneEquals(zone.apply_hosts_forward(self.options, hosts, 'domain'), { })
+            self.assertZoneEquals(zone.apply_hosts_forward(hosts, 'domain'), { })
     
 
 class TestReverseZone(TestZoneMixin, unittest.TestCase):
@@ -397,7 +397,7 @@
         ]
         
         with self.assertRaises(zone.HostZoneError):
-            self.assertZoneEquals(zone.apply_hosts_reverse(self.options, hosts, ipaddr.IPNetwork('192.0.2.1/24')), { })
+            self.assertZoneEquals(zone.apply_hosts_reverse(hosts, ipaddr.IPNetwork('192.0.2.1/24')), { })
 
     def testHostsGenerateUnknown(self):
         hosts = [
@@ -409,9 +409,10 @@
                 ),
         ]
         
-        self.options.unknown_host = 'ufc'
-        self.options.hosts_domain = 'domain'
-        self.assertZoneEquals(zone.apply_hosts_reverse(self.options, hosts, ipaddr.IPNetwork('192.0.2.1/29')), {
+        self.assertZoneEquals(zone.apply_hosts_reverse(hosts, ipaddr.IPNetwork('192.0.2.1/29'),
+                unknown_host = 'ufc',
+                unknown_domain = 'domain',
+        ), {
             ('1', 'PTR'): ['foo.domain.'],
             ('2', 'PTR'): ['ufc.domain.'],
             ('3', 'PTR'): ['ufc.domain.'],
--- a/pvl/hosts/zone.py	Thu Feb 26 15:26:26 2015 +0200
+++ b/pvl/hosts/zone.py	Thu Feb 26 15:29:03 2015 +0200
@@ -125,16 +125,22 @@
     else:
         log.info("%s %s[%s]: omit", host, prefix, ip)
  
-def apply_hosts_forward (options, hosts, origin) :
+def apply_hosts_forward (hosts, origin,
+        add_origin  = False,
+) :
     """
         Generate DNS ZoneRecords for for hosts within the given zone origin.
 
         Verifies that there are no overlapping name/type records, or CNAME records from aliases.
 
+            hosts: [Host]       - Host's to render
+            origin: str         - generate records relative to given zone origin
+            add_origin: bool    - generate an explicit $ORIGIN directive
+
         Yields ZoneRecords in Host-order
     """
 
-    if options.add_origin :
+    if add_origin:
         yield pvl.dns.ZoneDirective.build(None, 'ORIGIN', origin)
 
     by_name = dict()
@@ -158,15 +164,23 @@
             # preserve ordering
             yield rr
 
-def apply_hosts_reverse (options, hosts, prefix) :
+def apply_hosts_reverse (hosts, prefix,
+        unknown_host    = None,
+        unknown_domain  = None,
+) :
     """
         Generate DNS ZoneRecords within the given prefix's reverse-dns zone for hosts.
 
+            hosts: [Host]               - Host's to render PTRs for
+            prefix: ipaddr.IPNetwork    - IPv4/IPv6 prefix to render PTRs for within associated in-addr.arpa/ip6.arpa zone
+            unknown_host: str           - render a PTR to the given host @unknown_domain for unassigned IPs within prefix
+            unknown_domain: str         - required if unknown_host is given
+
         Yields ZoneRecords in IPAddress-order
     """
         
-    if options.unknown_host and not options.hosts_domain:
-        raise Exception("--unknown-host requires --hosts-domain")
+    if unknown_host and not unknown_domain:
+        raise ValueError("unknown_host requires unknown_domain=")
     
     # collect data for records
     by_ip = dict()
@@ -179,7 +193,7 @@
             # do not retain order
             by_ip[ip] = rr
 
-    if options.unknown_host :
+    if unknown_host :
         # enumerate all of them
         iter_ips = prefix.iterhosts()
     else :
@@ -189,12 +203,12 @@
         if ip in by_ip :
             yield by_ip[ip]
 
-        elif options.unknown_host:
+        elif unknown_host:
             # synthesize a record
             label = pvl.dns.reverse_label(prefix, ip)
-            fqdn = pvl.dns.fqdn(options.unknown_host, options.hosts_domain)
+            fqdn = pvl.dns.fqdn(unknown_host, unknown_domain)
 
-            log.info("%s %s[%s]: unused PTR %s", options.unknown_host, ip, prefix, fqdn)
+            log.info("%s %s[%s]: unused PTR %s", unknown_host, ip, prefix, fqdn)
 
             yield pvl.dns.ZoneRecord.PTR(label, fqdn)