Skip to content

Commit

Permalink
Merge pull request #19 from sensu/check_name
Browse files Browse the repository at this point in the history
Add check_name for checks
  • Loading branch information
ghoneycutt authored Aug 19, 2019
2 parents 1ee798f + 45aab3e commit 3f22645
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
18 changes: 9 additions & 9 deletions lib/puppet/provider/sensuclassic_check/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,32 @@ def flush

def pre_create
conf['checks'] = {}
conf['checks'][resource[:name]] = {}
conf['checks'][resource[:check_name]] = {}
end

def sort_properties!
conf['checks'][resource[:name]] = Hash[conf['checks'][resource[:name]].sort]
conf['checks'][resource[:check_name]] = Hash[conf['checks'][resource[:check_name]].sort]
end

def is_property?(prop)
SENSU_CHECK_PROPERTIES.map(&:to_s).include? prop
end

def custom
conf['checks'][resource[:name]].reject { |k,v| is_property?(k) }
conf['checks'][resource[:check_name]].reject { |k,v| is_property?(k) }
end

def custom=(value)
conf['checks'][resource[:name]].delete_if { |k,v| not is_property?(k) }
conf['checks'][resource[:name]].merge!(to_type(value))
conf['checks'][resource[:check_name]].delete_if { |k,v| not is_property?(k) }
conf['checks'][resource[:check_name]].merge!(to_type(value))
end

def destroy
@conf = nil
end

def exists?
conf.has_key?('checks') and conf['checks'].has_key?(resource[:name])
conf.has_key?('checks') and conf['checks'].has_key?(resource[:check_name])
end

def config_file
Expand All @@ -121,15 +121,15 @@ def config_file
end

def get_property(property)
value = conf['checks'][resource[:name]][property.to_s]
value = conf['checks'][resource[:check_name]][property.to_s]
value.nil? ? :absent : value
end

def set_property(property, value)
if value == :absent
conf['checks'][resource[:name]].delete(property.to_s)
conf['checks'][resource[:check_name]].delete(property.to_s)
else
conf['checks'][resource[:name]][property.to_s] = value
conf['checks'][resource[:check_name]][property.to_s] = value
end
end
end
7 changes: 7 additions & 0 deletions lib/puppet/type/sensuclassic_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def initialize(*args)
desc "The name of the check."
end

newparam(:check_name) do
desc "The name of the check, defaults to value of `name`"
defaultto do
@resource[:name]
end
end

newproperty(:command) do
desc "Command to be run by the check"
newvalues(/.*/, :absent)
Expand Down
19 changes: 15 additions & 4 deletions manifests/check.pp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
#
# This define manages Sensu checks
#
# @param check_name The name of the check.
# If not specified, defaults to the name of the resource. Overriding it
# allows the check name to differ from the check configuration filename,
# useful when having checks of the same name from different proxy clients.
# (see the 'source' parameter)
#
# @param command The check command to run
#
# @param ensure Whether the check should be present or not.
Expand Down Expand Up @@ -102,6 +108,7 @@
# of the Hash value.
#
define sensuclassic::check (
Optional[String] $check_name = undef,
Optional[String] $command = undef,
Enum['present','absent'] $ensure = 'present',
Optional[String] $type = undef,
Expand Down Expand Up @@ -153,7 +160,11 @@
}
}

$check_name = regsubst(regsubst($name, ' ', '_', 'G'), '[\(\)]', '', 'G')
$check_file_name = regsubst(regsubst($name, ' ', '_', 'G'), '[\(\)]', '', 'G')
$check_config_name = $check_name ? {
undef => $check_file_name,
default => $check_name,
}

# If cron is specified, interval should not be written to the configuration
if $cron and $cron != 'absent' {
Expand Down Expand Up @@ -203,7 +214,7 @@
}
}

# This Hash map will ultimately exist at `{"checks" => {"$check_name" =>
# This Hash map will ultimately exist at `{"checks" => {"$check_config_name" =>
# $check_config}}`
$check_config_start = {
type => $type,
Expand Down Expand Up @@ -251,7 +262,7 @@

# Merge together the "checks" scope with any arbitrary config specified via
# `content`.
$checks_scope_start = { $check_name => $check_config }
$checks_scope_start = { $check_config_name => $check_config }
if $content['checks'] == undef {
$checks_scope = { 'checks' => $checks_scope_start }
} else {
Expand All @@ -262,7 +273,7 @@
# on top of any arbitrary plugin and extension configuration in $content.
$content_real = $content + $checks_scope

sensuclassic::write_json { "${sensuclassic::conf_dir}/checks/${check_name}.json":
sensuclassic::write_json { "${sensuclassic::conf_dir}/checks/${check_file_name}.json":
ensure => $ensure,
content => $content_real,
owner => $sensuclassic::user,
Expand Down
12 changes: 12 additions & 0 deletions spec/defines/sensuclassic_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,16 @@
end
end

describe 'check_name' do
context 'check_name defined' do
let(:params_override) do
{ check_name: 'testcheck' }
end
let(:expected_content_new) do
check = expected_content['checks']['mycheck']
{ 'checks' => { 'testcheck' => check } }
end
it { should contain_sensuclassic__write_json(fpath).with_content(expected_content_new) }
end
end
end
11 changes: 11 additions & 0 deletions spec/unit/sensuclassic_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
let(:resource_hash_override) { {} }
let(:resource_hash) { resource_hash_base.merge(resource_hash_override) }

describe 'check_name parameter' do
subject { described_class.new(resource_hash)[:check_name] }
it 'defaults to name' do
is_expected.to eq resource_hash[:title]
end
describe 'check_name defined' do
let(:resource_hash_override) { { check_name: 'foo' } }
it { is_expected.to eq 'foo' }
end
end

describe 'contacts parameter' do
subject { described_class.new(resource_hash)[:contacts] }

Expand Down

0 comments on commit 3f22645

Please sign in to comment.