From: Frank Brehm Date: Tue, 1 Nov 2022 12:07:09 +0000 (+0100) Subject: Creating method add_entry() to class BaseLdapApplication and using it in class Mirror... X-Git-Tag: 0.7.0^2~1^2~12 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=97c05e5d81e23ff2335291bc87785e1b0dd2da07;p=pixelpark%2Fpp-admin-tools.git Creating method add_entry() to class BaseLdapApplication and using it in class MirrorLdapApplication --- diff --git a/lib/pp_admintools/app/ldap.py b/lib/pp_admintools/app/ldap.py index 868ff23..3cf77c9 100644 --- a/lib/pp_admintools/app/ldap.py +++ b/lib/pp_admintools/app/ldap.py @@ -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) diff --git a/lib/pp_admintools/app/mirror_ldap.py b/lib/pp_admintools/app/mirror_ldap.py index 7473499..d33c4b3 100644 --- a/lib/pp_admintools/app/mirror_ldap.py +++ b/lib/pp_admintools/app/mirror_ldap.py @@ -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