8000 Add grant_admin_privileges method to the client. · kipe/influxdb-python@3520714 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3520714

Browse files
committed
Add grant_admin_privileges method to the client.
1 parent e683a1c commit 3520714

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

influxdb/client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,18 @@ def delete_series(self, name, database=None):
618618
database = database or self._database
619619
self.query('DROP SERIES \"%s\"' % name, database=database)
620620

621+
def grant_admin_privileges(self, username):
622+
"""Grant cluster administration privileges to an user.
623+
624+
:param username: the username to grant privileges to
625+
:type username: str
626+
627+
.. note:: Only a cluster administrator can create/ drop databases
628+
and manage users.
629+
"""
630+
text = "GRANT ALL PRIVILEGES TO {}".format(username)
631+
self.query(text)
632+
621633
def send_packet(self, packet):
622634
"""Send an UDP packet.
623635

tests/influxdb/client_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,28 @@ def test_get_list_users_empty(self):
624624

625625
self.assertListEqual(self.cli.get_list_users(), [])
626626

627+
def test_grant_admin_privileges(self):
628+
example_response = '{"results":[{}]}'
629+
630+
with requests_mock.Mocker() as m:
631+
m.register_uri(
632+
requests_mock.GET,
633+
"http://localhost:8086/query",
634+
text=example_response
635+
)
636+
self.cli.grant_admin_privileges('test')
637+
638+
self.assertEqual(
639+
m.last_request.qs['q'][0],
640+
'grant all privileges to test'
641+
)
642+
643+
@raises(Exception)
644+
def test_grant_admin_privileges_invalid(self):
645+
cli = InfluxDBClient('host', 8086, 'username', 'password')
646+
with _mocked_session(cli, 'get', 400):
647+
self.cli.grant_admin_privileges('')
648+
627649

628650
class FakeClient(InfluxDBClient):
629651
fail = False

tests/influxdb/client_test_with_server.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,21 @@ def test_drop_user_invalid(self):
376376
'found invalid, expected',
377377
ctx.exception.content)
378378

379+
def test_grant_admin_privileges(self):
380+
self.cli.create_user('test', 'test')
381+
self.assertEqual([{'user': 'test', 'admin': False}],
382+
self.cli.get_list_users())
383+
self.cli.grant_admin_privileges('test')
384+
self.assertEqual([{'user': 'test', 'admin': True}],
385+
self.cli.get_list_users())
386+
387+
def test_grant_admin_privileges_invalid(self):
388+
with self.assertRaises(InfluxDBClientError) as ctx:
389+
self.cli.grant_admin_privileges('')
390+
self.assertEqual(400, ctx.exception.code)
391+
self.assertIn('{"error":"error parsing query: ',
392+
ctx.exception.content)
393+
379394

380395
############################################################################
381396

0 commit comments

Comments
 (0)
0