1
1
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
3
3
from packaging import version
4
4
5
5
6
6
def main (args , settings , file_path ):
7
7
grafana_url = settings .get ('GRAFANA_URL' )
8
8
http_post_headers = settings .get ('HTTP_POST_HEADERS' )
9
- http_post_headers [ 'x-disable-provenance' ] = '*'
9
+ http_get_headers = settings . get ( 'HTTP_GET_HEADERS' )
10
10
verify_ssl = settings .get ('VERIFY_SSL' )
11
11
client_cert = settings .get ('CLIENT_CERT' )
12
12
debug = settings .get ('DEBUG' )
@@ -19,7 +19,16 @@ def main(args, settings, file_path):
19
19
20
20
if minimum_version <= grafana_version :
21
21
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 )
23
32
print ("create alert rule: {0}, status: {1}, msg: {2}" .format (alert_rule ['title' ], result [0 ], result [1 ]))
24
33
else :
25
34
print ("Unable to create alert rules, requires Grafana version {0} or above. Current version is {1}" .format (minimum_version , grafana_version ))
0 commit comments