Skip to content

Commit

Permalink
rubocop: autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
bastelfreak committed Mar 10, 2022
1 parent 0d42b83 commit 01450ec
Show file tree
Hide file tree
Showing 52 changed files with 362 additions and 121 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
inherit_from: .rubocop_todo.yml

# Managed by modulesync - DO NOT EDIT
# https://voxpupuli.org/docs/updating-files-managed-with-modulesync/

Expand Down
47 changes: 47 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2022-03-09 21:00:49 UTC using RuboCop version 1.22.3.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 3
# Configuration parameters: MaximumRangeSize.
Lint/MissingCopEnableDirective:
Exclude:
- 'spec/acceptance/zabbix_host_spec.rb'
- 'spec/acceptance/zabbix_proxy_spec.rb'
- 'spec/unit/puppet/type/zabbix_host_spec.rb'

# Offense count: 1
Lint/MissingSuper:
Exclude:
- 'spec/serverspec_type_zabbixapi.rb'

# Offense count: 7
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
# AllowedNames: at, by, db, id, in, io, ip, of, on, os, pp, to
Naming/MethodParameterName:
Exclude:
- 'lib/puppet/provider/zabbix_proxy/ruby.rb'
- 'lib/puppet/type/zabbix_host.rb'
- 'lib/puppet/type/zabbix_template.rb'

# Offense count: 4
RSpec/EmptyExampleGroup:
Exclude:
- 'spec/classes/agent_spec.rb'
- 'spec/classes/javagateway_spec.rb'
- 'spec/classes/server_spec.rb'

# Offense count: 4
RSpec/RepeatedExampleGroupDescription:
Exclude:
- 'spec/classes/database_mysql_spec.rb'
- 'spec/defines/startup_spec.rb'

# Offense count: 1
Style/MixinUsage:
Exclude:
- 'spec/serverspec_type_zabbixapi.rb'
2 changes: 2 additions & 0 deletions lib/puppet/feature/zabbixapi.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'puppet/util/feature'

