From a1287be9b59c0a3addda8117ecb902631bcf1862 Mon Sep 17 00:00:00 2001 From: Wolodja Wentland Date: Wed, 8 Jun 2016 11:24:58 +0200 Subject: [PATCH 1/2] Add get_list_privileges() to InfluxDBClient This allows library users to get a list of all privileges an InfluxDB user has been granted. --- influxdb/client.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/influxdb/client.py b/influxdb/client.py index b1c7c976..e7dedd69 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -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. From 3742a0ec68b0b4204697ceb36debd6f9dd9725fa Mon Sep 17 00:00:00 2001 From: Wolodja Wentland Date: Thu, 9 Jun 2016 10:48:28 +0200 Subject: [PATCH 2/2] Add tests for get_list_privileges() These test check for correct return value and exception handling in case of incorrect usage. --- influxdb/tests/client_test.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index f1adaa9b..997a0431 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -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')