-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_users.py
72 lines (50 loc) · 2.07 KB
/
import_users.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Example of how to read a list of users from an Excel spreadsheet and bulk
# create them using the REST API.
import deepinstinct3 as di
import pandas
def main():
#prompt for config parameters
fqdn = input('Enter FQDN of DI Server, or press enter to accept the default [di-service.customers.deepinstinctweb.com]: ')
if fqdn == '':
fqdn = 'di-service.customers.deepinstinctweb.com'
key = input('Enter API Key for DI Server: ')
print("""
This script accepts input from a single sheet Excel workbook.
The first row of the input file must be column labels, which are cASE SeNSITIve.
The input file must contain the following 6 columns:
username
password
first_name
last_name
email
role
The 'role' column must contain one of these 6 values for each row:
MASTER_ADMINISTRATOR
ADMINISTRATOR
IT_ADMIN
SOC_ADMIN
ACCOUNT_ADMINISTRATOR
READ_ONLY
Column order is irrelevant.
Extra columns are OK and will be ignored.
""")
file_name = input('Enter name of file containing users to import, or press enter to accept the default [users.xlsx]: ')
if file_name == '':
file_name = 'users.xlsx'
#run the import
return run_user_import(fqdn=fqdn, key=key, file_name=file_name)
def run_user_import(fqdn, key, file_name):
di.fqdn = fqdn
di.key = key
#read users to import from file on disk as Pandas dataframe
user_list_df = pandas.read_excel(file_name)
#replace any null values with empty string to avoid subsequent errors
user_list_df.fillna('', inplace=True)
#convert Pandas dataframe to Python dictionary
user_list = user_list_df.to_dict('records')
print('INFO: Successful read', len(user_list), 'records from', file_name)
#iterate though the imported user list
for user in user_list:
di.create_user(username=user['username'], password=user['password'], first_name=user['first_name'], last_name=user['last_name'], email=user['email'], role=user['role'])
if __name__ == "__main__":
main()