From 771a032c991efb7e527aad9e23330855d76f22a9 Mon Sep 17 00:00:00 2001 From: Christos Papageorgiou Date: Tue, 30 Nov 2021 16:08:42 +0200 Subject: [PATCH] Rubocop autofix --- .../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 | 40 ++--- lib/puppet/provider/grafana_folder/grafana.rb | 52 +++---- .../provider/grafana_notification/grafana.rb | 24 ++- .../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 | 28 ++-- lib/puppet/type/grafana_conn_validator.rb | 10 +- lib/puppet/type/grafana_dashboard.rb | 21 +-- lib/puppet/type/grafana_datasource.rb | 18 +-- 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 | 14 +- 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 | 12 +- 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 | 139 +++++++++--------- .../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 | 4 +- .../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 | 4 +- .../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, 362 insertions(+), 375 deletions(-) diff --git a/lib/puppet/functions/grafana/deep_find_and_remove.rb b/lib/puppet/functions/grafana/deep_find_and_remove.rb index 6ff436c9a..20a1c2da7 100644 --- a/lib/puppet/functions/grafana/deep_find_and_remove.rb +++ b/lib/puppet/functions/grafana/deep_find_and_remove.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # == Function: deep_find_and_remove # # This function takes a hash as input, along with a string @@ -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 diff --git a/lib/puppet/functions/grafana/get_sub_paths.rb b/lib/puppet/functions/grafana/get_sub_paths.rb index b2ff27f2c..78e0c7eaf 100644 --- a/lib/puppet/functions/grafana/get_sub_paths.rb +++ b/lib/puppet/functions/grafana/get_sub_paths.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # == Function get_sub_paths # # This function receives an input path as an input parameter, and @@ -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 diff --git a/lib/puppet/provider/grafana.rb b/lib/puppet/provider/grafana.rb index 3159dc5e3..c0c644415 100644 --- a/lib/puppet/provider/grafana.rb +++ b/lib/puppet/provider/grafana.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Copyright 2015 Mirantis, Inc. # require 'cgi' @@ -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 @@ -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', diff --git a/lib/puppet/provider/grafana_conn_validator/net_http.rb b/lib/puppet/provider/grafana_conn_validator/net_http.rb index 7799e840c..6cfafc778 100644 --- a/lib/puppet/provider/grafana_conn_validator/net_http.rb +++ b/lib/puppet/provider/grafana_conn_validator/net_http.rb @@ -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. @@ -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 diff --git a/lib/puppet/provider/grafana_dashboard/grafana.rb b/lib/puppet/provider/grafana_dashboard/grafana.rb index d01e89226..cddeb1e8a 100644 --- a/lib/puppet/provider/grafana_dashboard/grafana.rb +++ b/lib/puppet/provider/grafana_dashboard/grafana.rb @@ -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 @@ -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) @@ -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) @@ -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) @@ -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 @@ -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], @@ -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 @@ -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 diff --git a/lib/puppet/provider/grafana_dashboard_permission/grafana.rb b/lib/puppet/provider/grafana_dashboard_permission/grafana.rb index 0e1be74df..829631af1 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] + permission = case resource[:permission] # rubocop:disable Style/HashLikeCase when :View 1 when :Edit @@ -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]) diff --git a/lib/puppet/provider/grafana_datasource/grafana.rb b/lib/puppet/provider/grafana_datasource/grafana.rb index 6688b30d2..35e66c7ae 100644 --- a/lib/puppet/provider/grafana_datasource/grafana.rb +++ b/lib/puppet/provider/grafana_datasource/grafana.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Copyright 2015 Mirantis, Inc. # require 'json' @@ -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) @@ -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) @@ -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 @@ -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], @@ -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 diff --git a/lib/puppet/provider/grafana_folder/grafana.rb b/lib/puppet/provider/grafana_folder/grafana.rb index 6e1f81d24..7c5acbf89 100644 --- a/lib/puppet/provider/grafana_folder/grafana.rb +++ b/lib/puppet/provider/grafana_folder/grafana.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'json' require File.expand_path(File.join(File.dirname(__FILE__), '..', 'grafana')) @@ -19,9 +21,8 @@ def permissions find_folder unless @folder response = send_request('GET', format('%s/folders/%s/permissions', resource[:grafana_api_path], @folder['uid'])) - if response.code != '200' - raise format('Failed to retrieve permissions %s (HTTP response: %s/%s)', resource[:title], response.code, response.body) - end + raise format('Failed to retrieve permissions %s (HTTP response: %s/%s)', resource[:title], response.code, response.body) if response.code != '200' + begin permissions = JSON.parse(response.body) rescue JSON::ParserError @@ -46,18 +47,14 @@ def permissions=(value) 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) @@ -72,22 +69,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) @@ -106,9 +98,7 @@ def find_folder def save_folder(folder) 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' # if folder exists, update object based on uid # else, create object @@ -119,9 +109,8 @@ def save_folder(folder) } response = send_request('POST', format('%s/folders', resource[:grafana_api_path]), data) - 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 + raise format('Failed to create folder %s (HTTP response: %s/%s)', resource[:title], response.code, response.body) if (response.code != '200') && (response.code != '412') + folders find_folder save_permissions(resource[:permissions]) @@ -134,25 +123,27 @@ 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]) - 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' + 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 @@ -165,6 +156,7 @@ 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 7777dfaa6..1ca66643c 100644 --- a/lib/puppet/provider/grafana_notification/grafana.rb +++ b/lib/puppet/provider/grafana_notification/grafana.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Copyright 2015 Mirantis, Inc. # require 'json' @@ -15,18 +17,14 @@ def grafana_api_path def notifications response = send_request('GET', format('%s/alert-notifications', resource[:grafana_api_path])) - if response.code != '200' - raise format('Fail to retrieve notifications (HTTP response: %s/%s)', response.code, response.body) - end + raise format('Fail to retrieve notifications (HTTP response: %s/%s)', response.code, response.body) if response.code != '200' 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) - if response.code != '200' - raise format('Failed to retrieve notification %d (HTTP response: %s/%s)', id, response.code, response.body) - end + raise format('Failed to retrieve notification %d (HTTP response: %s/%s)', id, response.code, response.body) if response.code != '200' notification = JSON.parse(response.body) @@ -46,9 +44,7 @@ def notifications end def notification - unless @notification - @notification = notifications.find { |x| x[:name] == resource[:name] } - end + @notification ||= notifications.find { |x| x[:name] == resource[:name] } @notification end @@ -117,18 +113,16 @@ def save_notification data[:id] = notification[:id] response = send_request 'PUT', format('%s/alert-notifications/%s', resource[:grafana_api_path], notification[: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.notification = nil end def delete_notification response = send_request 'DELETE', format('%s/alert-notifications/%s', resource[:grafana_api_path], notification[:id]) - if response.code != '200' - raise format('Failed to delete notification %s (HTTP response: %s/%s', resource[:name], response.code, response.body) - end + raise format('Failed to delete notification %s (HTTP response: %s/%s', resource[:name], response.code, response.body) if response.code != '200' + self.notification = nil end diff --git a/lib/puppet/provider/grafana_organization/grafana.rb b/lib/puppet/provider/grafana_organization/grafana.rb index 9cf02395f..4b0377c84 100644 --- a/lib/puppet/provider/grafana_organization/grafana.rb +++ b/lib/puppet/provider/grafana_organization/grafana.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'json' require File.expand_path(File.join(File.dirname(__FILE__), '..', 'grafana')) @@ -9,18 +11,14 @@ def organizations response = send_request('GET', format('%s/orgs', resource[:grafana_api_path])) - if response.code != '200' - raise format('Failed to retrieve organizations (HTTP response: %s/%s)', response.code, response.body) - end + raise format('Failed to retrieve organizations (HTTP response: %s/%s)', response.code, response.body) if response.code != '200' 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) - 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' organization = JSON.parse(response.body) @@ -36,16 +34,12 @@ def organizations end def organization - unless @organization - @organization = organizations.find { |x| x[:name] == resource[:name] } - end + @organization ||= organizations.find { |x| x[:name] == resource[:name] } @organization end attr_writer :organization - # rubocop:enable Style/PredicateName - def id organization[:id] end @@ -73,23 +67,20 @@ def save_organization response = send_request('POST', format('%s/orgs', resource[:grafana_api_path]), data) if organization.nil? - 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.organization = nil end def delete_organization response = send_request 'DELETE', format('%s/orgs/%s', resource[:grafana_api_path], organization[:id]) - if response.code != '200' - raise format('Failed to delete organization %s (HTTP response: %s/%s)', resource[:name], response.code, response.body) - end + raise format('Failed to delete organization %s (HTTP response: %s/%s)', resource[:name], response.code, response.body) if response.code != '200' + # change back to default organization response = send_request 'POST', format('%s/user/using/1', resource[:grafana_api_path]) - 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' + 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 c84e54707..34df6bcc9 100644 --- a/lib/puppet/provider/grafana_plugin/grafana_cli.rb +++ b/lib/puppet/provider/grafana_plugin/grafana_cli.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Puppet::Type.type(:grafana_plugin).provide(:grafana_cli) do has_command(:grafana_cli, 'grafana-cli') do is_optional @@ -11,6 +13,7 @@ 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}") @@ -33,7 +36,7 @@ def self.instances def self.prefetch(resources) plugins = instances - resources.keys.each do |name| + resources.each_key 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 223ef73ea..824e9c9a3 100644 --- a/lib/puppet/provider/grafana_team/grafana.rb +++ b/lib/puppet/provider/grafana_team/grafana.rb @@ -213,6 +213,7 @@ 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 9f01e1fb3..56d9d4d2a 100644 --- a/lib/puppet/provider/grafana_user/grafana.rb +++ b/lib/puppet/provider/grafana_user/grafana.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'json' require File.expand_path(File.join(File.dirname(__FILE__), '..', 'grafana')) @@ -9,18 +11,14 @@ def users response = send_request('GET', format('%s/users', resource[:grafana_api_path])) - if response.code != '200' - raise format('Fail to retrieve users (HTTP response: %s/%s)', response.code, response.body) - end + raise format('Fail to retrieve users (HTTP response: %s/%s)', response.code, response.body) if response.code != '200' 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)) - if response.code != '200' - raise format('Fail to retrieve user %d (HTTP response: %s/%s)', id, response.code, response.body) - end + raise format('Fail to retrieve user %d (HTTP response: %s/%s)', id, response.code, response.body) if response.code != '200' user = JSON.parse(response.body) { @@ -39,7 +37,7 @@ def users end def user - @user = users.find { |x| x[:name] == resource[:name] } unless @user + @user ||= users.find { |x| x[:name] == resource[:name] } @user end @@ -120,9 +118,8 @@ def save_user response = send_request('PUT', format('%s/users/%s', resource[:grafana_api_path], user[:id]), data) end - if response.code != '200' - raise format('Failed to create user %s (HTTP response: %s/%s)', resource[:name], response.code, response.body) - end + raise format('Failed to create user %s (HTTP response: %s/%s)', resource[:name], response.code, response.body) if response.code != '200' + self.user = nil end @@ -137,19 +134,14 @@ def check_password http.request(request) end - if response.code == '200' - true - else - false - end + response.code == '200' end def delete_user response = send_request('DELETE', format('%s/admin/users/%s', resource[:grafana_api_path], user[:id])) - if response.code != '200' - raise format('Failed to delete user %s (HTTP response: %s/%s', resource[:name], response.code, response.body) - end + raise format('Failed to delete user %s (HTTP response: %s/%s', resource[:name], response.code, response.body) if response.code != '200' + self.user = nil end diff --git a/lib/puppet/type/grafana_conn_validator.rb b/lib/puppet/type/grafana_conn_validator.rb index 7c659ed55..cfed80ffa 100644 --- a/lib/puppet/type/grafana_conn_validator.rb +++ b/lib/puppet/type/grafana_conn_validator.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Puppet::Type.newtype(:grafana_conn_validator) do desc <<-DESC Verify connectivity to the Grafana API @@ -14,9 +16,7 @@ defaultto 'http://localhost:3000' validate do |value| - unless value =~ %r{^https?://} - raise ArgumentError, format('%s is not a valid URL', value) - end + raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} end end @@ -25,9 +25,7 @@ defaultto '/api/health' validate do |value| - unless value =~ %r{^/.*/?api/.*$} - raise ArgumentError, format('%s is not a valid API path', value) - end + raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api/.*$} end end diff --git a/lib/puppet/type/grafana_dashboard.rb b/lib/puppet/type/grafana_dashboard.rb index b6cd648ad..87226d68c 100644 --- a/lib/puppet/type/grafana_dashboard.rb +++ b/lib/puppet/type/grafana_dashboard.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Copyright 2015 Mirantis, Inc. # require 'json' @@ -19,11 +21,9 @@ desc 'The JSON representation of the dashboard.' validate do |value| - begin - JSON.parse(value) - rescue JSON::ParserError - raise ArgumentError, 'Invalid JSON string for content' - end + JSON.parse(value) + rescue JSON::ParserError + raise ArgumentError, 'Invalid JSON string for content' end munge do |value| @@ -45,9 +45,7 @@ def should_to_s(value) defaultto '' validate do |value| - unless value =~ %r{^https?://} - raise ArgumentError, format('%s is not a valid URL', value) - end + raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} end end @@ -64,9 +62,7 @@ def should_to_s(value) defaultto '/api' validate do |value| - unless value =~ %r{^/.*/?api$} - raise ArgumentError, format('%s is not a valid API path', value) - end + raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} end end @@ -75,9 +71,8 @@ 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? + fail('content is required when ensure is present') if self[:ensure] == :present && self[:content].nil? # rubocop:disable Style/SignalException end autorequire(:service) do 'grafana-server' diff --git a/lib/puppet/type/grafana_datasource.rb b/lib/puppet/type/grafana_datasource.rb index 7df69a19e..d60b81e97 100644 --- a/lib/puppet/type/grafana_datasource.rb +++ b/lib/puppet/type/grafana_datasource.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Copyright 2015 Mirantis, Inc. # Puppet::Type.newtype(:grafana_datasource) do @@ -14,9 +16,7 @@ defaultto '/api' validate do |value| - unless value =~ %r{^/.*/?api$} - raise ArgumentError, format('%s is not a valid API path', value) - end + raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} end end @@ -25,9 +25,7 @@ defaultto '' validate do |value| - unless value =~ %r{^https?://} - raise ArgumentError, format('%s is not a valid URL', value) - end + raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} end end @@ -103,9 +101,7 @@ desc 'Additional JSON data to configure the datasource (optional)' validate do |value| - unless value.nil? || value.is_a?(Hash) - raise ArgumentError, 'json_data should be a Hash!' - end + raise ArgumentError, 'json_data should be a Hash!' unless value.nil? || value.is_a?(Hash) end end @@ -114,9 +110,7 @@ sensitive true validate do |value| - unless value.nil? || value.is_a?(Hash) - raise ArgumentError, 'secure_json_data should be a Hash!' - end + raise ArgumentError, 'secure_json_data should be a Hash!' unless value.nil? || value.is_a?(Hash) end end diff --git a/lib/puppet/type/grafana_folder.rb b/lib/puppet/type/grafana_folder.rb index e8325a9ed..97a97d5b7 100644 --- a/lib/puppet/type/grafana_folder.rb +++ b/lib/puppet/type/grafana_folder.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'json' Puppet::Type.newtype(:grafana_folder) do @@ -18,9 +20,7 @@ defaultto '' validate do |value| - unless value =~ %r{^https?://} - raise ArgumentError, format('%s is not a valid URL', value) - end + raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} end end @@ -37,9 +37,7 @@ defaultto '/api' validate do |value| - unless value =~ %r{^/.*/?api$} - raise ArgumentError, format('%s is not a valid API path', value) - end + raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} end end diff --git a/lib/puppet/type/grafana_ldap_config.rb b/lib/puppet/type/grafana_ldap_config.rb index 23b4f04e7..ee52f46d8 100644 --- a/lib/puppet/type/grafana_ldap_config.rb +++ b/lib/puppet/type/grafana_ldap_config.rb @@ -1,19 +1,21 @@ +# 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 @@ -94,7 +96,7 @@ def should_content ldap_servers.each do |server_k, server_v| # convert symbols to strings - server_params = Hash[server_v.original_parameters.map { |k, v| [k.to_s, v] }] + server_params = server_v.original_parameters.transform_keys(&:to_s) server_attributes = server_params['attributes'] server_params.delete('attributes') @@ -106,8 +108,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 @@ -164,11 +166,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 bcf5934c8..5d5ea8f66 100644 --- a/lib/puppet/type/grafana_ldap_group_mapping.rb +++ b/lib/puppet/type/grafana_ldap_group_mapping.rb @@ -1,3 +1,5 @@ +# 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 c97e602e4..8bd1168f4 100644 --- a/lib/puppet/type/grafana_ldap_server.rb +++ b/lib/puppet/type/grafana_ldap_server.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'puppet/parameter/boolean' Puppet::Type.newtype(:grafana_ldap_server) do @@ -154,7 +156,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 > 0 + raise ArgumentError, _("attributes contains an unknown key, allowed: #{valid_attributes.join(', ')}") if value.keys.reject { |key| valid_attributes.include?(key) }.count.positive? end end @@ -168,7 +170,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 = Hash[resource.original_parameters.map { |k, v| [k.to_s, v] }] + group_mapping = resource.original_parameters.transform_keys(&:to_s) 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 9cad69f30..4b701c5fa 100644 --- a/lib/puppet/type/grafana_notification.rb +++ b/lib/puppet/type/grafana_notification.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Copyright 2015 Mirantis, Inc. # Puppet::Type.newtype(:grafana_notification) do @@ -14,9 +16,7 @@ defaultto '/api' validate do |value| - unless value =~ %r{^/.*/?api$} - raise ArgumentError, format('%s is not a valid API path', value) - end + raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} end end @@ -25,9 +25,7 @@ defaultto '' validate do |value| - unless value =~ %r{^https?://} - raise ArgumentError, format('%s is not a valid URL', value) - end + raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} end end @@ -63,9 +61,7 @@ desc 'Additional JSON data to configure the notification' validate do |value| - unless value.nil? || value.is_a?(Hash) - raise ArgumentError, 'settings should be a Hash!' - end + raise ArgumentError, 'settings should be a Hash!' unless value.nil? || value.is_a?(Hash) end end diff --git a/lib/puppet/type/grafana_organization.rb b/lib/puppet/type/grafana_organization.rb index 146a76517..cf8fbd215 100644 --- a/lib/puppet/type/grafana_organization.rb +++ b/lib/puppet/type/grafana_organization.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Puppet::Type.newtype(:grafana_organization) do @doc = 'Manage organizations in Grafana' @@ -16,9 +18,7 @@ defaultto '/api' validate do |value| - unless value =~ %r{^/.*/?api$} - raise ArgumentError, format('%s is not a valid API path', value) - end + raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} end end @@ -27,9 +27,7 @@ defaultto '' validate do |value| - unless value =~ %r{^https?://} - raise ArgumentError, format('%s is not a valid URL', value) - end + raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} end end @@ -49,9 +47,7 @@ desc 'Additional JSON data to configure the organization address (optional)' validate do |value| - unless value.nil? || value.is_a?(Hash) - raise ArgumentError, 'address should be a Hash!' - end + raise ArgumentError, 'address should be a Hash!' unless value.nil? || value.is_a?(Hash) end end autorequire(:service) do diff --git a/lib/puppet/type/grafana_plugin.rb b/lib/puppet/type/grafana_plugin.rb index 6e9280109..9e9701980 100644 --- a/lib/puppet/type/grafana_plugin.rb +++ b/lib/puppet/type/grafana_plugin.rb @@ -1,30 +1,32 @@ +# 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) @@ -44,18 +46,14 @@ newparam(:repo) do desc 'The URL of an internal plugin server' validate do |value| - unless value =~ %r{^https?://} - raise ArgumentError, format('%s is not a valid URL', value) - end + raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} end end newparam(:plugin_url) do desc 'Full url to the plugin zip file' validate do |value| - unless value =~ %r{^https?://} - raise ArgumentError, format('%s is not a valid URL', value) - end + raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} end end end diff --git a/lib/puppet/type/grafana_team.rb b/lib/puppet/type/grafana_team.rb index 18209e87d..ac9bb5878 100644 --- a/lib/puppet/type/grafana_team.rb +++ b/lib/puppet/type/grafana_team.rb @@ -14,9 +14,7 @@ defaultto '/api' validate do |value| - unless value =~ %r{^/.*/?api$} - raise ArgumentError, format('%s is not a valid API path', value) - end + raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} end end @@ -25,9 +23,7 @@ defaultto '' validate do |value| - unless value =~ %r{^https?://} - raise ArgumentError, format('%s is not a valid URL', value) - end + raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} end end diff --git a/lib/puppet/type/grafana_user.rb b/lib/puppet/type/grafana_user.rb index 53beb6c33..b39cee371 100644 --- a/lib/puppet/type/grafana_user.rb +++ b/lib/puppet/type/grafana_user.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Puppet::Type.newtype(:grafana_user) do @doc = 'Manage users in Grafana' @@ -12,9 +14,7 @@ defaultto '/api' validate do |value| - unless value =~ %r{^/.*/?api$} - raise ArgumentError, format('%s is not a valid API path', value) - end + raise ArgumentError, format('%s is not a valid API path', value) unless value =~ %r{^/.*/?api$} end end @@ -23,9 +23,7 @@ defaultto '' validate do |value| - unless value =~ %r{^https?://} - raise ArgumentError, format('%s is not a valid URL', value) - end + raise ArgumentError, format('%s is not a valid URL', value) unless value =~ %r{^https?://} end end @@ -43,7 +41,7 @@ newproperty(:password) do desc 'The password for the user' - def insync?(_is) + def insync?(_is) # rubocop:disable Naming/MethodParameterName provider.check_password end end diff --git a/lib/puppet/util/grafana_conn_validator.rb b/lib/puppet/util/grafana_conn_validator.rb index 2f8e97e01..129b86d6c 100644 --- a/lib/puppet/util/grafana_conn_validator.rb +++ b/lib/puppet/util/grafana_conn_validator.rb @@ -1,11 +1,12 @@ +# 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 - attr_reader :grafana_api_path + attr_reader :grafana_url, :grafana_api_path def initialize(grafana_url, grafana_api_path) @grafana_url = grafana_url @@ -35,10 +36,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 - return true + true rescue Exception => e # rubocop:disable Lint/RescueException Puppet.notice "Unable to connect to Grafana server (#{grafana_scheme}://#{grafana_host}:#{grafana_port}): #{e.message}" - return false + false end end end diff --git a/spec/acceptance/class_spec.rb b/spec/acceptance/class_spec.rb index 57dd24394..7f127d71f 100644 --- a/spec/acceptance/class_spec.rb +++ b/spec/acceptance/class_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper_acceptance' supported_versions.each do |grafana_version| @@ -9,6 +11,7 @@ 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 bb951e174..3ab67fd3d 100644 --- a/spec/acceptance/grafana_folder_spec.rb +++ b/spec/acceptance/grafana_folder_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper_acceptance' supported_versions.each do |grafana_version| @@ -57,17 +59,20 @@ 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) @@ -113,6 +118,7 @@ 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 38c081ed8..1a9ae12b4 100644 --- a/spec/acceptance/grafana_plugin_spec.rb +++ b/spec/acceptance/grafana_plugin_spec.rb @@ -1,3 +1,5 @@ +# 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 e066323eb..781e5b3e9 100644 --- a/spec/acceptance/grafana_team_spec.rb +++ b/spec/acceptance/grafana_team_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper_acceptance' supported_versions.each do |grafana_version| @@ -136,7 +138,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 @@ -197,14 +199,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 a094e34c4..a808ebd77 100644 --- a/spec/acceptance/grafana_user_spec.rb +++ b/spec/acceptance/grafana_user_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper_acceptance' supported_versions.each do |grafana_version| @@ -22,6 +24,7 @@ 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 aff6bde10..54446368a 100644 --- a/spec/classes/grafana_spec.rb +++ b/spec/classes/grafana_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'grafana' do @@ -15,6 +17,7 @@ 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 { @@ -29,10 +32,11 @@ describe 'use archive to fetch the package to a temporary location' do it do - is_expected.to contain_archive('/tmp/grafana.deb').with_source( + expect(subject).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 @@ -175,22 +179,14 @@ end case facts[:osfamily] - when 'Archlinux' + when 'Archlinux', 'Debian', 'RedHat' 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 - when 'FreBSD' + when 'FreeBSD' 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 @@ -259,26 +255,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' } } } @@ -295,18 +291,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 @@ -321,31 +317,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' } } ] @@ -353,31 +349,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 @@ -407,7 +403,7 @@ end it do - is_expected.to contain_file('/var/lib/grafana/dashboards').with( + expect(subject).to contain_file('/var/lib/grafana/dashboards').with( ensure: 'directory', owner: 'grafana', group: 'grafana', @@ -465,6 +461,7 @@ 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 ebb4fc638..9b3540051 100644 --- a/spec/grafana_dashboard_permission_type_spec.rb +++ b/spec/grafana_dashboard_permission_type_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Puppet::Type.type(:grafana_dashboard_permission) do @@ -31,13 +33,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 4886757be..718441db4 100644 --- a/spec/grafana_membership_type_spec.rb +++ b/spec/grafana_membership_type_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Puppet::Type.type(:grafana_membership) do @@ -32,6 +34,7 @@ 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') @@ -39,7 +42,6 @@ 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 fb5f0cbea..4d617f393 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', __FILE__)) +ENV['COVERAGE'] ||= 'yes' if Dir.exist?(File.expand_path('../lib', __dir__)) 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'))) - if facts - facts.each do |name, value| - add_custom_fact name.to_sym, value - end + facts&.each do |name, value| + add_custom_fact name.to_sym, value end end diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb index d3b906bfe..d3a6e23cf 100644 --- a/spec/spec_helper_acceptance.rb +++ b/spec/spec_helper_acceptance.rb @@ -1,3 +1,5 @@ +# 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 996f5ae07..420f55435 100644 --- a/spec/support/acceptance/prepare_host.rb +++ b/spec/support/acceptance/prepare_host.rb @@ -1,3 +1,5 @@ +# 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 71c754ddb..8ef388f01 100644 --- a/spec/support/acceptance/supported_versions.rb +++ b/spec/support/acceptance/supported_versions.rb @@ -1,3 +1,5 @@ +# 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 af34ab1e0..287f222f6 100644 --- a/spec/unit/puppet/provider/grafana_plugin/grafana_cli_spec.rb +++ b/spec/unit/puppet/provider/grafana_plugin/grafana_cli_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' provider_class = Puppet::Type.type(:grafana_plugin).provider(:grafana_cli) @@ -11,26 +13,25 @@ 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(['grafana-simple-json-datasource', 'jdbranham-diagram-panel']) + expect(provider_class.instances.map(&:name)).to match_array(%w[grafana-simple-json-datasource jdbranham-diagram-panel]) expect(provider_class).to have_received(:grafana_cli) end @@ -40,7 +41,6 @@ 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 32b76e675..2114b17af 100644 --- a/spec/unit/puppet/type/grafana_dashboard_type_spec.rb +++ b/spec/unit/puppet/type/grafana_dashboard_type_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Copyright 2015 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -36,11 +38,13 @@ 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') @@ -52,11 +56,13 @@ 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 80f75e3cb..33a78f21d 100644 --- a/spec/unit/puppet/type/grafana_datasource_type_spec.rb +++ b/spec/unit/puppet/type/grafana_datasource_type_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Copyright 2015 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -53,6 +55,7 @@ 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') @@ -71,7 +74,6 @@ 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 f3ffa1015..273664f45 100644 --- a/spec/unit/puppet/type/grafana_folder_type_spec.rb +++ b/spec/unit/puppet/type/grafana_folder_type_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Copyright 2015 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -30,10 +32,12 @@ 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') @@ -45,11 +49,13 @@ 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 c917c2875..305e85cbb 100644 --- a/spec/unit/puppet/type/grafana_ldap_config_spec.rb +++ b/spec/unit/puppet/type/grafana_ldap_config_spec.rb @@ -1,3 +1,5 @@ +# 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. @@ -162,3 +164,4 @@ 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 81d33b4af..a6c52043e 100644 --- a/spec/unit/puppet/type/grafana_ldap_group_mapping_spec.rb +++ b/spec/unit/puppet/type/grafana_ldap_group_mapping_spec.rb @@ -1,3 +1,5 @@ +# 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. @@ -149,3 +151,4 @@ 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 8cff01978..2a812cd04 100644 --- a/spec/unit/puppet/type/grafana_ldap_server_spec.rb +++ b/spec/unit/puppet/type/grafana_ldap_server_spec.rb @@ -1,3 +1,5 @@ +# 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. @@ -335,3 +337,4 @@ 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 e1b995366..7f51f9e30 100644 --- a/spec/unit/puppet/type/grafana_notification_type_spec.rb +++ b/spec/unit/puppet/type/grafana_notification_type_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Copyright 2015 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -38,6 +40,7 @@ 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') @@ -47,7 +50,6 @@ 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 e1959205f..770087430 100644 --- a/spec/unit/puppet/type/grafana_organization_type_spec.rb +++ b/spec/unit/puppet/type/grafana_organization_type_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Puppet::Type.type(:grafana_organization) do @@ -23,6 +25,7 @@ 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') @@ -30,7 +33,6 @@ 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 7ba911403..00a100dbd 100644 --- a/spec/unit/puppet/type/grafana_plugin_spec.rb +++ b/spec/unit/puppet/type/grafana_plugin_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Puppet::Type.type(:grafana_plugin) do let(:plugin) do @@ -8,6 +10,7 @@ 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({}) @@ -18,6 +21,7 @@ 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 a7140919f..df4f0b67c 100644 --- a/spec/unit/puppet/type/grafana_team_type_spec.rb +++ b/spec/unit/puppet/type/grafana_team_type_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Puppet::Type.type(:grafana_team) do @@ -19,6 +21,7 @@ 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') @@ -28,7 +31,6 @@ 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 c45aa90f7..ca83c6664 100644 --- a/spec/unit/puppet/type/grafana_user_type_spec.rb +++ b/spec/unit/puppet/type/grafana_user_type_spec.rb @@ -1,3 +1,5 @@ +# 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 @@ -22,12 +24,14 @@ 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') @@ -39,11 +43,13 @@ 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')