Skip to content

Commit

Permalink
Properly require zabbixapi gem
Browse files Browse the repository at this point in the history
  • Loading branch information
baurmatt committed Dec 12, 2018
1 parent 09f50f8 commit 2e92089
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 51 deletions.
3 changes: 3 additions & 0 deletions lib/puppet/feature/zabbixapi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'puppet/util/feature'

Puppet.features.add(:zabbixapi, libs: 'zabbixapi')
6 changes: 0 additions & 6 deletions lib/puppet/provider/zabbix.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
# zabbix provider type for puppet
class Puppet::Provider::Zabbix < Puppet::Provider
# Require the zabbixapi gem
def self.require_zabbix
require 'zabbixapi'
end

# Create the api connection
def self.create_connection(zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl)
protocol = apache_use_ssl ? 'https' : 'http'
Expand Down Expand Up @@ -37,7 +32,6 @@ def self.check_host(host, zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl)

# Check if proxy exists. When error raised, return false.
def self.check_proxy(host, zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl)
require_zabbix
zbx = create_connection(zabbix_url, zabbix_user, zabbix_pass, apache_use_ssl)
zbx.proxies.get_id(host: host)
rescue Puppet::ExecutionFailure
Expand Down
6 changes: 3 additions & 3 deletions lib/puppet/provider/zabbix_application/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'zabbix'))
require_relative '../zabbix'
Puppet::Type.type(:zabbix_application).provide(:ruby, parent: Puppet::Provider::Zabbix) do
def connect
self.class.require_zabbix if @resource[:zabbix_url] != ''
confine feature: :zabbixapi

def connect
@zbx ||= self.class.create_connection(@resource[:zabbix_url], @resource[:zabbix_user], @resource[:zabbix_pass], @resource[:apache_use_ssl])
@zbx
end
Expand Down
18 changes: 5 additions & 13 deletions lib/puppet/provider/zabbix_host/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'zabbix'))
require_relative '../zabbix'
Puppet::Type.type(:zabbix_host).provide(:ruby, parent: Puppet::Provider::Zabbix) do
def create
zabbix_url = @resource[:zabbix_url]

self.class.require_zabbix if zabbix_url != ''
confine feature: :zabbixapi

def create
# Set some vars
host = @resource[:hostname]
ipaddress = @resource[:ipaddress]
Expand Down Expand Up @@ -77,11 +75,8 @@ def create
end

def exists?
zabbix_url = @resource[:zabbix_url]

self.class.require_zabbix if zabbix_url != ''

host = @resource[:hostname]
zabbix_url = @resource[:zabbix_url]
zabbix_user = @resource[:zabbix_user]
zabbix_pass = @resource[:zabbix_pass]
apache_use_ssl = @resource[:apache_use_ssl]
Expand All @@ -108,11 +103,8 @@ def exists?
end

def destroy
zabbix_url = @resource[:zabbix_url]

self.class.require_zabbix if zabbix_url != ''

host = @resource[:hostname]
zabbix_url = @resource[:zabbix_url]
zabbix_user = @resource[:zabbix_user]
zabbix_pass = @resource[:zabbix_pass]
apache_use_ssl = @resource[:apache_use_ssl]
Expand Down
4 changes: 1 addition & 3 deletions lib/puppet/provider/zabbix_hostgroup/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'zabbix'))
require_relative '../zabbix'
Puppet::Type.type(:zabbix_hostgroup).provide(:ruby, parent: Puppet::Provider::Zabbix) do
def connect
self.class.require_zabbix if @resource[:zabbix_url] != ''

@zbx ||= self.class.create_connection(@resource[:zabbix_url], @resource[:zabbix_user], @resource[:zabbix_pass], @resource[:apache_use_ssl])
@zbx
end
Expand Down
14 changes: 4 additions & 10 deletions lib/puppet/provider/zabbix_proxy/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'zabbix'))
Puppet::Type.type(:zabbix_proxy).provide(:ruby, parent: Puppet::Provider::Zabbix) do
def create
zabbix_url = @resource[:zabbix_url]

