E573 Add revoke_admin_privileges to the client. · kipe/influxdb-python@a4072b1 · GitHub
[go: up one dir, main page]

Skip to content

Commit a4072b1

Browse files
committed
Add revoke_admin_privileges to the client.
1 parent 3520714 commit a4072b1

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

influxdb/client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,18 @@ def grant_admin_privileges(self, username):
630630
text = "GRANT ALL PRIVILEGES TO {}".format(username)
631631
self.query(text)
632632

633+
def revoke_admin_privileges(self, username):
634+
"""Revoke cluster administration privileges from an user.
635+
636+
:param username: the username to revoke privileges from
637+
:type username: str
638+
639+
.. note:: Only a cluster administrator can create/ drop databases
640+
and manage users.
641+
"""
642+
text = "REVOKE ALL PRIVILEGES FROM {}".format(username)
643+
self.query(text)
644+
633645
def send_packet(self, packet):
634646
"""Send an UDP packet.
635647

tests/influxdb/client_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,28 @@ def test_grant_admin_privileges_invalid(self):
646646
with _mocked_session(cli, 'get', 400):
647647
self.cli.grant_admin_privileges('')
648648

649+
def test_revoke_admin_privileges(self):
650+
example_response = '{"results":[{}]}'
651+
652+
with requests_mock.Mocker() as m:
653+
m.register_uri(
654+
requests_mock.GET,
655+
"http://localhost:8086/query",
656+
text=example_response
657+
)
658+
self.cli.revoke_admin_privileges('test')
659+
660+
self.assertEqual(
661+
m.last_request.qs['q'][0],
662+
'revoke all privileges from test'
663+
)
664+
665+
@raises(Exception)
666+
def test_revoke_admin_privileges_invalid(self):
667+
cli = InfluxDBClient('host', 8086, 'username', 'password')
668+
with _mocked_session(cli, 'get', 400):
669+
self.cli.revoke_admin_privileges('')
670+
649671

650672
class FakeClient(InfluxDBClient):
651673
fail = False

tests/influxdb/client_test_with_server.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,22 @@ def test_grant_admin_privileges_invalid(self):
391391
self.assertIn('{"error":"error parsing query: ',
392392
ctx.exception.content)
393393

394+
def test_revoke_admin_privileges(self):
395+
self.cli.create_user('test', 'test')
396+
self.cli.grant_admin_privileges('test')
397+
self.assertEqual([{'user': 'test', 'admin': True}],
398+
self.cli.get_list_users())
399+
self.cli.revoke_admin_privileges('test')
400+
self.assertEqual([{'user': 'test', 'admin': False}],
401+
self.cli.get_list_users())
402+
403+
def test_revoke_admin_privileges_invalid(self):
404+
with self.assertRaises(InfluxDBClientError) as ctx:
405+
self.cli.revoke_admin_privileges('')
406+
self.assertEqual(400, ctx.exception.code)
407+
self.assertIn('{"error":"error parsing query: ',
408+
ctx.exception.content)
409+
394410

395411
############################################################################
396412

0 commit comments

Comments
 (0)
0