-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusers.py
More file actions
100 lines (96 loc) · 5.11 KB
/
users.py
File metadata and controls
100 lines (96 loc) · 5.11 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Licensed to Alpine Data, Inc.
# Copyright 2017 Alpine Data All Rights reserved.
# Sample
"""Simple Command-Line Sample For Alpine API.
Command-line application to login and logout with Alpine API
Usage:
$ python users.py
To get debug log output run:
$ python users.py --logging_level=DEBUG
"""
import sys
from alpine.exception import *
from alpine import *
if __name__ == '__main__':
try:
input = raw_input # To runs in both Python 2 and Python 3
except NameError:
pass
self = sys.modules['__main__']
host = input(">>> Host: ")
port = input(">>> Port: ")
username = input(">>> Login User: ")
password = input(">>> Password: ")
# host = "10.10.0.204"
# port = "8080"
# username = "demoadmin"
# password = "password"
alpine = APIClient(host, port)
alpine.login(username, password)
# alpine = APIClient(host, port, username, password)
while True:
# input_option = input("Press Enter to Continue...")
print("----------------------------------------------------------------")
input_option = input("Please select the number of functions you want to use: \n"
"1. view the list of users.\n"
"2. view info of a user.\n"
"3. delete users.\n"
"q. exit.\n")
if input_option == 'q':
break
if input_option == '1':
user_list = alpine.user.get_list()
print("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}".format("User_ID", "User Name", "Email","First Name",
"Last Name", "App Role", "Department", "Title"))
for user in sorted(user_list, key=lambda x: x['id'], reverse=True):
print("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}".format(user['id'], user['username'], user['email'],
user['first_name'], user['last_name'],
user['user_type'],user['dept'],user['title']))
elif input_option == '2':
user_name = input(">>> username: ")
try:
user_id = alpine.user.get_id(user_name)
user = alpine.user.get(user_id)
print("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}".format("User_ID", "User Name", "Email","First Name",
"Last Name", "App Role", "Department", "Title"))
print("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}".format(user['id'], user['username'], user['email'],
user['first_name'], user['last_name'],
user['user_type'], user['dept'],user['title']))
except UserNotFoundException:
print("User '{0}' Not Found".format(user_name))
elif input_option == '3':
user_names = input(">>> Please enter user names to be deleted, split by ',' : ")
for user_name in user_names.split(','):
if not user_name:
continue
try:
delete_safe_flag = True
user_id = alpine.user.get_id(user_name)
# Check whether there are any workspace owned by the user
workspace_list = alpine.workspace.get_list(user_id)
if workspace_list:
for workspace in workspace_list:
workspace['owner']['username']
if workspace['owner']['username'] == user_name:
delete_safe_flag = False
print ("User '{0}' is owner of workspace '{1}', Please transfer ownership of " \
"active workspaces to another person.".format(user_name, workspace['name']))
# Check whether there are any Datasource owned by the user
db_database_list = alpine.datasource.get_list("Database")
if db_database_list:
for db_datasource in db_database_list:
if db_datasource['owner']['username'] == user_name:
delete_safe_flag = False
print ("User '{0}' is owner of data source '{1}', Please transfer ownership of " \
"the data source to another person.".format(user_name, db_datasource['name']))
# Delete the user when it is safe to do so.
if delete_safe_flag:
alpine.user.delete(user_id)
print ("User '{0}' successfully deleted".format(user_name))
except UserNotFoundException:
print ("User '{0}' not found, please double check the username exists".format(user_name))
continue
else:
print("Invalid Input")