|
25 | 25 | #
|
26 | 26 | sdclient = SdcClient(sdc_token)
|
27 | 27 |
|
| 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 | + |
28 | 47 | with open(alerts_dump_file, 'r') as f:
|
29 | 48 | j = json.load(f)
|
30 | 49 | 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 |
33 | 60 | if not res[0]:
|
34 | 61 | print res[1]
|
35 | 62 | sys.exit(1)
|
36 | 63 |
|
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