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

Fix dashboard api call in grafana_dashboard #267

Merged
merged 8 commits into from Nov 26, 2021

Conversation

joernott
Copy link

Pull Request (PR) description

The grafana_dashboard function was changed from using the old deprecated api to the recommended endpoint using the uid. However, the module still passed the dashboard title to the API instead of the uid, always resulting in a dashboard not found error. This PR fixes the problem.

This Pull Request (PR) fixes the following issues

Fixes #261

@bastelfreak bastelfreak added the bug Something isn't working label Nov 24, 2021
@joernott
Copy link
Author

I found another issue I fixed: When the code was changed, the uid was overwritten by slug (normalized string derived from the title). This breaks existing dashboards by changing their UID (and applications using them).

There is also an idempotence issue, I am currently tracking down. The dashboards are being updated every puppet run. I use the JSON export provided by grafana as input but that seems to differ from what the module uses to assert changes. So, before merging this PR, I would like to figure out, what is causing the problem.

@joernott
Copy link
Author

The idempotence issue was actually caused by me taking the json from the Export/Share dashboard functionality (or the API call to /dashboards/uid/ without removing the fields "id", "uid", "title" and "version" from it. As the ruby code merges these fields into the hash, the order of fields changes due to the way ruby works. This results in the comparation between current and target to fail as the field order of the two hases is different. I added a sentence to the README for other users to stumble upon it.

Copy link
Member

@root-expert root-expert left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Have you tested if that works for both old generated dashboard and new generated?

The idea of using uid to create dashboard was to completely replace slug with uid. But yes I agree no migration was provided for old dashboards

lib/puppet/provider/grafana_dashboard/grafana.rb Outdated Show resolved Hide resolved
lib/puppet/provider/grafana_dashboard/grafana.rb Outdated Show resolved Hide resolved
@root-expert
Copy link
Member

The idempotence issue was actually caused by me taking the json from the Export/Share dashboard functionality (or the API call to /dashboards/uid/ without removing the fields "id", "uid", "title" and "version" from it. As the ruby code merges these fields into the hash, the order of fields changes due to the way ruby works. This results in the comparation between current and target to fail as the field order of the two hases is different. I added a sentence to the README for other users to stumble upon it.

About this. There have been multiple PRs, as far as I saw, in the past trying to address the idempotence issue. There are some measures in places to avoid this (here and here). If you think you can help with that it would be awesome! As far as I see, as a first step in the second link we need to add uid parameter to the list.

From your comment, I suppose after merging the two hashes we can sort it as well to ensure the order doesn't change.

@joernott
Copy link
Author

I am not that skilled in ruby to actually solve these idempotence issues in the code. I have encountered something similar (when handling yaml files) in https://github.com/voxpupuli/puppet-elasticsearch/blob/master/lib/puppet_x/elastic/hash.rb, but that thing is scary...

@joernott
Copy link
Author

Fixed the acceptance test of the folder and set the uid to slug for new dashboards. By setting it to slug, we become predictable and by not assuming its slug, we are backwards compatible

@@ -139,7 +139,7 @@ class { 'grafana':
end

it 'folder contains dashboard' do
shell('curl --user admin:admin http://localhost:3000/api/dashboards/uid/example-dashboard') do |f|
shell('curl --user admin:admin http://localhost:3000/api/search?query=example-dashboard') do |f|
Copy link
Member

Choose a reason for hiding this comment

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

Why the search query instead of uid since the uid is now predictable?

Copy link
Author

Choose a reason for hiding this comment

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

The uid is only predictable when creating new dashboards. Even though I changed the behaviour for new and the old test works for now, I still consider it a more stable solution to not rely on uid=slug. The search works with the name, no matter what is in the uid field.

@bastelfreak bastelfreak merged commit ad967b0 into voxpupuli:master Nov 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

grafana_dashboards ov version 9.0.1 does not work with Grafana 6.7.6
3 participants