Merge ~jamon/influxdb-charm:master into influxdb-charm:master

Proposed by Jamon Camisso
Status: Merged
Merged at revision: 068e6dff6f3526d4e4f62f70bd865ffae835581a
Proposed branch: ~jamon/influxdb-charm:master
Merge into: influxdb-charm:master
Diff against target: 195 lines (+161/-2)
5 files modified
actions.yaml (+8/-0)
actions/create-database (+41/-0)
actions/create-user (+57/-0)
actions/grant-privilege (+48/-0)
layer.yaml (+7/-2)
Reviewer Review Type Date Requested Status
David Lawson (community) Approve
Review via email: mp+332631@code.launchpad.net

Commit message

Add actions to create user, database, and privileges

Description of the change

Add actions to create user, database, and privileges

To post a comment you must log in.
Revision history for this message
David Lawson (deej) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/actions.yaml b/actions.yaml
0new file mode 1006440new file mode 100644
index 0000000..9540fd5
--- /dev/null
+++ b/actions.yaml
@@ -0,0 +1,8 @@
1create-database:
2 description: Creates a database. Use format 'database=dbname'
3create-user:
4 description: Create a user. Use format 'user=username'
5grant-privilege:
6 description: |
7 Grant privileges for a user to a database. Format is a string
8 following privilege='privilege,database,username' format.
diff --git a/actions/create-database b/actions/create-database
0new file mode 1007559new file mode 100755
index 0000000..555ff4d
--- /dev/null
+++ b/actions/create-database
@@ -0,0 +1,41 @@
1#!/usr/bin/python3
2# Create a database
3
4import influxdb
5
6import requests
7import traceback
8from charmhelpers.core.hookenv import (
9 action_fail,
10 action_set,
11 action_get,
12 log,
13)
14
15action = "create-database"
16
17try:
18 database = action_get('database')
19except Exception:
20 action_fail('Unhandled exception')
21 tb = traceback.format_exc()
22 action_set(dict(traceback=tb))
23 log('Unhandled exception in action {}'.format(action))
24 print(tb)
25
26
27try:
28 client = influxdb.InfluxDBClient('127.0.0.1', 8086)
29except requests.exceptions.ConnectionError:
30 action_fail("Failed to connect to InfluxDB!")
31 exit(0)
32try:
33 print("Create database: " + database)
34 client.create_database(database)
35 action_set({"created": database})
36except:
37 action_fail('Unhandled exception')
38 tb = traceback.format_exc()
39 action_set(dict(traceback=tb))
40 log('Unhandled exception in action {}'.format(action))
41 print(tb)
diff --git a/actions/create-user b/actions/create-user
0new file mode 10075542new file mode 100755
index 0000000..2e65191
--- /dev/null
+++ b/actions/create-user
@@ -0,0 +1,57 @@
1#!/usr/bin/python3
2# Create a database
3
4import influxdb
5import random
6import string
7import traceback
8
9from charmhelpers.core.hookenv import (
10 action_fail,
11 action_set,
12 action_get,
13 log,
14)
15
16action = "create-user"
17charset = string.ascii_lowercase + string.ascii_uppercase + string.digits
18password = ''.join(random.sample(charset, 16))
19
20
21def unhandled_exception():
22 action_fail('Unhandled exception')
23 tb = traceback.format_exc()
24 action_set(dict(traceback=tb))
25 log('Unhandled exception in action {}'.format(action))
26 print(tb)
27
28
29try:
30 user = action_get('user')
31except Exception:
32 action_fail('Unhandled exception')
33 tb = traceback.format_exc()
34 action_set(dict(traceback=tb))
35 log('Unhandled exception in action {}'.format(action))
36 print(tb)
37
38if user is None:
39 action_fail("Couldn't get 'user' value.")
40 exit(0)
41
42try:
43 client = influxdb.InfluxDBClient('127.0.0.1', 8086)
44except requests.exceptions.ConnectionError:
45 action_fail("Failed to connect to InfluxDB!")
46 exit(0)
47try:
48 client.create_user(user, password)
49 action_set({"created": user, "password": password})
50 print("Created user: " + user)
51except influxdb.exceptions.InfluxDBClientError as e:
52 if e.content == "user already exists":
53 action_fail("User {} already exists".format(user))
54 else:
55 unhandled_exception()
56except:
57 unhandled_exception()
diff --git a/actions/grant-privilege b/actions/grant-privilege
0new file mode 10075558new file mode 100755
index 0000000..62e0759
--- /dev/null
+++ b/actions/grant-privilege
@@ -0,0 +1,48 @@
1#!/usr/bin/python3
2# Create a database
3
4import influxdb
5import traceback
6
7from charmhelpers.core.hookenv import (
8 action_fail,
9 action_set,
10 action_get,
11 log,
12)
13
14action = "grant-privilege"
15
16
17def unhandled_exception():
18 action_fail('Unhandled exception')
19 tb = traceback.format_exc()
20 action_set(dict(traceback=tb))
21 log('Unhandled exception in action {}'.format(action))
22 print(tb)
23
24
25try:
26 privilege, database, user = action_get('privilege').split(',')
27except Exception:
28 action_fail('Unhandled exception')
29 tb = traceback.format_exc()
30 action_set(dict(traceback=tb))
31 log('Unhandled exception in action {}'.format(action))
32 print(tb)
33
34if privilege is None:
35 action_fail("Couldn't get 'privilege' value.")
36 exit(0)
37
38try:
39 client = influxdb.InfluxDBClient('127.0.0.1', 8086)
40except requests.exceptions.ConnectionError:
41 action_fail("Failed to connect to InfluxDB!")
42 exit(0)
43try:
44 client.grant_privilege(privilege, database, user)
45 action_set({"granted": user, "privilege": privilege, "database": database})
46 print("Granted {} {} on {}".format(user, privilege, database))
47except:
48 unhandled_exception()
diff --git a/layer.yaml b/layer.yaml
index 37d19f1..e4abc0e 100644
--- a/layer.yaml
+++ b/layer.yaml
@@ -7,5 +7,10 @@ includes:
7 - interface:influxdb-api7 - interface:influxdb-api
8options:8options:
9 basic:9 basic:
10 use_venv: true10 packages:
11 include_system_packages: true11 # we need to specify these here due to https://github.com/juju-solutions/charms.reactive/issues/82
12 - build-essential
13 - python3-setuptools
14 - python3-wheel
15 - python3-yaml
16 - python3-influxdb

Subscribers

People subscribed via source and target branches

to all changes: