forked from PagerDuty/public-support-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_all_future_maintenance_windows.py
executable file
·48 lines (42 loc) · 1.83 KB
/
remove_all_future_maintenance_windows.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
import argparse
import pdpyras
def remove_all_future_maintenance_windows(args):
session = pdpyras.APISession(args.api_key)
progress_printer = lambda o, i, n: (print("Deleting %d/%d: %s"%(
i, n, o['summary']
)))
mw_params = {"filter":"future"}
if len(args.service_ids):
mw_params['service_ids[]'] = args.service_ids
for mw in session.iter_all("maintenance_windows",
item_hook=progress_printer, params=mw_params, total=True):
if args.dry_run:
continue
try:
session.delete(mw['self'])
except PDClientError as e:
message = "API Error: %s"%e
if e.response is not None:
message += " HTTP %d: %s"%(e.response.status_code,
e.response.text)
print(message)
continue
if args.dry_run:
print("(Didn't actually delete anything, since -n/--dry-run was given)")
def main(argv=None):
ap = argparse.ArgumentParser(description="Deletes all future maintenance "
"windows in a PagerDuty account. Useful for when an automated process "
"for creating maintenance windows was used incorrectly or errored.")
ap.add_argument('-k', '--api-key', required=True, help="REST API key")
ap.add_argument('-s', '--service', dest='service_ids', action='append',
default=[], help="One or more service IDs to which the deletion should "
"be limited. Note, if unspecified, MAINTENANCE WINDOWS FOR ALL "
"SERVICES will be deleted.")
ap.add_argument('-n', '--dry-run', default=False, action='store_true',
help="Don't perform any action; instead, show the maintenance windows "
"that would be deleted.")
args = ap.parse_args()
remove_all_future_maintenance_windows(args)
if __name__=='__main__':
main()