From: Frank Brehm Date: Wed, 24 May 2023 13:48:29 +0000 (+0200) Subject: Evaluating all LDAP entries to create. X-Git-Tag: 0.9.0~1^2~24 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=241a4aa24ba86f07107fa1ca894a98d53f58ce60;p=pixelpark%2Fpp-admin-tools.git Evaluating all LDAP entries to create. --- diff --git a/lib/pp_admintools/app/barracuda_sync.py b/lib/pp_admintools/app/barracuda_sync.py index 7b93c35..f0a0c12 100644 --- a/lib/pp_admintools/app/barracuda_sync.py +++ b/lib/pp_admintools/app/barracuda_sync.py @@ -13,6 +13,7 @@ from __future__ import absolute_import import logging import re # import sys +from functools import cmp_to_key from pathlib import Path # Third party modules @@ -27,7 +28,7 @@ from ldap3 import BASE from .ldap import BaseLdapApplication from ..xlate import XLATOR -__version__ = '0.8.5' +__version__ = '0.8.6' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -519,13 +520,21 @@ class BarracudaSyncApp(BaseLdapApplication): # ------------------------------------------------------------------------- def eval_diffs(self): - """Evaluate all entries to create, to change or to modify.""" - LOG.info(_('Evaluating all LDAP entries to create, to change or to modify.')) + """Evaluate all entries to create, to remove or to modify.""" + LOG.info(_('Evaluating all LDAP entries to create, to remove or to modify.')) + + self.eval_entries_to_remove() + self.eval_entries_to_create() - self.aliases_to_create = [] - self.aliases_to_remove = [] self.aliases_to_modify = [] + # ------------------------------------------------------------------------- + def eval_entries_to_remove(self): + """Evaluate all LDAP entries to remove.""" + LOG.debug(_('Evaluating all LDAP entries to remove ...')) + + self.aliases_to_remove = [] + for dn in self.ldap_aliases: entry = self.ldap_aliases[dn] do_delete = False @@ -543,10 +552,64 @@ class BarracudaSyncApp(BaseLdapApplication): if do_delete: self.aliases_to_remove.append(dn) - if self.berbose > 1: + self.aliases_to_remove = sorted( + self.aliases_to_remove, key=cmp_to_key(self.compare_ldap_dns)) + + nr_remove = len(self.aliases_to_remove) + + if nr_remove: + msg = ngettext( + '{} LDAP entry should be removed.', '{} LDAP entries should be removed.', + nr_remove).format(nr_remove) + else: + msg = _('No LDAP entries should be removed.') + LOG.info(msg) + if self.verbose > 2: msg = _('LDAP Entries to remove:') + '\n' + pp(self.aliases_to_remove) LOG.debug(msg) + # ------------------------------------------------------------------------- + def eval_entries_to_create(self): + """Evaluate all LDAP entries to create.""" + LOG.debug(_('Evaluating all LDAP entries to create ...')) + + self.aliases_to_create = [] + + for cn in self.existing_aliases.keys(): + local_alias = self.existing_aliases[cn] + mail = local_alias['alias'] + + if mail in self.ldap_mail_dns: + if self.verbose > 2: + LOG.debug(_( + 'Mail {!r} already exists in regular LDAP entries.').format(mail)) + continue + + do_create = True + for dn in self.ldap_aliases: + entry = self.ldap_aliases[dn] + if mail in entry['mail']: + do_create = False + break + if do_create: + self.aliases_to_create.append(cn) + + self.aliases_to_create.sort(key=str.lower) + + nr_create = len(self.aliases_to_create) + + if nr_create: + msg = ngettext( + '{} LDAP entry should be created.', '{} LDAP entries should be created.', + nr_create).format(nr_create) + else: + msg = _('No LDAP entries should be created.') + LOG.info(msg) + if self.verbose > 2: + msg = _('LDAP Entries to create:') + '\n' + pp(self.aliases_to_create) + LOG.debug(msg) + + # ------------------------------------------------------------------------- def _run(self):