]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Evaluating all LDAP entries to create.
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 24 May 2023 13:48:29 +0000 (15:48 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 24 May 2023 13:48:29 +0000 (15:48 +0200)
lib/pp_admintools/app/barracuda_sync.py

index 7b93c3567595fa800f06e4097eaaa5693fd688a5..f0a0c12274b53e49c27a0c75d065586ff4211718 100644 (file)
@@ -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):