From 375bd042adda29445f6f08a9bb970dc11bcf72fc Mon Sep 17 00:00:00 2001 From: Paul Boayue Date: Tue, 18 Feb 2020 15:42:40 -0500 Subject: [PATCH 1/2] Added csv flag, create csv output of teams Added base role and email to output Changes to be committed: modified: get_team_roles/team_roles.py --- get_team_roles/team_roles.py | 49 +++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/get_team_roles/team_roles.py b/get_team_roles/team_roles.py index db80b63..fdb15a1 100644 --- a/get_team_roles/team_roles.py +++ b/get_team_roles/team_roles.py @@ -1,39 +1,64 @@ import argparse import sys import pdpyras +import csv -def get_teams(session, comma_separated): - if comma_separated: - sys.stdout.write("Team ID, Team Name, User ID, User name, Team role\n") +def get_teams(session): + if args.comma_separated: + sys.stdout.write("Team ID', 'Team Name', 'User ID', 'User name', 'Email', 'Base role', 'Team role'\n") + elif args.csv_file: + tf = csv.writer(args.csv_file) + sys.stdout.write("Creating / Clearing : {}\n".format(args.csv_file.name)) + tf.writerow(['Team ID', 'Team Name', 'User ID', 'User name', 'Email', "Base role", 'Team role']) try: for team in session.iter_all('teams'): - get_team_members(team['id'], team['name'], session, comma_separated) + get_team_members(team['id'], team['name'], session) except pdpyras.PDClientError as e: raise e -def get_team_members(team_id, team_name, session, comma_separated): +def get_team_members(team_id, team_name, session): 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'])) + for member in session.iter_all('teams/{}/members'.format(team_id), paginate=True): + user = get_user_data(member['user']['id'], session) + if args.comma_separated: + sys.stdout.write("{}, {}, {}, {}, {}\n".format(team_id, team_name, user['id'], user['name'], user['email'], user['role'], member['role'])) + elif args.csv_file: + tf = csv.writer(args.csv_file) + tf.writerow([team_id, team_name, user['id'], user['name'], user['email'], user['role'], member['role']]) + sys.stdout.write("Added to {}: {}, {}, {}, {}, {}\n".format(args.csv_file.name, team_id, team_name, user['id'], user['name'], user['email'], user['role'], 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("User ID: {}\n".format(user['id'])) + sys.stdout.write("User name: {}\n".format(user['name'])) + sys.stdout.write("User email: {}\n".format(user['email'])) + sys.stdout.write("Base role: {}\n".format(user['role'])) 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 - +def get_user_data(user_id, session): + if user_id in Users: + return Users[user_id] + else: + try: + user = session.rget('users/{}'.format(user_id)) + Users[user_id] = user + return Users[user_id] + except pdpyras.PDClientError as e: + print("Could not get user date for {}".format(user_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") + ap.add_argument('-f', '--csv-file', required=False, default=False, type=argparse.FileType('w'), help="Output to a csv file") + Users = {} args = ap.parse_args() session = pdpyras.APISession(args.api_key) - get_teams(session, args.comma_separated) \ No newline at end of file + get_teams(session) \ No newline at end of file From 94e4d5b2f4baed1ca3b233ad92f7bce694bf211e Mon Sep 17 00:00:00 2001 From: Paul Boayue Date: Tue, 14 Apr 2020 14:48:38 -0400 Subject: [PATCH 2/2] rework update based on comments --- get_team_roles/team_roles.py | 82 ++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 26 deletions(-) diff --git a/get_team_roles/team_roles.py b/get_team_roles/team_roles.py index fdb15a1..371c585 100644 --- a/get_team_roles/team_roles.py +++ b/get_team_roles/team_roles.py @@ -1,54 +1,85 @@ +#!/usr/bin/env python +# +# Copyright (c) 2020, PagerDuty, Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of PagerDuty Inc nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL PAGERDUTY INC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +# PagerDuty Support asset: get_team_roles + import argparse import sys -import pdpyras import csv +from pdpyras import APISession, PDClientError -def get_teams(session): +def get_teams(session, args): if args.comma_separated: - sys.stdout.write("Team ID', 'Team Name', 'User ID', 'User name', 'Email', 'Base role', 'Team role'\n") + print("'Team ID', 'Team Name', 'User ID', 'User name', 'Email', 'Base role', 'Team role'\n") elif args.csv_file: tf = csv.writer(args.csv_file) - sys.stdout.write("Creating / Clearing : {}\n".format(args.csv_file.name)) + print("Creating / Clearing : {}\n".format(args.csv_file.name)) tf.writerow(['Team ID', 'Team Name', 'User ID', 'User name', 'Email', "Base role", 'Team role']) try: for team in session.iter_all('teams'): get_team_members(team['id'], team['name'], session) - except pdpyras.PDClientError as e: + except PDClientError as e: raise e def get_team_members(team_id, team_name, session): + users = {} try: for member in session.iter_all('teams/{}/members'.format(team_id), paginate=True): - user = get_user_data(member['user']['id'], session) + add_to_cache_users(member['user']['id'], users, session) + user = users[member['user']['id']] if args.comma_separated: - sys.stdout.write("{}, {}, {}, {}, {}\n".format(team_id, team_name, user['id'], user['name'], user['email'], user['role'], member['role'])) + print("{}\n".format(",".join([team_id, team_name, user['id'], user['name'], user['email'], user['role'], member['role']]))) elif args.csv_file: tf = csv.writer(args.csv_file) tf.writerow([team_id, team_name, user['id'], user['name'], user['email'], user['role'], member['role']]) - sys.stdout.write("Added to {}: {}, {}, {}, {}, {}\n".format(args.csv_file.name, team_id, team_name, user['id'], user['name'], user['email'], user['role'], member['role'])) + print("Added to {}: {}\n".format(args.csv_file.name, ",".join([team_id, team_name, user['id'], user['name'], user['email'], user['role'], 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(user['id'])) - sys.stdout.write("User name: {}\n".format(user['name'])) - sys.stdout.write("User email: {}\n".format(user['email'])) - sys.stdout.write("Base role: {}\n".format(user['role'])) - sys.stdout.write("Team role: {}\n".format(member['role'])) - sys.stdout.write("-----\n") - except pdpyras.PDClientError as e: + print("Team ID: {}\n".format(team_id)) + print("Team Name: {}\n".format(team_name)) + print("User ID: {}\n".format(user['id'])) + print("User name: {}\n".format(user['name'])) + print("User email: {}\n".format(user['email'])) + print("Base role: {}\n".format(user['role'])) + print("Team role: {}\n".format(member['role'])) + print("-----\n") + except PDClientError as e: print("Could not get team members for team {} {}".format(team_name, team_id)) raise e -def get_user_data(user_id, session): - if user_id in Users: - return Users[user_id] +def add_to_cache_users(user_id, users, session): + if user_id in users: + return else: try: user = session.rget('users/{}'.format(user_id)) - Users[user_id] = user - return Users[user_id] - except pdpyras.PDClientError as e: + users[user_id] = user + return + except PDClientError as e: print("Could not get user date for {}".format(user_id)) raise e @@ -58,7 +89,6 @@ def get_user_data(user_id, session): 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") ap.add_argument('-f', '--csv-file', required=False, default=False, type=argparse.FileType('w'), help="Output to a csv file") - Users = {} args = ap.parse_args() - session = pdpyras.APISession(args.api_key) - get_teams(session) \ No newline at end of file + session = APISession(args.api_key) + get_teams(session, args)