]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Better output of results
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 26 May 2023 10:19:46 +0000 (12:19 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 26 May 2023 10:19:46 +0000 (12:19 +0200)
lib/pp_admintools/app/barracuda_sync.py

index 52b3d173eb48aa7b39c35259f6e817472f790d58..cc6b80ade17f93aabfd0ed933a41af0900835e71 100644 (file)
@@ -31,7 +31,7 @@ from ldap3 import MODIFY_ADD, MODIFY_DELETE, MODIFY_REPLACE
 from .ldap import BaseLdapApplication
 from ..xlate import XLATOR
 
-__version__ = '0.9.5'
+__version__ = '0.9.6'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -844,6 +844,7 @@ class BarracudaSyncApp(BaseLdapApplication):
         inst = self.ldap_instances[0]
 
         nr_aliases = len(self.aliases_to_remove)
+        i = 0
 
         if not nr_aliases:
             msg = _('No aliases found to remove in LDAP.')
@@ -852,18 +853,21 @@ class BarracudaSyncApp(BaseLdapApplication):
                 time.sleep(self.wait_interval * 10)
             return
 
-        for dn in self.aliases_to_remove:
-            self.delete_entry(inst, dn)
-            if self.wait_interval:
-                time.sleep(self.wait_interval)
+        try:
+            for dn in self.aliases_to_remove:
+                self.delete_entry(inst, dn)
+                i += 1
+                if self.wait_interval:
+                    time.sleep(self.wait_interval)
 
-        self.empty_line()
-        msg = ngettext(
-            'One alias entry removed in LDAP.', '{} alias entries removed in LDAP.',
-            nr_aliases).format(nr_aliases)
-        LOG.info(msg)
-        if self.wait_interval:
-            time.sleep(self.wait_interval * 10)
+        finally:
+            self.empty_line()
+            msg = ngettext(
+                'One alias entry removed in LDAP.', '{} alias entries removed in LDAP.',
+                i).format(i)
+            LOG.info(msg)
+            if self.wait_interval:
+                time.sleep(self.wait_interval * 10)
 
     # -------------------------------------------------------------------------
     def add_failing_ldap_entries(self):
@@ -873,6 +877,7 @@ class BarracudaSyncApp(BaseLdapApplication):
         inst = self.ldap_instances[0]
 
         nr_aliases = len(self.aliases_to_create)
+        i = 0
 
         if not nr_aliases:
             msg = _('No aliases found to create in LDAP.')
@@ -881,21 +886,24 @@ class BarracudaSyncApp(BaseLdapApplication):
                 time.sleep(self.wait_interval * 10)
             return
 
-        for cn in self.aliases_to_create:
-            dn = 'cn=' + cn + ',' + self.barracuda_base_dn
-            (oclasses, attributes) = self._create_ldap_entry_for_create(cn)
-            LOG.info(_('Creating LDAP alias {a!r} => {dn!r}.').format(a=cn, dn=dn))
-            self.add_entry(inst, dn, oclasses, attributes)
+        try:
+            for cn in self.aliases_to_create:
+                dn = 'cn=' + cn + ',' + self.barracuda_base_dn
+                (oclasses, attributes) = self._create_ldap_entry_for_create(cn)
+                LOG.info(_('Creating LDAP alias {a!r} => {dn!r}.').format(a=cn, dn=dn))
+                self.add_entry(inst, dn, oclasses, attributes)
+                i += 1
+                if self.wait_interval:
+                    time.sleep(self.wait_interval)
+
+        finally:
+            self.empty_line()
+            msg = ngettext(
+                'One alias entry created in LDAP.', '{} alias entries created in LDAP.',
+                i).format(i)
+            LOG.info(msg)
             if self.wait_interval:
-                time.sleep(self.wait_interval)
-
-        self.empty_line()
-        msg = ngettext(
-            'One alias entry created in LDAP.', '{} alias entries created in LDAP.',
-            nr_aliases).format(nr_aliases)
-        LOG.info(msg)
-        if self.wait_interval:
-            time.sleep(self.wait_interval * 10)
+                time.sleep(self.wait_interval * 10)
 
     # -------------------------------------------------------------------------
     def modify_existing_entries(self):
@@ -905,6 +913,7 @@ class BarracudaSyncApp(BaseLdapApplication):
         inst = self.ldap_instances[0]
 
         nr_aliases = len(self.aliases_to_modify.keys())
+        i = 0
 
         if not nr_aliases:
             msg = _('No aliases found to mofify in LDAP.')
@@ -913,20 +922,23 @@ class BarracudaSyncApp(BaseLdapApplication):
                 time.sleep(self.wait_interval * 10)
             return
 
-        for dn in sorted(self.aliases_to_modify.keys(), key=cmp_to_key(self.compare_ldap_dns)):
-            modify_data = self.aliases_to_modify[dn]
-            LOG.info(_('Modifying LDAP alias {!r} ...').format(dn))
-            self.modify_entry(inst, dn, modify_data)
+        try:
+            for dn in sorted(self.aliases_to_modify.keys(), key=cmp_to_key(self.compare_ldap_dns)):
+                modify_data = self.aliases_to_modify[dn]
+                LOG.info(_('Modifying LDAP alias {!r} ...').format(dn))
+                self.modify_entry(inst, dn, modify_data)
+                i += 1
+                if self.wait_interval:
+                    time.sleep(self.wait_interval)
+
+        finally:
+            self.empty_line()
+            msg = ngettext(
+                'One alias entry modified in LDAP.', '{} alias entries modified in LDAP.',
+                i).format(i)
+            LOG.info(msg)
             if self.wait_interval:
-                time.sleep(self.wait_interval)
-
-        self.empty_line()
-        msg = ngettext(
-            'One alias entry modified in LDAP.', '{} alias entries modified in LDAP.',
-            nr_aliases).format(nr_aliases)
-        LOG.info(msg)
-        if self.wait_interval:
-            time.sleep(self.wait_interval * 10)
+                time.sleep(self.wait_interval * 10)
 
     # -------------------------------------------------------------------------
     def _run(self):