Puppet.features.add(:zabbixapi, libs: 'zabbixapi')
10 changes: 6 additions & 4 deletions lib/puppet/provider/zabbix.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# zabbix provider type for puppet
class Puppet::Provider::Zabbix < Puppet::Provider
# This method is vendored from the AWS SDK, rather than including an
Expand Down Expand Up @@ -46,15 +48,14 @@ def zbx
# Create the api connection
def self.create_connection
protocol = api_config['default']['apache_use_ssl'] == 'true' ? 'https' : 'http'
zbx = ZabbixApi.connect(
ZabbixApi.connect(
url: "#{protocol}://#{api_config['default']['zabbix_url']}/api_jsonrpc.php",
user: api_config['default']['zabbix_user'],
password: api_config['default']['zabbix_pass'],
http_user: api_config['default']['zabbix_user'],
http_password: api_config['default']['zabbix_pass'],
ignore_version: true
)
zbx
end

def create_connection
Expand All @@ -80,6 +81,7 @@ def check_host(host)
# Get the template id from the name.
def get_template_id(zbx, template)
return template if a_number?(template)

zbx.templates.get_id(host: template)
end

Expand All @@ -95,7 +97,7 @@ def transform_to_array_hash(key, value_array)
end

# Is it a number?
def a_number?(s)
s.to_s.match(%r{\A[+-]?\d+?(\.\d+)?\Z}).nil? ? false : true
def a_number?(value)
!value.to_s.match(%r{\A[+-]?\d+?(\.\d+)?\Z}).nil?
end
end
6 changes: 4 additions & 2 deletions lib/puppet/provider/zabbix_application/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative '../zabbix'
Puppet::Type.type(:zabbix_application).provide(:ruby, parent: Puppet::Provider::Zabbix) do
confine feature: :zabbixapi
Expand All @@ -23,7 +25,7 @@ def exists?

def destroy
zbx.applications.delete(application_id)
rescue => error
raise(Puppet::Error, "Zabbix Application Delete Failed\n#{error.message}")
rescue StandardError => e
raise(Puppet::Error, "Zabbix Application Delete Failed\n#{e.message}")
end
end
6 changes: 5 additions & 1 deletion lib/puppet/provider/zabbix_host/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative '../zabbix'
Puppet::Type.type(:zabbix_host).provide(:ruby, parent: Puppet::Provider::Zabbix) do
confine feature: :zabbixapi
Expand Down Expand Up @@ -94,7 +96,8 @@ def get_groupids(group_array, create)
group_array.each do |g|
id = zbx.hostgroups.get_id(name: g)
if id.nil?
raise Puppet::Error, 'The hostgroup (' + g + ') does not exist in zabbix. Please use the correct one or set group_create => true.' unless create
raise Puppet::Error, "The hostgroup (#{g}) does not exist in zabbix. Please use the correct one or set group_create => true." unless create

groupids << zbx.hostgroups.create(name: g)
else
groupids << id
Expand All @@ -108,6 +111,7 @@ def get_templateids(template_array)
template_array.each do |t|
template_id = zbx.templates.get_id(host: t)
raise Puppet::Error, "The template #{t} does not exist in Zabbix. Please use a correct one." if template_id.nil?

templateids << template_id
end
templateids
Expand Down
5 changes: 3 additions & 2 deletions lib/puppet/provider/zabbix_hostgroup/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative '../zabbix'
Puppet::Type.type(:zabbix_hostgroup).provide(:ruby, parent: Puppet::Provider::Zabbix) do
confine feature: :zabbixapi
Expand All @@ -22,8 +24,7 @@ def self.prefetch(resources)

def create
# Connect to zabbix api
hgid = zbx.hostgroups.create(name: @resource[:name])
hgid
zbx.hostgroups.create(name: @resource[:name])
end

def exists?
Expand Down
14 changes: 9 additions & 5 deletions lib/puppet/provider/zabbix_proxy/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative '../zabbix'
Puppet::Type.type(:zabbix_proxy).provide(:ruby, parent: Puppet::Provider::Zabbix) do
confine feature: :zabbixapi
Expand Down Expand Up @@ -40,13 +42,13 @@ def self.proxy_properties_hash(p)
ensure: :present,
# Proxy object properties
proxyid: p['proxyid'].to_i,
name: p['host'],
mode: status_to_mode(p['status']),
name: p['host'],
mode: status_to_mode(p['status']),
# Proxy Interface object properties
interfaceid: p['interface'].is_a?(Hash) ? p['interface']['interfaceid'] : nil,
ipaddress: p['interface'].is_a?(Hash) ? p['interface']['ip'] : nil,
use_ip: if p['interface'].is_a?(Hash) then p['interface']['useip'] == '1' ? :true : :false end,
port: p['interface'].is_a?(Hash) ? p['interface']['port'].to_i : nil
ipaddress: p['interface'].is_a?(Hash) ? p['interface']['ip'] : nil,
use_ip: if p['interface'].is_a?(Hash) then p['interface']['useip'] == '1' ? :true : :false end,
port: p['interface'].is_a?(Hash) ? p['interface']['port'].to_i : nil
}
end

Expand Down Expand Up @@ -179,6 +181,7 @@ def update

# At present, the only properties other than mode that can be updated are the interface properties applicable to passive proxies only.
raise Puppet::Error, "Can't update proxy interface properties for an active proxy" unless @property_hash[:mode] == :passive

update_interface_properties
end

Expand All @@ -195,6 +198,7 @@ def self.status_to_mode(status)

def self.mode_to_status(mode)
return 5 if mode == :active

