Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Add get_list_privileges() to InfluxDBClient #330

Merged
merged 2 commits into from
Jun 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions influxdb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,28 @@ def revoke_privilege(self, privilege, database, username):
username)
self.query(text)

def get_list_privileges(self, username):
"""Get the list of all privileges granted to given user.

:param username: the username to get privileges of
:type username: str

:returns: all privileges granted to given user
:rtype: list of dictionaries

:Example:

::

>> privileges = client.get_list_privileges('user1')
>> privileges
[{u'privilege': u'WRITE', u'database': u'db1'},
{u'privilege': u'ALL PRIVILEGES', u'database': u'db2'},
{u'privilege': u'NO PRIVILEGES', u'database': u'db3'}]
"""
text = "SHOW GRANTS FOR {0}".format(username)
return list(self.query(text).get_points())

def send_packet(self, packet):
"""Send an UDP packet.

Expand Down
25 changes: 25 additions & 0 deletions influxdb/tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,31 @@ def test_revoke_privilege_invalid(self):
with _mocked_session(cli, 'get', 400):
self.cli.revoke_privilege('', 'testdb', 'test')

def test_get_list_privileges(self):
data = {'results': [
{'series': [
{'columns': ['database', 'privilege'],
'values': [
['db1', 'READ'],
['db2', 'ALL PRIVILEGES'],
['db3', 'NO PRIVILEGES']]}
]}
]}

with _mocked_session(self.cli, 'get', 200, json.dumps(data)):
self.assertListEqual(
self.cli.get_list_privileges('test'),
[{'database': 'db1', 'privilege': 'READ'},
{'database': 'db2', 'privilege': 'ALL PRIVILEGES'},
{'database': 'db3', 'privilege': 'NO PRIVILEGES'}]
)

@raises(Exception)
def test_get_list_privileges_fails(self):
cli = InfluxDBClient('host', 8086, 'username', 'password')
with _mocked_session(cli, 'get', 401):
cli.get_list_privileges('test')

def test_invalid_port_fails(self):
with self.assertRaises(ValueError):
InfluxDBClient('host', '80/redir', 'username', 'password')
Expand Down