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
diff --git a/README.md b/README.md
index c3881b8..37aed68 100644
--- a/README.md
+++ b/README.md
@@ -14,6 +14,19 @@ To retrieve autogenerated password run:
14 juju run --service grafana "scripts/get_admin_password"14 juju run --service grafana "scripts/get_admin_password"
1515
1616
17#Actions
18
19This charm supports importing dashboards, simply run:
20
21 $ juju run-action grafana/0 import-dashboard dashboard="$(base64 mydashboard.json)"
22
23where mydashboard.json is a json file:
24
25 { "dashboard": { exported-json-dashboard },
26 "overwrite": true }
27
28If you don't want to overwrite the dashboard, set overwrite to false.
29
17#Development30#Development
1831
19Explicitly set `JUJU_REPOSITORY`:32Explicitly set `JUJU_REPOSITORY`:
diff --git a/actions.yaml b/actions.yaml
20new file mode 10064433new file mode 100644
index 0000000..4d5f671
--- /dev/null
+++ b/actions.yaml
@@ -0,0 +1,8 @@
1import-dashboard:
2 description: Imports a dashboard to grafana, takes dashboard argument, base64 encoded json dashboard
3 params:
4 dashboard:
5 type: string
6 description: Contains the dashboard to be imported, in base64 encoded json format
7 required: [dashboard]
8 additionalProperties: false
diff --git a/actions/import-dashboard b/actions/import-dashboard
0new file mode 1007559new file mode 100755
index 0000000..d61ff5f
--- /dev/null
+++ b/actions/import-dashboard
@@ -0,0 +1,55 @@
1#!/usr/bin/python3
2# Imports a json based dashboard into grafana
3
4import requests
5import json
6import base64
7from charmhelpers.core.hookenv import (
8 action_fail,
9 action_set,
10 action_get,
11 config,
12)
13
14from charmhelpers.core import unitdata
15
16kv = unitdata.kv()
17if kv.get('grafana.admin_password'):
18 # print('Admin password: {}'.format(kv.get('grafana.admin_password')))
19 passwd = kv.get('grafana.admin_password')
20elif config('admin_password'):
21 passwd = config('admin_password')
22 # print('Admin password: {}'.format(config('admin_password')))
23else:
24 action_fail('ERROR! Unable to retrieve password.')
25 exit(0)
26
27grafana = "http://localhost:3000"
28api_auth = ('admin', passwd)
29api_dash_import_url = "/api/dashboards/import"
30try:
31 dashboard_data = json.loads(base64.b64decode(action_get('dashboard')).decode('utf-8'))
32except base64.binascii.Error:
33 action_fail("Failed to base64 decode dashboard!")
34 exit(0)
35except json.JSONDecodeError as e:
36 action_fail("Fail to json decode the dashboard: %s" % (e.msg))
37 exit(0)
38else:
39 action_fail("Unknown error dealing with dashboard!")
40 exit(0)
41
42# Needs to be the format:
43# { "dashboard": { dashboard },
44# "overwrite": true } - use false here if you don't want to overwrite
45# existing dashboards
46
47headers = {'Content-Type': 'application/json'}
48r = requests.post(grafana + api_dash_import_url, auth=api_auth, headers=headers,
49 data=json.dumps(dashboard_data))
50title = dashboard_data['dashboard']['title']
51if r.status_code == 200:
52 action_set({"loaded": title})
53else:
54 action_fail("Dashboard %s failed to load" % (title))
55

Subscribers

People subscribed via source and target branches