Skip to content

Instantly share code, notes, and snippets.

@lfepp lfepp/create_users.py
Last active Jun 6, 2016

Embed
What would you like to do?
Script to create a list of users in PagerDuty from a file named "users.csv"
#!/usr/bin/env python
#
# Copyright (c) 2016, PagerDuty, Inc. <info@pagerduty.com>
# 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.
#
# Script to create a list of users given a CSV named "users.csv" in the format:
#
# name,email,role,address,type
# John Doe,john.doe@example.com,admin,5555555555,phone
# Jane Doe,jane.doe@example.com,user,5555555554,sms
#
# CLI Usage: ./create_users api_key email
# api_key: Your PagerDuty API access Token
# email: Your PagerDuty email address
import requests
import json
import csv
import sys
base_url = 'https://api.pagerduty.com/users'
def create_user(headers, email, payload):
headers['From'] = email
r = requests.post(base_url, headers=headers, data=json.dumps(payload))
print 'User creation response code: ' + str(r.status_code)
return r.json()['user']
def create_contact_method(headers, user_id, payload):
url = base_url + '/' + user_id + '/contact_methods'
r = requests.post(url, headers=headers, data=json.dumps(payload))
print 'Contact method response code: ' + str(r.status_code)
return r.json()['contact_method']['id']
def create_notification_rule(headers, user_id, payload):
url = base_url + '/' + user_id + '/notification_rules'
r = requests.post(url, headers=headers, data=json.dumps(payload))
print 'Notification rule response code: ' + str(r.status_code)
return
def process_csv(headers, email):
reader = csv.DictReader(open('users.csv'), fieldnames=('name','email','role','address','type'))
reader.next()
for row in reader:
print 'Creating user: ' + row['name']
user = {
'name': row['name'],
'email': row['email'],
'role': row['role'],
'type': 'user',
'summary': row['name']
}
r = create_user(headers, email, user)
user_id = r['id']
if row['type'] == 'phone':
contact_method = {
'contact_method': {
'type': 'phone_contact_method',
'address': row['address']
}
}
elif row['type'] == 'sms':
contact_method = {
'contact_method': {
'type': 'sms_contact_method',
'address': row['address']
}
}
else:
print 'Error: You did not enter a valid address type.\nExiting...'
break
# Create phone contact and notification rule
contact_method_id = create_contact_method(headers, user_id, contact_method)
notification_rule = {
'notification_rule': {
'start_delay_in_minutes': 0,
'contact_method': {
'id': contact_method_id,
'type': contact_method['contact_method']['type']
}
}
}
create_notification_rule(headers, user_id, notification_rule)
if __name__ == '__main__':
if len(sys.argv) == 1:
print 'Error: You did not enter any parameters.\nUsage: ./create_users api_key email\n\tapi_key: Your PagerDuty API access Token\n\temail: Your PagerDuty email address'
elif len(sys.argv) == 2:
print 'Error: You did not enter your PagerDuty email address.\nUsage: ./create_users api_key email\n\tapi_key: Your PagerDuty API access Token\n\temail: Your PagerDuty email address'
else:
headers = {
'Authorization': 'Token token=' + sys.argv[1],
'Content-type': 'application/json',
'Accept': 'application/vnd.pagerduty+json;version=2'
}
process_csv(headers, sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.