1
1
import json
2
- from grafana_backup .dashboardApi import create_contact_point , get_grafana_version
2
+ from grafana_backup .dashboardApi import create_contact_point , get_grafana_version , search_contact_points , update_contact_point
3
3
from packaging import version
4
4
5
5
@@ -23,12 +23,30 @@ def main(args, settings, file_path):
23
23
with open (file_path , 'r' ) as f :
24
24
data = f .read ()
25
25
26
+ result = search_contact_points (grafana_url , http_post_headers , verify_ssl , client_cert , debug )
27
+ status_code = result [0 ]
28
+ existing_contact_points = []
29
+ if status_code == 200 :
30
+ # Successfully received list of contact points.
31
+ # Append contact points to list of existing contact points
32
+ for ecp in result [1 ]:
33
+ existing_contact_points .append (ecp ["uid" ])
34
+
26
35
contact_points = json .loads (data )
27
36
for cp in contact_points :
28
- result = create_contact_point (json .dumps (
29
- cp ), grafana_url , http_post_headers , verify_ssl , client_cert , debug )
30
- print ("create contact_point: {0}, status: {1}, msg: {2}" .format (
31
- cp ['name' ], result [0 ], result [1 ]))
37
+ if cp ["uid" ] in existing_contact_points :
38
+ print ("Contact point {0} already exists, updating" .format (cp ["uid" ]))
39
+ result = update_contact_point (cp ["uid" ], json .dumps (cp ), grafana_url , http_post_headers , verify_ssl , client_cert , debug )
40
+ if result [0 ] == 202 :
41
+ print ("Successfully updated contact point" )
42
+ else :
43
+ print ("[ERROR] Contact point {0} failed to update. Return code:{1} - {2}" .format (cp ["uid" ], result [0 ], result [1 ]))
44
+ else :
45
+ print ("Contact point {0} does not exist, creating" .format (cp ["uid" ]))
46
+ result = create_contact_point (json .dumps (cp ), grafana_url , http_post_headers , verify_ssl , client_cert , debug )
47
+ if result [0 ] == 202 :
48
+ print ("Successfully create contact point" )
49
+ else :
50
+ print ("[ERROR] Contact point {0} failed to create. Retufn code:{1} - {2}" .format (cp ["uid" ], result [0 ], result [1 ]))
32
51
else :
33
- print ("Unable to create contact points, requires Grafana version {0} or above. Current version is {1}" .format (
34
- minimum_version , grafana_version ))
52
+ print ("Unable to create contact points, requires Grafana version {0} or above. Current version is {1}" .format (minimum_version , grafana_version ))
0 commit comments