diff options
| author | Brad Marshall <brad.marshall@canonical.com> | 2017-09-11 12:07:56 +1000 |
|---|---|---|
| committer | Brad Marshall <brad.marshall@canonical.com> | 2017-09-11 12:07:56 +1000 |
| commit | 6199d92275d6ee54b975a531d134f37db8dd93be (patch) | |
| tree | 5690f668dc16219dcd1446a1ffd25d02d83159da | |
| parent | bcbb603b40ebe9267d83341284059db3b200bf85 (diff) | |
First draft of adding organisational role, some debugging
| -rw-r--r-- | actions.yaml | 20 | ||||
| -rwxr-xr-x | actions/create-user | 95 |
2 files changed, 115 insertions, 0 deletions
diff --git a/actions.yaml b/actions.yaml index de14f9b..617b8da 100644 --- a/actions.yaml +++ b/actions.yaml @@ -19,3 +19,23 @@ create-api-key: description: Role for key, one of Viewer, Editor, Read Only Editor or Admin required: [keyname, keyrole] additionalProperties: false +create-user: + description: Create an user + params: + name: + type: string + description: Name of user to be created + email: + type: string + description: Email of user to be created + login: + type: string + description: Login of user to be created + password: + type: string + description: Password of user to be created + role: + type: string + description: role of user to be created, one of Viewer, Editor, Read Only Editor or Admin + required: [name, email, login, password, role] + additionalProperties: false diff --git a/actions/create-user b/actions/create-user new file mode 100755 index 0000000..48ab86c --- /dev/null +++ b/actions/create-user @@ -0,0 +1,95 @@ +#!/usr/bin/python3 +# Creates a user in grafana + +import requests +import json +import sys +from charmhelpers.core.hookenv import ( + action_fail, + action_set, + action_get, + log, +) + +from grafana_utils import get_admin_password + +action = "create-user" + +admin_passwd = get_admin_password() + +if admin_passwd is None: + action_fail('Unable to retrieve password.') + sys.exit(0) + +grafana = "http://localhost:3000" +api_auth = ('admin', admin_passwd) + +# http://docs.grafana.org/http_api/admin/#global-users +api_create_user_url = "/api/admin/users" +# Needs the following json posted to it: +# { +# "name":"User", +# "email":"user@graf.com", +# "login":"user", +# "password":"userpassword" +# } + +name = action_get('name') +email = action_get('email') +login = action_get('login') +user_password = action_get('password') +user_role = action_get('role') +user_data = '{ \ + "name": "%s", \ + "email": "%s", \ + "login": "%s", \ + "password": "%s" \ +}' % (name, email, login, user_password) + +headers = {'Content-Type': 'application/json'} +r_create = requests.post(grafana + api_create_user_url, auth=api_auth, headers=headers, + data=json.dumps(user_data)) + +log("user data is %s" % (json.dumps(user_data))) + +if r_create.status_code == 200: + action_set({"created": login}) +else: + action_fail("User %s failed to create" % (login)) + log("User %s failed to create" % (login)) + log("HTTP error is %s" % (r_create.text)) + sys.exit(1) + +# http://docs.grafana.org/http_api/org/#get-organisation-by-id +api_org_url = "/api/orgs/1" + +# http://docs.grafana.org/http_api/org/#add-a-new-user-to-the-actual-organisation +api_org_user_url = "/api/org/users" +# { +# "role": "Admin", +# "loginOrEmail": "admin" +# } + +r_org = requests.get(grafana + api_org_url, auth=api_auth) +if r_org.status_code == 200: + org_data = r_org.json + org_id = org_data['id'] + org_name = org_data['name'] + org_user_data = '{ \ +"role": %s, \ +"loginOrEmail": %s \ +}' % (user_role, login) + r_userorg = requests.post(grafana + api_org_user_url, auth=api_auth, headers=headers, + data=json.dumps(org_user_data)) + if r_userorg.status_code == 200: + action_set({"organisation_name": org_name}) + action_set({"organisation_role": user_role}) + log("Found organisation %s, gave user %s role %s" % (org_name, login, user_role)) + else: + action_fail("Failed to add user to organisation") + log("Failed to add user to organisation") + sys.exit(1) +else: + action_fail("Failed to retrieve organisation details") + log("Failed to retrieve organisation details") + sys.exit(1) |
