Merge ~brad-marshall/charm-grafana/+git/grafana-charm-dashboard-action:actions into ~prometheus-charmers/charm-grafana:master

Proposed by Brad Marshall
Status: Merged
Merged at revision: afd175ae866d18f2aab6c99f261ccd1df61fd4b0
Proposed branch: ~brad-marshall/charm-grafana/+git/grafana-charm-dashboard-action:actions
Merge into: ~prometheus-charmers/charm-grafana:master
Diff against target: 99 lines (+76/-0)
3 files modified
README.md (+13/-0)
actions.yaml (+8/-0)
actions/import-dashboard (+55/-0)
Reviewer Review Type Date Requested Status
Prometheus Charmers Pending
Review via email: mp+326911@code.launchpad.net

Description of the change

Include add-dashboard action to import json exported dashboards

To post a comment you must log in.
Revision history for this message
Jacek Nykis (jacekn) wrote :

A few small comments in line but IMO it's fine. They only comment I'd like answered is about trusty vs xenial

Revision history for this message
Brad Marshall (brad-marshall) wrote :

I'll work on this soon, some good feedback here.

Revision history for this message
Jacek Nykis (jacekn) wrote :

LGTM, merged. I did _not_ compose new charm, feel free to do that when convenient.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/README.md b/README.md
2index c3881b8..37aed68 100644
3--- a/README.md
4+++ b/README.md
5@@ -14,6 +14,19 @@ To retrieve autogenerated password run:
6 juju run --service grafana "scripts/get_admin_password"
7
8
9+#Actions
10+
11+This charm supports importing dashboards, simply run:
12+
13+ $ juju run-action grafana/0 import-dashboard dashboard="$(base64 mydashboard.json)"
14+
15+where mydashboard.json is a json file:
16+
17+ { "dashboard": { exported-json-dashboard },
18+ "overwrite": true }
19+
20+If you don't want to overwrite the dashboard, set overwrite to false.
21+
22 #Development
23
24 Explicitly set `JUJU_REPOSITORY`:
25diff --git a/actions.yaml b/actions.yaml
26new file mode 100644
27index 0000000..4d5f671
28--- /dev/null
29+++ b/actions.yaml
30@@ -0,0 +1,8 @@
31+import-dashboard:
32+ description: Imports a dashboard to grafana, takes dashboard argument, base64 encoded json dashboard
33+ params:
34+ dashboard:
35+ type: string
36+ description: Contains the dashboard to be imported, in base64 encoded json format
37+ required: [dashboard]
38+ additionalProperties: false
39diff --git a/actions/import-dashboard b/actions/import-dashboard
40new file mode 100755
41index 0000000..d61ff5f
42--- /dev/null
43+++ b/actions/import-dashboard
44@@ -0,0 +1,55 @@
45+#!/usr/bin/python3
46+# Imports a json based dashboard into grafana
47+
48+import requests
49+import json
50+import base64
51+from charmhelpers.core.hookenv import (
52+ action_fail,
53+ action_set,
54+ action_get,
55+ config,
56+)
57+
58+from charmhelpers.core import unitdata
59+
60+kv = unitdata.kv()
61+if kv.get('grafana.admin_password'):
62+ # print('Admin password: {}'.format(kv.get('grafana.admin_password')))
63+ passwd = kv.get('grafana.admin_password')
64+elif config('admin_password'):
65+ passwd = config('admin_password')
66+ # print('Admin password: {}'.format(config('admin_password')))
67+else:
68+ action_fail('ERROR! Unable to retrieve password.')
69+ exit(0)
70+
71+grafana = "http://localhost:3000"
72+api_auth = ('admin', passwd)
73+api_dash_import_url = "/api/dashboards/import"
74+try:
75+ dashboard_data = json.loads(base64.b64decode(action_get('dashboard')).decode('utf-8'))
76+except base64.binascii.Error:
77+ action_fail("Failed to base64 decode dashboard!")
78+ exit(0)
79+except json.JSONDecodeError as e:
80+ action_fail("Fail to json decode the dashboard: %s" % (e.msg))
81+ exit(0)
82+else:
83+ action_fail("Unknown error dealing with dashboard!")
84+ exit(0)
85+
86+# Needs to be the format:
87+# { "dashboard": { dashboard },
88+# "overwrite": true } - use false here if you don't want to overwrite
89+# existing dashboards
90+
91+headers = {'Content-Type': 'application/json'}
92+r = requests.post(grafana + api_dash_import_url, auth=api_auth, headers=headers,
93+ data=json.dumps(dashboard_data))
94+title = dashboard_data['dashboard']['title']
95+if r.status_code == 200:
96+ action_set({"loaded": title})
97+else:
98+ action_fail("Dashboard %s failed to load" % (title))
99+

Subscribers

People subscribed via source and target branches