Skip to content

Commit d87c9a8

Browse files
lilatomicamotl
authored andcommitted
Feature: specify grafana organisation for client
1 parent bf2dbb8 commit d87c9a8

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
* Add methods `get_permissions_by_uid` and `update_permissions_by_uid` for dashboards.
66
Thanks, @meyerder.
7+
* Add `GrafanaApi.organization_id` for targeting all requests to a Grafana organization
8+
Thanks, @lilatomic
79

810

911
## 3.9.2 (2023-10-14)

grafana_client/api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def __init__(
4747
verify=True,
4848
timeout=DEFAULT_TIMEOUT,
4949
user_agent: str = None,
50+
organization_id: int = None,
5051
):
5152
self.client = GrafanaClient(
5253
auth,
@@ -57,6 +58,7 @@ def __init__(
5758
verify=verify,
5859
timeout=timeout,
5960
user_agent=user_agent,
61+
organization_id=organization_id,
6062
)
6163
self.url = None
6264
self.admin = Admin(self.client)

grafana_client/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def __init__(
7979
verify=True,
8080
timeout=DEFAULT_TIMEOUT,
8181
user_agent: str = None,
82+
organization_id: int = None,
8283
):
8384
self.auth = auth
8485
self.verify = verify
@@ -111,6 +112,12 @@ def construct_api_url():
111112

112113
self.s = requests.Session()
113114
self.s.headers["User-Agent"] = self.user_agent
115+
116+
self.organization_id = organization_id
117+
if self.organization_id:
118+
# orgId is defined in the openapi3 spec as an int64, but headers need to be a str
119+
self.s.headers["X-Grafana-Org-Id"] = str(self.organization_id)
120+
114121
if self.auth is not None:
115122
if isinstance(self.auth, requests.auth.AuthBase):
116123
pass

test/test_grafana_client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ def test_grafana_client_user_agent_custom(self):
3939
)
4040
self.assertEqual(grafana.client.s.headers["User-Agent"], "foobar/3000")
4141

42+
def test_grafana_client_no_org(self):
43+
grafana = GrafanaApi(
44+
("admin", "admin"), host="localhost", url_path_prefix="", protocol="https", organization_id=None
45+
)
46+
self.assertNotIn("X-Grafana-Org-Id", grafana.client.s.headers)
47+
48+
def test_grafana_client_org(self):
49+
org_id = 2
50+
grafana = GrafanaApi(
51+
("admin", "admin"), host="localhost", url_path_prefix="", protocol="https", organization_id=org_id
52+
)
53+
self.assertEqual(grafana.client.s.headers["X-Grafana-Org-Id"], str(org_id))
54+
4255
@patch("grafana_client.client.GrafanaClient.__getattr__")
4356
def test_grafana_client(self, mock_get):
4457
mock_get.return_value = Mock()

0 commit comments

Comments
 (0)