pvl.hosts.zone: make non-CNAME check_conflicts optional
authorTero Marttila <tero.marttila@aalto.fi>
Mon, 02 Mar 2015 13:26:34 +0200
changeset 687 f99f9e3d02cf
parent 686 6416c448a713
child 688 dfc5fcb6a06c
pvl.hosts.zone: make non-CNAME check_conflicts optional
bin/pvl.hosts-forward
pvl/hosts/tests.py
pvl/hosts/zone.py
--- a/bin/pvl.hosts-forward	Mon Mar 02 13:12:35 2015 +0200
+++ b/bin/pvl.hosts-forward	Mon Mar 02 13:26:34 2015 +0200
@@ -25,6 +25,9 @@
     parser.add_option('--add-origin',           action='store_true',
             help="Include $ORIGIN directive in zone")
 
+    parser.add_option('--check-conflicts',      action='store_true',
+            help="Check for dupliate forward records, e.g. multiple A records for the same name")
+
     # input
     options, args = pvl.args.parse(parser, argv)
  
@@ -52,7 +55,8 @@
     # process
     try:
         for rr in pvl.hosts.zone.apply_hosts_forward(hosts, origin,
-                add_origin  = options.add_origin,
+                add_origin      = options.add_origin,
+                check_conflicts = options.check_conflicts,
         ):
             print unicode(rr)
     except pvl.hosts.HostError as error:
--- a/pvl/hosts/tests.py	Mon Mar 02 13:12:35 2015 +0200
+++ b/pvl/hosts/tests.py	Mon Mar 02 13:26:34 2015 +0200
@@ -368,7 +368,7 @@
         ]
         
         with self.assertRaises(zone.HostZoneError):
-            self.assertZoneEquals(zone.apply_hosts_forward(hosts, 'domain'), { })
+            self.assertZoneEquals(zone.apply_hosts_forward(hosts, 'domain', check_conflicts=True), { })
 
     def testHostsAliasConflict(self):
         hosts = [
@@ -401,7 +401,7 @@
         ]
         
         with self.assertRaises(zone.HostZoneError):
-            self.assertZoneEquals(zone.apply_hosts_forward(hosts, 'domain'), { })
+            self.assertZoneEquals(zone.apply_hosts_forward(hosts, 'domain', check_conflicts=True), { })
     
 
 class TestReverseZone(TestZoneMixin, unittest.TestCase):
--- a/pvl/hosts/zone.py	Mon Mar 02 13:12:35 2015 +0200
+++ b/pvl/hosts/zone.py	Mon Mar 02 13:26:34 2015 +0200
@@ -117,7 +117,8 @@
         log.info("%s %s[%s]: omit", host, prefix, ip)
  
 def apply_hosts_forward (hosts, origin,
-        add_origin  = False,
+        add_origin      = False,
+        check_conflicts = False,
 ) :
     """
         Generate DNS ZoneRecords for for hosts within the given zone origin.
@@ -127,6 +128,8 @@
             hosts: [Host]       - Host's to render
             origin: str         - generate records relative to given zone origin
             add_origin: bool    - generate an explicit $ORIGIN directive
+            check_conflits: bool- raise HostZoneError on dupliate records for the same name/type
+                                  overlapping CNAME records will always raise
 
         Yields ZoneRecords in Host-order
     """
@@ -143,7 +146,7 @@
                 raise HostZoneError(host, u"{cname} CNAME conflict with {other}".format(cname=rr.name, other=by_name_type[rr.name, 'CNAME']))
             elif rr.type == 'CNAME' and rr.name in by_name:
                 raise HostZoneError(host, u"{cname} CNAME conflict with {other}".format(cname=rr.name, other=by_name[rr.name]))
-            elif (rr.name, rr.type) in by_name_type:
+            elif check_conflicts and (rr.name, rr.type) in by_name_type:
                 raise HostZoneError(host, u"{name} {type} conflict with {other}".format(type=rr.type, name=rr.name, other=by_name_type[rr.name, rr.type]))
             
             by_name[rr.name] = host