Skip to content

Commit 045e76f

Browse files
authored
Merge pull request influxdata#330 from babilen/show-privileges
Add get_list_privileges() to InfluxDBClient
2 parents 62e9bb8 + 3742a0e commit 045e76f

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

influxdb/client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,28 @@ def revoke_privilege(self, privilege, database, username):
740740
username)
741741
self.query(text)
742742

743+
def get_list_privileges(self, username):
744+
"""Get the list of all privileges granted to given user.
745+
746+
:param username: the username to get privileges of
747+
:type username: str
748+
749+
:returns: all privileges granted to given user
750+
:rtype: list of dictionaries
751+
752+
:Example:
753+
754+
::
755+
756+
>> privileges = client.get_list_privileges('user1')
757+
>> privileges
758+
[{u'privilege': u'WRITE', u'database': u'db1'},
759+
{u'privilege': u'ALL PRIVILEGES', u'database': u'db2'},
760+
{u'privilege': u'NO PRIVILEGES', u'database': u'db3'}]
761+
"""
762+
text = "SHOW GRANTS FOR {0}".format(username)
763+
return list(self.query(text).get_points())
764+
743765
def send_packet(self, packet):
744766
"""Send an UDP packet.
745767

influxdb/tests/client_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,31 @@ def test_revoke_privilege_invalid(self):
798798
with _mocked_session(cli, 'get', 400):
799799
self.cli.revoke_privilege('', 'testdb', 'test')
800800

801+
def test_get_list_privileges(self):
802+
data = {'results': [
803+
{'series': [
804+
{'columns': ['database', 'privilege'],
805+
'values': [
806+
['db1', 'READ'],
807+
['db2', 'ALL PRIVILEGES'],
808+
['db3', 'NO PRIVILEGES']]}
809+
]}
810+
]}
811+
812+
with _mocked_session(self.cli, 'get', 200, json.dumps(data)):
813+
self.assertListEqual(
814+
self.cli.get_list_privileges('test'),
815+
[{'database': 'db1', 'privilege': 'READ'},
816+
{'database': 'db2', 'privilege': 'ALL PRIVILEGES'},
817+
{'database': 'db3', 'privilege': 'NO PRIVILEGES'}]
818+
)
819+
820+
@raises(Exception)
821+
def test_get_list_privileges_fails(self):
822+
cli = InfluxDBClient('host', 8086, 'username', 'password')
823+
with _mocked_session(cli, 'get', 401):
824+
cli.get_list_privileges('test')
825+
801826
def test_invalid_port_fails(self):
802827
with self.assertRaises(ValueError):
803828
InfluxDBClient('host', '80/redir', 'username', 'password')

0 commit comments

Comments
 (0)