Skip to content

Commit 1e14e49

Browse files
feat: Added ability to update contact points (#248)
1 parent eee7659 commit 1e14e49

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
# [1.5.0] - 2023-11-10
8+
9+
### Changed
10+
- If a contact point already exits it will be updated. Previous behaviour was to only to create which failed for existing contact points.
11+
12+
713
# [1.4.2] - 2023-11-01
814

915
### Added

grafana_backup/create_contact_point.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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
33
from packaging import version
44

55

@@ -23,12 +23,30 @@ def main(args, settings, file_path):
2323
with open(file_path, 'r') as f:
2424
data = f.read()
2525

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+
2635
contact_points = json.loads(data)
2736
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]))
3251
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))

grafana_backup/dashboardApi.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,9 @@ def search_contact_points(grafana_url, http_get_headers, verify_ssl, client_cert
473473
def create_contact_point(json_palyload, grafana_url, http_post_headers, verify_ssl, client_cert, debug):
474474
return send_grafana_post('{0}/api/v1/provisioning/contact-points'.format(grafana_url), json_palyload, http_post_headers, verify_ssl, client_cert, debug)
475475

476+
def update_contact_point(uid, json_palyload, grafana_url, http_post_headers, verify_ssl, client_cert, debug):
477+
return send_grafana_put('{0}/api/v1/provisioning/contact-points/{1}'.format(grafana_url, uid), json_palyload, http_post_headers, verify_ssl, client_cert, debug)
478+
476479

477480
def search_notification_policies(grafana_url, http_get_headers, verify_ssl, client_cert, debug):
478481
return send_grafana_get('{0}/api/v1/provisioning/policies'.format(grafana_url), http_get_headers, verify_ssl, client_cert, debug)

0 commit comments

Comments
 (0)