|
| 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) |
0 commit comments