Skip to content

Commit

Permalink
Add custom parameter to subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
bodgit committed Aug 19, 2014
1 parent ff37f7b commit 8d71bf5
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/puppet/provider/sensu_client_subscription/json.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
require 'rubygems' if RUBY_VERSION < '1.9.0' && Puppet.version < '3'
require 'json' if Puppet.features.json?

begin
require 'puppet_x/sensu/to_type'
rescue LoadError => e
libdir = Pathname.new(__FILE__).parent.parent.parent.parent
require File.join(libdir, 'puppet_x/sensu/to_type')
end

Puppet::Type.type(:sensu_client_subscription).provide(:json) do
confine :feature => :json
include Puppet_X::Sensu::Totype

def conf
begin
Expand All @@ -23,13 +31,19 @@ def config_file
end

def create
conf['client'] = {'subscriptions' => [ resource[:name] ] }
conf['client'] = {}
self.subscriptions = [ resource[:name] ]
self.custom = resource[:custom] unless resource[:custom].nil?
end

def destroy
@conf = nil
end

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

def subscriptions
conf['client']['subscriptions']
end
Expand All @@ -38,6 +52,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!(to_type(value))
end

def exists?
conf.has_key?('client') && conf['client'].has_key?('subscriptions')
end
Expand Down
34 changes: 34 additions & 0 deletions lib/puppet/type/sensu_client_subscription.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
begin
require 'puppet_x/sensu/to_type'
rescue LoadError => e
libdir = Pathname.new(__FILE__).parent.parent.parent
require File.join(libdir, 'puppet_x/sensu/to_type')
end
Puppet::Type.newtype(:sensu_client_subscription) do
@doc = ""

Expand Down Expand Up @@ -38,6 +44,34 @@ def initialize(*args)
end
end

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

include Puppet_X::Sensu::Totype

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

def insync?(is)
if defined? @should[0]
if is == @should[0].each { |k, v| value[k] = to_type(v) }
true
else
false
end
else
true
end
end

defaultto {}
end

autorequire(:package) do
['sensu']
end
Expand Down
6 changes: 6 additions & 0 deletions manifests/subscription.pp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
# String. Whether the check should be present or not
# Default: present
# Valid values: present, absent

# [*custom*]
# Hash. Custom client variables
# Default: {}
#
define sensu::subscription (
$ensure = 'present',
$custom = {},
) {

validate_re($ensure, ['^present$', '^absent$'] )
Expand All @@ -25,6 +30,7 @@

sensu_client_subscription { $name:
ensure => $ensure,
custom => $custom,
notify => Class['sensu::client::service'],
}

Expand Down
35 changes: 35 additions & 0 deletions spec/defines/sensu_subscription_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'spec_helper'

describe 'sensu::subscription', :type => :define do
context 'without whitespace in name' do
let(:title) { 'mysubscription' }

context 'defaults' do
it { should contain_sensu_client_subscription('mysubscription') }
end

context 'setting params' do
let(:params) { {
:custom => { 'a' => 'b', 'array' => [ 'c', 'd' ] },
} }

it { should contain_sensu_client_subscription('mysubscription').with(
:custom => { 'a' => 'b', 'array' => [ 'c', 'd' ] }
) }
end

context 'ensure absent' do
let(:params) { {
:ensure => 'absent',
} }

it { should contain_sensu_client_subscription('mysubscription').with_ensure('absent') }
end
end

context 'notifications' do
let(:title) { 'mysubscription' }

it { should contain_sensu_client_subscription('mysubscription').with(:notify => ['Class[Sensu::Client::Service]' ]) }
end
end

0 comments on commit 8d71bf5

Please sign in to comment.