Skip to content

Commit 9cec864

Browse files
authored
Merge pull request #32 from draios/restore-alerts-with-update
Enhance "restore alerts" example to include updates (previously was only creates)
2 parents d5be90e + bcf27df commit 9cec864

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ script:
2929
- examples/print_explore_grouping.py XXX
3030
- examples/print_user_info.py XXX
3131
- examples/list_sysdig_captures.py XXX
32-
- examples/create_sysdig_capture.py XXX ip-10-0-1-140.ec2.internal apicapture 10
32+
- examples/create_sysdig_capture.py XXX ip-10-0-1-115.ec2.internal apicapture 10
3333
- examples/notification_channels.py XXX
3434
- examples/user_team_mgmt.py XXX example-team [email protected]
3535
- echo "Testing pip version"

examples/restore_alerts.py

+31-3
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)