Skip to content

Commit 85a4acf

Browse files
author
Phil Rzewski
committed
Alert restore now includes updates, not just creates
1 parent d5be90e commit 85a4acf

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

examples/restore_alerts.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,41 @@
2525
#
2626
sdclient = SdcClient(sdc_token)
2727

28+
#
29+
# If the dump we're restoring from has an Alert with the same name
30+
# as one that's already configured, we'll update the existing Alert
31+
# so it will have the config from the dump. When we do this, however,
32+
# we need to give the ID and Version # of the existing Alert as a
33+
# basis. We save them off here so we can refer to them later.
34+
#
35+
existing_alerts = {}
36+
res = sdclient.get_alerts()
37+
if res[0]:
38+
for alert in res[1]['alerts']:
39+
existing_alerts[alert['name']] = { 'id': alert['id'], 'version': alert['version'] }
40+
else:
41+
print res[1]
42+
sys.exit(1)
43+
44+
created_count = 0
45+
updated_count = 0
46+
2847
with open(alerts_dump_file, 'r') as f:
2948
j = json.load(f)
3049
for a in j['alerts']:
31-
a['description'] += ' (created via restore_alerts.py)'
32-
res = sdclient.create_alert(alert_obj=a)
50+
if a['name'] in existing_alerts:
51+
a['id'] = existing_alerts[a['name']]['id']
52+
a['version'] = existing_alerts[a['name']]['version']
53+
a['description'] += ' (updated via restore_alerts.py)'
54+
res = sdclient.update_alert(a)
55+
updated_count += 1
56+
else:
57+
a['description'] += ' (created via restore_alerts.py)'
58+
res = sdclient.create_alert(alert_obj=a)
59+
created_count += 1
3360
if not res[0]:
3461
print res[1]
3562
sys.exit(1)
3663

37-
print 'All Alerts in ' + alerts_dump_file + ' created successfully.'
64+
print ('All Alerts in ' + alerts_dump_file + ' restored successfully (' +
65+
str(created_count) + ' created, ' + str(updated_count) + ' updated)')

0 commit comments

Comments
 (0)