From 6fd49dc9e3127fea3028208252f5fb4c57995255 Mon Sep 17 00:00:00 2001 From: Christos Papageorgiou Date: Tue, 30 Nov 2021 15:52:16 +0200 Subject: [PATCH] Revert "rubocop: autofix" This reverts commit 70c2adc1abca80bf679f2cd87f72b34e578c912e. --- .../functions/grafana/deep_find_and_remove.rb | 6 +- lib/puppet/functions/grafana/get_sub_paths.rb | 7 +- lib/puppet/provider/grafana.rb | 14 +- .../grafana_conn_validator/net_http.rb | 6 +- .../provider/grafana_dashboard/grafana.rb | 51 ++++--- .../grafana_dashboard_permission/grafana.rb | 4 +- .../provider/grafana_datasource/grafana.rb | 56 ++++--- lib/puppet/provider/grafana_folder/grafana.rb | 52 ++++--- .../provider/grafana_notification/grafana.rb | 36 +++-- .../provider/grafana_organization/grafana.rb | 31 ++-- .../provider/grafana_plugin/grafana_cli.rb | 5 +- lib/puppet/provider/grafana_team/grafana.rb | 1 - lib/puppet/provider/grafana_user/grafana.rb | 36 +++-- lib/puppet/type/grafana_conn_validator.rb | 10 +- lib/puppet/type/grafana_dashboard.rb | 21 ++- lib/puppet/type/grafana_datasource.rb | 30 ++-- lib/puppet/type/grafana_folder.rb | 10 +- lib/puppet/type/grafana_ldap_config.rb | 40 +++-- lib/puppet/type/grafana_ldap_group_mapping.rb | 2 - lib/puppet/type/grafana_ldap_server.rb | 6 +- lib/puppet/type/grafana_notification.rb | 22 +-- lib/puppet/type/grafana_organization.rb | 14 +- lib/puppet/type/grafana_plugin.rb | 52 +++---- lib/puppet/type/grafana_team.rb | 8 +- lib/puppet/type/grafana_user.rb | 18 ++- lib/puppet/util/grafana_conn_validator.rb | 9 +- spec/acceptance/class_spec.rb | 3 - spec/acceptance/grafana_folder_spec.rb | 6 - spec/acceptance/grafana_plugin_spec.rb | 2 - spec/acceptance/grafana_team_spec.rb | 8 +- spec/acceptance/grafana_user_spec.rb | 3 - spec/classes/grafana_spec.rb | 137 +++++++++--------- .../grafana_dashboard_permission_type_spec.rb | 4 +- spec/grafana_membership_type_spec.rb | 4 +- spec/spec_helper.rb | 10 +- spec/spec_helper_acceptance.rb | 2 - spec/support/acceptance/prepare_host.rb | 2 - spec/support/acceptance/supported_versions.rb | 2 - .../grafana_plugin/grafana_cli_spec.rb | 20 +-- .../type/grafana_dashboard_type_spec.rb | 6 - .../type/grafana_datasource_type_spec.rb | 10 +- .../puppet/type/grafana_folder_type_spec.rb | 6 - .../puppet/type/grafana_ldap_config_spec.rb | 3 - .../type/grafana_ldap_group_mapping_spec.rb | 3 - .../puppet/type/grafana_ldap_server_spec.rb | 3 - .../type/grafana_notification_type_spec.rb | 8 +- .../type/grafana_organization_type_spec.rb | 4 +- spec/unit/puppet/type/grafana_plugin_spec.rb | 4 - .../puppet/type/grafana_team_type_spec.rb | 4 +- .../puppet/type/grafana_user_type_spec.rb | 6 - 50 files changed, 410 insertions(+), 397 deletions(-) diff --git a/lib/puppet/functions/grafana/deep_find_and_remove.rb b/lib/puppet/functions/grafana/deep_find_and_remove.rb index 20a1c2da7..6ff436c9a 100644 --- a/lib/puppet/functions/grafana/deep_find_and_remove.rb +++ b/lib/puppet/functions/grafana/deep_find_and_remove.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # == Function: deep_find_and_remove # # This function takes a hash as input, along with a string @@ -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 diff --git a/lib/puppet/functions/grafana/get_sub_paths.rb b/lib/puppet/functions/grafana/get_sub_paths.rb index 78e0c7eaf..b2ff27f2c 100644 --- a/lib/puppet/functions/grafana/get_sub_paths.rb +++ b/lib/puppet/functions/grafana/get_sub_paths.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # == Function get_sub_paths # # This function receives an input path as an input parameter, and @@ -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 diff --git a/lib/puppet/provider/grafana.rb b/lib/puppet/provider/grafana.rb index c0c644415..3159dc5e3 100644 --- a/lib/puppet/provider/grafana.rb +++ b/lib/puppet/provider/grafana.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # Copyright 2015 Mirantis, Inc. # require 'cgi' @@ -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 @@ -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', diff --git a/lib/puppet/provider/grafana_conn_validator/net_http.rb b/lib/puppet/provider/grafana_conn_validator/net_http.rb index 6cfafc778..7799e840c 100644 --- a/lib/puppet/provider/grafana_conn_validator/net_http.rb +++ b/lib/puppet/provider/grafana_conn_validator/net_http.rb @@ -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. @@ -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 diff --git a/lib/puppet/provider/grafana_dashboard/grafana.rb b/lib/puppet/provider/grafana_dashboard/grafana.rb index cddeb1e8a..d01e89226 100644 --- a/lib/puppet/provider/grafana_dashboard/grafana.rb +++ b/lib/puppet/provider/grafana_dashboard/grafana.rb @@ -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 @@ -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) @@ -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) @@ -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) @@ -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 @@ -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], @@ -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 @@ -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 diff --git a/lib/puppet/provider/grafana_dashboard_permission/grafana.rb b/lib/puppet/provider/grafana_dashboard_permission/grafana.rb index 829631af1..0e1be74df 100644 --- a/lib/puppet/provider/grafana_dashboard_permission/grafana.rb +++ b/lib/puppet/provider/grafana_dashboard_permission/grafana.rb @@ -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 @@ -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]) diff --git a/lib/puppet/provider/grafana_datasource/grafana.rb b/lib/puppet/provider/grafana_datasource/grafana.rb index d94a8b127..6688b30d2 100644 --- a/lib/puppet/provider/grafana_datasource/grafana.rb +++ b/lib/puppet/provider/grafana_datasource/grafana.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # Copyright 2015 Mirantis, Inc. # require 'json' @@ -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) @@ -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) @@ -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'], @@ -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 @@ -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 @@ -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] @@ -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], @@ -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] } @@ -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 diff --git a/lib/puppet/provider/grafana_folder/grafana.rb b/lib/puppet/provider/grafana_folder/grafana.rb index 7c5acbf89..6e1f81d24 100644 --- a/lib/puppet/provider/grafana_folder/grafana.rb +++ b/lib/puppet/provider/grafana_folder/grafana.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'json' require File.expand_path(File.join(File.dirname(__FILE__), '..', 'grafana')) @@ -21,8 +19,9 @@ def permissions find_folder unless @folder response = send_request('GET', format('%s/folders/%s/permissions', resource[:grafana_api_path], @folder['uid'])) - raise format('Failed to retrieve permissions %s (HTTP response: %s/%s)', resource[:title], response.code, response.body) if response.code != '200' - + if response.code != '200' + raise format('Failed to retrieve permissions %s (HTTP response: %s/%s)', resource[:title], response.code, response.body) + end begin permissions = JSON.parse(response.body) rescue JSON::ParserError @@ -47,14 +46,18 @@ def permissions=(value) 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) @@ -69,17 +72,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) @@ -98,7 +106,9 @@ def find_folder def save_folder(folder) 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 # if folder exists, update object based on uid # else, create object @@ -109,8 +119,9 @@ def save_folder(folder) } response = send_request('POST', format('%s/folders', resource[:grafana_api_path]), data) - raise format('Failed to create folder %s (HTTP response: %s/%s)', resource[:title], response.code, response.body) if (response.code != '200') && (response.code != '412') - + if (response.code != '200') && (response.code != '412') + raise format('Failed to create folder %s (HTTP response: %s/%s)', resource[:title], response.code, response.body) + end folders find_folder save_permissions(resource[:permissions]) @@ -123,27 +134,25 @@ def save_folder(folder) response = send_request('POST', format('%s/folders/%s', resource[:grafana_api_path], @folder['uid']), data) return unless (response.code != '200') && (response.code != '412') - raise format('Failed to update folder %s (HTTP response: %s/%s)', resource[:title], response.code, response.body) end end def save_permissions(value) return if value.nil? - 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 find_folder unless @folder data = { 'items' => value } response = send_request 'POST', format('%s/folders/%s/permissions', resource[:grafana_api_path], @folder['uid']), data return if response.code == '200' - raise format('Failed to update permissions %s (HTTP response: %s/%s)', fetch_organization[:id], 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 create @@ -156,7 +165,6 @@ def destroy response = send_request('DELETE', format('%s/folders/%s', resource[:grafana_api_path], @folder['uid'])) return unless response.code != '200' - raise Puppet::Error, format('Failed to delete folder %s (HTTP response: %s/%s)', resource[:title], response.code, response.body) end diff --git a/lib/puppet/provider/grafana_notification/grafana.rb b/lib/puppet/provider/grafana_notification/grafana.rb index 471495102..7777dfaa6 100644 --- a/lib/puppet/provider/grafana_notification/grafana.rb +++ b/lib/puppet/provider/grafana_notification/grafana.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # Copyright 2015 Mirantis, Inc. # require 'json' @@ -17,14 +15,18 @@ def grafana_api_path def notifications response = send_request('GET', format('%s/alert-notifications', resource[:grafana_api_path])) - raise format('Fail to retrieve notifications (HTTP response: %s/%s)', response.code, response.body) if response.code != '200' + if response.code != '200' + raise format('Fail to retrieve notifications (HTTP response: %s/%s)', response.code, response.body) + end begin notifications = JSON.parse(response.body) notifications.map { |x| x['id'] }.map do |id| response = send_request 'GET', format('%s/alert-notifications/%s', resource[:grafana_api_path], id) - raise format('Failed to retrieve notification %d (HTTP response: %s/%s)', id, response.code, response.body) if response.code != '200' + if response.code != '200' + raise format('Failed to retrieve notification %d (HTTP response: %s/%s)', id, response.code, response.body) + end notification = JSON.parse(response.body) @@ -32,8 +34,8 @@ def notifications id: notification['id'], name: notification['name'], type: notification['type'], - is_default: notification['isDefault'] ? true : false, - send_reminder: notification['sendReminder'] ? true : false, + is_default: notification['isDefault'] ? :true : :false, + send_reminder: notification['sendReminder'] ? :true : :false, frequency: notification['frequency'], settings: notification['settings'] } @@ -44,7 +46,9 @@ def notifications end def notification - @notification ||= notifications.find { |x| x[:name] == resource[:name] } + unless @notification + @notification = notifications.find { |x| x[:name] == resource[:name] } + end @notification end @@ -59,7 +63,7 @@ def type=(value) save_notification end - # rubocop:disable Naming/PredicateName + # rubocop:disable Style/PredicateName def is_default notification[:is_default] end @@ -77,7 +81,7 @@ def send_reminder=(value) resource[:send_reminder] = value save_notification end - # rubocop:enable Naming/PredicateName + # rubocop:enable Style/PredicateName def frequency notification[:frequency] @@ -101,8 +105,8 @@ def save_notification data = { name: resource[:name], type: resource[:type], - isDefault: (resource[:is_default] == true), - sendReminder: (resource[:send_reminder] == true), + isDefault: (resource[:is_default] == :true), + sendReminder: (resource[:send_reminder] == :true), frequency: resource[:frequency], settings: resource[:settings] } @@ -113,16 +117,18 @@ def save_notification data[:id] = notification[:id] response = send_request 'PUT', format('%s/alert-notifications/%s', resource[:grafana_api_path], notification[: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.notification = nil end def delete_notification response = send_request 'DELETE', format('%s/alert-notifications/%s', resource[:grafana_api_path], notification[:id]) - raise format('Failed to delete notification %s (HTTP response: %s/%s', resource[:name], response.code, response.body) if response.code != '200' - + if response.code != '200' + raise format('Failed to delete notification %s (HTTP response: %s/%s', resource[:name], response.code, response.body) + end self.notification = nil end diff --git a/lib/puppet/provider/grafana_organization/grafana.rb b/lib/puppet/provider/grafana_organization/grafana.rb index 4b0377c84..9cf02395f 100644 --- a/lib/puppet/provider/grafana_organization/grafana.rb +++ b/lib/puppet/provider/grafana_organization/grafana.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'json' require File.expand_path(File.join(File.dirname(__FILE__), '..', 'grafana')) @@ -11,14 +9,18 @@ def organizations response = send_request('GET', format('%s/orgs', resource[:grafana_api_path])) - raise format('Failed to retrieve organizations (HTTP response: %s/%s)', response.code, response.body) if response.code != '200' + if response.code != '200' + raise format('Failed to retrieve organizations (HTTP response: %s/%s)', response.code, response.body) + end begin organizations = JSON.parse(response.body) 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 organization = JSON.parse(response.body) @@ -34,12 +36,16 @@ def organizations end def organization - @organization ||= organizations.find { |x| x[:name] == resource[:name] } + unless @organization + @organization = organizations.find { |x| x[:name] == resource[:name] } + end @organization end attr_writer :organization + # rubocop:enable Style/PredicateName + def id organization[:id] end @@ -67,20 +73,23 @@ def save_organization response = send_request('POST', format('%s/orgs', resource[:grafana_api_path]), data) if organization.nil? - 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.organization = nil end def delete_organization response = send_request 'DELETE', format('%s/orgs/%s', resource[:grafana_api_path], organization[:id]) - raise format('Failed to delete organization %s (HTTP response: %s/%s)', resource[:name], response.code, response.body) if response.code != '200' - + if response.code != '200' + raise format('Failed to delete organization %s (HTTP response: %s/%s)', resource[:name], response.code, response.body) + end # change back to default organization response = send_request 'POST', format('%s/user/using/1', resource[:grafana_api_path]) - 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 self.organization = nil end diff --git a/lib/puppet/provider/grafana_plugin/grafana_cli.rb b/lib/puppet/provider/grafana_plugin/grafana_cli.rb index 34df6bcc9..c84e54707 100644 --- a/lib/puppet/provider/grafana_plugin/grafana_cli.rb +++ b/lib/puppet/provider/grafana_plugin/grafana_cli.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - Puppet::Type.type(:grafana_plugin).provide(:grafana_cli) do has_command(:grafana_cli, 'grafana-cli') do is_optional @@ -13,7 +11,6 @@ def self.all_plugins plugins = [] grafana_cli('plugins', 'ls').split(%r{\n}).each do |line| next unless line =~ %r{^(\S+)\s+@\s+((?:\d\.).+)\s*$} - name = Regexp.last_match(1) version = Regexp.last_match(2) Puppet.debug("Found grafana plugin #{name} #{version}") @@ -36,7 +33,7 @@ def self.instances def self.prefetch(resources) plugins = instances - resources.each_key do |name| + resources.keys.each do |name| if (provider = plugins.find { |plugin| plugin.name == name }) resources[name].provider = provider end diff --git a/lib/puppet/provider/grafana_team/grafana.rb b/lib/puppet/provider/grafana_team/grafana.rb index 824e9c9a3..223ef73ea 100644 --- a/lib/puppet/provider/grafana_team/grafana.rb +++ b/lib/puppet/provider/grafana_team/grafana.rb @@ -213,7 +213,6 @@ def get_dashboard_folder_id(ident) dashboard = parse_response(response.body) return nil unless dashboard.first - dashboard.first['id'] end diff --git a/lib/puppet/provider/grafana_user/grafana.rb b/lib/puppet/provider/grafana_user/grafana.rb index 5892d5e52..9f01e1fb3 100644 --- a/lib/puppet/provider/grafana_user/grafana.rb +++ b/lib/puppet/provider/grafana_user/grafana.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'json' require File.expand_path(File.join(File.dirname(__FILE__), '..', 'grafana')) @@ -11,14 +9,18 @@ def users response = send_request('GET', format('%s/users', resource[:grafana_api_path])) - raise format('Fail to retrieve users (HTTP response: %s/%s)', response.code, response.body) if response.code != '200' + if response.code != '200' + raise format('Fail to retrieve users (HTTP response: %s/%s)', response.code, response.body) + end begin users = JSON.parse(response.body) users.map { |x| x['id'] }.map do |id| response = send_request('GET', format('%s/users/%s', resource[:grafana_api_path], id)) - raise format('Fail to retrieve user %d (HTTP response: %s/%s)', id, response.code, response.body) if response.code != '200' + if response.code != '200' + raise format('Fail to retrieve user %d (HTTP response: %s/%s)', id, response.code, response.body) + end user = JSON.parse(response.body) { @@ -28,7 +30,7 @@ def users email: user['email'], theme: user['theme'], password: nil, - is_admin: user['isGrafanaAdmin'] ? true : false + is_admin: user['isGrafanaAdmin'] ? :true : :false } end rescue JSON::ParserError @@ -37,7 +39,7 @@ def users end def user - @user ||= users.find { |x| x[:name] == resource[:name] } + @user = users.find { |x| x[:name] == resource[:name] } unless @user @user end @@ -88,7 +90,7 @@ def password=(value) save_user end - # rubocop:disable Naming/PredicateName + # rubocop:disable Style/PredicateName def is_admin user[:is_admin] end @@ -97,7 +99,7 @@ def is_admin=(value) resource[:is_admin] = value save_user end - # rubocop:enable Naming/PredicateName + # rubocop:enable Style/PredicateName def save_user data = { @@ -106,7 +108,7 @@ def save_user email: resource[:email], password: resource[:password], theme: resource[:theme], - isGrafanaAdmin: (resource[:is_admin] == true) + isGrafanaAdmin: (resource[:is_admin] == :true) } if user.nil? @@ -118,8 +120,9 @@ def save_user response = send_request('PUT', format('%s/users/%s', resource[:grafana_api_path], user[:id]), data) end - raise format('Failed to create user %s (HTTP response: %s/%s)', resource[:name], response.code, response.body) if response.code != '200' - + if response.code != '200' + raise format('Failed to create user %s (HTTP response: %s/%s)', resource[:name], response.code, response.body) + end self.user = nil end @@ -134,14 +137,19 @@ def check_password http.request(request) end - response.code == '200' + if response.code == '200' + true + else + false + end end def delete_user response = send_request('DELETE', format('%s/admin/users/%s', resource[:grafana_api_path], user[:id])) - raise format('Failed to delete user %s (HTTP response: %s/%s', resource[:name], response.code, response.body) if response.code != '200' - + if response.code != '200' + raise format('Failed to delete user %s (HTTP response: %s/%s', resource[:name], response.code, response.body) + end self.user = nil end diff --git a/lib/puppet/type/grafana_conn_validator.rb b/lib/puppet/type/grafana_conn_validator.rb index cfed80ffa..7c659ed55 100644 --- a/lib/puppet/type/grafana_conn_validator.rb +++ b/lib/puppet/type/grafana_conn_validator.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - Puppet::Type.newtype(:grafana_conn_validator) do desc <<-DESC Verify connectivity to the Grafana API @@ -16,7 +14,9 @@ defaultto 'http://localhost:3000' validate do |value| - raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} + unless value =~ %r{^https?://} + raise ArgumentError, format('%s is not a valid URL', value) + end end end @@ -25,7 +25,9 @@ defaultto '/api/health' validate do |value| - raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api/.*$} + unless value =~ %r{^/.*/?api/.*$} + raise ArgumentError, format('%s is not a valid API path', value) + end end end diff --git a/lib/puppet/type/grafana_dashboard.rb b/lib/puppet/type/grafana_dashboard.rb index 87226d68c..b6cd648ad 100644 --- a/lib/puppet/type/grafana_dashboard.rb +++ b/lib/puppet/type/grafana_dashboard.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # Copyright 2015 Mirantis, Inc. # require 'json' @@ -21,9 +19,11 @@ desc 'The JSON representation of the dashboard.' validate do |value| - JSON.parse(value) - rescue JSON::ParserError - raise ArgumentError, 'Invalid JSON string for content' + begin + JSON.parse(value) + rescue JSON::ParserError + raise ArgumentError, 'Invalid JSON string for content' + end end munge do |value| @@ -45,7 +45,9 @@ def should_to_s(value) defaultto '' validate do |value| - raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} + unless value =~ %r{^https?://} + raise ArgumentError, format('%s is not a valid URL', value) + end end end @@ -62,7 +64,9 @@ def should_to_s(value) defaultto '/api' validate do |value| - raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} + unless value =~ %r{^/.*/?api$} + raise ArgumentError, format('%s is not a valid API path', value) + end end end @@ -71,8 +75,9 @@ def should_to_s(value) defaultto 1 end + # rubocop:disable Style/SignalException validate do - fail('content is required when ensure is present') if self[:ensure] == :present && self[:content].nil? # rubocop:disable Style/SignalException + fail('content is required when ensure is present') if self[:ensure] == :present && self[:content].nil? end autorequire(:service) do 'grafana-server' diff --git a/lib/puppet/type/grafana_datasource.rb b/lib/puppet/type/grafana_datasource.rb index 6a76b330c..7df69a19e 100644 --- a/lib/puppet/type/grafana_datasource.rb +++ b/lib/puppet/type/grafana_datasource.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # Copyright 2015 Mirantis, Inc. # Puppet::Type.newtype(:grafana_datasource) do @@ -16,7 +14,9 @@ defaultto '/api' validate do |value| - raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} + unless value =~ %r{^/.*/?api$} + raise ArgumentError, format('%s is not a valid API path', value) + end end end @@ -25,7 +25,9 @@ defaultto '' validate do |value| - raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} + unless value =~ %r{^https?://} + raise ArgumentError, format('%s is not a valid URL', value) + end end end @@ -71,14 +73,14 @@ newproperty(:is_default) do desc 'Whether the datasource is the default one' - newvalues(true, false) - defaultto false + newvalues(:true, :false) + defaultto :false end newproperty(:basic_auth) do desc 'Whether basic auth is enabled or not' - newvalues(true, false) - defaultto false + newvalues(:true, :false) + defaultto :false end newproperty(:basic_auth_user) do @@ -93,15 +95,17 @@ newproperty(:with_credentials) do desc 'Whether credentials such as cookies or auth headers should be sent with cross-site requests' - newvalues(true, false) - defaultto false + newvalues(:true, :false) + defaultto :false end newproperty(:json_data) do desc 'Additional JSON data to configure the datasource (optional)' validate do |value| - raise ArgumentError, 'json_data should be a Hash!' unless value.nil? || value.is_a?(Hash) + unless value.nil? || value.is_a?(Hash) + raise ArgumentError, 'json_data should be a Hash!' + end end end @@ -110,7 +114,9 @@ sensitive true validate do |value| - raise ArgumentError, 'secure_json_data should be a Hash!' unless value.nil? || value.is_a?(Hash) + unless value.nil? || value.is_a?(Hash) + raise ArgumentError, 'secure_json_data should be a Hash!' + end end end diff --git a/lib/puppet/type/grafana_folder.rb b/lib/puppet/type/grafana_folder.rb index 97a97d5b7..e8325a9ed 100644 --- a/lib/puppet/type/grafana_folder.rb +++ b/lib/puppet/type/grafana_folder.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'json' Puppet::Type.newtype(:grafana_folder) do @@ -20,7 +18,9 @@ defaultto '' validate do |value| - raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} + unless value =~ %r{^https?://} + raise ArgumentError, format('%s is not a valid URL', value) + end end end @@ -37,7 +37,9 @@ defaultto '/api' validate do |value| - raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} + unless value =~ %r{^/.*/?api$} + raise ArgumentError, format('%s is not a valid API path', value) + end end end diff --git a/lib/puppet/type/grafana_ldap_config.rb b/lib/puppet/type/grafana_ldap_config.rb index ee52f46d8..23b4f04e7 100644 --- a/lib/puppet/type/grafana_ldap_config.rb +++ b/lib/puppet/type/grafana_ldap_config.rb @@ -1,21 +1,19 @@ -# frozen_string_literal: true - require 'toml' require 'puppet/parameter/boolean' Puppet::Type.newtype(:grafana_ldap_config) do @doc = 'Manage Grafana LDAP configuration' - @toml_header = <<~EOF - # - # Grafana LDAP configuration - # - # generated by Puppet module puppet-grafana - # https://github.com/voxpupuli/puppet-grafana - # - # *** Edit at your own peril *** - # - # ############################################# # - EOF + @toml_header = <<-EOF +# +# Grafana LDAP configuration +# +# generated by Puppet module puppet-grafana +# https://github.com/voxpupuli/puppet-grafana +# +# *** Edit at your own peril *** +# +# ############################################# # +EOF # currently not ensurable as we are not parsing the LDAP toml config. # ensurable @@ -96,7 +94,7 @@ def should_content ldap_servers.each do |server_k, server_v| # convert symbols to strings - server_params = server_v.original_parameters.transform_keys(&:to_s) + server_params = Hash[server_v.original_parameters.map { |k, v| [k.to_s, v] }] server_attributes = server_params['attributes'] server_params.delete('attributes') @@ -108,8 +106,8 @@ def should_content server_group_mappings = server_v.group_mappings server_block = { - 'servers' => [server_params], - 'servers.attributes' => server_attributes, + 'servers' => [server_params], + 'servers.attributes' => server_attributes, 'servers.group_mappings' => server_group_mappings }.compact @@ -166,11 +164,11 @@ def eval_generate toml_contents << ldap_servers.map do |k, v| str = [] str << "\n\n" - str << <<~EOF - # - # #{k} - # - EOF + str << <<-EOF +# +# #{k} +# +EOF str << TOML::Generator.new(v).body str.join end diff --git a/lib/puppet/type/grafana_ldap_group_mapping.rb b/lib/puppet/type/grafana_ldap_group_mapping.rb index 5d5ea8f66..bcf5934c8 100644 --- a/lib/puppet/type/grafana_ldap_group_mapping.rb +++ b/lib/puppet/type/grafana_ldap_group_mapping.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'puppet/parameter/boolean' Puppet::Type.newtype(:grafana_ldap_group_mapping) do diff --git a/lib/puppet/type/grafana_ldap_server.rb b/lib/puppet/type/grafana_ldap_server.rb index 8bd1168f4..c97e602e4 100644 --- a/lib/puppet/type/grafana_ldap_server.rb +++ b/lib/puppet/type/grafana_ldap_server.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'puppet/parameter/boolean' Puppet::Type.newtype(:grafana_ldap_server) do @@ -156,7 +154,7 @@ value.each { |k, v| raise ArgumentError, _('attributes hash keys and values must be Strings') unless k.is_a?(String) && v.is_a?(String) } - raise ArgumentError, _("attributes contains an unknown key, allowed: #{valid_attributes.join(', ')}") if value.keys.reject { |key| valid_attributes.include?(key) }.count.positive? + raise ArgumentError, _("attributes contains an unknown key, allowed: #{valid_attributes.join(', ')}") if value.keys.reject { |key| valid_attributes.include?(key) }.count > 0 end end @@ -170,7 +168,7 @@ def group_mappings next unless resource.is_a?(Puppet::Type.type(:grafana_ldap_group_mapping)) next unless resource[:ldap_server_name] == self[:name] - group_mapping = resource.original_parameters.transform_keys(&:to_s) + group_mapping = Hash[resource.original_parameters.map { |k, v| [k.to_s, v] }] group_mapping.delete('ldap_server_name') group_mapping diff --git a/lib/puppet/type/grafana_notification.rb b/lib/puppet/type/grafana_notification.rb index f50a92739..9cad69f30 100644 --- a/lib/puppet/type/grafana_notification.rb +++ b/lib/puppet/type/grafana_notification.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # Copyright 2015 Mirantis, Inc. # Puppet::Type.newtype(:grafana_notification) do @@ -16,7 +14,9 @@ defaultto '/api' validate do |value| - raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} + unless value =~ %r{^/.*/?api$} + raise ArgumentError, format('%s is not a valid API path', value) + end end end @@ -25,7 +25,9 @@ defaultto '' validate do |value| - raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} + unless value =~ %r{^https?://} + raise ArgumentError, format('%s is not a valid URL', value) + end end end @@ -43,14 +45,14 @@ newproperty(:is_default) do desc 'Whether the notification is the default one' - newvalues(true, false) - defaultto false + newvalues(:true, :false) + defaultto :false end newproperty(:send_reminder) do desc 'Whether automatic message resending is enabled or not' - newvalues(true, false) - defaultto false + newvalues(:true, :false) + defaultto :false end newproperty(:frequency) do @@ -61,7 +63,9 @@ desc 'Additional JSON data to configure the notification' validate do |value| - raise ArgumentError, 'settings should be a Hash!' unless value.nil? || value.is_a?(Hash) + unless value.nil? || value.is_a?(Hash) + raise ArgumentError, 'settings should be a Hash!' + end end end diff --git a/lib/puppet/type/grafana_organization.rb b/lib/puppet/type/grafana_organization.rb index cf8fbd215..146a76517 100644 --- a/lib/puppet/type/grafana_organization.rb +++ b/lib/puppet/type/grafana_organization.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - Puppet::Type.newtype(:grafana_organization) do @doc = 'Manage organizations in Grafana' @@ -18,7 +16,9 @@ defaultto '/api' validate do |value| - raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} + unless value =~ %r{^/.*/?api$} + raise ArgumentError, format('%s is not a valid API path', value) + end end end @@ -27,7 +27,9 @@ defaultto '' validate do |value| - raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} + unless value =~ %r{^https?://} + raise ArgumentError, format('%s is not a valid URL', value) + end end end @@ -47,7 +49,9 @@ desc 'Additional JSON data to configure the organization address (optional)' validate do |value| - raise ArgumentError, 'address should be a Hash!' unless value.nil? || value.is_a?(Hash) + unless value.nil? || value.is_a?(Hash) + raise ArgumentError, 'address should be a Hash!' + end end end autorequire(:service) do diff --git a/lib/puppet/type/grafana_plugin.rb b/lib/puppet/type/grafana_plugin.rb index 9e9701980..6e9280109 100644 --- a/lib/puppet/type/grafana_plugin.rb +++ b/lib/puppet/type/grafana_plugin.rb @@ -1,32 +1,30 @@ -# frozen_string_literal: true - Puppet::Type.newtype(:grafana_plugin) do - desc <<~DESC - manages grafana plugins + desc <<-DESC +manages grafana plugins - @example Install a grafana plugin - grafana_plugin { 'grafana-simple-json-datasource': } +@example Install a grafana plugin + grafana_plugin { 'grafana-simple-json-datasource': } - @example Install a grafana plugin from different repo - grafana_plugin { 'grafana-simple-json-datasource': - ensure => 'present', - repo => 'https://nexus.company.com/grafana/plugins', - } +@example Install a grafana plugin from different repo + grafana_plugin { 'grafana-simple-json-datasource': + ensure => 'present', + repo => 'https://nexus.company.com/grafana/plugins', + } - @example Install a grafana plugin from a plugin url - grafana_plugin { 'grafana-example-custom-plugin': - ensure => 'present', - plugin_url => 'https://github.com/example/example-custom-plugin/zipball/v1.0.0' - } +@example Install a grafana plugin from a plugin url + grafana_plugin { 'grafana-example-custom-plugin': + ensure => 'present', + plugin_url => 'https://github.com/example/example-custom-plugin/zipball/v1.0.0' + } - @example Uninstall a grafana plugin - grafana_plugin { 'grafana-simple-json-datasource': - ensure => 'absent', - } +@example Uninstall a grafana plugin + grafana_plugin { 'grafana-simple-json-datasource': + ensure => 'absent', + } - @example Show resources - $ puppet resource grafana_plugin - DESC +@example Show resources + $ puppet resource grafana_plugin +DESC ensurable do defaultto(:present) @@ -46,14 +44,18 @@ newparam(:repo) do desc 'The URL of an internal plugin server' validate do |value| - raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} + unless value =~ %r{^https?://} + raise ArgumentError, format('%s is not a valid URL', value) + end end end newparam(:plugin_url) do desc 'Full url to the plugin zip file' validate do |value| - raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} + unless value =~ %r{^https?://} + raise ArgumentError, format('%s is not a valid URL', value) + end end end end diff --git a/lib/puppet/type/grafana_team.rb b/lib/puppet/type/grafana_team.rb index ac9bb5878..18209e87d 100644 --- a/lib/puppet/type/grafana_team.rb +++ b/lib/puppet/type/grafana_team.rb @@ -14,7 +14,9 @@ defaultto '/api' validate do |value| - raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} + unless value =~ %r{^/.*/?api$} + raise ArgumentError, format('%s is not a valid API path', value) + end end end @@ -23,7 +25,9 @@ defaultto '' validate do |value| - raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} + unless value =~ %r{^https?://} + raise ArgumentError, format('%s is not a valid URL', value) + end end end diff --git a/lib/puppet/type/grafana_user.rb b/lib/puppet/type/grafana_user.rb index 47b6d7c92..53beb6c33 100644 --- a/lib/puppet/type/grafana_user.rb +++ b/lib/puppet/type/grafana_user.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - Puppet::Type.newtype(:grafana_user) do @doc = 'Manage users in Grafana' @@ -14,7 +12,9 @@ defaultto '/api' validate do |value| - raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} + unless value =~ %r{^/.*/?api$} + raise ArgumentError, format('%s is not a valid API path', value) + end end end @@ -23,7 +23,9 @@ defaultto '' validate do |value| - raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} + unless value =~ %r{^https?://} + raise ArgumentError, format('%s is not a valid URL', value) + end end end @@ -41,7 +43,7 @@ newproperty(:password) do desc 'The password for the user' - def insync?(_is) # rubocop:disable Naming/MethodParameterName + def insync?(_is) provider.check_password end end @@ -56,11 +58,11 @@ def insync?(_is) # rubocop:disable Naming/MethodParameterName newproperty(:is_admin) do desc 'Whether the user is a grafana admin' - newvalues(true, false) - defaultto false + newvalues(:true, :false) + defaultto :false end - def set_sensitive_parameters(sensitive_parameters) # rubocop:disable Naming/AccessorMethodName + def set_sensitive_parameters(sensitive_parameters) # rubocop:disable Style/AccessorMethodName parameter(:password).sensitive = true if parameter(:password) super(sensitive_parameters) end diff --git a/lib/puppet/util/grafana_conn_validator.rb b/lib/puppet/util/grafana_conn_validator.rb index 129b86d6c..2f8e97e01 100644 --- a/lib/puppet/util/grafana_conn_validator.rb +++ b/lib/puppet/util/grafana_conn_validator.rb @@ -1,12 +1,11 @@ -# frozen_string_literal: true - require 'net/http' module Puppet module Util # Validator class, for testing that Grafana is alive class GrafanaConnValidator - attr_reader :grafana_url, :grafana_api_path + attr_reader :grafana_url + attr_reader :grafana_api_path def initialize(grafana_url, grafana_api_path) @grafana_url = grafana_url @@ -36,10 +35,10 @@ def attempt_connection Puppet.notice "Unable to connect to Grafana server (#{grafana_scheme}://#{grafana_host}:#{grafana_port}): [#{response.code}] #{response.msg}" return false end - true + return true rescue Exception => e # rubocop:disable Lint/RescueException Puppet.notice "Unable to connect to Grafana server (#{grafana_scheme}://#{grafana_host}:#{grafana_port}): #{e.message}" - false + return false end end end diff --git a/spec/acceptance/class_spec.rb b/spec/acceptance/class_spec.rb index 7f127d71f..57dd24394 100644 --- a/spec/acceptance/class_spec.rb +++ b/spec/acceptance/class_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'spec_helper_acceptance' supported_versions.each do |grafana_version| @@ -11,7 +9,6 @@ install_module_from_forge('puppetlabs/apt', '>= 7.5.0 < 9.0.0') end # Using puppet_apply as a helper - it 'works idempotently with no errors' do pp = <<-EOS class { 'grafana': diff --git a/spec/acceptance/grafana_folder_spec.rb b/spec/acceptance/grafana_folder_spec.rb index 3ab67fd3d..bb951e174 100644 --- a/spec/acceptance/grafana_folder_spec.rb +++ b/spec/acceptance/grafana_folder_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'spec_helper_acceptance' supported_versions.each do |grafana_version| @@ -59,20 +57,17 @@ class { 'grafana': 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) @@ -118,7 +113,6 @@ class { 'grafana': # 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) diff --git a/spec/acceptance/grafana_plugin_spec.rb b/spec/acceptance/grafana_plugin_spec.rb index 1a9ae12b4..38c081ed8 100644 --- a/spec/acceptance/grafana_plugin_spec.rb +++ b/spec/acceptance/grafana_plugin_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'spec_helper_acceptance' supported_versions.each do |grafana_version| diff --git a/spec/acceptance/grafana_team_spec.rb b/spec/acceptance/grafana_team_spec.rb index 781e5b3e9..e066323eb 100644 --- a/spec/acceptance/grafana_team_spec.rb +++ b/spec/acceptance/grafana_team_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'spec_helper_acceptance' supported_versions.each do |grafana_version| @@ -138,7 +136,7 @@ class { 'grafana': 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| + '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 @@ -199,14 +197,14 @@ class { 'grafana': 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| + '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| + 'curl --user admin:admin http://localhost:3000/api/teams') do |f| expect(f.stdout).not_to match(%r{example-team-on-org}) end end diff --git a/spec/acceptance/grafana_user_spec.rb b/spec/acceptance/grafana_user_spec.rb index a808ebd77..a094e34c4 100644 --- a/spec/acceptance/grafana_user_spec.rb +++ b/spec/acceptance/grafana_user_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'spec_helper_acceptance' supported_versions.each do |grafana_version| @@ -24,7 +22,6 @@ class { 'grafana': apply_manifest(pp, catch_changes: true) end end - it 'runs successfully' do pp = <<-EOS grafana_user { 'user1': diff --git a/spec/classes/grafana_spec.rb b/spec/classes/grafana_spec.rb index 934bee003..aff6bde10 100644 --- a/spec/classes/grafana_spec.rb +++ b/spec/classes/grafana_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'spec_helper' describe 'grafana' do @@ -17,7 +15,6 @@ it { is_expected.to contain_class('grafana::service') } end - # rubocop:disable RSpec/EmptyExampleGroup context 'with parameter install_method is set to package' do let(:params) do { @@ -32,11 +29,10 @@ describe 'use archive to fetch the package to a temporary location' do it do - expect(subject).to contain_archive('/tmp/grafana.deb').with_source( + is_expected.to contain_archive('/tmp/grafana.deb').with_source( 'https://dl.grafana.com/oss/release/grafana_5.4.2_amd64.deb' ) end - it { is_expected.to contain_archive('/tmp/grafana.deb').that_comes_before('Package[grafana]') } end @@ -179,7 +175,11 @@ end case facts[:osfamily] - when 'Archlinux', 'Debian', 'RedHat' + when 'Archlinux' + describe 'create data_dir' do + it { is_expected.to contain_file('/var/lib/grafana').with_ensure('directory') } + end + when 'Debian' describe 'create data_dir' do it { is_expected.to contain_file('/var/lib/grafana').with_ensure('directory') } end @@ -187,6 +187,10 @@ describe 'create data_dir' do it { is_expected.to contain_file('/var/db/grafana').with_ensure('directory') } end + when 'RedHat' + describe 'create data_dir' do + it { is_expected.to contain_file('/var/lib/grafana').with_ensure('directory') } + end end describe 'manage install_dir' do @@ -255,26 +259,26 @@ { cfg: { 'app_mode' => 'production', - 'section' => { - 'string' => 'production', - 'number' => 8080, + 'section' => { + 'string' => 'production', + 'number' => 8080, 'boolean' => false, - 'empty' => '' + 'empty' => '' } }, ldap_cfg: { 'servers' => [ { 'host' => 'server1', - 'use_ssl' => true, - 'search_filter' => '(sAMAccountName=%s)', + 'use_ssl' => true, + 'search_filter' => '(sAMAccountName=%s)', 'search_base_dns' => ['dc=domain1,dc=com'] } ], 'servers.attributes' => { - 'name' => 'givenName', - 'surname' => 'sn', - 'username' => 'sAMAccountName', + 'name' => 'givenName', + 'surname' => 'sn', + 'username' => 'sAMAccountName', 'member_of' => 'memberOf', - 'email' => 'email' + 'email' => 'email' } } } @@ -291,18 +295,18 @@ it { is_expected.to contain_file('grafana.ini').with_content(expected) } ldap_expected = "\n[[servers]]\n"\ - "host = \"server1\"\n"\ - "search_base_dns = [\"dc=domain1,dc=com\"]\n"\ - "search_filter = \"(sAMAccountName=%s)\"\n"\ - "use_ssl = true\n"\ - "\n"\ - "[servers.attributes]\n"\ - "email = \"email\"\n"\ - "member_of = \"memberOf\"\n"\ - "name = \"givenName\"\n"\ - "surname = \"sn\"\n"\ - "username = \"sAMAccountName\"\n"\ - "\n" + "host = \"server1\"\n"\ + "search_base_dns = [\"dc=domain1,dc=com\"]\n"\ + "search_filter = \"(sAMAccountName=%s)\"\n"\ + "use_ssl = true\n"\ + "\n"\ + "[servers.attributes]\n"\ + "email = \"email\"\n"\ + "member_of = \"memberOf\"\n"\ + "name = \"givenName\"\n"\ + "surname = \"sn\"\n"\ + "username = \"sAMAccountName\"\n"\ + "\n" it { is_expected.to contain_file('/etc/grafana/ldap.toml').with_content(ldap_expected) } end @@ -317,31 +321,31 @@ { 'servers' => [ { 'host' => 'server1a server1b', - 'use_ssl' => true, - 'search_filter' => '(sAMAccountName=%s)', + 'use_ssl' => true, + 'search_filter' => '(sAMAccountName=%s)', 'search_base_dns' => ['dc=domain1,dc=com'] } ], 'servers.attributes' => { - 'name' => 'givenName', - 'surname' => 'sn', - 'username' => 'sAMAccountName', + 'name' => 'givenName', + 'surname' => 'sn', + 'username' => 'sAMAccountName', 'member_of' => 'memberOf', - 'email' => 'email' + 'email' => 'email' } }, { 'servers' => [ { 'host' => 'server2a server2b', - 'use_ssl' => true, - 'search_filter' => '(sAMAccountName=%s)', + 'use_ssl' => true, + 'search_filter' => '(sAMAccountName=%s)', 'search_base_dns' => ['dc=domain2,dc=com'] } ], 'servers.attributes' => { - 'name' => 'givenName', - 'surname' => 'sn', - 'username' => 'sAMAccountName', + 'name' => 'givenName', + 'surname' => 'sn', + 'username' => 'sAMAccountName', 'member_of' => 'memberOf', - 'email' => 'email' + 'email' => 'email' } } ] @@ -349,31 +353,31 @@ end ldap_expected = "\n[[servers]]\n"\ - "host = \"server1a server1b\"\n"\ - "search_base_dns = [\"dc=domain1,dc=com\"]\n"\ - "search_filter = \"(sAMAccountName=%s)\"\n"\ - "use_ssl = true\n"\ - "\n"\ - "[servers.attributes]\n"\ - "email = \"email\"\n"\ - "member_of = \"memberOf\"\n"\ - "name = \"givenName\"\n"\ - "surname = \"sn\"\n"\ - "username = \"sAMAccountName\"\n"\ - "\n"\ - "\n[[servers]]\n"\ - "host = \"server2a server2b\"\n"\ - "search_base_dns = [\"dc=domain2,dc=com\"]\n"\ - "search_filter = \"(sAMAccountName=%s)\"\n"\ - "use_ssl = true\n"\ - "\n"\ - "[servers.attributes]\n"\ - "email = \"email\"\n"\ - "member_of = \"memberOf\"\n"\ - "name = \"givenName\"\n"\ - "surname = \"sn\"\n"\ - "username = \"sAMAccountName\"\n"\ - "\n" + "host = \"server1a server1b\"\n"\ + "search_base_dns = [\"dc=domain1,dc=com\"]\n"\ + "search_filter = \"(sAMAccountName=%s)\"\n"\ + "use_ssl = true\n"\ + "\n"\ + "[servers.attributes]\n"\ + "email = \"email\"\n"\ + "member_of = \"memberOf\"\n"\ + "name = \"givenName\"\n"\ + "surname = \"sn\"\n"\ + "username = \"sAMAccountName\"\n"\ + "\n"\ + "\n[[servers]]\n"\ + "host = \"server2a server2b\"\n"\ + "search_base_dns = [\"dc=domain2,dc=com\"]\n"\ + "search_filter = \"(sAMAccountName=%s)\"\n"\ + "use_ssl = true\n"\ + "\n"\ + "[servers.attributes]\n"\ + "email = \"email\"\n"\ + "member_of = \"memberOf\"\n"\ + "name = \"givenName\"\n"\ + "surname = \"sn\"\n"\ + "username = \"sAMAccountName\"\n"\ + "\n" it { is_expected.to contain_file('/etc/grafana/ldap.toml').with_content(ldap_expected) } end @@ -403,7 +407,7 @@ end it do - expect(subject).to contain_file('/var/lib/grafana/dashboards').with( + is_expected.to contain_file('/var/lib/grafana/dashboards').with( ensure: 'directory', owner: 'grafana', group: 'grafana', @@ -461,7 +465,6 @@ end end end - # rubocop:enable RSpec/EmptyExampleGroup end end end diff --git a/spec/grafana_dashboard_permission_type_spec.rb b/spec/grafana_dashboard_permission_type_spec.rb index 9b3540051..ebb4fc638 100644 --- a/spec/grafana_dashboard_permission_type_spec.rb +++ b/spec/grafana_dashboard_permission_type_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'spec_helper' describe Puppet::Type.type(:grafana_dashboard_permission) do @@ -33,13 +31,13 @@ described_class.new title: 'foo title', user: 'foo_user', team: 'foo_team' end.to raise_error(Puppet::Error, %r{Only user or team can be set, not both}) end - it 'accepts valid parameters' do expect(gpermission[:user]).to eq('foo_user') expect(gpermission[:grafana_api_path]).to eq('/api') expect(gpermission[:grafana_url]).to eq('http://example.com/') expect(gpermission[:dashboard]).to eq('foo_dashboard') end + # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new diff --git a/spec/grafana_membership_type_spec.rb b/spec/grafana_membership_type_spec.rb index 718441db4..4886757be 100644 --- a/spec/grafana_membership_type_spec.rb +++ b/spec/grafana_membership_type_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'spec_helper' describe Puppet::Type.type(:grafana_membership) do @@ -34,7 +32,6 @@ described_class.new title: 'foo title', membership_type: 'foo' end.to raise_error(Puppet::Error, %r{Invalid value "foo"}) end - it 'accepts valid parameters' do expect(gmembership[:user_name]).to eq('foo_user') expect(gmembership[:target_name]).to eq('foo_target') @@ -42,6 +39,7 @@ expect(gmembership[:grafana_url]).to eq('http://example.com/') expect(gmembership[:membership_type]).to eq(:organization) end + # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4d617f393..fb5f0cbea 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,17 +1,17 @@ -# frozen_string_literal: true - # Managed by modulesync - DO NOT EDIT # https://voxpupuli.org/docs/updating-files-managed-with-modulesync/ # puppetlabs_spec_helper will set up coverage if the env variable is set. # We want to do this if lib exists and it hasn't been explicitly set. -ENV['COVERAGE'] ||= 'yes' if Dir.exist?(File.expand_path('../lib', __dir__)) +ENV['COVERAGE'] ||= 'yes' if Dir.exist?(File.expand_path('../../lib', __FILE__)) require 'voxpupuli/test/spec_helper' if File.exist?(File.join(__dir__, 'default_module_facts.yml')) facts = YAML.safe_load(File.read(File.join(__dir__, 'default_module_facts.yml'))) - facts&.each do |name, value| - add_custom_fact name.to_sym, value + if facts + facts.each do |name, value| + add_custom_fact name.to_sym, value + end end end diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb index d3a6e23cf..d3b906bfe 100644 --- a/spec/spec_helper_acceptance.rb +++ b/spec/spec_helper_acceptance.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # Managed by modulesync - DO NOT EDIT # https://voxpupuli.org/docs/updating-files-managed-with-modulesync/ diff --git a/spec/support/acceptance/prepare_host.rb b/spec/support/acceptance/prepare_host.rb index 420f55435..996f5ae07 100644 --- a/spec/support/acceptance/prepare_host.rb +++ b/spec/support/acceptance/prepare_host.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - def prepare_host cleanup_script = <<-SHELL /opt/puppetlabs/bin/puppet resource package grafana ensure=purged diff --git a/spec/support/acceptance/supported_versions.rb b/spec/support/acceptance/supported_versions.rb index 8ef388f01..71c754ddb 100644 --- a/spec/support/acceptance/supported_versions.rb +++ b/spec/support/acceptance/supported_versions.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - def supported_versions %w[6.0.0 7.0.0 8.0.0] end diff --git a/spec/unit/puppet/provider/grafana_plugin/grafana_cli_spec.rb b/spec/unit/puppet/provider/grafana_plugin/grafana_cli_spec.rb index 287f222f6..af34ab1e0 100644 --- a/spec/unit/puppet/provider/grafana_plugin/grafana_cli_spec.rb +++ b/spec/unit/puppet/provider/grafana_plugin/grafana_cli_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'spec_helper' provider_class = Puppet::Type.type(:grafana_plugin).provider(:grafana_cli) @@ -13,25 +11,26 @@ describe '#instances' do let(:plugins_ls_two) do - <<~PLUGINS - installed plugins: - grafana-simple-json-datasource @ 1.3.4 - jdbranham-diagram-panel @ 1.4.0 + <<-PLUGINS +installed plugins: +grafana-simple-json-datasource @ 1.3.4 +jdbranham-diagram-panel @ 1.4.0 - Restart grafana after installing plugins . +Restart grafana after installing plugins . PLUGINS + # rubocop:enable Layout/TrailingWhitespace end let(:plugins_ls_none) do - <<~PLUGINS + <<-PLUGINS - Restart grafana after installing plugins . +Restart grafana after installing plugins . PLUGINS end it 'has the correct names' do allow(provider_class).to receive(:grafana_cli).with('plugins', 'ls').and_return(plugins_ls_two) - expect(provider_class.instances.map(&:name)).to match_array(%w[grafana-simple-json-datasource jdbranham-diagram-panel]) + expect(provider_class.instances.map(&:name)).to match_array(['grafana-simple-json-datasource', 'jdbranham-diagram-panel']) expect(provider_class).to have_received(:grafana_cli) end @@ -41,6 +40,7 @@ expect(provider.exists?).to eq(false) expect(provider_class).to have_received(:grafana_cli) end + # rubocop:enable RSpec/MultipleExpectations end it '#create' do diff --git a/spec/unit/puppet/type/grafana_dashboard_type_spec.rb b/spec/unit/puppet/type/grafana_dashboard_type_spec.rb index 2114b17af..32b76e675 100644 --- a/spec/unit/puppet/type/grafana_dashboard_type_spec.rb +++ b/spec/unit/puppet/type/grafana_dashboard_type_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # Copyright 2015 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -38,13 +36,11 @@ described_class.new name: 'foo', grafana_url: 'http://example.com/', content: '{invalid', ensure: :present end.to raise_error(Puppet::Error, %r{Invalid JSON}) end - it 'accepts valid parameters' do expect(gdashboard[:name]).to eq('foo') expect(gdashboard[:grafana_url]).to eq('http://example.com/') expect(gdashboard[:content]).to eq({}) end - it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') @@ -56,13 +52,11 @@ end expect(relationship).to be_a Puppet::Relationship end - it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gdashboard expect(gdashboard.autorequire).to be_empty end - it 'autorequires grafana_conn_validator' do catalog = Puppet::Resource::Catalog.new validator = Puppet::Type.type(:grafana_conn_validator).new(name: 'grafana') diff --git a/spec/unit/puppet/type/grafana_datasource_type_spec.rb b/spec/unit/puppet/type/grafana_datasource_type_spec.rb index ea1b4f75f..80f75e3cb 100644 --- a/spec/unit/puppet/type/grafana_datasource_type_spec.rb +++ b/spec/unit/puppet/type/grafana_datasource_type_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # Copyright 2015 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -55,7 +53,6 @@ described_class.new name: 'foo', grafana_url: 'http://example.com', secure_json_data: 'invalid', ensure: :present end.to raise_error(Puppet::Error, %r{json_data should be a Hash}) end - it 'accepts valid parameters' do expect(gdatasource[:name]).to eq('foo') expect(gdatasource[:grafana_url]).to eq('http://example.com') @@ -63,17 +60,18 @@ expect(gdatasource[:type]).to eq('elasticsearch') expect(gdatasource[:organization]).to eq('test_org') expect(gdatasource[:access_mode]).to eq(:proxy) - expect(gdatasource[:is_default]).to eq(:true) # rubocop:disable Lint/BooleanSymbol - expect(gdatasource[:basic_auth]).to eq(:true) # rubocop:disable Lint/BooleanSymbol + expect(gdatasource[:is_default]).to eq(:true) + expect(gdatasource[:basic_auth]).to eq(:true) expect(gdatasource[:basic_auth_user]).to eq('user') expect(gdatasource[:basic_auth_password]).to eq('password') - expect(gdatasource[:with_credentials]).to eq(:true) # rubocop:disable Lint/BooleanSymbol + expect(gdatasource[:with_credentials]).to eq(:true) expect(gdatasource[:database]).to eq('test_db') expect(gdatasource[:user]).to eq('db_user') expect(gdatasource[:password]).to eq('db_password') expect(gdatasource[:json_data]).to eq(esVersion: 5, timeField: '@timestamp', timeInterval: '1m') expect(gdatasource[:secure_json_data]).to eq(password: '5ecretPassw0rd') end + # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new diff --git a/spec/unit/puppet/type/grafana_folder_type_spec.rb b/spec/unit/puppet/type/grafana_folder_type_spec.rb index 273664f45..f3ffa1015 100644 --- a/spec/unit/puppet/type/grafana_folder_type_spec.rb +++ b/spec/unit/puppet/type/grafana_folder_type_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # Copyright 2015 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -32,12 +30,10 @@ described_class.new name: 'foo', grafana_url: 'http://example.com', grafana_api_path: '/invalidpath', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid API path}) end - it 'accepts valid parameters' do expect(gfolder[:name]).to eq('foo') expect(gfolder[:grafana_url]).to eq('http://example.com/') end - it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') @@ -49,13 +45,11 @@ end expect(relationship).to be_a Puppet::Relationship end - it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource gfolder expect(gfolder.autorequire).to be_empty end - it 'autorequires grafana_conn_validator' do catalog = Puppet::Resource::Catalog.new validator = Puppet::Type.type(:grafana_conn_validator).new(name: 'grafana') diff --git a/spec/unit/puppet/type/grafana_ldap_config_spec.rb b/spec/unit/puppet/type/grafana_ldap_config_spec.rb index 305e85cbb..c917c2875 100644 --- a/spec/unit/puppet/type/grafana_ldap_config_spec.rb +++ b/spec/unit/puppet/type/grafana_ldap_config_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -164,4 +162,3 @@ end end end -# rubocop:enable RSpec/VoidExpect diff --git a/spec/unit/puppet/type/grafana_ldap_group_mapping_spec.rb b/spec/unit/puppet/type/grafana_ldap_group_mapping_spec.rb index a6c52043e..81d33b4af 100644 --- a/spec/unit/puppet/type/grafana_ldap_group_mapping_spec.rb +++ b/spec/unit/puppet/type/grafana_ldap_group_mapping_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -151,4 +149,3 @@ end end end -# rubocop:enable RSpec/VoidExpect diff --git a/spec/unit/puppet/type/grafana_ldap_server_spec.rb b/spec/unit/puppet/type/grafana_ldap_server_spec.rb index 2a812cd04..8cff01978 100644 --- a/spec/unit/puppet/type/grafana_ldap_server_spec.rb +++ b/spec/unit/puppet/type/grafana_ldap_server_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -337,4 +335,3 @@ end end end -# rubocop:enable RSpec/VoidExpect diff --git a/spec/unit/puppet/type/grafana_notification_type_spec.rb b/spec/unit/puppet/type/grafana_notification_type_spec.rb index ac5826f14..e1b995366 100644 --- a/spec/unit/puppet/type/grafana_notification_type_spec.rb +++ b/spec/unit/puppet/type/grafana_notification_type_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # Copyright 2015 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -40,16 +38,16 @@ described_class.new name: 'foo', grafana_url: 'http://example.com', settings: 'invalid' end.to raise_error(Puppet::Error, %r{settings should be a Hash}) end - it 'accepts valid parameters' do expect(gnotification[:name]).to eq('foo') expect(gnotification[:grafana_url]).to eq('http://example.com') expect(gnotification[:type]).to eq('email') - expect(gnotification[:is_default]).to eq(:true) # rubocop:disable Lint/BooleanSymbol - expect(gnotification[:send_reminder]).to eq(:true) # rubocop:disable Lint/BooleanSymbol + expect(gnotification[:is_default]).to eq(:true) + expect(gnotification[:send_reminder]).to eq(:true) expect(gnotification[:frequency]).to eq('20m') expect(gnotification[:settings]).to eq(adresses: 'test@example.com') end + # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new diff --git a/spec/unit/puppet/type/grafana_organization_type_spec.rb b/spec/unit/puppet/type/grafana_organization_type_spec.rb index 770087430..e1959205f 100644 --- a/spec/unit/puppet/type/grafana_organization_type_spec.rb +++ b/spec/unit/puppet/type/grafana_organization_type_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'spec_helper' describe Puppet::Type.type(:grafana_organization) do @@ -25,7 +23,6 @@ described_class.new name: 'foo', grafana_url: 'example.com', content: '{}', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid URL}) end - it 'accepts valid parameters' do expect(gorganization[:name]).to eq('foo') expect(gorganization[:grafana_user]).to eq('admin') @@ -33,6 +30,7 @@ expect(gorganization[:grafana_url]).to eq('http://example.com') expect(gorganization[:address]).to eq(address1: 'test address1', address2: 'test address2', city: 'CityName', state: 'NewState', zipcode: '12345', country: 'USA') end + # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new diff --git a/spec/unit/puppet/type/grafana_plugin_spec.rb b/spec/unit/puppet/type/grafana_plugin_spec.rb index 00a100dbd..7ba911403 100644 --- a/spec/unit/puppet/type/grafana_plugin_spec.rb +++ b/spec/unit/puppet/type/grafana_plugin_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'spec_helper' describe Puppet::Type.type(:grafana_plugin) do let(:plugin) do @@ -10,7 +8,6 @@ plugin[:name] = 'plugin-name' expect(plugin[:name]).to eq('plugin-name') end - it 'requires a name' do expect do Puppet::Type.type(:grafana_plugin).new({}) @@ -21,7 +18,6 @@ plugin[:repo] = 'https://nexus.company.com/grafana/plugins' expect(plugin[:repo]).to eq('https://nexus.company.com/grafana/plugins') end - it 'accepts a plugin url' do plugin[:plugin_url] = 'https://grafana.com/api/plugins/grafana-simple-json-datasource/versions/latest/download' expect(plugin[:plugin_url]).to eq('https://grafana.com/api/plugins/grafana-simple-json-datasource/versions/latest/download') diff --git a/spec/unit/puppet/type/grafana_team_type_spec.rb b/spec/unit/puppet/type/grafana_team_type_spec.rb index df4f0b67c..a7140919f 100644 --- a/spec/unit/puppet/type/grafana_team_type_spec.rb +++ b/spec/unit/puppet/type/grafana_team_type_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require 'spec_helper' describe Puppet::Type.type(:grafana_team) do @@ -21,7 +19,6 @@ described_class.new name: 'foo', grafana_url: 'example.com', content: '{}', ensure: :present end.to raise_error(Puppet::Error, %r{not a valid URL}) end - it 'accepts valid parameters' do expect(gteam[:name]).to eq('foo') expect(gteam[:grafana_user]).to eq('admin') @@ -31,6 +28,7 @@ expect(gteam[:home_dashboard]).to eq('foo_dashboard') expect(gteam[:organization]).to eq('foo_organization') end + # rubocop:enable RSpec/MultipleExpectations it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new diff --git a/spec/unit/puppet/type/grafana_user_type_spec.rb b/spec/unit/puppet/type/grafana_user_type_spec.rb index ca83c6664..c45aa90f7 100644 --- a/spec/unit/puppet/type/grafana_user_type_spec.rb +++ b/spec/unit/puppet/type/grafana_user_type_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -24,14 +22,12 @@ described_class.new name: 'test', grafana_url: 'example.com' end.to raise_error(Puppet::Error, %r{not a valid URL}) end - it 'accepts valid parameters' do expect(guser[:name]).to eq('test') expect(guser[:full_name]).to eq('Mr tester') expect(guser[:password]).to eq('t3st') expect(guser[:grafana_url]).to eq('http://example.com/') end - it 'autorequires the grafana-server for proper ordering' do catalog = Puppet::Resource::Catalog.new service = Puppet::Type.type(:service).new(name: 'grafana-server') @@ -43,13 +39,11 @@ end expect(relationship).to be_a Puppet::Relationship end - it 'does not autorequire the service it is not managed' do catalog = Puppet::Resource::Catalog.new catalog.add_resource guser expect(guser.autorequire).to be_empty end - it 'autorequires grafana_conn_validator' do catalog = Puppet::Resource::Catalog.new validator = Puppet::Type.type(:grafana_conn_validator).new(name: 'grafana')