Skip to content

Commit

Permalink
Bug fixes and adding organization option to dashboards
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Riden committed Mar 5, 2018
1 parent f79e9c1 commit 39c236a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
32 changes: 17 additions & 15 deletions lib/puppet/provider/grafana_dashboard/grafana.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ 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'
Expand All @@ -26,7 +30,7 @@ def fetch_organizations
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))
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
Expand All @@ -45,9 +49,14 @@ def fetch_organizations

def fetch_organization
unless @fetch_organization
@fetch_organization = fetch_organizations.find { |x| x[:name] == resource[: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
@organization
@fetch_organization
end

# Return the list of dashboards
Expand Down Expand Up @@ -82,17 +91,10 @@ def find_dashboard
end

def save_dashboard(dashboard)
if fetch_organization.nil?
response = send_request('POST', format('%s/user/using/1', resource[:grafana_api_path]))
if response.code != '200'
raise format('Failed to switch to org 1 (HTTP response: %s/%s)', response.code, response.body)
end
else
organization_id = fetch_organization[:id]
response = send_request 'POST', format('%s/user/using/%s', resource[:grafana_api_path], organization_id)
if response.code != '200'
raise format('Failed to switch to org %s (HTTP response: %s/%s)', organization_id, response.code, response.body)
end
# 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 = {
Expand All @@ -103,7 +105,7 @@ def save_dashboard(dashboard)
}

response = send_request('POST', format('%s/dashboards/db', resource[:grafana_api_path]), data)
return unless response.code != '200'
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

Expand Down
39 changes: 20 additions & 19 deletions lib/puppet/provider/grafana_datasource/grafana.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright 2015 Mirantis, Inc.
#
require 'json'
require 'pry'
require 'pry-byebug'

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'grafana'))

Expand All @@ -13,6 +15,10 @@ 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'
Expand All @@ -21,9 +27,8 @@ def fetch_organizations

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))
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
Expand All @@ -42,9 +47,13 @@ def fetch_organizations

def fetch_organization
unless @fetch_organization
@fetch_organization = fetch_organizations.find { |x| x[:name] == resource[: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
@organization
@fetch_organization
end

def datasources
Expand All @@ -57,7 +66,7 @@ def datasources
datasources = JSON.parse(response.body)

datasources.map { |x| x['id'] }.map do |id|
response = send_request('GET', format('%s/datasources/%s', resource[:grafana_api_path], id))
response = send_request 'GET', format('%s/datasources/%s', resource[:grafana_api_path], id)
if response.code != '200'
raise format('Failed to retrieve datasource %d (HTTP response: %s/%s)', id, response.code, response.body)
end
Expand Down Expand Up @@ -206,17 +215,10 @@ def json_data=(value)
end

def save_datasource
if fetch_organization.nil?
response = send_request('POST', format('%s/user/using/1', resource[:grafana_api_path]))
if response.code != '200'
raise format('Failed to switch to org 1 (HTTP response: %s/%s)', response.code, response.body)
end
else
organization_id = fetch_organization[:id]
response = send_request 'POST', format('%s/user/using/%s', resource[:grafana_api_path], organization_id)
if response.code != '200'
raise format('Failed to switch to org %s (HTTP response: %s/%s)', organization_id, response.code, response.body)
end
# 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 = {
Expand All @@ -239,17 +241,16 @@ def save_datasource
response = send_request('POST', format('%s/datasources', resource[:grafana_api_path]), data)
else
data[:id] = datasource[:id]
response = send_request('PUT', format('%s/datasources/%s', resource[:grafana_api_path], datasource[:id]), data)
response = send_request 'PUT', format('%s/datasources/%s', resource[:grafana_api_path], datasource[:id]), data
end

if response.code != '200'
raise format('Failed to create save %s (HTTP response: %s/%s)', resource[:name], response.code, response.body)
end
self.datasource = nil
end

def delete_datasource
response = send_request('DELETE', format('%s/datasources/%s', resource[:grafana_api_path], datasource[:id]))
response = send_request 'DELETE', format('%s/datasources/%s', resource[:grafana_api_path], datasource[:id])

if response.code != '200'
raise format('Failed to delete datasource %s (HTTP response: %s/%s', resource[:name], response.code, response.body)
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/provider/grafana_organization/grafana.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def save_organization
end

def delete_organization
response = send_request('DELETE', format('%s/orgs/%s', resource[:grafana_api_path], organization[:id]))
response = send_request 'DELETE', format('%s/orgs/%s', resource[:grafana_api_path], organization[:id])

if response.code != '200'
raise format('Failed to delete organization %s (HTTP response: %s/%s)', resource[:name], response.code, response.body)
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet/type/grafana_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def should_to_s(value)
end
end

newproperty(:organization) do
newparam(:organization) do
desc 'The organization name to create the datasource on'
defaultto '1'
defaultto 1
end

# rubocop:disable Style/SignalException
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet/type/grafana_datasource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
newvalues(:influxdb, :elasticsearch, :graphite, :kairosdb, :opentsdb, :prometheus)
end

newproperty(:organization) do
newparam(:organization) do
desc 'The organization name to create the datasource on'
defaultto '1'
defaultto 1
end

newproperty(:user) do
Expand Down

0 comments on commit 39c236a

Please sign in to comment.