Skip to content

Commit 966d192

Browse files
Gilles Hamelamotl
authored andcommitted
Add support for a subset of "RBAC" API
1 parent ef2733f commit 966d192

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ versions of Grafana might not support certain features or subsystems.
180180
| Organisation | + |
181181
| Other | + |
182182
| Preferences | + |
183+
| Rbac | +- |
183184
| Snapshot | + |
184185
| Teams | + |
185186
| User | + |

grafana_client/api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
Notifications,
2424
Organization,
2525
Organizations,
26+
Rbac,
2627
Search,
2728
Snapshots,
2829
Teams,
@@ -70,6 +71,7 @@ def __init__(
7071
self.search = Search(self.client)
7172
self.user = User(self.client)
7273
self.users = Users(self.client)
74+
self.rbac = Rbac(self.client)
7375
self.teams = Teams(self.client)
7476
self.annotations = Annotations(self.client)
7577
self.snapshots = Snapshots(self.client)

grafana_client/elements/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .health import Health
1111
from .notifications import Notifications
1212
from .organization import Organization, Organizations
13+
from .rbac import Rbac
1314
from .search import Search
1415
from .snapshots import Snapshots
1516
from .team import Teams

grafana_client/elements/rbac.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from .base import Base
2+
3+
4+
class Rbac(Base):
5+
def __init__(self, client):
6+
super(Rbac, self).__init__(client)
7+
self.client = client
8+
9+
def get_rbac_roles_all(self):
10+
"""
11+
The Rbac is only available in Grafana Enterprise.
12+
13+
:return:
14+
"""
15+
roles_path = "/access-control/roles"
16+
r = self.client.GET(roles_path)
17+
return r
18+
19+
def add_rbac_role_team(self, team_id, role_uid):
20+
"""
21+
The Rbac is only available in Grafana Enterprise.
22+
23+
:param team_id:
24+
:param role_uid:
25+
:return:
26+
"""
27+
role_team_path = "/access-control/teams/%s/roles" % team_id
28+
r = self.client.POST(role_team_path, json={"roleUid": role_uid})
29+
return r
30+
31+
def add_rbac_roles_team(self, team_id, role_uids):
32+
"""
33+
The Rbac is only available in Grafana Enterprise.
34+
35+
:param team_id:
36+
:param role_uids:
37+
:return:
38+
"""
39+
role_team_path = "/access-control/teams/%s/roles" % team_id
40+
r = self.client.PUT(role_team_path, json={"roleUids": role_uids})
41+
return r
42+
43+
def remove_rbac_role_team(self, team_id, role_uid):
44+
"""
45+
The Rbac is only available in Grafana Enterprise.
46+
47+
:param team_id:
48+
:param role_uid:
49+
:return:
50+
"""
51+
role_team_path = "/access-control/teams/%s/roles/%s" % (team_id, role_uid)
52+
r = self.client.DELETE(role_team_path)
53+
return r

test/elements/test_rbac.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import unittest
2+
3+
import requests_mock
4+
5+
from grafana_client import GrafanaApi
6+
7+
8+
class RbacTestCase(unittest.TestCase):
9+
def setUp(self):
10+
self.grafana = GrafanaApi(("admin", "admin"), host="localhost", url_path_prefix="", protocol="http")
11+
12+
@requests_mock.Mocker()
13+
def test_get_rbac_roles_all(self, m):
14+
m.get(
15+
"http://localhost/api/access-control/roles",
16+
json=[
17+
{
18+
"version": 5,
19+
"uid": "vi9mlLjGz",
20+
"name": "fixed:datasources.permissions:writer",
21+
"description": "Create, read or delete data source permissions.",
22+
"global": True,
23+
"updated": "2021-05-13T22:41:49+02:00",
24+
"created": "2021-05-13T16:24:26+02:00",
25+
}
26+
],
27+
)
28+
roles = self.grafana.rbac.get_rbac_roles_all()
29+
self.assertEqual(roles[0]["name"], "fixed:datasources.permissions:writer")
30+
self.assertEqual(len(roles), 1)
31+
32+
@requests_mock.Mocker()
33+
def test_add_rbac_role_teams(self, m):
34+
m.post(
35+
"http://localhost/api/access-control/teams/1/roles",
36+
json={"message": "Role added to the team."},
37+
)
38+
history = m.request_history
39+
40+
r = self.grafana.rbac.add_rbac_role_team("1", "XvHQJq57z")
41+
self.assertEqual(history[0].json()["roleUid"], "XvHQJq57z")
42+
self.assertEqual(r["message"], "Role added to the team.")
43+
44+
@requests_mock.Mocker()
45+
def test_add_rbac_roles_teams(self, m):
46+
m.put(
47+
"http://localhost/api/access-control/teams/1/roles",
48+
json={"message": "Team roles have been updated."},
49+
)
50+
history = m.request_history
51+
52+
roleUids = ["ZiHQJq5nk", "GzNQ1357k"]
53+
r = self.grafana.rbac.add_rbac_roles_team("1", roleUids)
54+
self.assertEqual(history[0].json()["roleUids"], roleUids)
55+
self.assertEqual(r["message"], "Team roles have been updated.")
56+
57+
@requests_mock.Mocker()
58+
def test_remove_rbac_role_team(self, m):
59+
m.delete(
60+
"http://localhost/api/access-control/teams/1/roles/AFUXBHKnk",
61+
json={"message": "Role removed from team."},
62+
)
63+
r = self.grafana.rbac.remove_rbac_role_team("1", "AFUXBHKnk")
64+
self.assertEqual(r["message"], "Role removed from team.")

0 commit comments

Comments
 (0)