Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch Grafana version from /api/frontend/settings #144

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions grafana_wtf/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,28 +321,32 @@
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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole response is quite large. It makes sense to return only basic buildInfo which contains similar to /api/health response data.

if not response:
error = f"{error_template}: No buildInfo found in the settings response"

Check warning on line 336 in grafana_wtf/core.py

View check run for this annotation

Codecov / codecov/patch

grafana_wtf/core.py#L336

Added line #L336 was not covered by tests

except Exception as ex:
error = f"{error_template}: {ex}"

if error:
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:
Expand Down
17 changes: 8 additions & 9 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,28 @@ 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"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

database status is not represented in the buildInfo response.

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"


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")