Move data to LDAP with python Change / Delete (Writer and Reader)

Introduction

Last time and Last time added and acquired LDAP. This time, I will summarize other functions such as deletion, data movement, and renaming.

change name

Rename using only the connection

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

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.

Move entity

Moving entities using only connections

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

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.

Delete

Delete using only the connection

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)

Delete using Writer

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()

in conclusion

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

Move data to LDAP with python Change / Delete (Writer and Reader)
Get additional data to LDAP with python (Writer and Reader)
[LDAP environment construction: 7] Add / search / change / delete users with Python
Get additional data in LDAP with python
Data pipeline construction with Python and Luigi
Change Python 64bit environment to 32bit environment with Anaconda
Convert Excel data to JSON with python
Fractal to make and play with Python
Convert FX 1-minute data to 5-minute data with Python
Compress python data and write to sqlite
[Python] How to create a dictionary type list, add / change / delete elements, and extract with a for statement
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC
Scraping tabelog with python and outputting to CSV
MessagePack-Try to link Java and Python with RPC
I tried to get CloudWatch data with Python
Write CSV data to AWS-S3 with AWS-Lambda + Python
List of Python code to move and remember
Data analysis with python 2
How to scrape image data from flickr with python
Reading, summarizing, visualizing, and exporting time series data to an Excel file with Python
I want to handle optimization with python and cplex
Change IP settings to ACL of conoha with python
Try to operate DB with Python and visualize with d3
Reading Note: An Introduction to Data Analysis with Python
Quickly create a Python data analysis dashboard with Streamlit and deploy it to AWS
Investigate Java and python data exchange with Apache Arrow
I tried to analyze J League data with Python
Folder creation / file move / compress / delete operations with python
Data analysis with Python
Something to enjoy with Prim Pro (X-Play) and Python
Return the image data with Flask of Python and draw it to the canvas element of HTML
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
Easy to use Nifty Cloud API with botocore and python
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
I want to be able to analyze data with Python (Part 3)
screen and split screen with python and ssh login to remote server
I tried to make various "dummy data" with Python faker
I want to be able to analyze data with Python (Part 1)
[Python] How to play with class variables with decorator and metaclass
Send experiment results (text and images) to slack with Python
20200329_Introduction to Data Analysis with Python Second Edition Personal Summary
I want to be able to analyze data with Python (Part 4)
[Part.2] Crawling with Python! Click the web page to move!
Get rid of dirty data with Python and regular expressions
Solve the spiral book (algorithm and data structure) with python!
Try to bring up a subwindow with PyQt5 and Python
Build a Python environment and transfer data to the server
How to do Bulk Update with PyMySQL and notes [Python]
[Let's play with Python] Image processing to monochrome and dots
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
[Introduction to Python] Combine Nikkei 225 and NY Dow csv data
Convert video to black and white with ffmpeg + python + opencv
I tried to make GUI tic-tac-toe with Python and Tkinter
Upload and delete files to Google Cloud Storages with django-storage
[Introduction to Python] How to get data with the listdir function
How to log in to AtCoder with Python and submit automatically
Programming with Python and Tkinter
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
Connect to Wikipedia with Python
Post to slack with Python 3