# Check if we have an zabbix_url. If so, we are about
# to run on the zabbix-server.
self.class.require_zabbix if zabbix_url != ''
confine feature: :zabbixapi

def create
# Set some vars
host = @resource[:hostname]
ipaddress = @resource[:ipaddress]
Expand All @@ -17,6 +13,7 @@ def create
use_ip = @resource[:use_ip]
port = @resource[:port]
templates = @resource[:templates]
zabbix_url = @resource[:zabbix_url]
zabbix_user = @resource[:zabbix_user]
zabbix_pass = @resource[:zabbix_pass]
apache_use_ssl = @resource[:apache_use_ssl]
Expand Down Expand Up @@ -50,11 +47,8 @@ def create
end

def exists?
zabbix_url = @resource[:zabbix_url]

self.class.require_zabbix if zabbix_url != ''

host = @resource[:hostname]
zabbix_url = @resource[:zabbix_url]
zabbix_user = @resource[:zabbix_user]
zabbix_pass = @resource[:zabbix_pass]
apache_use_ssl = @resource[:apache_use_ssl]
Expand Down
5 changes: 3 additions & 2 deletions lib/puppet/provider/zabbix_template/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'zabbix'))
require_relative '../zabbix'
Puppet::Type.type(:zabbix_template).provide(:ruby, parent: Puppet::Provider::Zabbix) do
confine feature: :zabbixapi

def connect
self.class.require_zabbix if @resource[:zabbix_url] != ''
@zbx ||= self.class.create_connection(@resource[:zabbix_url], @resource[:zabbix_user], @resource[:zabbix_pass], @resource[:apache_use_ssl])
@zbx
end
Expand Down
5 changes: 2 additions & 3 deletions lib/puppet/provider/zabbix_template_host/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'zabbix'))
require_relative '../zabbix'
Puppet::Type.type(:zabbix_template_host).provide(:ruby, parent: Puppet::Provider::Zabbix) do
confine feature: :zabbixapi
def template_name
@template_name ||= @resource[:name].split('@')[0]
end
Expand All @@ -19,8 +20,6 @@ def hostid
end

def connect
self.class.require_zabbix if @resource[:zabbix_url] != ''

@zbx ||= self.class.create_connection(@resource[:zabbix_url], @resource[:zabbix_user], @resource[:zabbix_pass], @resource[:apache_use_ssl])
@zbx
end
Expand Down
18 changes: 7 additions & 11 deletions lib/puppet/provider/zabbix_userparameters/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'zabbix'))
require_relative '../zabbix'
Puppet::Type.type(:zabbix_userparameters).provide(:ruby, parent: Puppet::Provider::Zabbix) do
def create
zabbix_url = @resource[:zabbix_url]

self.class.require_zabbix if zabbix_url != ''
confine feature: :zabbixapi

def create
host = @resource[:hostname]
template = @resource[:template]
zabbix_url = @resource[:zabbix_url]
zabbix_user = @resource[:zabbix_user]
zabbix_pass = @resource[:zabbix_pass]
apache_use_ssl = @resource[:apache_use_ssl]
Expand All @@ -23,12 +22,9 @@ def create
end

def exists?
zabbix_url = @resource[:zabbix_url]

self.class.require_zabbix if zabbix_url != ''

host = @resource[:hostname]
template = @resource[:template]
zabbix_url = @resource[:zabbix_url]
zabbix_user = @resource[:zabbix_user]
zabbix_pass = @resource[:zabbix_pass]
apache_use_ssl = @resource[:apache_use_ssl]
Expand All @@ -37,7 +33,7 @@ def exists?
end

def destroy
zabbix_url = @resource[:zabbix_url]
self.class.require_zabbix if zabbix_url != ''
# TODO: Implement?!
true
end
end

0 comments on commit 2e92089

Please sign in to comment.