Skip to content

Commit

Permalink
Revert "rubocop: autofix"
Browse files Browse the repository at this point in the history
This reverts commit 70c2adc.
  • Loading branch information
root-expert committed Nov 30, 2021
1 parent 92e9097 commit 6fd49dc
Show file tree
Hide file tree
Showing 50 changed files with 410 additions and 397 deletions.
6 changes: 3 additions & 3 deletions lib/puppet/functions/grafana/deep_find_and_remove.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

# == Function: deep_find_and_remove
#
# This function takes a hash as input, along with a string
Expand Down Expand Up @@ -30,7 +28,9 @@ def deep_find_and_remove(key, object, removekey = 'puppetsource')
foundpaths << object[key].dup
object[key].delete(removekey)
end
foundpaths << object.map { |*a| deep_find_and_remove(key, a.last) } if object.is_a? Enumerable
if object.is_a? Enumerable
foundpaths << object.map { |*a| deep_find_and_remove(key, a.last) }
end
foundpaths.flatten.compact
foundpaths
end
Expand Down
7 changes: 2 additions & 5 deletions lib/puppet/functions/grafana/get_sub_paths.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

# == Function get_sub_paths
#
# This function receives an input path as an input parameter, and
Expand Down Expand Up @@ -30,11 +28,10 @@ def get_sub_paths(inputpath)
parts = ip.split('/')
parts.each_with_index do |value, index|
next if index.zero? || index == (parts.length - 1)

allsubs << if index == 1
"/#{value}"
'/' + value
else
"#{allsubs[index - 2]}/#{value}"
allsubs[index - 2] + '/' + value
end
end
allsubs
Expand Down
14 changes: 8 additions & 6 deletions lib/puppet/provider/grafana.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

# Copyright 2015 Mirantis, Inc.
#
require 'cgi'
Expand All @@ -9,17 +7,19 @@
class Puppet::Provider::Grafana < Puppet::Provider
# Helper methods
def grafana_host
@grafana_host ||= URI.parse(resource[:grafana_url]).host
@grafana_host = URI.parse(resource[:grafana_url]).host unless @grafana_host
@grafana_host
end

def grafana_port
@grafana_port ||= URI.parse(resource[:grafana_url]).port
@grafana_port = URI.parse(resource[:grafana_url]).port unless @grafana_port
@grafana_port
end

def grafana_scheme
@grafana_scheme ||= URI.parse(resource[:grafana_url]).scheme
unless @grafana_scheme
@grafana_scheme = URI.parse(resource[:grafana_url]).scheme
end
@grafana_scheme
end

Expand Down Expand Up @@ -59,7 +59,9 @@ def send_request(operation = 'GET', path = '', data = nil, search_path = {})
end

request.content_type = 'application/json'
request.basic_auth resource[:grafana_user], resource[:grafana_password] if resource[:grafana_user] && resource[:grafana_password]
if resource[:grafana_user] && resource[:grafana_password]
request.basic_auth resource[:grafana_user], resource[:grafana_password]
end

Net::HTTP.start(grafana_host, grafana_port,
use_ssl: grafana_scheme == 'https',
Expand Down
6 changes: 3 additions & 3 deletions lib/puppet/provider/grafana_conn_validator/net_http.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

# In this case I'm trying the relative path first, then falling back to normal
# mechanisms. This should be fixed in future versions of puppet but it looks
# like we'll need to maintain this for some time perhaps.
Expand Down Expand Up @@ -38,7 +36,9 @@ def exists?
success = validator.attempt_connection
end

Puppet.notice("Failed to connect to Grafana within timeout window of #{timeout} seconds; giving up.") unless success
unless success
Puppet.notice("Failed to connect to Grafana within timeout window of #{timeout} seconds; giving up.")
end

success
end
Expand Down
51 changes: 31 additions & 20 deletions lib/puppet/provider/grafana_dashboard/grafana.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# frozen_string_literal: true

# 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
# 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
Expand All @@ -24,14 +22,18 @@ def grafana_api_path

def fetch_organizations
response = send_request('GET', format('%s/orgs', resource[:grafana_api_path]))
raise format('Fail to retrieve organizations (HTTP response: %s/%s)', response.code, response.body) if response.code != '200'
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)
raise format('Failed to retrieve organization %d (HTTP response: %s/%s)', id, response.code, response.body) if response.code != '200'
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)

