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

Proposed by Jamon Camisso on 2017-10-23
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 2017-10-23 Approve on 2017-10-23
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.
David Lawson (deej) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/actions.yaml b/actions.yaml
2new file mode 100644
3index 0000000..9540fd5
4--- /dev/null
5+++ b/actions.yaml
6@@ -0,0 +1,8 @@
7+create-database:
8+ description: Creates a database. Use format 'database=dbname'
9+create-user:
10+ description: Create a user. Use format 'user=username'
11+grant-privilege:
12+ description: |
13+ Grant privileges for a user to a database. Format is a string
14+ following privilege='privilege,database,username' format.
15diff --git a/actions/create-database b/actions/create-database
16new file mode 100755
17index 0000000..555ff4d
18--- /dev/null
19+++ b/actions/create-database
20@@ -0,0 +1,41 @@
21+#!/usr/bin/python3
22+# Create a database
23+
24+import influxdb
25+
26+import requests
27+import traceback
28+from charmhelpers.core.hookenv import (
29+ action_fail,
30+ action_set,
31+ action_get,
32+ log,
33+)
34+
35+action = "create-database"
36+
37+try:
38+ database = action_get('database')
39+except Exception:
40+ action_fail('Unhandled exception')
41+ tb = traceback.format_exc()
42+ action_set(dict(traceback=tb))
43+ log('Unhandled exception in action {}'.format(action))
44+ print(tb)
45+
46+
47+try:
48+ client = influxdb.InfluxDBClient('127.0.0.1', 8086)
49+except requests.exceptions.ConnectionError:
50+ action_fail("Failed to connect to InfluxDB!")
51+ exit(0)
52+try:
53+ print("Create database: " + database)
54+ client.create_database(database)
55+ action_set({"created": database})
56+except:
57+ action_fail('Unhandled exception')
58+ tb = traceback.format_exc()
59+ action_set(dict(traceback=tb))
60+ log('Unhandled exception in action {}'.format(action))
61+ print(tb)
62diff --git a/actions/create-user b/actions/create-user
63new file mode 100755
64index 0000000..2e65191
65--- /dev/null
66+++ b/actions/create-user
67@@ -0,0 +1,57 @@
68+#!/usr/bin/python3
69+# Create a database
70+
71+import influxdb
72+import random
73+import string
74+import traceback
75+
76+from charmhelpers.core.hookenv import (
77+ action_fail,
78+ action_set,
79+ action_get,
80+ log,
81+)
82+
83+action = "create-user"
84+charset = string.ascii_lowercase + string.ascii_uppercase + string.digits
85+password = ''.join(random.sample(charset, 16))
86+
87+
88+def unhandled_exception():
89+ action_fail('Unhandled exception')
90+ tb = traceback.format_exc()
91+ action_set(dict(traceback=tb))
92+ log('Unhandled exception in action {}'.format(action))
93+ print(tb)
94+
95+
96+try:
97+ user = action_get('user')
98+except Exception:
99+ action_fail('Unhandled exception')
100+ tb = traceback.format_exc()
101+ action_set(dict(traceback=tb))
102+ log('Unhandled exception in action {}'.format(action))
103+ print(tb)
104+
105+if user is None:
106+ action_fail("Couldn't get 'user' value.")
107+ exit(0)
108+
109+try:
110+ client = influxdb.InfluxDBClient('127.0.0.1', 8086)
111+except requests.exceptions.ConnectionError:
112+ action_fail("Failed to connect to InfluxDB!")
113+ exit(0)
114+try:
115+ client.create_user(user, password)
116+ action_set({"created": user, "password": password})
117+ print("Created user: " + user)
118+except influxdb.exceptions.InfluxDBClientError as e:
119+ if e.content == "user already exists":
120+ action_fail("User {} already exists".format(user))
121+ else:
122+ unhandled_exception()
123+except:
124+ unhandled_exception()
125diff --git a/actions/grant-privilege b/actions/grant-privilege
126new file mode 100755
127index 0000000..62e0759
128--- /dev/null
129+++ b/actions/grant-privilege
130@@ -0,0 +1,48 @@
131+#!/usr/bin/python3
132+# Create a database
133+
134+import influxdb
135+import traceback
136+
137+from charmhelpers.core.hookenv import (
138+ action_fail,
139+ action_set,
140+ action_get,
141+ log,
142+)
143+
144+action = "grant-privilege"
145+
146+
147+def unhandled_exception():
148+ action_fail('Unhandled exception')
149+ tb = traceback.format_exc()
150+ action_set(dict(traceback=tb))
151+ log('Unhandled exception in action {}'.format(action))
152+ print(tb)
153+
154+
155+try:
156+ privilege, database, user = action_get('privilege').split(',')
157+except Exception:
158+ action_fail('Unhandled exception')
159+ tb = traceback.format_exc()
160+ action_set(dict(traceback=tb))
161+ log('Unhandled exception in action {}'.format(action))
162+ print(tb)
163+
164+if privilege is None:
165+ action_fail("Couldn't get 'privilege' value.")
166+ exit(0)
167+
168+try:
169+ client = influxdb.InfluxDBClient('127.0.0.1', 8086)
170+except requests.exceptions.ConnectionError:
171+ action_fail("Failed to connect to InfluxDB!")
172+ exit(0)
173+try:
174+ client.grant_privilege(privilege, database, user)
175+ action_set({"granted": user, "privilege": privilege, "database": database})
176+ print("Granted {} {} on {}".format(user, privilege, database))
177+except:
178+ unhandled_exception()
179diff --git a/layer.yaml b/layer.yaml
180index 37d19f1..e4abc0e 100644
181--- a/layer.yaml
182+++ b/layer.yaml
183@@ -7,5 +7,10 @@ includes:
184 - interface:influxdb-api
185 options:
186 basic:
187- use_venv: true
188- include_system_packages: true
189+ packages:
190+ # we need to specify these here due to https://github.com/juju-solutions/charms.reactive/issues/82
191+ - build-essential
192+ - python3-setuptools
193+ - python3-wheel
194+ - python3-yaml
195+ - python3-influxdb

Subscribers

People subscribed via source and target branches

to all changes: