From 9153a574252fcf74cb6369d33265fa423684aae7 Mon Sep 17 00:00:00 2001 From: ilya Date: Sat, 28 Sep 2024 20:38:16 +0100 Subject: [PATCH] Fetch Grafana version from `/api/frontend/settings` (#144) --- grafana_wtf/core.py | 16 ++++++++++------ tests/test_core.py | 17 ++++++++--------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/grafana_wtf/core.py b/grafana_wtf/core.py index 74afe07..af7aae9 100644 --- a/grafana_wtf/core.py +++ b/grafana_wtf/core.py @@ -321,15 +321,20 @@ def info(self): return response @property - def health(self): + def build_info(self): response = None error = None - error_template = f"The request to {self.grafana_url.rstrip('/')}/api/health failed" + error_template = f"The request to {self.grafana_url.rstrip('/')}/api/frontend/settings failed" try: - response = self.grafana.client.GET("/health") + response = self.grafana.client.GET("/frontend/settings") if not isinstance(response, dict): error = f"{error_template}: Invalid response, content was: {response}" + response = Munch(response) + response = response.get("buildInfo") + if not response: + error = f"{error_template}: No buildInfo found in the settings response" + except Exception as ex: error = f"{error_template}: {ex}" @@ -337,12 +342,11 @@ def health(self): log.critical(error) raise ConnectionError(error) - if response: - return Munch(response) + return response @property def version(self): - return self.health.get("version") + return self.build_info.get("version") def dashboard_details(self): for dashboard in self.data.dashboards: diff --git a/tests/test_core.py b/tests/test_core.py index ea097fb..53bb15b 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -40,23 +40,22 @@ def test_collect_datasource_items_variable_all(): def test_connect_success(): wtf = GrafanaWtf("https://play.grafana.org") - health = wtf.health - assert "commit" in health - assert "version" in health - assert health.database == "ok" + build_info = wtf.build_info + assert "commit" in build_info + assert "version" in build_info def test_connect_failure(): wtf = GrafanaWtf("http://localhost:1234") with pytest.raises(ConnectionError) as ex: - _ = wtf.health - assert ex.match("The request to http://localhost:1234/api/health failed") + _ = wtf.build_info + assert ex.match("The request to http://localhost:1234/api/frontend/settings failed") @patch("grafana_client.client.GrafanaClient.__getattr__") def test_connect_version(mock_get): mock_get.return_value = Mock() - mock_get.return_value.return_value = {"commit": "14e988bd22", "database": "ok", "version": "9.0.1"} + mock_get.return_value.return_value = {"buildInfo": {"version": "9.0.1", "commit": "14e988bd22"}} wtf = GrafanaWtf("http://localhost:1234") assert wtf.version == "9.0.1" @@ -64,5 +63,5 @@ def test_connect_version(mock_get): def test_connect_non_json_response(): wtf = GrafanaWtf("https://example.org/") with pytest.raises(ConnectionError) as ex: - _ = wtf.health - assert ex.match("The request to https://example.org/api/health failed") + _ = wtf.build_info + assert ex.match("The request to https://example.org/api/frontend/settings failed")