-
Notifications
You must be signed in to change notification settings - Fork 13
/
report-roles.py
executable file
·114 lines (93 loc) · 3.73 KB
/
report-roles.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
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
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
"""
Pulls a report of all the users and assigned roles in a given tenant.
"""
# TODO Allow for specification of output file
# TODO support dynamic subtenant by name or ID
# TODO output data to prettytable
__version__ = "$Revision$"
# $Source$
import getpass
import argparse
import vralib
import csv
def getargs():
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--server',
required=True,
action='store',
help='FQDN of vRealize Automation.')
parser.add_argument('-u', '--username',
required=False,
action='store',
help='Username to access the cloud provider')
parser.add_argument('-t', '--tenant',
required=True,
action='store',
help='vRealize tenant')
parser.add_argument('-c', '--csv',
required=True,
action='store',
help='Filename to output CSV report to.')
parser.add_argument('-b', '--businessgroup',
required=False,
action='store',
help='Business group to retrieve roles from')
args = parser.parse_args()
return args
def main():
args = getargs()
cloudurl = args.server
username = args.username
tenant = args.tenant
outfile = args.csv
if not username:
username = raw_input("vRA Username (user@domain):")
else:
pass
password = getpass.getpass("vRA Password:")
vra = vralib.Session.login(username, password, cloudurl, tenant, ssl_verify=False)
subtenant = 'f41a35f5-040e-42e0-a5c2-6ca4e7bf328b'
subtenantroles = vra.get_subtenant_roles(token, cloudurl, tenant, subtenant)
with open(outfile, "w") as f:
csv_file = csv.writer(f)
csv_file.writerow(['user', 'domain', 'type', 'id', 'role', 'scope'])
for i,val in enumerate(subtenantroles['content']):
if val['name'] == 'Basic User':
scoperole = val['scopeRoleRef']
attype = val['@type']
role_name = val['name']
role_id = val['id']
for user in val['principalId']:
csv_file.writerow([user['name'],
user['domain'],
attype,
role_id,
role_name,
scoperole])
elif val['name'] == 'Business Group Manager':
scoperole = val['scopeRoleRef']
attype = val['@type']
role_name = val['name']
role_id = val['id']
for user in val['principalId']:
csv_file.writerow([user['name'],
user['domain'],
attype,
role_id,
role_name,
scoperole])
elif val['name'] == 'Support User':
scoperole = val['scopeRoleRef']
attype = val['@type']
role_name = val['name']
role_id = val['id']
for user in val['principalId']:
csv_file.writerow([user['name'],
user['domain'],
attype,
role_id,
role_name,
scoperole])
if __name__ == '__main__':
main()