]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Creating method add_entry() to class BaseLdapApplication and using it in class Mirror...
authorFrank Brehm <frank@brehm-online.com>
Tue, 1 Nov 2022 12:07:09 +0000 (13:07 +0100)
committerFrank Brehm <frank@brehm-online.com>
Tue, 1 Nov 2022 12:07:09 +0000 (13:07 +0100)
lib/pp_admintools/app/ldap.py
lib/pp_admintools/app/mirror_ldap.py

index 868ff23e63b7c9e1f4ca200697c673bb4b049363..3cf77c9da43fa5c775dc8890c0ba6ef8fbd9687c 100644 (file)
@@ -54,7 +54,7 @@ from ..config.ldap import LdapConnectionInfo, LdapConfiguration
 # rom ..config.ldap import DEFAULT_PORT_LDAP, DEFAULT_PORT_LDAPS
 from ..config.ldap import DEFAULT_TIMEOUT
 
-__version__ = '0.10.2'
+__version__ = '0.10.3'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -1160,13 +1160,61 @@ class BaseLdapApplication(BaseDPXApplication):
         return attribs
 
     # -------------------------------------------------------------------------
-    def modify_entry(self, inst, dn, changes, ldap=None):
+    def add_entry(self, inst, dn, object_classes, target_entry, ldap=None):
+        """Creating a LDAP entry."""
+        connect_info = self.cfg.ldap_connection[inst]
+        if not ldap:
+            ldap = self.ldap_connection[inst]
+
+        if self.verbose > 1:
+            msg = _("Creating changes on {uri} to DN {dn!r}:").format(
+                uri=connect_info.url, dn=dn)
+            LOG.debug(msg + '\n' + pp(changes))
+
+        if self.simulate:
+            LOG.info(_("Simulation mode - entry will not be created."))
+            return True
+
+        try:
+            req_status, req_result, req_response, req_whatever = ldap.add(
+                dn, object_class=object_classes, attributes=target_entry)
+        except LDAPException as e:
+            msg = _("Creation of entry {dn!r} NOT successfull - {c}: {e}").format(
+                dn=dn, c=e.__class__.__name__, e=e)
+            msg += '\nobjectClasses:\n' + pp(object_classes)
+            msg += "\nAttributes:\n" + pp(target_entry)
+            raise WriteLDAPItemError(msg)
+
+        # Example result on a not successful modification:
+        # {     'description': 'objectClassViolation',
+        #       'dn': '',
+        #       'message': 'attribute "loginShell" not allowed\n',
+        #       'referrals': None,
+        #       'result': 65,
+        #       'type': 'modifyResponse'}
+
+        if self.verbose > 1:
+            LOG.debug(_("Creation status: {!r}.").format(req_status))
+        if self.verbose > 2:
+            LOG.debug(_("Result of creating:") + '\n' + pp(req_result))
+
+        if not req_status:
+            msg = _("Creation NOT successful: {desc} - {msg}").format(
+                desc=req_result['description'], msg=req_result['message'].strip())
+            raise WriteLDAPItemError(msg)
+
+        LOG.debug(_('Creation successful.'))
+        return True
 
+
+
+    # -------------------------------------------------------------------------
+    def modify_entry(self, inst, dn, changes, ldap=None):
+        """Mofifying an existing LDAP entry."""
         connect_info = self.cfg.ldap_connection[inst]
         if not ldap:
             ldap = self.ldap_connection[inst]
 
-        # connect_info = self.cfg.ldap_connection[inst]
         if self.verbose > 1:
             msg = _("Applying changes on {uri} to DN {dn!r}:").format(
                 uri=connect_info.url, dn=dn)
index 74734997c873ab462409c636ea17f51a3d8da6a6..d33c4b35e660c76ed95d399315de7d82b53fedbf 100644 (file)
@@ -39,7 +39,7 @@ from .ldap import BaseLdapApplication
 from ..argparse_actions import NonNegativeItegerOptionAction
 from ..argparse_actions import LimitedFloatOptionAction
 
-__version__ = '0.7.2'
+__version__ = '0.7.3'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -588,6 +588,7 @@ class MirrorLdapApplication(BaseLdapApplication):
                     LOG.info(_("Modifying entry {!r} ...").format(dn))
                     msg = _("Got modify data for DN {!r}:").format(dn)
                     LOG.debug(msg + '\n' + pp(changes))
+                    self.modify_entry(self.tgt_instance, dn, changes)
                     self.mirrored_entries += 1
                     count += 1
                 else:
@@ -603,6 +604,7 @@ class MirrorLdapApplication(BaseLdapApplication):
                 msg += '\nobjectClasses:\n' + pp(object_classes)
                 msg += "\nAttributes:\n" + pp(target_entry)
                 LOG.debug(msg)
+                self.add_entry(self.tgt_instance, dn, object_classes, target_entry)
                 self.mirrored_entries += 1
                 count += 1