-
-
Save pnancarrow/591e226c655b638bf04f6650131cf88c to your computer and use it in GitHub Desktop.
Export a list of your PagerDuty users to a CSV file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Export a list of all users to a CSV file. | |
| #This script is not supported by PagerDuty. | |
| #!/usr/bin/env python | |
| import datetime | |
| import requests | |
| import sys | |
| import csv | |
| #Your PagerDuty API key. A read-only key will work for this. | |
| AUTH_TOKEN = 'YOUR_API_KEY' | |
| #The API base url, make sure to include the subdomain | |
| BASE_URL = 'https://YOUR_SUBDOMAIN.pagerduty.com/api/v1' | |
| csvfile = "users.csv" | |
| HEADERS = { | |
| 'Authorization': 'Token token={0}'.format(AUTH_TOKEN), | |
| 'Content-type': 'application/json', | |
| } | |
| user_count = 0 | |
| def get_user_count(): | |
| global user_count | |
| count = requests.get( | |
| '{0}/users'.format(BASE_URL), | |
| headers=HEADERS | |
| ) | |
| user_count = count.json()['total'] | |
| def get_users(offset): | |
| global user_count | |
| params = { | |
| 'offset':offset | |
| } | |
| all_users = requests.get( | |
| '{0}/users'.format(BASE_URL), | |
| headers=HEADERS, | |
| params=params | |
| ) | |
| print "Exporting all users to " + csvfile | |
| for user in all_users.json()['users']: | |
| with open(csvfile, "a") as output: | |
| writer = csv.writer(output, lineterminator='\n') | |
| writer.writerow( [user['id']] + [user['email']] + [user['name']]) | |
| def main(argv=None): | |
| if argv is None: | |
| argv = sys.argv | |
| get_user_count() | |
| for offset in xrange(0,user_count): | |
| if offset % 25 == 0: | |
| get_users(offset) | |
| if __name__=='__main__': | |
| sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment