-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e90104f
commit f0bba4d
Showing
8 changed files
with
231 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require 'rubygems' if RUBY_VERSION < '1.9.0' && Puppet.version < '3' | ||
require 'json' if Puppet.features.json? | ||
|
||
require 'puppet_x/sensu/to_type' | ||
|
||
Puppet::Type.type(:sensu_filter).provide(:json) do | ||
confine :feature => :json | ||
include Puppet_X::Sensu::Totype | ||
|
||
def initialize(*args) | ||
super | ||
@conf = nil | ||
end | ||
|
||
def conf | ||
begin | ||
@conf ||= JSON.parse(File.read("/etc/sensu/conf.d/filters/#{resource[:name]}.json")) | ||
rescue | ||
@conf ||= {} | ||
end | ||
end | ||
|
||
def flush | ||
File.open("/etc/sensu/conf.d/filters/#{resource[:name]}.json", 'w') do |f| | ||
f.puts JSON.pretty_generate(conf) | ||
end | ||
end | ||
|
||
def create | ||
conf['filter'] = {} | ||
conf['filter'][resource[:name]] = {} | ||
self.negate = resource[:negate] | ||
end | ||
|
||
def destroy | ||
conf = nil | ||
end | ||
|
||
def exists? | ||
conf.has_key?('filter') and conf['filter'].has_key?(resource[:name]) | ||
end | ||
|
||
def negate | ||
case conf['filter'][resource[:name]]['negate'] | ||
when true | ||
:true | ||
else | ||
:false | ||
end | ||
end | ||
|
||
def negate=(value) | ||
case value | ||
when true, 'true', 'True', :true, 1 | ||
conf['filter'][resource[:name]]['negate'] = true | ||
else | ||
conf['filter'][resource[:name]]['negate'] = false | ||
end | ||
end | ||
|
||
def attributes | ||
conf['filter'][resource[:name]]['attributes'] | ||
end | ||
|
||
def attributes=(value) | ||
conf['filter'][resource[:name]]['attributes'].merge!(to_type value) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'puppet_x/sensu/to_type' | ||
Puppet::Type.newtype(:sensu_filter) do | ||
@doc = "" | ||
|
||
def initialize(*args) | ||
super | ||
end | ||
|
||
ensurable do | ||
newvalue(:present) do | ||
provider.create | ||
end | ||
|
||
newvalue(:absent) do | ||
provider.destroy | ||
end | ||
|
||
defaultto :present | ||
end | ||
|
||
newparam(:name) do | ||
desc "The name of the filter." | ||
end | ||
|
||
|
||
newparam(:attributes) do | ||
desc "" | ||
|
||
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 | ||
|
||
newproperty(:negate) do | ||
desc "" | ||
end | ||
|
||
autorequire(:package) do | ||
['sensu'] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# = Define: sensu::filter | ||
# | ||
# Defines Sensu filters | ||
# | ||
# == Parameters | ||
# | ||
# [*ensure*] | ||
# String. Whether the check should be present or not | ||
# Default: present | ||
# Valid values: present, absent | ||
# | ||
# [*negate*] | ||
# Boolean. Negate the filter | ||
# Default: undef | ||
# Valid values: true, false | ||
# | ||
# [*attributes*] | ||
# Hash. Hash of attributes for the filter | ||
# Default: undef | ||
# | ||
define sensu::filter ( | ||
$ensure = 'present', | ||
$negate = undef, | ||
$attributes = undef, | ||
) { | ||
|
||
validate_re($ensure, ['^present$', '^absent$'] ) | ||
if $negate { | ||
validate_bool($negate) | ||
} | ||
|
||
if $attributes and !is_hash($attributes) { | ||
fail('attributes must be a hash') | ||
} | ||
|
||
file { "/etc/sensu/conf.d/filters/${name}.json": | ||
ensure => $ensure, | ||
owner => 'sensu', | ||
group => 'sensu', | ||
mode => '0444', | ||
before => Sensu_client_subscription[$name], | ||
} | ||
|
||
sensu_filter { $name: | ||
ensure => $ensure, | ||
negate => $negate, | ||
attributes => $attributes, | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'spec_helper' | ||
|
||
describe 'sensu::filter', :type => :define do | ||
let(:title) { 'myfilter' } | ||
|
||
context 'negate' do | ||
let(:params) { {:negate => false } } | ||
it { should contain_file('/etc/sensu/conf.d/filters/myfilter.json').with(:ensure => 'present') } | ||
it { should contain_sensu_filter('myfilter').with( :negate => false ) } | ||
end | ||
|
||
context 'attributes' do | ||
let(:params) { { | ||
:attributes => { 'a' => 'b', 'c' => 'd' } | ||
} } | ||
it { should contain_file('/etc/sensu/conf.d/filters/myfilter.json').with(:ensure => 'present') } | ||
it { should contain_sensu_filter('myfilter').with(:attributes => { 'a' => 'b', 'c' => 'd' } ) } | ||
end | ||
|
||
context 'absent' do | ||
let(:params) { { | ||
:ensure => 'absent' | ||
} } | ||
it { should contain_file('/etc/sensu/conf.d/filters/myfilter.json').with(:ensure => 'absent') } | ||
it { should contain_sensu_filter('myfilter').with(:ensure => 'absent') } | ||
end | ||
|
||
end |