Created
July 8, 2021 13:18
-
-
Save gbvanrenswoude/a2ffdf641508e4977b90bbbab870814f to your computer and use it in GitHub Desktop.
A grafana dashboard CR handler leveraging requests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
import requests | |
from requests.structures import CaseInsensitiveDict | |
headers = {"Accept": "application/json","Content-Type": "application/json" ,"Authorization": "Bearer " + os.environ['grafana_pw']} | |
# package dashboard with on the fs since usually the string is too long | |
with open("dashboard.json") as file: | |
dashboard_file = file.read() | |
dashboard_object = json.loads(dashboard_file) | |
dashboard_object['title'] = os.environ['dashboard_app_name'] | |
if len(os.environ['dashboard_app_name']) > 40: | |
dashboard_name = os.environ['dashboard_app_name'][40] | |
else: | |
dashboard_name = os.environ['dashboard_app_name'] | |
dashboard_object['uid'] = dashboard_name | |
dashboard_object['id'] = None | |
print(f'Modified dashboard object to {json.dumps(dashboard_object)}') | |
payload = { | |
"dashboard": dashboard_object, | |
"overwrite": True, | |
} | |
grafana_url = os.getenv('grafana_url', 'https://pimn-grafana.dev.corp-cloud.nl') | |
print(f'Set target {grafana_url}') | |
def on_event(event, context): | |
if event['RequestType'] == 'Delete': | |
try: | |
physical_id = event["PhysicalResourceId"] | |
print(f'Deleting {dashboard_name} from {grafana_url}/api/dashboards/uid/{physical_id}') | |
response = requests.delete(f'{grafana_url}/api/dashboards/uid/{physical_id}', headers=headers, json=payload) | |
print(response.text) | |
sendResponse(event, context, 'SUCCESS', {}, physical_id) | |
except Exception as e: | |
print(e) | |
responseData = {'Failed': f'Dashboard POST to {grafana_url} for {dashboard_name}'} | |
sendResponse(event, context, 'FAILED', responseData, physical_id) | |
else: | |
try: | |
print(f'Posting {dashboard_name} to {grafana_url}') | |
response = requests.post(f'{grafana_url}/api/dashboards/db', headers=headers, json=payload) | |
print(response.text) | |
sendResponse(event, context, 'SUCCESS', {}, dashboard_name) | |
except Exception as e: | |
print(e) | |
responseData = {'Failed': f'Dashboard POST to {grafana_url} for {dashboard_name}'} | |
sendResponse(event, context, 'FAILED', responseData, dashboard_name) | |
def sendResponse(event, context, responseStatus, responseData, id): | |
responseBody = { | |
'Status': responseStatus, | |
'Reason': 'See the details in CloudWatch Log Stream: ' + context.log_stream_name, | |
'PhysicalResourceId': id, | |
'StackId': event['StackId'], | |
'RequestId': event['RequestId'], | |
'LogicalResourceId': event['LogicalResourceId'], | |
'Data': responseData | |
} | |
try: | |
print(f'Sending response to CF: {responseBody}') | |
response = requests.put(event['ResponseURL'], data=json.dumps(responseBody)) | |
if response.status_code != 200: | |
print(response.text) | |
raise Exception(f'Received non 200 response while sending response to CF Stack. We did try sending: {responseBody}') | |
return | |
except Exception as e: | |
print(e) | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment