]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Generating of LDAP entries to create
authorFrank Brehm <frank@brehm-online.com>
Mon, 31 Oct 2022 16:55:22 +0000 (17:55 +0100)
committerFrank Brehm <frank@brehm-online.com>
Mon, 31 Oct 2022 16:55:22 +0000 (17:55 +0100)
lib/pp_admintools/app/ldap.py
lib/pp_admintools/app/mirror_ldap.py

index 9ca0932236847a2e539ad15c60ad75651afe3997..868ff23e63b7c9e1f4ca200697c673bb4b049363 100644 (file)
@@ -1499,6 +1499,34 @@ class BaseLdapApplication(BaseDPXApplication):
 
         return attr_changes
 
+    # -------------------------------------------------------------------------
+    def generate_create_entry(self, src_attribs):
+
+        object_classes = []
+        target_entry = {}
+
+        for attrib_name in src_attribs:
+
+            if attrib_name.lower() == 'memberof':
+                if self.verbose > 2:
+                    msg = _("Attribute {!r} will not be touched.").format(attrib_name)
+                    LOG.debug(msg)
+                continue
+
+            if attrib_name.lower() == 'objectclass':
+                for object_class in sorted(src_attribs[attrib_name], key=str.lower):
+                    object_classes.append(object_class)
+                continue
+
+            if not src_attribs[attrib_name]:
+                continue
+
+            target_entry[attrib_name] = []
+            for value in src_attribs[attrib_name]:
+                target_entry[attrib_name].append(value)
+
+        return (object_classes, target_entry)
+
 
 # =============================================================================
 if __name__ == "__main__":
index fb2b58f161a5c5fb5ac6e481b3117a8dbc3caa7e..0ebcc6808d487285971974582c0b7173eaec77bf 100644 (file)
@@ -24,6 +24,7 @@ from ldap3 import ALL_ATTRIBUTES
 # from fb_tools.common import to_bool, is_sequence
 # from fb_tools.collections import FrozenCIStringSet, CIStringSet, CIDict
 from fb_tools.collections import CIDict, CIStringSet
+from fb_tools.xlate import format_list
 
 from .. import pp
 
@@ -298,6 +299,27 @@ class MirrorLdapApplication(BaseLdapApplication):
                 continue
             self.src_struct_dns.add(dn)
 
+        no_total = len(self.src_dns)
+        no_struct = len(self.src_struct_dns)
+        no_non_struct = no_total - no_struct
+
+        msgs = []
+
+        msgs.append(ngettext(
+            "Found total one entry in source LDAP", "Found {no} entries in source LDAP",
+            no_total).format(no=no_total))
+
+        msgs.append(ngettext(
+            "one structural entry in source LDAP", "{no} structural entries in source LDAP",
+            no_struct).format(no=no_struct))
+
+        msgs.append(ngettext(
+            "one non-structural entry in source LDAP.",
+            "{no} non-structural entries in source LDAP.",
+            no_non_struct).format(no=no_non_struct))
+
+        LOG.info(format_list(msgs))
+
         if self.verbose > 2:
             msg = _("Found structural DNs in instance {!r}:").format(self.src_instance)
             LOG.debug(msg + '\n' + pp(self.src_struct_dns.as_list()))
@@ -332,6 +354,27 @@ class MirrorLdapApplication(BaseLdapApplication):
                 continue
             self.tgt_struct_dns_current.add(dn)
 
+        no_total = len(self.tgt_dns_current)
+        no_struct = len(self.tgt_struct_dns_current)
+        no_non_struct = no_total - no_struct
+
+        msgs = []
+
+        msgs.append(ngettext(
+            "Found total one entry in target LDAP", "Found {no} entries in target LDAP",
+            no_total).format(no=no_total))
+
+        msgs.append(ngettext(
+            "one structural entry in target LDAP", "{no} structural entries in target LDAP",
+            no_struct).format(no=no_struct))
+
+        msgs.append(ngettext(
+            "one non-structural entry in target LDAP.",
+            "{no} non-structural entries in target LDAP.",
+            no_non_struct).format(no=no_non_struct))
+
+        LOG.info(format_list(msgs))
+
         if self.verbose > 2:
             msg = _("Found structural DNs in instance {!r}:").format(self.tgt_instance)
             LOG.debug(msg + '\n' + pp(self.tgt_struct_dns_current.as_list()))
@@ -548,6 +591,13 @@ class MirrorLdapApplication(BaseLdapApplication):
 
             else:
                 LOG.debug(_("Target entry {!r} not found.").format(dn))
+                (object_classes, target_entry) = self.generate_create_entry(src_attribs)
+                msg = _("Got create data for DN {!r}:").format(dn)
+                msg += '\nobjectClasses:\n' + pp(object_classes)
+                msg += "\nAttributes:\n" + pp(target_entry)
+                LOG.debug(msg)
+                self.mirrored_entries += 1
+                count += 1
 
             if self.limit and self.mirrored_entries >= self.limit:
                 break