From 1e14e490268209d4b2e0f9c21bbaff9d9cc764e1 Mon Sep 17 00:00:00 2001 From: Derek McEachern Date: Mon, 27 Nov 2023 01:37:31 -0600 Subject: [PATCH] feat: Added ability to update contact points (#248) --- CHANGELOG.md | 6 +++++ grafana_backup/create_contact_point.py | 32 ++++++++++++++++++++------ grafana_backup/dashboardApi.py | 3 +++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a27545..45dc172 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +# [1.5.0] - 2023-11-10 + +### Changed +- If a contact point already exits it will be updated. Previous behaviour was to only to create which failed for existing contact points. + + # [1.4.2] - 2023-11-01 ### Added diff --git a/grafana_backup/create_contact_point.py b/grafana_backup/create_contact_point.py index ee1239e..138879d 100644 --- a/grafana_backup/create_contact_point.py +++ b/grafana_backup/create_contact_point.py @@ -1,5 +1,5 @@ import json -from grafana_backup.dashboardApi import create_contact_point, get_grafana_version +from grafana_backup.dashboardApi import create_contact_point, get_grafana_version, search_contact_points, update_contact_point from packaging import version @@ -23,12 +23,30 @@ def main(args, settings, file_path): with open(file_path, 'r') as f: data = f.read() + result = search_contact_points(grafana_url, http_post_headers, verify_ssl, client_cert, debug) + status_code = result[0] + existing_contact_points = [] + if status_code == 200: + # Successfully received list of contact points. + # Append contact points to list of existing contact points + for ecp in result[1]: + existing_contact_points.append(ecp["uid"]) + contact_points = json.loads(data) for cp in contact_points: - result = create_contact_point(json.dumps( - cp), grafana_url, http_post_headers, verify_ssl, client_cert, debug) - print("create contact_point: {0}, status: {1}, msg: {2}".format( - cp['name'], result[0], result[1])) + if cp["uid"] in existing_contact_points: + print("Contact point {0} already exists, updating".format(cp["uid"])) + result = update_contact_point(cp["uid"], json.dumps(cp), grafana_url, http_post_headers, verify_ssl, client_cert, debug) + if result[0] == 202: + print("Successfully updated contact point") + else: + print("[ERROR] Contact point {0} failed to update. Return code:{1} - {2}".format(cp["uid"], result[0], result[1])) + else: + print("Contact point {0} does not exist, creating".format(cp["uid"])) + result = create_contact_point(json.dumps(cp), grafana_url, http_post_headers, verify_ssl, client_cert, debug) + if result[0] == 202: + print("Successfully create contact point") + else: + print("[ERROR] Contact point {0} failed to create. Retufn code:{1} - {2}".format(cp["uid"], result[0], result[1])) else: - print("Unable to create contact points, requires Grafana version {0} or above. Current version is {1}".format( - minimum_version, grafana_version)) + print("Unable to create contact points, requires Grafana version {0} or above. Current version is {1}".format(minimum_version, grafana_version)) diff --git a/grafana_backup/dashboardApi.py b/grafana_backup/dashboardApi.py index 82b6177..8fbdaf8 100755 --- a/grafana_backup/dashboardApi.py +++ b/grafana_backup/dashboardApi.py @@ -473,6 +473,9 @@ def search_contact_points(grafana_url, http_get_headers, verify_ssl, client_cert def create_contact_point(json_palyload, grafana_url, http_post_headers, verify_ssl, client_cert, debug): return send_grafana_post('{0}/api/v1/provisioning/contact-points'.format(grafana_url), json_palyload, http_post_headers, verify_ssl, client_cert, debug) +def update_contact_point(uid, json_palyload, grafana_url, http_post_headers, verify_ssl, client_cert, debug): + 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) + def search_notification_policies(grafana_url, http_get_headers, verify_ssl, client_cert, debug): return send_grafana_get('{0}/api/v1/provisioning/policies'.format(grafana_url), http_get_headers, verify_ssl, client_cert, debug)