Create a gist now

Instantly share code, notes, and snippets.

Script to override all on-call schedules for a vacationing user with another user for the given time period within PagerDuty
#!/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 override all on-call schedules for a vacationing user with another user for the given time period
# CLI Usage: ./create_vacation_overrides api_key vacationing_user_name overriding_user_name vacation_start_date vacation_end_date
# api_key: PagerDuty API access token
# vacationing_user_name: Full name of the user going on vacation - must be in quotes
# overriding_user_name: Full name of the user overriding the vacationing user
# vacation_start_date: Date on which the vacation starts
# vacation_end_date: Date on which the vacation ends
import json
import requests
import sys
base_url = "https://api.pagerduty.com/"
def get_oncalls(headers, since, until, vacationing_user_id):
url = base_url + 'oncalls'
payload = {
'since': since,
'until': until,
'user_ids[]': vacationing_user_id
}
r = requests.get(url, headers=headers, params=payload)
return r.json()
def get_user_id_by_name(headers, name):
url = base_url + 'users'
payload = {
'query': name
}
r = requests.get(url, headers=headers, params=payload)
if not r.json()['users']:
print 'Error: No users found by the name: ' + name
elif len(r.json()['users']) > 1:
print 'Error: More than one user found for "' + name + '", please enter the *exact*, full, name of the user, enclosed in quotes'
elif r.json()['users'][0]['name'] == name:
return r.json()['users'][0]['id']
return False
def create_overrides(api_key, vacationing_user_name, overriding_user_name, vacation_start_date, vacation_end_date):
headers = {
'Authorization': 'Token token=' + api_key,
'Content-type': 'application/json',
'Accept': 'application/vnd.pagerduty+json;version=2'
}
vacationing_user_id = get_user_id_by_name(headers, vacationing_user_name)
overriding_user_id = get_user_id_by_name(headers, overriding_user_name)
oncalls = get_oncalls(headers, vacation_start_date, vacation_end_date, vacationing_user_id)
for oncall in oncalls['oncalls']:
if oncall['schedule'] is not None:
url = base_url + 'schedules/' + oncall['schedule']['id'] + '/overrides'
payload = {
'override': {
'start': oncall['start'],
'end': oncall['end'],
'user': {
'id': overriding_user_id,
'type': 'user_reference'
}
}
}
r = requests.post(url, headers=headers, data=json.dumps(payload))
print 'Override creation status code: ' + str(r.status_code)
print 'All overrides have been created'
if __name__ == '__main__':
if len(sys.argv) == 1:
print "Error: You did not enter any parameters.\nUsage: ./create_vacation_overrides api_key vacationing_user_name overriding_user_name vacation_start_date vacation_end_date\n\tapi_key: PagerDuty API access token\n\tvacationing_user_name: Full name of the user going on vacation - must be in quotes\n\toverriding_user_name: Full name of the user overriding the vacationing user\n\tvacation_start_date: Date on which the vacation starts\n\tvacation_end_date: Date on which the vacation ends"
elif len(sys.argv) == 2:
print "Error: You did not enter a vacationing_user_name.\nUsage: ./create_vacation_overrides api_key vacationing_user_name overriding_user_name vacation_start_date vacation_end_date\n\tapi_key: PagerDuty API access token\n\tvacationing_user_name: Full name of the user going on vacation - must be in quotes\n\toverriding_user_name: Full name of the user overriding the vacationing user\n\tvacation_start_date: Date on which the vacation starts\n\tvacation_end_date: Date on which the vacation ends"
elif len(sys.argv) == 3:
print "Error: You did not enter an overriding_user_name.\nUsage: ./create_vacation_overrides api_key vacationing_user_name overriding_user_name vacation_start_date vacation_end_date\n\tapi_key: PagerDuty API access token\n\tvacationing_user_name: Full name of the user going on vacation - must be in quotes\n\toverriding_user_name: Full name of the user overriding the vacationing user\n\tvacation_start_date: Date on which the vacation starts\n\tvacation_end_date: Date on which the vacation ends"
elif len(sys.argv) == 4:
print "Error: You did not enter a vacation_start_date.\nUsage: ./create_vacation_overrides api_key vacationing_user_name overriding_user_name vacation_start_date vacation_end_date\n\tapi_key: PagerDuty API access token\n\tvacationing_user_name: Full name of the user going on vacation - must be in quotes\n\toverriding_user_name: Full name of the user overriding the vacationing user\n\tvacation_start_date: Date on which the vacation starts\n\tvacation_end_date: Date on which the vacation ends"
elif len(sys.argv) == 5:
print "Error: You did not enter a vacation_end_date.\nUsage: ./create_vacation_overrides api_key vacationing_user_name overriding_user_name vacation_start_date vacation_end_date\n\tapi_key: PagerDuty API access token\n\tvacationing_user_name: Full name of the user going on vacation - must be in quotes\n\toverriding_user_name: Full name of the user overriding the vacationing user\n\tvacation_start_date: Date on which the vacation starts\n\tvacation_end_date: Date on which the vacation ends"
else:
create_overrides(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment