Skip to content

Commit adfd8b8

Browse files
authored
Merge pull request #217 from mt3593/perform-upsert-alert-rules
Perform upsert of alert rules
2 parents 4a85d57 + a2da25d commit adfd8b8

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

grafana_backup/create_alert_rule.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import json
2-
from grafana_backup.dashboardApi import create_alert_rule, get_grafana_version
2+
from grafana_backup.dashboardApi import get_alert_rule, create_alert_rule, update_alert_rule, get_grafana_version
33
from packaging import version
44

55

66
def main(args, settings, file_path):
77
grafana_url = settings.get('GRAFANA_URL')
88
http_post_headers = settings.get('HTTP_POST_HEADERS')
9-
http_post_headers['x-disable-provenance']='*'
9+
http_get_headers = settings.get('HTTP_GET_HEADERS')
1010
verify_ssl = settings.get('VERIFY_SSL')
1111
client_cert = settings.get('CLIENT_CERT')
1212
debug = settings.get('DEBUG')
@@ -19,7 +19,16 @@ def main(args, settings, file_path):
1919

2020
if minimum_version <= grafana_version:
2121
alert_rule = json.loads(data)
22-
result = create_alert_rule(json.dumps(alert_rule), grafana_url, http_post_headers, verify_ssl, client_cert, debug)
22+
del alert_rule['id']
23+
uid = alert_rule['uid']
24+
get_response= get_alert_rule(uid, grafana_url, http_get_headers, verify_ssl, client_cert, debug)
25+
status_code=get_response[0]
26+
print("Got a code: {0}", status_code)
27+
if status_code == 404:
28+
http_post_headers['x-disable-provenance']='*'
29+
result = create_alert_rule(json.dumps(alert_rule), grafana_url, http_post_headers, verify_ssl, client_cert, debug)
30+
else:
31+
result = update_alert_rule(alert_rule['uid'], json.dumps(alert_rule), grafana_url, http_post_headers, verify_ssl, client_cert, debug)
2332
print("create alert rule: {0}, status: {1}, msg: {2}".format(alert_rule['title'], result[0], result[1]))
2433
else:
2534
print("Unable to create alert rules, requires Grafana version {0} or above. Current version is {1}".format(minimum_version, grafana_version))

0 commit comments

Comments
 (0)