From c9f4be6cc915dc7e83c610409bafb58f05a9d3c6 Mon Sep 17 00:00:00 2001 From: Jakob Schlyter Date: Sat, 19 Feb 2022 12:37:56 +0100 Subject: [PATCH] implement dns.zone.Zone.get_soa() --- dns/zone.py | 16 ++++++++++++++++ tests/test_zone.py | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/dns/zone.py b/dns/zone.py index 71028ae3..c126f5c2 100644 --- a/dns/zone.py +++ b/dns/zone.py @@ -713,6 +713,22 @@ class Zone(dns.transaction.TransactionManager): if self.get_rdataset(name, dns.rdatatype.NS) is None: raise NoNS + def get_soa(self): + """Get the zone SOA RR. + + Raises ``KeyError`` if there is no SOA record. + + Returns a ``dns.node.Node``. + """ + if self.relativize: + origin_name = dns.name.empty + else: + origin_name = self.origin + soa = self.get_rdataset(origin_name, dns.rdatatype.SOA) + if soa is None: + raise KeyError + return soa[0] + def _compute_digest(self, hash_algorithm, scheme=DigestScheme.SIMPLE): hashinfo = _digest_hashers.get(hash_algorithm) if not hashinfo: diff --git a/tests/test_zone.py b/tests/test_zone.py index 4b5272da..8bdb49fc 100644 --- a/tests/test_zone.py +++ b/tests/test_zone.py @@ -1062,6 +1062,18 @@ class VersionedZoneTestCase(unittest.TestCase): dns.rdatatype.RRSIG, dns.rdatatype.NSEC)) + def testGetSoa(self): + z = dns.zone.from_text(example_text, 'example.', relativize=True, + zone_factory=dns.versioned.Zone) + soa = z.get_soa() + self.assertTrue(soa.rdtype, dns.rdatatype.SOA) + self.assertEqual(soa.serial, 1) + + def testGetSoaEmptyZone(self): + z = dns.zone.Zone('example.') + with self.assertRaises(KeyError): + soa = z.get_soa() + if __name__ == '__main__': unittest.main() -- 2.47.3