forked from PagerDuty/public-support-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
team_roles.py
executable file
·41 lines (35 loc) · 1.75 KB
/
team_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
#!/usr/bin/env python3
import argparse
import sys
import pdpyras
def get_teams(session, comma_separated):
if comma_separated:
sys.stdout.write("Team ID, Team Name, User ID, User name, Team role\n")
try:
for team in session.iter_all('teams'):
get_team_members(team['id'], team['name'], session, comma_separated)
except pdpyras.PDClientError as e:
raise e
def get_team_members(team_id, team_name, session, comma_separated):
try:
for member in session.iter_all('teams/{}/members'.format(team_id)):
if comma_separated:
sys.stdout.write("{}, {}, {}, {}, {}\n".format(team_id, team_name, member['user']['id'], member['user']['summary'], member['role']))
else:
sys.stdout.write("Team ID: {}\n".format(team_id))
sys.stdout.write("Team Name: {}\n".format(team_name))
sys.stdout.write("User ID: {}\n".format(member['user']['id']))
sys.stdout.write("User name: {}\n".format(member['user']['summary']))
sys.stdout.write("Team role: {}\n".format(member['role']))
sys.stdout.write("-----\n")
except pdpyras.PDClientError as e:
print("Could not get team members for team {} {}".format(team_name, team_id))
raise e
if __name__ == '__main__':
ap = argparse.ArgumentParser(description="Retrieves team roles for"
"users in a PagerDuty account")
ap.add_argument('-k', '--api-key', required=True, help="REST API key")
ap.add_argument('-c', '--comma-separated', required=False, default=False, action='store_true', help="Format output separated by commas")
args = ap.parse_args()
session = pdpyras.APISession(args.api_key)
get_teams(session, args.comma_separated)