Skip to content

Commit

Permalink
Feature: specify grafana organisation for client
Browse files Browse the repository at this point in the history
  • Loading branch information
lilatomic authored and amotl committed Oct 30, 2023
1 parent bf2dbb8 commit d87c9a8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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


## 3.9.2 (2023-10-14)
Expand Down
2 changes: 2 additions & 0 deletions grafana_client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(
verify=True,
timeout=DEFAULT_TIMEOUT,
user_agent: str = None,
organization_id: int = None,
):
self.client = GrafanaClient(
auth,
Expand All @@ -57,6 +58,7 @@ def __init__(
verify=verify,
timeout=timeout,
user_agent=user_agent,
organization_id=organization_id,
)
self.url = None
self.admin = Admin(self.client)
Expand Down
7 changes: 7 additions & 0 deletions grafana_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def __init__(
verify=True,
timeout=DEFAULT_TIMEOUT,
user_agent: str = None,
organization_id: int = None,
):
self.auth = auth
self.verify = verify
Expand Down Expand Up @@ -111,6 +112,12 @@ def construct_api_url():

self.s = requests.Session()
self.s.headers["User-Agent"] = self.user_agent

self.organization_id = organization_id
if self.organization_id:
# orgId is defined in the openapi3 spec as an int64, but headers need to be a str
self.s.headers["X-Grafana-Org-Id"] = str(self.organization_id)

if self.auth is not None:
if isinstance(self.auth, requests.auth.AuthBase):
pass
Expand Down
13 changes: 13 additions & 0 deletions test/test_grafana_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ def test_grafana_client_user_agent_custom(self):
)
self.assertEqual(grafana.client.s.headers["User-Agent"], "foobar/3000")

def test_grafana_client_no_org(self):
grafana = GrafanaApi(
("admin", "admin"), host="localhost", url_path_prefix="", protocol="https", organization_id=None
)
self.assertNotIn("X-Grafana-Org-Id", grafana.client.s.headers)

def test_grafana_client_org(self):
org_id = 2
grafana = GrafanaApi(
("admin", "admin"), host="localhost", url_path_prefix="", protocol="https", organization_id=org_id
)
self.assertEqual(grafana.client.s.headers["X-Grafana-Org-Id"], str(org_id))

@patch("grafana_client.client.GrafanaClient.__getattr__")
def test_grafana_client(self, mock_get):
mock_get.return_value = Mock()
Expand Down

0 comments on commit d87c9a8

Please sign in to comment.