Skip to content

Commit 36e50ea

Browse files
committed
Add grant_priviliege method to the client.
1 parent a4072b1 commit 36e50ea

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

influxdb/client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,22 @@ def revoke_admin_privileges(self, username):
642642
text = "REVOKE ALL PRIVILEGES FROM {}".format(username)
643643
self.query(text)
644644

645+
def grant_privilege(self, privilege, database, username):
646+
"""Grant a privilege on a database to an user.
647+
648+
:param privilege: the privilege to grant, one of 'read', 'write'
649+
or 'all'. The string is case-insensitive
650+
:type privilege: str
651+
:param database: the database to grant the privilege on
652+
:type database: str
653+
:param username: the username to grant the privilege to
654+
:type username: str
655+
"""
656+
text = "GRANT {} ON {} TO {}".format(privilege,
657+
database,
658+
username)
659+
self.query(text)
660+
645661
def send_packet(self, packet):
646662
"""Send an UDP packet.
647663

tests/influxdb/client_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,28 @@ def test_revoke_admin_privileges_invalid(self):
668668
with _mocked_session(cli, 'get', 400):
669669
self.cli.revoke_admin_privileges('')
670670

671+
def test_grant_privilege(self):
672+
example_response = '{"results":[{}]}'
673+
674+
with requests_mock.Mocker() as m:
675+
m.register_uri(
676+
requests_mock.GET,
677+
"http://localhost:8086/query",
678+
text=example_response
679+
)
680+
self.cli.grant_privilege('read', 'testdb', 'test')
681+
682+
self.assertEqual(
683+
m.last_request.qs['q'][0],
684+
'grant read on testdb to test'
685+
)
686+
687+
@raises(Exception)
688+
def test_grant_privilege_invalid(self):
689+
cli = InfluxDBClient('host', 8086, 'username', 'password')
690+
with _mocked_session(cli, 'get', 400):
691+
self.cli.grant_privilege('', 'testdb', 'test')
692+
671693

672694
class FakeClient(InfluxDBClient):
673695
fail = False

tests/influxdb/client_test_with_server.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,21 @@ def test_revoke_admin_privileges_invalid(self):
407407
self.assertIn('{"error":"error parsing query: ',
408408
ctx.exception.content)
409409

410+
def test_grant_privilege(self):
411+
self.cli.create_user('test', 'test')
412+
self.cli.create_database('testdb')
413+
self.cli.grant_privilege('all', 'testdb', 'test')
414+
# TODO: when supported by InfluxDB, check if privileges are granted
415+
416+
def test_grant_privilege_invalid(self):
417+
self.cli.create_user('test', 'test')
418+
self.cli.create_database('testdb')
419+
with self.assertRaises(InfluxDBClientError) as ctx:
420+
self.cli.grant_privilege('', 'testdb', 'test')
421+
self.assertEqual(400, ctx.exception.code)
422+
self.assertIn('{"error":"error parsing query: ',
423+
ctx.exception.content)
424+
410425

411426
############################################################################
412427

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy