Skip to content

Commit 0fe9f14

Browse files
author
Eric Lei
committed
Add create_user_from_file.py to user creation from CSV file. This is useful when creaing bunch of external users from LDAP
1 parent 1025e42 commit 0fe9f14

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

examples/create_user_from_file.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Created on Jan 8, 2020
3+
4+
@author: ylei
5+
6+
Create new users from file (user_list.csv)
7+
"python create_user_from_file.py -f user_list.csv"
8+
9+
CSV File requires 4 columns titled 'username', 'first', 'last', and 'email'
10+
A sample user_list.csv is like below:
11+
username,first,last,email
12+
flast1,first1,last1,[email protected]
13+
flast2,first2,last2,[email protected]
14+
flast3,first3,last3,[email protected]
15+
16+
'''
17+
import argparse
18+
import logging
19+
import sys
20+
import csv
21+
22+
from blackduck.HubRestApi import HubInstance
23+
24+
25+
parser = argparse.ArgumentParser("Create bunch of new users (For example, LDAP users) from file")
26+
parser.add_argument("-f", "--filename", default="user_list.csv", help="File to store user list")
27+
28+
args = parser.parse_args()
29+
30+
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
31+
logging.getLogger("requests").setLevel(logging.WARNING)
32+
logging.getLogger("urllib3").setLevel(logging.WARNING)
33+
34+
hub = HubInstance()
35+
36+
filename = args.filename
37+
with open(filename, 'r') as f:
38+
rows = csv.DictReader(f)
39+
for row in rows:
40+
print("Create user: ", row['username'])
41+
data = {
42+
"userName" : row['username'],
43+
"externalUserName" : row['username'],
44+
"firstName" : row['first'],
45+
"lastName" : row['last'],
46+
"email" : row['email'],
47+
"active" : True,
48+
"type": "EXTERNAL"
49+
}
50+
hub.create_user(data)

examples/user_list.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
username,first,last,email
2+
flast9,first9,last9,[email protected]
3+
flast7,first7,last7,[email protected]
4+
flast8,first8,last8,[email protected]

0 commit comments

Comments
 (0)