Expand All @@ -46,17 +48,22 @@ def fetch_organizations
end

def 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
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]))
raise format('Fail to retrieve the folders (HTTP response: %s/%s)', response.code, response.body) if response.code != '200'
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)
Expand All @@ -80,10 +87,13 @@ def find_folder
def dashboards
# change organizations
response = send_request 'POST', format('%s/user/using/%s', resource[:grafana_api_path], fetch_organization[:id])
raise format('Failed to switch to org %s (HTTP response: %s/%s)', fetch_organization[:id], response.code, response.body) unless response.code == '200'

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)
raise format('Fail to retrieve the dashboards (HTTP response: %s/%s)', response.code, response.body) if response.code != '200'
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)
Expand All @@ -99,7 +109,9 @@ def find_dashboard

response = send_request('GET', format('%s/dashboards/uid/%s', resource[:grafana_api_path], db['uid']))

raise format('Fail to retrieve dashboard %s by uid %s (HTTP response: %s/%s)', resource[:title], db['uid'], response.code, response.body) if response.code != '200'
if response.code != '200'
raise format('Fail to retrieve dashboard %s by uid %s (HTTP response: %s/%s)', resource[:title], db['uid'], response.code, response.body)
end

begin
# Cache the dashboard's content
Expand All @@ -114,7 +126,9 @@ def save_dashboard(dashboard)

# change organizations
response = send_request 'POST', format('%s/user/using/%s', resource[:grafana_api_path], fetch_organization[:id])
raise format('Failed to switch to org %s (HTTP response: %s/%s)', fetch_organization[:id], response.code, response.body) unless response.code == '200'
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],
Expand All @@ -127,12 +141,11 @@ def save_dashboard(dashboard)

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\- ]}, '')
resource[:title].downcase.gsub(%r{[ \+]+}, '-').gsub(%r{[^\w\- ]}, '')
end

def content
Expand All @@ -150,11 +163,9 @@ def create
def destroy
db = dashboards.find { |x| x['title'] == resource[:title] }
raise Puppet::Error, format('Failed to delete dashboard %s, dashboard not found', resource[:title]) if db.nil?

response = send_request('DELETE', format('%s/dashboards/uid/%s', resource[:grafana_api_path], db['uid']))

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

Expand Down
4 changes: 2 additions & 2 deletions lib/puppet/provider/grafana_dashboard_permission/grafana.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def permission=(value)
def new_permission
key = resource[:user] ? :userId : :teamId
subject_id = resource[:user] ? user[:id] : team[:id]
permission = case resource[:permission] # rubocop:disable Style/HashLikeCase
permission = case resource[:permission]
when :View
1
when :Edit
Expand Down Expand Up @@ -218,7 +218,7 @@ def existing_permissions
end
end

def permission_data(destroy = false) # rubocop:disable Style/OptionalBooleanParameter
def permission_data(destroy = false)
raise format('Unknown dashboard: %s', resource[:dashboard]) unless dashboard

endpoint = format('%s/dashboards/id/%s/permissions', grafana_api_path, dashboard[:id])
Expand Down
56 changes: 35 additions & 21 deletions lib/puppet/provider/grafana_datasource/grafana.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

# Copyright 2015 Mirantis, Inc.
#
require 'json'
Expand All @@ -21,13 +19,17 @@ def grafana_api_path

def fetch_organizations
response = send_request('GET', format('%s/orgs', resource[:grafana_api_path]))
raise format('Fail to retrieve organizations (HTTP response: %s/%s)', response.code, response.body) if response.code != '200'
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)
raise format('Failed to retrieve organization %d (HTTP response: %s/%s)', id, response.code, response.body) if response.code != '200'
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)

