Skip to content

Commit 9153a57

Browse files
authored
Fetch Grafana version from /api/frontend/settings (#144)
1 parent a5db83e commit 9153a57

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

grafana_wtf/core.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,28 +321,32 @@ def info(self):
321321
return response
322322

323323
@property
324-
def health(self):
324+
def build_info(self):
325325
response = None
326326
error = None
327-
error_template = f"The request to {self.grafana_url.rstrip('/')}/api/health failed"
327+
error_template = f"The request to {self.grafana_url.rstrip('/')}/api/frontend/settings failed"
328328
try:
329-
response = self.grafana.client.GET("/health")
329+
response = self.grafana.client.GET("/frontend/settings")
330330
if not isinstance(response, dict):
331331
error = f"{error_template}: Invalid response, content was: {response}"
332332

333+
response = Munch(response)
334+
response = response.get("buildInfo")
335+
if not response:
336+
error = f"{error_template}: No buildInfo found in the settings response"
337+
333338
except Exception as ex:
334339
error = f"{error_template}: {ex}"
335340

336341
if error:
337342
log.critical(error)
338343
raise ConnectionError(error)
339344

340-
if response:
341-
return Munch(response)
345+
return response
342346

343347
@property
344348
def version(self):
345-
return self.health.get("version")
349+
return self.build_info.get("version")
346350

347351
def dashboard_details(self):
348352
for dashboard in self.data.dashboards:

tests/test_core.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,28 @@ def test_collect_datasource_items_variable_all():
4040

4141
def test_connect_success():
4242
wtf = GrafanaWtf("https://play.grafana.org")
43-
health = wtf.health
44-
assert "commit" in health
45-
assert "version" in health
46-
assert health.database == "ok"
43+
build_info = wtf.build_info
44+
assert "commit" in build_info
45+
assert "version" in build_info
4746

4847

4948
def test_connect_failure():
5049
wtf = GrafanaWtf("http://localhost:1234")
5150
with pytest.raises(ConnectionError) as ex:
52-
_ = wtf.health
53-
assert ex.match("The request to http://localhost:1234/api/health failed")
51+
_ = wtf.build_info
52+
assert ex.match("The request to http://localhost:1234/api/frontend/settings failed")
5453

5554

5655
@patch("grafana_client.client.GrafanaClient.__getattr__")
5756
def test_connect_version(mock_get):
5857
mock_get.return_value = Mock()
59-
mock_get.return_value.return_value = {"commit": "14e988bd22", "database": "ok", "version": "9.0.1"}
58+
mock_get.return_value.return_value = {"buildInfo": {"version": "9.0.1", "commit": "14e988bd22"}}
6059
wtf = GrafanaWtf("http://localhost:1234")
6160
assert wtf.version == "9.0.1"
6261

6362

6463
def test_connect_non_json_response():
6564
wtf = GrafanaWtf("https://example.org/")
6665
with pytest.raises(ConnectionError) as ex:
67-
_ = wtf.health
68-
assert ex.match("The request to https://example.org/api/health failed")
66+
_ = wtf.build_info
67+
assert ex.match("The request to https://example.org/api/frontend/settings failed")

0 commit comments

Comments
 (0)