6
end
end
2 changes: 2 additions & 0 deletions lib/puppet/provider/zabbix_template/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative '../zabbix'
Puppet::Type.type(:zabbix_template).provide(:ruby, parent: Puppet::Provider::Zabbix) do
confine feature: :zabbixapi
Expand Down
2 changes: 2 additions & 0 deletions lib/puppet/provider/zabbix_template_host/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative '../zabbix'
Puppet::Type.type(:zabbix_template_host).provide(:ruby, parent: Puppet::Provider::Zabbix) do
confine feature: :zabbixapi
Expand Down
2 changes: 2 additions & 0 deletions lib/puppet/provider/zabbix_userparameters/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative '../zabbix'
Puppet::Type.type(:zabbix_userparameters).provide(:ruby, parent: Puppet::Provider::Zabbix) do
confine feature: :zabbixapi
Expand Down
2 changes: 2 additions & 0 deletions lib/puppet/type/zabbix_application.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Puppet::Type.newtype(:zabbix_application) do
@doc = %q(Manage zabbix applications
Expand Down
3 changes: 3 additions & 0 deletions lib/puppet/type/zabbix_host.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Puppet::Type.newtype(:zabbix_host) do
ensurable do
defaultvalues
Expand All @@ -9,6 +11,7 @@ def initialize(*args)

# Migrate group to groups
return if self[:group].nil?

self[:groups] = self[:group]
delete(:group)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/puppet/type/zabbix_hostgroup.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Puppet::Type.newtype(:zabbix_hostgroup) do
@doc = 'Manage zabbix hostgroups'

Expand Down
6 changes: 5 additions & 1 deletion lib/puppet/type/zabbix_proxy.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Puppet::Type.newtype(:zabbix_proxy) do
ensurable do
defaultvalues
Expand Down Expand Up @@ -55,7 +57,7 @@ def munge_boolean_to_symbol(value)

begin
IPAddr.new(value)
rescue => e
rescue StandardError => e
raise Puppet::Error, e.to_s
end
end
Expand All @@ -74,10 +76,12 @@ def munge_boolean_to_symbol(value)
if value.is_a?(String)
raise Puppet::Error, 'invalid port' unless value =~ %r{^\d+$}
raise Puppet::Error, 'invalid port' unless value.to_i.between?(1, 65_535)

return
end
if value.is_a?(Integer)
raise Puppet::Error, 'invalid port' unless value.between?(1, 65_535)

return
end
raise Puppet::Error, 'invalid port'
Expand Down
6 changes: 5 additions & 1 deletion lib/puppet/type/zabbix_template.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# frozen_string_literal: true

Puppet::Type.newtype(:zabbix_template) do
ensurable do
defaultvalues
defaultto :present

def insync?(is)
return false if is == :present && !template_xmls_match?

super
end

Expand All @@ -18,11 +21,12 @@ def source_xml
end

def clean_xml(dirty)
dirty.gsub(%r{>\s*}, '>').gsub(%r{\s*<}, '<').gsub(%r{<date>.*<\/date>}, 'DATEWASHERE')
dirty.gsub(%r{>\s*}, '>').gsub(%r{\s*<}, '<').gsub(%r{<date>.*</date>}, 'DATEWASHERE')
end

def change_to_s(currentvalue, newvalue)
return 'Template updated' if currentvalue == :present && newvalue == :present

super
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/puppet/type/zabbix_template_host.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Puppet::Type.newtype(:zabbix_template_host) do
@doc = <<-DOC
Link or Unlink template to host.
Expand All @@ -15,7 +17,7 @@
end

newparam(:name, namevar: true) do
newvalues(%r{.+\@.+})
newvalues(%r{.+@.+})
desc 'template_name@host_name'
end

Expand Down
2 changes: 2 additions & 0 deletions lib/puppet/type/zabbix_userparameters.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..'))
Puppet::Type.newtype(:zabbix_userparameters) do
ensurable do
Expand Down
12 changes: 9 additions & 3 deletions spec/acceptance/agent_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper_acceptance'

supported_versions.each do |version|
Expand Down Expand Up @@ -44,14 +46,16 @@ class { 'zabbix::agent':
listenip => '127.0.0.1',
zabbix_version => '#{version}',
}
EOS
EOS
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end

describe file('/etc/zabbix/zabbix_agentd.conf') do
its(:content) { is_expected.to match %r{ListenIP=127.0.0.1} }
end
end

context 'With ListenIP set to lo' do
it 'works idempotently with no errors' do
pp = <<-EOS
Expand All @@ -61,10 +65,11 @@ class { 'zabbix::agent':
listenip => 'lo',
zabbix_version => '#{version}',
}
EOS
EOS
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end

context 'With ListenIP set to an IP-Address' do
it 'works idempotently with no errors' do
pp = <<-EOS
Expand All @@ -74,10 +79,11 @@ class { 'zabbix::agent':
listenip => '127.0.0.1',
zabbix_version => '#{version}',
}
EOS
EOS
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end

describe file('/etc/zabbix/zabbix_agentd.conf') do
its(:content) { is_expected.to match %r{ListenIP=127.0.0.1} }
end
Expand Down
2 changes: 2 additions & 0 deletions spec/acceptance/server_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper_acceptance'
describe 'zabbix::server class' do
context 'default parameters' do
Expand Down
Loading

0 comments on commit 01450ec

Please sign in to comment.