Skip to content

Instantly share code, notes, and snippets.

@lfepp
Last active October 15, 2019 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lfepp/047a363d504d6aa3945f42a4b6d08886 to your computer and use it in GitHub Desktop.
Save lfepp/047a363d504d6aa3945f42a4b6d08886 to your computer and use it in GitHub Desktop.
Script to remove all future maintenance windows from your PagerDuty account or your PagerDuty services
#!/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 remove all future maintenance windows within your account
# CLI Usage: ./remove_future_maintenance_windows api_key [service_ids...]
# api_key: Your PagerDuty API access token
# service_ids: Unique identifier(s) for any service(s) you want to delete the maintenance windows from
import requests
import sys
import json
base_url = 'https://api.pagerduty.com/maintenance_windows'
def get_future_maintenance_windows(headers, output=[], offset=0):
payload = {
'filter': 'future',
'limit': 100,
'offset': offset
}
r = requests.get(base_url, headers=headers, params=payload)
output.extend(r.json()['maintenance_windows'])
if r.json()['more'] == True:
return get_future_maintenance_windows(headers, output, offset + 100)
else:
return output
def delete_maintenance_window(headers, window_id):
url = base_url + '/' + window_id
r = requests.delete(url, headers=headers)
if r.status_code == 204:
print 'Successfully deleted maintenance window with ID ' + window_id
else:
print 'Error: Failed to delete maintenance window with ID ' + window_id + '\nStatus code: ' + str(r.status_code) + '\nResponse: ' + r.text
def update_maintenance_window(headers, window):
url = base_url + '/' + window['id']
payload = {
'maintenance_window': window
}
r = requests.put(url, headers=headers, data=json.dumps(payload))
if r.status_code == 200:
print 'Successfully deleted service from maintenance window with ID ' + window['id']
else:
print 'Error: Failed to delete service from maintenance window with ID ' + window['id'] + '\nStatus code: ' + str(r.status_code) + '\nResponse: ' + r.text
def remove_all_future_maintenance_windows(api_key):
headers = {
'Authorization': 'Token token=' + api_key,
'Content-type': 'application/json',
'Accept': 'application/vnd.pagerduty+json;version=2'
}
all_windows = get_future_maintenance_windows(headers)
print 'Total number of future maintenance windows: ' + str(len(all_windows))
for window in all_windows:
delete_maintenance_window(headers, window['id'])
def update_future_maintenance_windows(api_key, service_list):
headers = {
'Authorization': 'Token token=' + api_key,
'Content-type': 'application/json',
'Accept': 'application/vnd.pagerduty+json;version=2'
}
all_windows = get_future_maintenance_windows(headers)
for service_id in service_list:
for window in all_windows:
for index, service in enumerate(window['services']):
if service['id'] == service_id:
del window['services'][index]
update_maintenance_window(headers, window)
if __name__=='__main__':
if len(sys.argv) == 1:
print 'Error: You did not enter any parameters.\nUsage: ./remove_future_maintenance_windows api_key [service_ids...]\n\tapi_key: Your PagerDuty API access token\n\tservice_ids: Unique identifier(s) for any service(s) you want to delete the maintenance windows from'
elif len(sys.argv) == 2:
print 'Warning! You did not enter any services. All future maintenance windows will be deleted...'
remove_all_future_maintenance_windows(sys.argv[1])
else:
service_list = []
for service in sys.argv[2:]:
service_list.append(service)
update_future_maintenance_windows(sys.argv[1], service_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment