Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added ability to update contact points #248

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 25 additions & 7 deletions grafana_backup/create_contact_point.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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))
3 changes: 3 additions & 0 deletions grafana_backup/dashboardApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading