Skip to content

Commit

Permalink
Rubocop autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
root-expert committed Nov 30, 2021
1 parent 6fd49dc commit 771a032
Show file tree
Hide file tree
Showing 50 changed files with 362 additions and 375 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,3 +1,5 @@
# frozen_string_literal: true

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

# == Function get_sub_paths
#
# This function receives an input path as an input parameter, and
Expand Down Expand Up @@ -28,10 +30,11 @@ 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: 6 additions & 8 deletions lib/puppet/provider/grafana.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

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

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

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

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

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

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,3 +1,5 @@
# 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 @@ -36,9 +38,7 @@ def exists?
success = validator.attempt_connection
end

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

success
end
Expand Down
51 changes: 20 additions & 31 deletions lib/puppet/provider/grafana_dashboard/grafana.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# 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 @@ -22,18 +24,14 @@ def grafana_api_path

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
raise format('Fail to retrieve organizations (HTTP response: %s/%s)', response.code, response.body) if response.code != '200'

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

fetch_organization = JSON.parse(response.body)

Expand All @@ -48,22 +46,17 @@ def fetch_organizations
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 ||= 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
@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
raise format('Fail to retrieve the folders (HTTP response: %s/%s)', response.code, response.body) if response.code != '200'

begin
@folders = JSON.parse(response.body)
Expand All @@ -87,13 +80,10 @@ def find_folder
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
raise format('Failed to switch to org %s (HTTP response: %s/%s)', fetch_organization[:id], response.code, response.body) unless response.code == '200'

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
raise format('Fail to retrieve the dashboards (HTTP response: %s/%s)', response.code, response.body) if response.code != '200'

begin
JSON.parse(response.body)
Expand All @@ -109,9 +99,7 @@ def find_dashboard

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

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
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'

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

# 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
raise format('Failed to switch to org %s (HTTP response: %s/%s)', fetch_organization[:id], response.code, response.body) unless response.code == '200'

data = {
dashboard: dashboard.merge('title' => resource[:title],
Expand All @@ -141,11 +127,12 @@ 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 @@ -163,9 +150,11 @@ 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]
permission = case resource[:permission] # rubocop:disable Style/HashLikeCase
when :View
1
when :Edit
Expand Down Expand Up @@ -218,7 +218,7 @@ def existing_permissions
end
end

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

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

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

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
raise format('Fail to retrieve organizations (HTTP response: %s/%s)', response.code, response.body) if response.code != '200'

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

fetch_organization = JSON.parse(response.body)

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

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

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

datasource = JSON.parse(response.body)

Expand Down Expand Up @@ -95,9 +87,7 @@ def datasources
end

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

Expand Down Expand Up @@ -225,9 +215,7 @@ 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])
unless response.code == '200'
raise format('Failed to switch to org %s (HTTP response: %s/%s)', fetch_organization[:id], response.code, response.body)
end
raise format('Failed to switch to org %s (HTTP response: %s/%s)', fetch_organization[:id], response.code, response.body) unless response.code == '200'

data = {
name: resource[:name],
Expand All @@ -252,18 +240,16 @@ def save_datasource
data[:id] = datasource[:id]
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
raise format('Failed to create save %s (HTTP response: %s/%s)', resource[:name], response.code, response.body) if response.code != '200'

self.datasource = nil
end

def delete_datasource
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)
end
raise format('Failed to delete datasource %s (HTTP response: %s/%s', resource[:name], response.code, response.body) if response.code != '200'

self.datasource = nil
end

Expand Down
Loading

0 comments on commit 771a032

Please sign in to comment.