summaryrefslogtreecommitdiff
diff options
authorBrad Marshall <brad.marshall@canonical.com>2017-07-13 16:21:48 +1000
committerBrad Marshall <brad.marshall@canonical.com>2017-07-13 16:21:48 +1000
commitdd582f7cf63a0b94aaf3fd00d66d73bae7c5f8ae (patch)
tree605407b71d939703aa5765a8dae3d5744001bb20
parent92a68424cb2af5e38bb3add8faa6b2df37cd2b18 (diff)
Add create-api-key and get-admin-password actions
-rw-r--r--actions.yaml13
-rwxr-xr-xactions/create-api-key58
-rwxr-xr-xactions/get-admin-password25
3 files changed, 96 insertions, 0 deletions
diff --git a/actions.yaml b/actions.yaml
index 4d5f671..de14f9b 100644
--- a/actions.yaml
+++ b/actions.yaml
@@ -6,3 +6,16 @@ import-dashboard:
description: Contains the dashboard to be imported, in base64 encoded json format
required: [dashboard]
additionalProperties: false
+get-admin-password:
+ description: Retrieves the admin password, either auto generated or set in config
+create-api-key:
+ description: Create an api key given a keyname and a keyrole out of Viewer, Editor, Read Only Editor or Admin
+ params:
+ keyname:
+ type: string
+ description: Name of key to be created
+ keyrole:
+ type: string
+ description: Role for key, one of Viewer, Editor, Read Only Editor or Admin
+ required: [keyname, keyrole]
+ additionalProperties: false
diff --git a/actions/create-api-key b/actions/create-api-key
new file mode 100755
index 0000000..bb710ee
--- /dev/null
+++ b/actions/create-api-key
@@ -0,0 +1,58 @@
+#!/usr/bin/python3
+
+import requests
+import json
+from charmhelpers.core.hookenv import (
+ action_fail,
+ action_set,
+ action_get,
+ config,
+ log,
+)
+
+from charmhelpers.core import unitdata
+
+roles = ['Viewer', 'Editor', 'Read Only Editor', 'Admin']
+
+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.')
+ exit(0)
+
+grafana = "http://localhost:3000"
+api_auth = ('admin', passwd)
+key_name = action_get('keyname')
+key_role = action_get('keyrole')
+api_key_url = "/api/auth/keys"
+api_data = {"role": key_role, "name": key_name}
+keyfound = False
+
+if key_role not in roles:
+ action_fail("Role %s isn't valid, needs to be one of %s" % (key_role, roles))
+ exit(0)
+
+r = requests.get(grafana + api_key_url, auth=api_auth)
+keylist = json.loads(r.text)
+for k in keylist:
+ if k['name'] == key_name:
+ keyfound = True
+ action_fail("%s key found, not creating" % (key_name))
+ exit(0)
+
+if not keyfound:
+ r = requests.post(grafana + api_key_url, auth=api_auth, json=api_data)
+ api_key = json.loads(r.text)
+
+ # print("Key %s created, key is %s" % (api_key['name'], api_key['key']))
+ action_set({"key-name": api_key['name']})
+ action_set({"key-value": api_key['key']})
+ action_set({"key-role": key_role})
+
+ # Use this key in headers like Authorization: Bearer $KEY
+
diff --git a/actions/get-admin-password b/actions/get-admin-password
new file mode 100755
index 0000000..85975de
--- /dev/null
+++ b/actions/get-admin-password
@@ -0,0 +1,25 @@
+#!/usr/bin/python3
+# Get admin password
+
+from charmhelpers.core.hookenv import (
+ action_fail,
+ action_set,
+ action_get,
+ config,
+ log,
+)
+
+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.')
+ exit(0)
+
+action_set({"password": passwd})