Last time and Last time added and acquired LDAP. This time, I will summarize other functions such as deletion, data movement, and renaming.
If you want to change only cn, you can change it using `modify_dn ()`
of Connection. Only cn can be changed by specifying the dn before the change and the cn after the change. Since the following example was summarized in the previous article, the Connection connection is broken.
main.py
#Display before updating
obj_cn_name = ObjectDef('inetOrgPerson', conn)
data_reader = Reader(conn, obj_cn_name, 'ou=sample-unit,dc=sample-component,dc=sample-ldap')
print(data_reader.search())
print('=======================')
# modify_Specify the dn to be moved and the changed cn in dn
conn.modify_dn('cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap', 'cn=sample-rename')
#Display after update
data_reader2 = Reader(conn, obj_cn_name, 'ou=sample-unit,dc=sample-component,dc=sample-ldap')
print(data_reader2.search())
result
[DN: cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T19:24:03.368406
cn: sample-name
objectClass: inetOrgPerson
sn: sample
]
=======================
[DN: cn=sample-rename,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T19:24:03.448482
cn: sample-rename
objectClass: inetOrgPerson
sn: sample
]
Looking at the LDAP values before and after the change, you can see that cn has been changed from sample-name to sample-rename. You can also see that the values inside have been moved as they are.
Renaming using Writer can be done using Writer's entry_rename () `` `. Unlike Connection, specify the path before change when reading Writer, and give the name after change to
entry_rename () `` `with the full path.
main.py
#Display before updating
obj_cn_name = ObjectDef('inetOrgPerson', conn)
data_reader = Reader(conn, obj_cn_name, 'ou=sample-unit,dc=sample-component,dc=sample-ldap')
print(data_reader.search())
print('=======================')
#Load the move target into Writer
data_reader = Reader(conn, obj_cn_name, 'cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap')
data_reader.search()
data_writer = Writer.from_cursor(data_reader)
#Specify the full path after the change
data_writer[0].entry_rename('cn=sample-rename,ou=sample-unit,dc=sample-component,dc=sample-ldap')
#Reflection of change results
data_writer.commit()
#Display after update
data_reader2 = Reader(conn, obj_cn_name, 'ou=sample-unit,dc=sample-component,dc=sample-ldap')
print(data_reader2.search())
result
[DN: cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T19:40:09.898199
cn: sample-name
objectClass: inetOrgPerson
sn: sample
]
=======================
[DN: cn=sample-rename,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T19:40:10.017186
cn: sample-rename
objectClass: inetOrgPerson
sn: sample
]
Looking at the LDAP values before and after the change, you can see that the cn has been changed from sample-name to sample-rename and the values inside have been moved as they are, similar to the Connection change.
If you want to move an entity to another path, you can change it using Connection's ``` modify_dn ()` `` as above. You can move the entity by specifying the dn to be moved, the changed cn, and the changed dn. Since the following example was summarized in the previous article, the Connection connection is broken.
main.py
from ldap3 import Server, Connection, ObjectDef, Reader, Writer
server = Server('localhost')
conn = Connection(server, 'cn=admin,dc=sample-ldap', password='LdapPass')
conn.bind()
#Display before updating
obj_cn_name = ObjectDef('inetOrgPerson', conn)
data_reader = Reader(conn, obj_cn_name, 'dc=sample-component,dc=sample-ldap')
print(data_reader.search())
print('=======================')
#Moving
conn.modify_dn('cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap', 'cn=sample-name', new_superior='ou=sample-unit-move,dc=sample-component,dc=sample-ldap')
#Display after update
data_reader2 = Reader(conn, obj_cn_name, 'dc=sample-component,dc=sample-ldap')
print(data_reader2.search())
result
[DN: cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T20:01:02.190680
cn: sample-name
objectClass: inetOrgPerson
sn: test
st: sample
, DN: cn=sample-name2,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T20:01:02.194679
cn: sample-name2
objectClass: inetOrgPerson
sn: test
st: sample
]
=======================
[DN: cn=sample-name,ou=sample-unit-move,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T20:01:02.233686
cn: sample-name
objectClass: inetOrgPerson
sn: test
st: sample
, DN: cn=sample-name2,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T20:01:02.236675
cn: sample-name2
objectClass: inetOrgPerson
sn: test
st: sample
]
You can see that ou with cn is sample-name has moved from sample-unit to sample-unit-move. Furthermore, the attributes inside have been moved together as before.
Moving entities using Writer can be changed using Writer's entry_move () `` `. In the same way as
entry_rename () `, specify the path before change when reading Writer, and give the path after change to ```entry_move ()`
as the full path. Please note here that the name of the moved entity should not be in the path and cannot be moved to a path that does not exist.
main.py
#Display before updating
obj_cn_name = ObjectDef('inetOrgPerson', conn)
data_reader = Reader(conn, obj_cn_name, 'dc=sample-component,dc=sample-ldap')
print(data_reader.search())
print('=======================')
#Load the move target into Writer
data_reader = Reader(conn, obj_cn_name, 'cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap')
data_reader.search()
data_writer = Writer.from_cursor(data_reader)
#Move value
data_writer[0].entry_move('cn=sample-name,ou=sample-unit-move,dc=sample-component,dc=sample-ldap')
#Reflection of change results
data_writer.commit()
#Display after update
data_reader2 = Reader(conn, obj_cn_name, 'dc=sample-component,dc=sample-ldap')
print(data_reader2.search())
result
[DN: cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T20:08:33.946805
cn: sample-name
objectClass: inetOrgPerson
sn: test
st: sample
, DN: cn=sample-name2,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T20:08:33.952774
cn: sample-name2
objectClass: inetOrgPerson
sn: test
st: sample
]
=======================
[DN: cn=sample-name,ou=sample-unit-move,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T20:08:34.045188
cn: sample-name
objectClass: inetOrgPerson
sn: test
st: sample
, DN: cn=sample-name2,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T20:08:34.051225
cn: sample-name2
objectClass: inetOrgPerson
sn: test
st: sample
]
You can see that ou with cn is sample-name has moved from sample-unit to sample-unit-move. Furthermore, the attributes inside have been moved together as before.
If you want to delete an entity, use Connection's `delete ()`
. You can delete it by specifying dn for this function. Since the following example was summarized in the previous article, the Connection connection is broken.
main.py
#Display before deletion
conn.search('ou=sample-unit,dc=sample-component,dc=sample-ldap', '(objectclass=inetOrgPerson)')
print(conn.entries)
print('=======================')
#Delete by specifying the path
ou_result = conn.delete('cn=sample-name2,ou=sample-unit,dc=sample-component,dc=sample-ldap')
print(ou_result)
#Display after deletion
conn.search('ou=sample-unit,dc=sample-component,dc=sample-ldap', '(objectclass=inetOrgPerson)')
print(conn.entries)
result
[DN: cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T20:19:59.281937
, DN: cn=sample-name2,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T20:19:59.281937
]
=======================
True
[DN: cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T20:19:59.322233
]
You can see that the cn of sample2 disappears after the deletion.
You can also delete all by combining with acquisition.
main.py
conn.search('dc=sample-component,dc=sample-ldap', '(objectclass=inetOrgPerson)')
for entry in conn.entries:
del_result = conn.delete(entry.entry_dn)
print(del_result)
If you want to delete an entity, use Writer's entry_delete () `` `. Just call the entity
entry_delete ()` `` after creating the Writer as you would with a previous Writer operation. Since the following example was summarized in the previous article, the Connection connection is broken.
main.py
#Display before updating
obj_cn_name = ObjectDef('inetOrgPerson', conn)
data_reader = Reader(conn, obj_cn_name, 'ou=sample-unit,dc=sample-component,dc=sample-ldap')
print(data_reader.search())
print('=======================')
#Load the move target into Writer
data_reader = Reader(conn, obj_cn_name, 'cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap')
data_reader.search()
data_writer = Writer.from_cursor(data_reader)
#Delete the value
data_writer[0].entry_delete()
data_writer.commit()
#Display after update
data_reader2 = Reader(conn, obj_cn_name, 'ou=sample-unit,dc=sample-component,dc=sample-ldap')
print(data_reader2.search())
result
[DN: cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T20:30:26.155112
cn: sample-name
objectClass: inetOrgPerson
sn: test
st: sample
, DN: cn=sample-name2,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T20:30:26.160111
cn: sample-name2
objectClass: inetOrgPerson
sn: test
st: sample
]
=======================
[DN: cn=sample-name2,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-29T20:30:26.264725
cn: sample-name2
objectClass: inetOrgPerson
sn: test
st: sample
]
Looking at the result after deletion, you can see that the cn of sample-name is gone.
You can also delete all by changing the search conditions
main.py
#Have Writer read the upper path
data_reader = Reader(conn, obj_cn_name, 'dc=sample-component,dc=sample-ldap')
data_reader.search()
data_writer = Writer.from_cursor(data_reader)
#Delete all values
for data_entity in data_writer:
data_entity.entry_delete()
We were able to summarize the additions, searches, deletions, and changes required to operate LDAP. In addition, there are some directory operations that are unique to directory operations, such as moves and renames that RDB does not have. Even if it has the function to save data in the same way, I thought that it would be more convenient if RDB and LDAP are used properly because there are clear differences.
Recommended Posts