Skip to content

Commit

Permalink
Implement self.prefetch for zabbix_proxy
Browse files Browse the repository at this point in the history
This is another change for #570.
  • Loading branch information
baurmatt committed Jan 7, 2020
1 parent e0f6dd1 commit aa4704c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 24 deletions.
47 changes: 34 additions & 13 deletions lib/puppet/provider/zabbix_proxy/ruby.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'zabbix'))
require_relative '../zabbix'
Puppet::Type.type(:zabbix_proxy).provide(:ruby, parent: Puppet::Provider::Zabbix) do
confine feature: :zabbixapi

def self.instances
proxies = zbx.query(
method: 'proxy.get',
params: {
output: 'extend',
selectInterface: %w[interfaceid type main ip port useip]
}
)

proxies.map do |p|
# p['interface'] is an Array if the host is a active proxy
# p['interface'] is a Hash if the host is a passive proxy
new(
ensure: :present,
name: p['host'],
ipaddress: p['interface'].is_a?(Hash) ? p['interface']['ip'] : nil,
use_ip: p['interface'].is_a?(Hash) ? p['interface']['use_ip'] : nil,
mode: p['status'].to_i - 5,
port: p['interface'].is_a?(Hash) ? p['interface']['port'] : nil
)
end
end

def self.prefetch(resources)
instances.each do |prov|
if (resource = resources[prov.name])
resource.provider = prov
end
end
end

def create
# Set some vars
host = @resource[:hostname]
Expand All @@ -12,18 +43,6 @@ def create

use_ip = @resource[:use_ip]
port = @resource[:port]
templates = @resource[:templates]

# Get the template ids.
template_array = []
if templates.is_a?(Array) == true
templates.each do |template|
template_id = get_template_id(zbx, template)
template_array.push template_id
end
else
template_array.push templates
end

# Check if we need to connect via ip or fqdn
use_ip = use_ip ? 1 : 0
Expand All @@ -43,4 +62,6 @@ def create
def exists?
check_proxy(@resource[:hostname])
end

mk_resource_methods
end
12 changes: 4 additions & 8 deletions lib/puppet/type/zabbix_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,21 @@
desc 'FQDN of the machine.'
end

newparam(:ipaddress) do
newproperty(:ipaddress) do
desc 'The IP address of the machine running zabbix proxy.'
end

newparam(:use_ip) do
newproperty(:use_ip) do
desc 'Using ipadress instead of dns to connect. Is used by the zabbix-api command.'
end

newparam(:mode) do
newproperty(:mode) do
desc 'The kind of mode the proxy running. Active (0) or passive (1).'
end

newparam(:port) do
newproperty(:port) do
desc 'The port that the zabbix proxy is listening on.'
end

newparam(:templates) do
desc 'Template which should be loaded for this host.'
end

autorequire(:file) { '/etc/zabbix/api.conf' }
end
1 change: 0 additions & 1 deletion manifests/proxy.pp
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@
use_ip => $use_ip,
mode => $mode,
port => $listenport,
templates => $zbx_templates,
}

zabbix::userparameters { 'Zabbix_Proxy': template => 'Template App Zabbix Proxy', }
Expand Down
2 changes: 0 additions & 2 deletions manifests/resources/proxy.pp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
$use_ip = undef,
$mode = undef,
$port = undef,
$templates = undef,
) {
@@zabbix_proxy { $hostname:
ipaddress => $ipaddress,
use_ip => $use_ip,
mode => $mode,
port => $port,
templates => $templates,
}
}
26 changes: 26 additions & 0 deletions spec/unit/puppet/provider/zabbix_proxy/ruby.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'

describe Puppet::Type.type(:zabbix_proxy).provider(:ruby) do
let(:resource) do
Puppet::Type.type(:zabbix_proxy).new(
name: 'Testproxy'
)
end
let(:provider) { resource.provider }

it 'be an instance of the correct provider' do
expect(provider).to be_an_instance_of Puppet::Type::Zabbix_proxy::ProviderRuby
end

[:instances, :prefetch].each do |method|
it "should respond to the class method #{method}" do
expect(described_class).to respond_to(method)
end
end

[:create, :exists?].each do |method|
it "should respond to the instance method #{method}" do
expect(described_class.new).to respond_to(method)
end
end
end
33 changes: 33 additions & 0 deletions spec/unit/puppet/type/zabbix_proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'spec_helper'

describe Puppet::Type.type(:zabbix_proxy) do
describe 'when validating params' do
[
:hostname,
:provider
].each do |param|
it "should have a #{param} parameter" do
expect(described_class.attrtype(param)).to eq(:param)
end
end
end

describe 'when validating properties' do
[
:ipaddress,
:use_ip,
:mode,
:port
].each do |param|
it "should have a #{param} property" do
expect(described_class.attrtype(param)).to eq(:property)
end
end
end

describe 'namevar' do
it 'has :hostname as its namevar' do
expect(described_class.key_attributes).to eq([:hostname])
end
end
end

0 comments on commit aa4704c

Please sign in to comment.