Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom client attributes #90

Merged
merged 3 commits into from
Jun 20, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions lib/puppet/provider/sensu_check/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,12 @@ def check_args
end

def custom
tmp = {}
conf['checks'][resource[:name]].each do |k,v|
if v.is_a?( Fixnum )
tmp.merge!( k => v.to_s )
else
tmp.merge!( k => v )
end
end
check_args.each do | del_arg |
tmp.delete(del_arg)
end
tmp
conf['checks'][resource[:name]].reject { |k,v| check_args.include?(k) }
end

def custom=(value)
tmp = custom
tmp.each_key do |k|
conf['checks'][resource[:name]].delete(k) unless check_args.include?(k)
end
value.each do | k, v |
conf['checks'][resource[:name]][ k ] = to_type( v )
end
conf['checks'][resource[:name]].delete_if { |k,v| not check_args.include?(k) }
conf['checks'][resource[:name]].merge!(value)
end

def to_type(value)
Expand Down
14 changes: 14 additions & 0 deletions lib/puppet/provider/sensu_client_config/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def create
self.address = resource[:address]
self.subscriptions = resource[:subscriptions]
self.safe_mode = resource[:safe_mode]
self.custom = resource[:custom] unless resource[:custom].nil?
end

def destroy
Expand All @@ -36,6 +37,10 @@ def exists?
@conf.has_key?('client')
end

def check_args
['name', 'address', 'subscriptions', 'safe_mode']
end

def client_name
@conf['client']['name']
end
Expand All @@ -60,6 +65,15 @@ def subscriptions=(value)
@conf['client']['subscriptions'] = value
end

def custom
@conf['client'].reject { |k,v| check_args.include?(k) }
end

def custom=(value)
@conf['client'].delete_if { |k,v| not check_args.include?(k) }
@conf['client'].merge!(value)
end

def safe_mode
case @conf['client']['safe_mode']
when true
Expand Down
12 changes: 11 additions & 1 deletion lib/puppet/type/sensu_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ def initialize(*args)
end

newproperty(:custom) do
desc "custom variable"
desc "Custom check variables"

def is_to_s(hash = @is)
hash.keys.sort.map {|key| "#{key} => #{hash[key]}"}.join(", ")
end

def should_to_s(hash = @should)
hash.keys.sort.map {|key| "#{key} => #{hash[key]}"}.join(", ")
end

defaultto {}
end

newproperty(:type) do
Expand Down
14 changes: 14 additions & 0 deletions lib/puppet/type/sensu_client_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ def initialize(*args)
newvalues(:true, :false)
end

newproperty(:custom) do
desc "Custom client variables"

def is_to_s(hash = @is)
hash.keys.sort.map {|key| "#{key} => #{hash[key]}"}.join(", ")
end

def should_to_s(hash = @should)
hash.keys.sort.map {|key| "#{key} => #{hash[key]}"}.join(", ")
end

defaultto {}
end

autorequire(:package) do
['sensu']
end
Expand Down
2 changes: 2 additions & 0 deletions manifests/client.pp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
$enabled = 'true',
$purge_config = 'false',
$safe_mode = false,
$custom = {}
) {

$ensure = $enabled ? {
Expand All @@ -29,6 +30,7 @@
address => $address,
subscriptions => $subscriptions,
safe_mode => $safe_mode,
custom => $custom,
}

}
2 changes: 2 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
$subscriptions = [],
$client_address = $::ipaddress,
$client_name = $::fqdn,
$client_custom = {},
$plugins = [],
$purge_config = false,
$use_embedded_ruby = false,
Expand Down Expand Up @@ -108,6 +109,7 @@
enabled => $client,
purge_config => $purge_config,
safe_mode => $safe_mode,
custom => $client_custom,
}

class { 'sensu::service::client': enabled => $client }
Expand Down
11 changes: 7 additions & 4 deletions spec/classes/sensu_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
'client_name' => 'host.domain.com',
'address' => '2.3.4.5',
'subscriptions' => [],
'ensure' => 'present'
'ensure' => 'present',
'custom' => {}
) }

end
Expand All @@ -21,15 +22,17 @@
:address => '1.2.3.4',
:subscriptions => ['all'],
:client_name => 'myclient',
:safe_mode => true
:safe_mode => true,
:custom => { 'foo' => 'bar', 'bool' => true }
} }

it { should contain_sensu_client_config('host.domain.com').with(
'client_name' => 'myclient',
'address' => '1.2.3.4',
'subscriptions' => ['all'],
'ensure' => 'present',
'safe_mode' => true
'safe_mode' => true,
'custom' => { 'foo' => 'bar', 'bool' => true }
) }

end
Expand All @@ -39,7 +42,7 @@
let(:params) { { :enabled => false } }
it { should contain_sensu_client_config('host.domain.com').with_ensure('absent') }
end

context 'purge_configs' do
let(:params) { { :purge_config => true } }

Expand Down