Expand All @@ -42,24 +44,30 @@ def fetch_organizations
end

def fetch_organization
@fetch_organization ||= if resource[:organization].is_a?(Numeric) || resource[:organization].match(%r{^[0-9]*$})
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 datasources
response = send_request('GET', format('%s/datasources', resource[:grafana_api_path]))
raise format('Fail to retrieve datasources (HTTP response: %s/%s)', response.code, response.body) if response.code != '200'
if response.code != '200'
raise format('Fail to retrieve datasources (HTTP response: %s/%s)', response.code, response.body)
end

begin
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)
raise format('Failed to retrieve datasource %d (HTTP response: %s/%s)', id, response.code, response.body) if response.code != '200'
if response.code != '200'
raise format('Failed to retrieve datasource %d (HTTP response: %s/%s)', id, response.code, response.body)
end

datasource = JSON.parse(response.body)

Expand All @@ -72,9 +80,9 @@ def datasources
password: datasource['password'],
database: datasource['database'],
access_mode: datasource['access'],
is_default: datasource['isDefault'] ? true : false,
with_credentials: datasource['withCredentials'] ? true : false,
basic_auth: datasource['basicAuth'] ? true : false,
is_default: datasource['isDefault'] ? :true : :false,
with_credentials: datasource['withCredentials'] ? :true : :false,
basic_auth: datasource['basicAuth'] ? :true : :false,
basic_auth_user: datasource['basicAuthUser'],
basic_auth_password: datasource['basicAuthPassword'],
json_data: datasource['jsonData'],
Expand All @@ -87,7 +95,9 @@ def datasources
end

def datasource
@datasource ||= datasources.find { |x| x[:name] == resource[:name] }
unless @datasource
@datasource = datasources.find { |x| x[:name] == resource[:name] }
end
@datasource
end

Expand Down Expand Up @@ -147,7 +157,7 @@ def password=(value)
save_datasource
end

# rubocop:disable Naming/PredicateName
# rubocop:disable Style/PredicateName
def is_default
datasource[:is_default]
end
Expand All @@ -156,7 +166,7 @@ def is_default=(value)
resource[:is_default] = value
save_datasource
end
# rubocop:enable Naming/PredicateName
# rubocop:enable Style/PredicateName

def basic_auth
datasource[:basic_auth]
Expand Down Expand Up @@ -215,7 +225,9 @@ def secure_json_data=(value)
def save_datasource
# change organizations
response = send_request 'POST', format('%s/user/using/%s', resource[:grafana_api_path], fetch_organization[:id])
raise format('Failed to switch to org %s (HTTP response: %s/%s)', fetch_organization[:id], response.code, response.body) unless response.code == '200'
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 = {
name: resource[:name],
Expand All @@ -225,11 +237,11 @@ def save_datasource
database: resource[:database],
user: resource[:user],
password: resource[:password],
isDefault: (resource[:is_default] == true),
basicAuth: (resource[:basic_auth] == true),
isDefault: (resource[:is_default] == :true),
basicAuth: (resource[:basic_auth] == :true),
basicAuthUser: resource[:basic_auth_user],
basicAuthPassword: resource[:basic_auth_password],
withCredentials: (resource[:with_credentials] == true),
withCredentials: (resource[:with_credentials] == :true),
jsonData: resource[:json_data],
secureJsonData: resource[:secure_json_data]
}
Expand All @@ -240,16 +252,18 @@ def save_datasource
data[:id] = datasource[:id]
response = send_request 'PUT', format('%s/datasources/%s', resource[:grafana_api_path], datasource[:id]), data
end
raise format('Failed to create save %s (HTTP response: %s/%s)', resource[:name], response.code, response.body) if response.code != '200'

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

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

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

Expand Down
Loading

0 comments on commit 6fd49dc

Please sign in to comment.