Last active
July 19, 2024 20:18
-
-
Save Johnsonkdavid/4600580d2e9c01854c97fd1fca63f733 to your computer and use it in GitHub Desktop.
Python api code to export Users list from Pagerduty to CSV file
This file contains 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
##Tested and verified in python3 env | |
#!/usr/bin/python3 | |
import requests | |
import csv | |
import json | |
headers = { | |
"Content-Type": "application/json", | |
"Accept": "application/vnd.pagerduty+json;version=2", | |
"Authorization": "Token token=xxxxxxxxxxxx" ##Replace your api token here | |
} | |
def get_pd_users(): | |
#CSV file header for your report file | |
header=['UserName', 'Email', 'Role'] | |
with open('pagerduty_users.csv', 'w', newline='') as file: | |
writer = csv.writer(file) | |
writer.writerow(header) | |
for offset in range(0, 2000, 100): | |
url = 'https://api.pagerduty.com/users?limit=100&offset={0}'.format(offset) | |
response = requests.request("GET", url, headers=headers) | |
p=json.loads(response.text) | |
for x in range(len(p['users'])): | |
Usr_name = p['users'][x]['name'] | |
Mail_id = p['users'][x]['email'] | |
Role = p['users'][x]['role'] | |
output = (Usr_name,Mail_id,Role) | |
out_list = list(output) | |
writer.writerow(out_list) | |
def main(): | |
get_pd_users() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Johnson,
Thank you for the script. Is there a way to include user last login date in this export?
Thanks again.