From 5c903b9b690d9e0d93866c35111cfe288fb3c1ed Mon Sep 17 00:00:00 2001 From: prupke2 <31414339+prupke2@users.noreply.github.com> Date: Thu, 16 Aug 2018 13:33:12 -0400 Subject: [PATCH 1/2] Added end_all_maintenance_windows.py Script to end all ongoing maintenance windows in your subdomain. --- .../end_all_maintenance_windows | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 REST_API_v2/MaintenanceWindows/end_all_maintenance_windows diff --git a/REST_API_v2/MaintenanceWindows/end_all_maintenance_windows b/REST_API_v2/MaintenanceWindows/end_all_maintenance_windows new file mode 100644 index 0000000..9d5e8a1 --- /dev/null +++ b/REST_API_v2/MaintenanceWindows/end_all_maintenance_windows @@ -0,0 +1,140 @@ +#!/usr/bin/env python +# +# Copyright (c) 2018, PagerDuty, Inc. +# 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. + +# -------------------------------------------------------------------------------- +# INSTRUCTIONS +# +# This script will search for all currently active maintenance windows on all +# services and end them. It does not support pagination, so it is limited to 100 +# maintenance windows (by default it will sort by name) +# +# REQUIRED: API_KEY, EMAIL +# +# OPTIONAL: You can set the END_TIME to a future date if required. +# +# # Date format: '2018-09-30T14:00:00' +# +# There are also other parameters for filtering maintenance windows. +# +# -------------------------------------------------------------------------------- + +import requests +import datetime + +current_time = datetime.datetime.now() + +# Update to match your v2 REST API key +API_KEY = '' + +# Update to match your login email +EMAIL = '' + +# Update to match your chosen parameters +# You can also specify a different end date here instead (Date format: '2018-09-30T14:00:00') +END_TIME = current_time +TEAM_IDS = [] +SERVICE_IDS = [] +INCLUDE = [] +FILTER = 'ongoing' +QUERY = '' + + +def get_maintenance_windows(): + url = 'https://api.pagerduty.com/maintenance_windows?total=true' + headers = { + 'Accept': 'application/vnd.pagerduty+json;version=2', + 'Authorization': 'Token token={token}'.format(token=API_KEY) + } + payload = { + 'team_ids[]': TEAM_IDS, + 'service_ids[]': SERVICE_IDS, + 'include[]': INCLUDE, + 'filter': FILTER, + 'query': QUERY + } + maintenance_windows_ids = [] + r = requests.get(url, headers=headers, params=payload) + print('Getting ongoing maintenance windows...') + print('\tStatus Code: {code}'.format(code=r.status_code)) + if r.status_code < 200 or r.status_code >= 204: + print("\tThere was an error getting your maintenance windows.") + print("\tPlease ensure that the login email and v2 REST API key in this script are correct.") + print(r.text) + return maintenance_windows_ids + maintenance_list = r.json() + ids = maintenance_list['maintenance_windows'] + total = int(maintenance_list['total']) + + if total > 0: + if total == 1: + print("\t1 ongoing maintenance window found:") + if total == 2: + print("\t", total, 'ongoing maintenance windows found') + for maintenance_window in range(total): + maintenance_windows_ids.append(ids[maintenance_window]['id']) + print("\t", maintenance_windows_ids) + else: + print("No ongoing maintenance windows found") + return maintenance_windows_ids + + +def end_maintenance_window(MAINTENANCE_WINDOWS): + if len(MAINTENANCE_WINDOWS) == 1: + print("Ending maintenance window...") + else: + print("Ending maintenance windows...") + headers = { + 'Accept': 'application/vnd.pagerduty+json;version=2', + 'Authorization': 'Token token={token}'.format(token=API_KEY) + } + for maintenance_window in range(len(MAINTENANCE_WINDOWS)): + url = 'https://api.pagerduty.com/maintenance_windows/{id}'.format(id=MAINTENANCE_WINDOWS[maintenance_window]) + print("\t", str(MAINTENANCE_WINDOWS[maintenance_window])) + r = requests.delete(url, headers=headers) + print('\tStatus Code: {code}'.format(code=r.status_code)) + if r.status_code == 204: + print('\tMaintenance window ended successfully!') + else: + print("\tThere was an error ending this maintenance window") + print("\tPlease ensure that the login email and v2 REST API key in this script have proper permissions") + return maintenance_windows_ids + print(r.text) + +if __name__ == '__main__': + + if EMAIL == '': + print("Please add your login email to this script and run it again.") + elif API_KEY == '': + print("Please add your v2 REST API key to this script and run it again.") + else: + MAINTENANCE_WINDOWS = get_maintenance_windows() + if MAINTENANCE_WINDOWS != []: + end_maintenance_window(MAINTENANCE_WINDOWS) + + +# ----------------------------------------------------------------------- + From 3f31569b6f08f81c05bf2c073e6e2fa1341bf52c Mon Sep 17 00:00:00 2001 From: prupke2 <31414339+prupke2@users.noreply.github.com> Date: Thu, 16 Aug 2018 13:54:22 -0400 Subject: [PATCH 2/2] Rename end_all_maintenance_windows to end_all_maintenance_windows.py --- ...end_all_maintenance_windows => end_all_maintenance_windows.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename REST_API_v2/MaintenanceWindows/{end_all_maintenance_windows => end_all_maintenance_windows.py} (100%) diff --git a/REST_API_v2/MaintenanceWindows/end_all_maintenance_windows b/REST_API_v2/MaintenanceWindows/end_all_maintenance_windows.py similarity index 100% rename from REST_API_v2/MaintenanceWindows/end_all_maintenance_windows rename to REST_API_v2/MaintenanceWindows/end_all_maintenance_windows.py