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

Support Grafana 10.x #348

Merged
merged 7 commits into from
Feb 3, 2024
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
45 changes: 34 additions & 11 deletions lib/puppet/provider/grafana_team/grafana.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def team
def map_preferences(preferences)
{
theme: preferences['theme'],
home_dashboard: preferences['homeDashboardId'],
home_dashboard: preferences['homeDashboardId'] || preferences['homeDashboardUID'],
timezone: preferences['timezone']
}
end
Expand All @@ -94,12 +94,16 @@ def preferences

def setup_save_preferences_data
endpoint = format('%s/teams/%s/preferences', resource[:grafana_api_path], @team[:id])
dash = get_dashboard(resource[:home_dashboard], resource[:home_dashboard_folder])
dash = get_dashboard(resource[:home_dashboard], resource[:home_dashboard_folder], true)
request_data = {
theme: resource[:theme],
homeDashboardId: dash[:id],
timezone: resource[:timezone]
}
if major_version >= 10
request_data[:homeDashboardUID] = dash[:uid]
else
request_data[:homeDashboardId] = dash[:id]
end
['PUT', endpoint, request_data]
end

Expand All @@ -123,7 +127,7 @@ def set_current_organization

def home_dashboard_folder
preferences unless @preferences
dash = get_dashboard(@preferences[:home_dashboard])
dash = get_dashboard(@preferences[:home_dashboard], nil)
return dash[:folder_name] if dash

nil
Expand All @@ -136,7 +140,7 @@ def home_dashboard_folder=(value)

def home_dashboard
preferences unless @preferences
dash = get_dashboard(@preferences[:home_dashboard])
dash = get_dashboard(@preferences[:home_dashboard], nil)
return dash[:name] if dash

nil
Expand All @@ -147,30 +151,48 @@ def home_dashboard=(value)
save_preferences
end

def setup_search_path(ident, folder_id = nil)
query = if ident.is_a?(Numeric) || ident.match(%r{/^[0-9]*$/})
def version
return if @version

response = send_request('GET', format('%s/health', resource[:grafana_api_path]))
data = parse_response(response.body)
@version = data['version']
end

def major_version
version unless @version
@version.split('.').first.to_i
end

def setup_search_path(ident, folder_id = nil, search = false)
query = if search
{
query: ident,
type: 'dash-db'
}
elsif ident.is_a?(Numeric) || ident.match(%r{/^[0-9]*$/})
{
dashboardIds: ident,
type: 'dash-db'
}
else
{
query: ident,
dashboardUIDs: ident,
type: 'dash-db'
}
end
query[:folderIds] = folder_id unless folder_id.nil?
query
end

def get_dashboard(ident, folder = nil)
def get_dashboard(ident, folder = nil, search = false)
set_current_organization
return { id: 0, name: 'Default' } if ident == 0 # rubocop:disable Style/NumericPredicate
return { id: 0, name: 'Default' } if ident == 0 || ident.nil? # rubocop:disable Style/NumericPredicate

folder_id = nil
folder_id = get_dashboard_folder_id(folder) unless folder.nil?

search_path = setup_search_path(ident, folder_id)
search_path = setup_search_path(ident, folder_id, search)
response = send_request('GET', format('%s/search', resource[:grafana_api_path]), nil, search_path)
raise_on_error(response.code, format('Fail to retrieve dashboars (HTTP response: %s/%s)', response.code, response.body))

Expand All @@ -183,6 +205,7 @@ def format_dashboard(dashboard)

{
id: dashboard.first['id'],
uid: dashboard.first['uid'],
name: dashboard.first['title'],
folder_uid: dashboard.first['folderUid'],
folder_name: dashboard.first['folderTitle'],
Expand Down
7 changes: 6 additions & 1 deletion lib/puppet/type/grafana_folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@
newproperty(:permissions, array_matching: :all) do
desc 'The permissions of the folder'
def insync?(is)
is.sort_by { |k| k['permission'] } == should.sort_by { |k| k['permission'] }
# Doing sort_by on array of values from each Hash was producing
# inconsistent results where Puppet would think changes were necessary when
# not actually necessary
is_m = is.map { |p| p.keys.sort.map { |k| p[k] }.join('-') }
should_m = should.map { |p| p.keys.sort.map { |k| p[k] }.join('-') }
is_m.sort == should_m.sort
end
end

Expand Down
7 changes: 6 additions & 1 deletion spec/acceptance/grafana_team_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ class { 'grafana':
it 'has set default home dashboard' do
shell('curl --user admin:admin http://localhost:3000/api/teams/1/preferences') do |f|
data = JSON.parse(f.stdout)
expect(data).to include('homeDashboardId' => 0)
# preferences are empty by default in Grafana 10
if grafana_version =~ %r{^(8|9)}
expect(data).to include('homeDashboardId' => 0)
else
expect(data).to be_empty
end
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/acceptance/grafana_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ class { 'grafana':
it_behaves_like 'an idempotent resource' do
let(:manifest) do
<<-PUPPET
grafana_user { 'admin1':
grafana_user { 'user1':
ensure => absent,
grafana_url => 'http://localhost:3000',
grafana_user => 'user1',
grafana_password => 'newpassword',
grafana_user => 'admin1',
grafana_password => 'Admin5ecret',
}
PUPPET
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/acceptance/supported_versions.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

def supported_versions
%w[8.5.22 9.2.15]
%w[8.5.22 9.2.15 10.3.1]
end
Loading