diff --git a/lib/puppet/provider/grafana_dashboard/grafana.rb b/lib/puppet/provider/grafana_dashboard/grafana.rb index bdfd77e..5b5770b 100644 --- a/lib/puppet/provider/grafana_dashboard/grafana.rb +++ b/lib/puppet/provider/grafana_dashboard/grafana.rb @@ -1,172 +1,174 @@ # Copyright 2015 Mirantis, Inc. # require 'json' require File.expand_path(File.join(File.dirname(__FILE__), '..', 'grafana')) # Note: this class doesn't implement the self.instances and self.prefetch # methods because the Grafana API doesn't allow to retrieve the dashboards and # all their properties in a single call. Puppet::Type.type(:grafana_dashboard).provide(:grafana, parent: Puppet::Provider::Grafana) do desc 'Support for Grafana dashboards stored into Grafana' defaultfor kernel: 'Linux' def organization resource[:organization] end def grafana_api_path resource[:grafana_api_path] end def fetch_organizations response = send_request('GET', format('%s/orgs', resource[:grafana_api_path])) if response.code != '200' raise format('Fail to retrieve organizations (HTTP response: %s/%s)', response.code, response.body) end begin fetch_organizations = JSON.parse(response.body) fetch_organizations.map { |x| x['id'] }.map do |id| response = send_request 'GET', format('%s/orgs/%s', resource[:grafana_api_path], id) if response.code != '200' raise format('Failed to retrieve organization %d (HTTP response: %s/%s)', id, response.code, response.body) end fetch_organization = JSON.parse(response.body) { id: fetch_organization['id'], name: fetch_organization['name'] } end rescue JSON::ParserError raise format('Failed to parse response: %s', response.body) end end def fetch_organization unless @fetch_organization @fetch_organization = if resource[:organization].is_a?(Numeric) || resource[:organization].match(%r{^[0-9]*$}) fetch_organizations.find { |x| x[:id] == resource[:organization] } else fetch_organizations.find { |x| x[:name] == resource[:organization] } end end @fetch_organization end def folders response = send_request('GET', format('%s/folders', resource[:grafana_api_path])) if response.code != '200' raise format('Fail to retrieve the folders (HTTP response: %s/%s)', response.code, response.body) end begin @folders = JSON.parse(response.body) rescue JSON::ParserError raise format('Fail to parse folders (HTTP response: %s/%s)', response.code, response.body) end end def find_folder folders unless @folders begin @folders.each do |folder| @folder = folder if folder['title'] == resource[:folder] end raise format('Folder not found: %s', resource[:folder]) unless @folder['title'] == resource[:folder] rescue JSON::ParserError raise format('Fail to parse folder %s: %s', resource[:folder], response.body) end end # Return the list of dashboards def dashboards # change organizations response = send_request 'POST', format('%s/user/using/%s', resource[:grafana_api_path], fetch_organization[:id]) unless response.code == '200' raise format('Failed to switch to org %s (HTTP response: %s/%s)', fetch_organization[:id], response.code, response.body) end response = send_request('GET', format('%s/search', resource[:grafana_api_path]), nil, q: '', starred: false) if response.code != '200' raise format('Fail to retrieve the dashboards (HTTP response: %s/%s)', response.code, response.body) end begin JSON.parse(response.body) rescue JSON::ParserError raise format('Fail to parse dashboards (HTTP response: %s/%s)', response.code, response.body) end end # Return the dashboard matching with the resource's title def find_dashboard return unless dashboards.find { |x| x['title'] == resource[:title] } - response = send_request('GET', format('%s/dashboards/db/%s', resource[:grafana_api_path], slug)) + response = send_request('GET', format('%s/dashboards/uid/%s', resource[:grafana_api_path], slug)) + if response.code != '200' raise format('Fail to retrieve dashboard %s (HTTP response: %s/%s)', resource[:title], response.code, response.body) end begin # Cache the dashboard's content @dashboard = JSON.parse(response.body)['dashboard'] rescue JSON::ParserError raise format('Fail to parse dashboard %s: %s', resource[:title], response.body) end end def save_dashboard(dashboard) find_folder if resource[:folder] # change organizations response = send_request 'POST', format('%s/user/using/%s', resource[:grafana_api_path], fetch_organization[:id]) unless response.code == '200' raise format('Failed to switch to org %s (HTTP response: %s/%s)', fetch_organization[:id], response.code, response.body) end data = { dashboard: dashboard.merge('title' => resource[:title], 'id' => @dashboard ? @dashboard['id'] : nil, + 'uid' => slug, 'version' => @dashboard ? @dashboard['version'] + 1 : 0), folderId: @folder ? @folder['id'] : nil, overwrite: !@dashboard.nil? } response = send_request('POST', format('%s/dashboards/db', resource[:grafana_api_path]), data) return unless (response.code != '200') && (response.code != '412') raise format('Fail to save dashboard %s (HTTP response: %s/%s', resource[:name], response.code, response.body) end def slug resource[:title].downcase.gsub(%r{[ \+]+}, '-').gsub(%r{[^\w\- ]}, '') end def content - @dashboard.reject { |k, _| k =~ %r{^id|version|title$} } + @dashboard.reject { |k, _| k =~ %r{^id|uid|version|title$} } end def content=(value) save_dashboard(value) end def create save_dashboard(resource[:content]) end def destroy - response = send_request('DELETE', format('%s/dashboards/db/%s', resource[:grafana_api_path], slug)) + response = send_request('DELETE', format('%s/dashboards/uid/%s', resource[:grafana_api_path], slug)) return unless response.code != '200' raise Puppet::Error, format('Failed to delete dashboard %s (HTTP response: %s/%s', resource[:title], response.code, response.body) end def exists? find_dashboard end end diff --git a/spec/acceptance/grafana_folder_spec.rb b/spec/acceptance/grafana_folder_spec.rb index 250c2b1..3cb160f 100644 --- a/spec/acceptance/grafana_folder_spec.rb +++ b/spec/acceptance/grafana_folder_spec.rb @@ -1,196 +1,196 @@ require 'spec_helper_acceptance' describe 'grafana_folder' do context 'setup grafana server' do it 'runs successfully' do pp = <<-EOS class { 'grafana': cfg => { security => { admin_user => 'admin', admin_password => 'admin' } } } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end end context 'create folder resource' do it 'creates the folders' do pp = <<-EOS include grafana::validator grafana_folder { 'example-folder': ensure => present, uid => 'example-folder', grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', permissions => [ {'permission' => 2, 'role' => 'Editor'}, {'permission' => 1, 'role' => 'Viewer'}, ], } grafana_folder { 'editor-folder': ensure => present, uid => 'editor-folder', grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', permissions => [ {'permission' => 1, 'role' => 'Editor'}, ], } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end it 'has created the example folder' do shell('curl --user admin:admin http://localhost:3000/api/folders') do |f| expect(f.stdout).to match(%r{example-folder}) end end it 'has created the editor folder' do shell('curl --user admin:admin http://localhost:3000/api/folders') do |f| expect(f.stdout).to match(%r{editor-folder}) end end it 'has created the example folder permissions' do shell('curl --user admin:admin http://localhost:3000/api/folders/example-folder/permissions') do |f| data = JSON.parse(f.stdout) expect(data).to include(hash_including('permission' => 2, 'role' => 'Editor'), hash_including('permission' => 1, 'role' => 'Viewer')) end end it 'has created the editor folder permissions' do shell('curl --user admin:admin http://localhost:3000/api/folders/editor-folder/permissions') do |f| data = JSON.parse(f.stdout) expect(data).to include(hash_including('permission' => 1, 'role' => 'Editor')) end end end context 'updates folder resource' do it 'updates the folders' do pp = <<-EOS grafana_folder { 'example-folder': ensure => present, uid => 'example-folder', grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', permissions => [ {'permission' => 2, 'role' => 'Editor'}, ], } grafana_folder { 'editor-folder': ensure => present, uid => 'editor-folder', grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', permissions => [ {'permission' => 1, 'role' => 'Viewer'}, ], } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end it 'has updated the example folder permissions' do shell('curl --user admin:admin http://localhost:3000/api/folders/example-folder/permissions') do |f| data = JSON.parse(f.stdout) expect(data).to include(hash_including('permission' => 2, 'role' => 'Editor')) # expect(data.size).to eq(1) # expect(data[0]['permission']).to eq(2) # expect(data[0]['role']).to eq('Editor') end end it 'has updated the editor folder permissions' do shell('curl --user admin:admin http://localhost:3000/api/folders/editor-folder/permissions') do |f| data = JSON.parse(f.stdout) expect(data).to include(hash_including('permission' => 1, 'role' => 'Viewer')) end end end context 'create folder containing dashboard' do it 'creates an example dashboard in the example folder' do pp = <<-EOS include grafana::validator grafana_dashboard { 'example-dashboard': ensure => present, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', - content => '{"uid": "abc123xy"}', + content => '{"description": "example dashboard"}', folder => 'example-folder' } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end it 'folder contains dashboard' do - shell('curl --user admin:admin http://localhost:3000/api/dashboards/db/example-dashboard') do |f| + shell('curl --user admin:admin http://localhost:3000/api/dashboards/uid/example-dashboard') do |f| expect(f.stdout).to match(%r{"folderId":1}) end end end context 'destroy resources' do it 'destroys the folders and dashboard' do pp = <<-EOS include grafana::validator grafana_folder { 'example-folder': ensure => absent, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', } grafana_folder { 'editor-folder': ensure => absent, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', } grafana_folder { 'nomatch-folder': ensure => absent, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', } grafana_dashboard { 'example-dashboard': ensure => absent, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end it 'has no example-folder' do shell('curl --user admin:admin http://localhost:3000/api/folders') do |f| expect(f.stdout).not_to match(%r{example-folder}) end end it 'has no editor-folder' do shell('curl --user admin:admin http://localhost:3000/api/folders') do |f| expect(f.stdout).not_to match(%r{editor-folder}) end end it 'has no nomatch-folder' do shell('curl --user admin:admin http://localhost:3000/api/folders') do |f| expect(f.stdout).not_to match(%r{nomatch-folder}) end end end end diff --git a/spec/acceptance/grafana_team_spec.rb b/spec/acceptance/grafana_team_spec.rb index 9381ae6..b82b66a 100644 --- a/spec/acceptance/grafana_team_spec.rb +++ b/spec/acceptance/grafana_team_spec.rb @@ -1,208 +1,208 @@ require 'spec_helper_acceptance' describe 'grafana_team' do context 'setup grafana server' do it 'runs successfully' do pp = <<-EOS class { 'grafana': cfg => { security => { admin_user => 'admin', admin_password => 'admin' } } } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end end context 'create team resource on `Main Org.`' do it 'creates the team' do pp = <<-EOS include grafana::validator grafana_team { 'example-team': ensure => present, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end it 'has created the example team' do shell('curl --user admin:admin http://localhost:3000/api/teams/search?name=example-team') do |f| expect(f.stdout).to match(%r{example-team}) end end 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) end end end context 'updates team resource' do it 'creates dashboard and sets team home dashboard' do pp = <<-EOS include grafana::validator grafana_dashboard { 'example-dashboard': ensure => present, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', - content => '{"uid": "zyx986bc"}', + content => '{"description": "example dashboard"}', } grafana_folder { 'example-folder': ensure => present, uid => 'example-folder', grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', } -> grafana_dashboard { 'example-dashboard2': ensure => present, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', - content => '{"uid": "niew0ahN"}', + content => '{"description": "example dashboard2"}', folder => 'example-folder', } grafana_team { 'example-team': ensure => present, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', home_dashboard => 'example-dashboard', } grafana_team { 'example-team2': ensure => present, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', home_dashboard_folder => 'example-folder', home_dashboard => 'example-dashboard2', } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end it 'has updated the example team home dashboard' do shell('curl --user admin:admin http://localhost:3000/api/teams/1/preferences') do |f| data = JSON.parse(f.stdout) expect(data['homeDashboardId']).not_to eq(0) end end it 'has updated the example team home dashboard with folder' do shell('curl --user admin:admin http://localhost:3000/api/teams/2/preferences') do |f| data = JSON.parse(f.stdout) expect(data['homeDashboardId']).not_to eq(0) end end end context 'create team resource on seperate organization' do it 'creates organization and team' do pp = <<-EOS include grafana::validator grafana_organization { 'example-organization': ensure => present, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', } grafana_team { 'example-team-on-org': ensure => present, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', organization => 'example-organization', } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end it 'creates team on organization' do shell('curl --user admin:admin -X POST http://localhost:3000/api/user/using/2 && '\ 'curl --user admin:admin http://localhost:3000/api/teams/search?name=example-team-on-org') do |f| expect(f.stdout).to match(%r{example-team-on-org}) end end end context 'destroy resources' do it 'destroys the teams, dashboard, and organization' do pp = <<-EOS include grafana::validator grafana_team { 'example-team': ensure => absent, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', } grafana_team { 'example-team2': ensure => absent, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', } grafana_team { 'example-team-on-org': ensure => absent, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', organization => 'example-organization', } grafana_dashboard { 'example-dashboard': ensure => absent, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', } grafana_dashboard { 'example-dashboard2': ensure => absent, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', } grafana_folder { 'example-folder': ensure => absent, uid => 'example-folder', grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', } grafana_organization { 'example-organization': ensure => absent, grafana_url => 'http://localhost:3000', grafana_user => 'admin', grafana_password => 'admin', } EOS apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end it 'has no example-team' do shell('curl --user admin:admin -X POST http://localhost:3000/api/user/using/1 && '\ 'curl --user admin:admin http://localhost:3000/api/teams/search') do |f| expect(f.stdout).not_to match(%r{example-team}) end end it 'has no example-team-on-org' do shell('curl --user admin:admin -X POST http://localhost:3000/api/user/using/2 && '\ 'curl --user admin:admin http://localhost:3000/api/teams') do |f| expect(f.stdout).not_to match(%r{example-team-on-org}) end end end end