Last active
February 20, 2025 19:13
-
-
Save atrepca/57f49e02782b5f6c60fe9f8ba97101e3 to your computer and use it in GitHub Desktop.
Get list of PagerDuty users
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
#!/usr/bin/env python3 | |
import csv | |
import os | |
import sys | |
import pagerduty | |
def fetch_and_export_users(api_token, csv_filename): | |
"""Fetch users from PagerDuty and export them to a CSV file.""" | |
# Initialize PagerDuty client | |
client = pagerduty.RestApiV2Client(api_token) | |
try: | |
# Open CSV and write headers | |
with open(csv_filename, 'w', newline='') as file: | |
writer = csv.writer(file) | |
writer.writerow(['Name', 'Email', 'Role']) | |
# Fetch and write user data | |
for user in client.iter_all('users'): | |
writer.writerow([ | |
user.get('name', 'N/A'), | |
user.get('email', 'N/A'), | |
user.get('role', 'N/A') | |
]) | |
print(f"User data successfully exported to {csv_filename}") | |
except Exception as e: | |
print(f"Error fetching users: {e}") | |
sys.exit(1) | |
def main(): | |
# Get API token from environment variable | |
api_token = os.getenv('PD_API_KEY') | |
if not api_token: | |
print("Error: PD_API_KEY environment variable not set.") | |
sys.exit(1) | |
# Output file | |
csv_filename = 'pagerduty_users.csv' | |
fetch_and_export_users(api_token, csv_filename) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment