I am using python-ldap to process an LDIF file and (conditionally) import the records from that file into an LDAP server. Every piece of documentation and every example I can find, just assumes LDIF records are additions. However the LDIF files my script processes are version 1 files with a changetype, like so:
version: 1
dn: cn=group,ou=groups,o=vault
changetype: add
cn: group
mail: [email protected]
member: cn=users,ou=users,o=vault
dn:cn=users,ou=users,o=vault
changetype: modify
add: memberOf
memberOf: cn=group,ou=groups,o=vault
-
According to the documentation of python-ldap I need to use modlist.addModlist for adds and modlist.modifyModlist for modifications.
My question is: how do I get from the parsed LDIF data to a modification in LDAP? Something along the lines of:
parser = ldif.LDIFRecordList(open(filename,'r'))
parser.parse()
for dn, entry in parser.all_records:
if entry['changetype'] == "add":
crud = modlist.addModlist(entry)
ldapcon.add_s(dn,crud)
else:
crud = modlist.modifyModlist(entry)
ldapcon.modify_s(dn,crud)
The above does not work; modlist.modifyModlist() requires two arguments.
Also, the entry contains just the exact lines from the LDIF in an array of tuples, including the changetype and the (mandatory) separator line with the single dash :-(
Do I really need to parse the entry data line by line and create my own modifications? What is the added value of the LDIF parser if that is the case?