-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorg_admins_import.py
More file actions
82 lines (72 loc) · 2.95 KB
/
org_admins_import.py
File metadata and controls
82 lines (72 loc) · 2.95 KB
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
73
74
75
76
77
78
79
80
81
82
'''
Python script to invite/add adminsitrators from a CSV file.
The CSV file must have 3 columns: email, first name, last name
You can run the script with the command "python3 org_admins_import.py <path_to_the_csv_file>"
The script has 3 different steps:
1) admin login
2) select the organisation where you want to add/invite the admins
3) choose if you want to give access to the whole organisation, or only to specific sites
'''
#### PARAMETERS #####
csv_separator = ","
privileges = []
#### IMPORTS ####
import mlib as mist_lib
from tabulate import tabulate
import mlib.cli as cli
import sys
import csv
#### CONSTANTS ####
roles = {"s": "admin", "n": "write", "o": "read", "h":"helpdesk"}
#### FUNCTIONS ####
def define_privileges(org_id):
'''
Generate the privilege parameters for the specified orgs.
Will ask if the privileges have to be applied to the entire org or to a specific site/s, and the privilege level.
There is no return value, the new privileges are stored into the global "privilege" variable
'''
role = ""
while role not in roles:
role = input("Which level of privilege at the org level (\"s\" for Super Admin, \"n\" for Network Admin,\"o\" for observer,\"h\" for helpdesk)? ")
while True:
all_sites = input("Do you want to select specific sites (y/N)? ")
if all_sites.lower()=="y":
site_ids = cli.select_site(mist, org_id, True)
for site_id in site_ids:
privileges.append({"scope": "site", "org_id": org_id, "site_id": site_id, "role":roles[role]})
break
elif all_sites.lower() == "n" or all_sites == "":
site_ids = mist_lib.requests.orgs.sites.get(mist, org_id)
site_id=""
for site in site_ids["result"]:
if "site_id" in site:
site_id = site["site_id"]
break
privileges.append({"scope": "org", "org_id": org_id, "site_id":site_id, "role":roles[role]})
break
def import_admins(file_path, org_id):
'''
create all the administrators from the "file_path" file.
'''
print("Opening CSV file %s" % file_path)
try:
with open(file_path, 'r') as my_file:
invite_file = csv.reader(my_file, delimiter=csv_separator)
for column in invite_file:
email= column[0]
first_name= column[1]
last_name = column[2]
print(', '.join(column))
mist_lib.requests.orgs.admins.create_invite(mist, org_id, email, privileges, first_name, last_name)
except:
print("Error while opening the CSV file... Aborting")
#### SCRIPT ENTRYPOINT ####
file_path = sys.argv[1]
mist = mist_lib.Mist_Session("./session.py")
org_id = cli.select_org(mist)
if privileges == []:
define_privileges(org_id)
import_admins(file_path, org_id)
admins = mist_lib.requests.orgs.admins.get(mist, org_id)
cli.show(admins)
exit(0)