From efd23a7d7889ce164c48bc61eda746aefdaa9b0e Mon Sep 17 00:00:00 2001 From: Brad Marshall Date: Mon, 10 Jul 2017 11:39:35 +1000 Subject: Tidied up redundant reference code, renamed action, more detail on import format --- actions.yaml | 6 +++--- actions/add-dashboard | 47 ----------------------------------------------- actions/import-dashboard | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 50 deletions(-) delete mode 100755 actions/add-dashboard create mode 100755 actions/import-dashboard diff --git a/actions.yaml b/actions.yaml index 55c9ed7..4d5f671 100644 --- a/actions.yaml +++ b/actions.yaml @@ -1,8 +1,8 @@ -add-dashboard: - description: Adds a dashboard to grafana, takes dashboard argument, base64 encoded json dashboard +import-dashboard: + description: Imports a dashboard to grafana, takes dashboard argument, base64 encoded json dashboard params: dashboard: type: string - description: Contains the dashboard to be imported, in base64 json format + description: Contains the dashboard to be imported, in base64 encoded json format required: [dashboard] additionalProperties: false diff --git a/actions/add-dashboard b/actions/add-dashboard deleted file mode 100755 index cf31480..0000000 --- a/actions/add-dashboard +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/python3 -# Imports a json based dashboard into grafana - -import requests -import json -import base64 -from charmhelpers.core.hookenv import ( - action_fail, - action_set, - action_get, - config, -) - -from charmhelpers.core import unitdata - -kv = unitdata.kv() -if kv.get('grafana.admin_password'): - # print('Admin password: {}'.format(kv.get('grafana.admin_password'))) - passwd = kv.get('grafana.admin_password') -elif config('admin_password'): - passwd = config('admin_password') - # print('Admin password: {}'.format(config('admin_password'))) -else: - action_fail('ERROR! Unable to retrieve password.') - -grafana = "http://localhost:3000" -api_auth = ('admin', passwd) -api_dash_import_url = "/api/dashboards/import" -dashboard_data = json.loads(base64.b64decode(action_get('dashboard')).decode('utf-8')) -# Needs to be the format: -# { "dashboard": { dashboard }, -# "overwrite": true } - -# For trusty, use for better compatability -headers = {'Content-Type': 'application/json'} -r = requests.post(grafana + api_dash_import_url, auth=api_auth, headers=headers, - data=json.dumps(dashboard_data)) - -# For xenial+ -# r = requests.post(grafana + api_dash_import_url, auth=api_auth, json=dashboard_data) -# if r.status_code == requests.codes.ok: # For python-requests 2.18 or so -title = dashboard_data['dashboard']['title'] -if r.status_code == 200: - action_set({"loaded": title}) -else: - action_fail("Dashboard %s failed to load" % (title)) - diff --git a/actions/import-dashboard b/actions/import-dashboard new file mode 100755 index 0000000..be72e03 --- /dev/null +++ b/actions/import-dashboard @@ -0,0 +1,43 @@ +#!/usr/bin/python3 +# Imports a json based dashboard into grafana + +import requests +import json +import base64 +from charmhelpers.core.hookenv import ( + action_fail, + action_set, + action_get, + config, +) + +from charmhelpers.core import unitdata + +kv = unitdata.kv() +if kv.get('grafana.admin_password'): + # print('Admin password: {}'.format(kv.get('grafana.admin_password'))) + passwd = kv.get('grafana.admin_password') +elif config('admin_password'): + passwd = config('admin_password') + # print('Admin password: {}'.format(config('admin_password'))) +else: + action_fail('ERROR! Unable to retrieve password.') + +grafana = "http://localhost:3000" +api_auth = ('admin', passwd) +api_dash_import_url = "/api/dashboards/import" +dashboard_data = json.loads(base64.b64decode(action_get('dashboard')).decode('utf-8')) +# Needs to be the format: +# { "dashboard": { dashboard }, +# "overwrite": true } - use false here if you don't want to overwrite +# existing dashboards + +headers = {'Content-Type': 'application/json'} +r = requests.post(grafana + api_dash_import_url, auth=api_auth, headers=headers, + data=json.dumps(dashboard_data)) +title = dashboard_data['dashboard']['title'] +if r.status_code == 200: + action_set({"loaded": title}) +else: + action_fail("Dashboard %s failed to load" % (title)) + -- cgit v1.2.3