From f7231276b66c989d99e236da9bf4367c62c03c55 Mon Sep 17 00:00:00 2001 From: Wolodja Wentland Date: Thu, 9 Jun 2016 09:04:00 +0200 Subject: [PATCH 1/2] Add grant_admin_privileges() to InfluxDBClient This allows users to easily alter the 'admin' attribute of a user account and complements the already existing 'revove_admin_privileges' method. --- influxdb/client.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/influxdb/client.py b/influxdb/client.py index b1c7c976..4192614a 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -684,6 +684,18 @@ def delete_series(self, database=None, measurement=None, tags=None): for k, v in tags.items()]) self.query(query_str, database=database) + def grant_admin_privileges(self, username): + """Grant cluster administration privileges to a user. + + :param username: the username to grant privileges to + :type username: str + + .. note:: Only a cluster administrator can create/drop databases + and manage users. + """ + text = "GRANT ALL PRIVILEGES TO {0}".format(username) + self.query(text) + def revoke_admin_privileges(self, username): """Revoke cluster administration privileges from a user. From df4ee82642b8bdc043b21682f12e992c34e40fed Mon Sep 17 00:00:00 2001 From: Wolodja Wentland Date: Thu, 9 Jun 2016 10:33:57 +0200 Subject: [PATCH 2/2] Add tests for grant_admin_privileges() These tests simply check for correct query generation and exception handling if used incorrectly by passing in an empty string as username. --- influxdb/tests/client_test.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/influxdb/tests/client_test.py b/influxdb/tests/client_test.py index f1adaa9b..dabf8a8a 100644 --- a/influxdb/tests/client_test.py +++ b/influxdb/tests/client_test.py @@ -710,6 +710,28 @@ def test_get_list_users_empty(self): self.assertListEqual(self.cli.get_list_users(), []) + def test_grant_admin_privileges(self): + example_response = '{"results":[{}]}' + + with requests_mock.Mocker() as m: + m.register_uri( + requests_mock.GET, + "http://localhost:8086/query", + text=example_response + ) + self.cli.grant_admin_privileges('test') + + self.assertEqual( + m.last_request.qs['q'][0], + 'grant all privileges to test' + ) + + @raises(Exception) + def test_grant_admin_privileges_invalid(self): + cli = InfluxDBClient('host', 8086, 'username', 'password') + with _mocked_session(cli, 'get', 400): + self.cli.grant_admin_privileges('') + def test_revoke_admin_privileges(self): example_response = '{"results":[{}]}'