diff options
author | Martin Basti <mbasti@redhat.com> | 2015-10-07 17:15:34 +0200 |
---|---|---|
committer | Martin Basti <mbasti@redhat.com> | 2015-10-15 18:37:52 +0200 |
commit | 65c89cc711331e5ae97f95b1f39190be1e9fdc3c (patch) | |
tree | a078aa32b03e9e8956e1cca956600bbcbc043e88 | |
parent | 63638ac9a32b528677694b438b276812e75917c4 (diff) | |
download | freeipa-65c89cc711331e5ae97f95b1f39190be1e9fdc3c.tar.gz freeipa-65c89cc711331e5ae97f95b1f39190be1e9fdc3c.tar.xz freeipa-65c89cc711331e5ae97f95b1f39190be1e9fdc3c.zip |
Add method to read changes from LDIF
modifications_from_ldif will read LDIF file and changes in LDIF will
be cached until parse() is called. After calling parse() method changes
will be applied into destination LDIF.
Only changetype modify is supported, the default operation is add.
https://fedorahosted.org/freeipa/ticket/4949
Also fixes:
https://fedorahosted.org/freeipa/ticket/4048
https://fedorahosted.org/freeipa/ticket/1930
Reviewed-By: Martin Babinsky <mbabinsk@redhat.com>
-rw-r--r-- | ipaserver/install/installutils.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/ipaserver/install/installutils.py b/ipaserver/install/installutils.py index 8e9e43d15..1d3551f8b 100644 --- a/ipaserver/install/installutils.py +++ b/ipaserver/install/installutils.py @@ -1280,6 +1280,46 @@ class ModifyLDIF(ldif.LDIFParser): self.remove_value(dn, attr) self.add_value(dn, attr, values) + def modifications_from_ldif(self, ldif_file): + """ + Parse ldif file. Default operation is add, only changetypes "add" + and "modify" are supported. + :param ldif_file: an opened file for read + :raises: ValueError + """ + parser = ldif.LDIFRecordList(ldif_file) + parser.parse() + + last_dn = None + for dn, entry in parser.all_records: + if dn is None: + # ldif parser return None, if records belong to previous DN + dn = last_dn + else: + last_dn = dn + + if "replace" in entry: + for attr in entry["replace"]: + try: + self.replace_value(dn, attr, entry[attr]) + except KeyError: + raise ValueError("replace: {dn}, {attr}: values are " + "missing".format(dn=dn, attr=attr)) + elif "delete" in entry: + for attr in entry["delete"]: + self.remove_value(dn, attr, entry.get(attr, None)) + elif "add" in entry: + for attr in entry["add"]: + try: + self.replace_value(dn, attr, entry[attr]) + except KeyError: + raise ValueError("add: {dn}, {attr}: values are " + "missing".format(dn=dn, attr=attr)) + else: + root_logger.error("Ignoring entry: %s : only modifications " + "are allowed (missing \"changetype: " + "modify\")", dn) + def handle(self, dn, entry): if dn in self.modifications: self.dn_updated.add(dn) |