From: Frank Brehm Date: Fri, 9 Feb 2018 14:04:21 +0000 (+0100) Subject: Möglichkeit, sich die Anzahl der Record pro Zone anzeigen zu lassen X-Git-Tag: 0.1.2~6^2~23 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=29f2c65169e8e0ad9bcb58d68ea1fa9aaac01d8d;p=pixelpark%2Fadmin-tools.git Möglichkeit, sich die Anzahl der Record pro Zone anzeigen zu lassen --- diff --git a/pp_lib/pdns_list_zones.py b/pp_lib/pdns_list_zones.py index e7131e2..ffcaca3 100644 --- a/pp_lib/pdns_list_zones.py +++ b/pp_lib/pdns_list_zones.py @@ -21,7 +21,7 @@ from .common import compare_fqdn from .pdns_app import PpPDNSAppError, PpPDNSApplication from .pdns_zone import PdnsApiZone -__version__ = '0.3.2' +__version__ = '0.4.1' LOG = logging.getLogger(__name__) @@ -39,6 +39,8 @@ class PpPDNSListZonesApp(PpPDNSApplication): # ------------------------------------------------------------------------- def __init__(self, appname=None, version=__version__): + self.show_numbers = False + description = textwrap.dedent('''\ Lists all available zones from given PowerDNS API. ''') @@ -49,6 +51,34 @@ class PpPDNSListZonesApp(PpPDNSApplication): self.initialized = True + # ------------------------------------------------------------------------- + def init_arg_parser(self): + """ + Method to initiate the argument parser. + + This method should be explicitely called by all init_arg_parser() + methods in descendant classes. + """ + + super(PpPDNSListZonesApp, self).init_arg_parser() + + self.arg_parser.add_argument( + '-N', '--numbers', action='store_true', dest='show_numbers', + help="Show number of Ressource Record Sets and Records for each zone", + ) + + # ------------------------------------------------------------------------- + def perform_arg_parser(self): + """ + Public available method to execute some actions after parsing + the command line parameters. + """ + + super(PpPDNSListZonesApp, self).perform_arg_parser() + + if self.args.show_numbers: + self.show_numbers = True + # ------------------------------------------------------------------------- def _run(self): @@ -62,15 +92,37 @@ class PpPDNSListZonesApp(PpPDNSApplication): if len(zone.name_unicode) > len_zone: len_zone = len(zone.name_unicode) - tpl = PdnsApiZone.get_list_template() + if self.show_numbers: + tpl = PdnsApiZone.get_list_template(show_numbers=True) + else: + tpl = PdnsApiZone.get_list_template(show_numbers=False) line = tpl.format( name="Zone", len_zone=len_zone, kind="Type", serial="Serial", - dnssec="DNSSEC", account="Account information") + dnssec="DNSSEC", nr_rrsets='RR Sets', nr_records='Records', + account="Account information") print(line) print('-' * len(line)) + nr_zones = 0 + nr_rrsets = 0 + nr_records = 0 for zone in sorted(zone_list, key=lambda x: cmp_to_key(compare_fqdn)(x.name_unicode)): - print(zone.get_line(len_zone)) + if self.show_numbers: + nr_zones += 1 + zone_complete = self.get_api_zone(zone.name) + for rrset in zone_complete.rrsets: + nr_rrsets += 1 + for record in rrset.records: + nr_records += 1 + print(zone.get_line(len_zone, zone_complete.rrsets)) + else: + print(zone.get_line(len_zone)) + + print('-' * len(line)) + line = tpl.format( + name="Total:", len_zone=len_zone, kind="", serial=nr_zones, + dnssec="Zones", nr_rrsets=nr_rrsets, nr_records=nr_records, account="") + print(line) # ============================================================================= diff --git a/pp_lib/pdns_zone.py b/pp_lib/pdns_zone.py index f082465..64f8eeb 100644 --- a/pp_lib/pdns_zone.py +++ b/pp_lib/pdns_zone.py @@ -23,7 +23,7 @@ from .common import RE_DOT_AT_END from .obj import PpBaseObjectError, PpBaseObject from .pdns_record import PdnsApiRrset, PdnsSoaData -__version__ = '0.5.4' +__version__ = '0.5.5' LOG = logging.getLogger(__name__) @@ -348,14 +348,21 @@ class PdnsApiZone(PpBaseObject): # ------------------------------------------------------------------------- @classmethod - def get_list_template(cls): + def get_list_template(cls, show_numbers=False): - return "{name:<{len_zone}} {kind:<8} {serial:>10} {dnssec:<6} {account}" + tpl = "{name:<{len_zone}} {kind:<8} {serial:>10} {dnssec:<6}" + if show_numbers: + tpl += ' {nr_rrsets:>8} {nr_records:>8} ' + tpl += ' {account}' + return tpl # ------------------------------------------------------------------------- - def get_line(self, len_zone=20): + def get_line(self, len_zone=20, rrsets=None): - tpl = self.get_list_template() + if rrsets: + tpl = self.get_list_template(True) + else: + tpl = self.get_list_template(False) params = { 'name': self.name_unicode, @@ -364,12 +371,22 @@ class PdnsApiZone(PpBaseObject): 'serial': self.serial, 'dnssec': 'no', 'account': '', + 'nr_rrsets': '', + 'nr_records': '', } if self.dnssec: params['dnssec'] = 'yes' if self.account: params['account'] = self.account + if rrsets: + params['nr_rrsets'] = 0 + params['nr_records'] = 0 + for rrset in rrsets: + params['nr_rrsets'] += 1 + for record in rrset.records: + params['nr_records'] += 1 + return tpl.format(**params) # -------------------------------------------------------------------------