diff --git a/lib/puppet/provider/sensu_check/json.rb b/lib/puppet/provider/sensu_check/json.rb index 5c333b8f2e..781e6d0c53 100644 --- a/lib/puppet/provider/sensu_check/json.rb +++ b/lib/puppet/provider/sensu_check/json.rb @@ -1,8 +1,11 @@ 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_check).provide(:json) do confine :feature => :json + include Puppet_X::Sensu::Totype def initialize(*args) super @@ -48,22 +51,8 @@ def custom end def custom=(value) - value.each { |k, v| value[k] = to_type(v) } conf['checks'][resource[:name]].delete_if { |k,v| not check_args.include?(k) } - conf['checks'][resource[:name]].merge!(value) - end - - def to_type(value) - case value - when true, 'true', 'True', :true - true - when false, 'false', 'False', :false - false - when /^([0-9])+$/ - value.to_i - else - value - end + conf['checks'][resource[:name]].merge!(to_type value) end def destroy diff --git a/lib/puppet/provider/sensu_client_config/json.rb b/lib/puppet/provider/sensu_client_config/json.rb index 80140124bd..fe867f2226 100644 --- a/lib/puppet/provider/sensu_client_config/json.rb +++ b/lib/puppet/provider/sensu_client_config/json.rb @@ -1,8 +1,11 @@ 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_client_config).provide(:json) do confine :feature => :json + include Puppet_X::Sensu::Totype def initialize(*args) super @@ -71,7 +74,7 @@ def custom def custom=(value) @conf['client'].delete_if { |k,v| not check_args.include?(k) } - @conf['client'].merge!(value) + @conf['client'].merge!(to_type value) end def safe_mode diff --git a/lib/puppet/type/sensu_check.rb b/lib/puppet/type/sensu_check.rb index 2311fd52bd..d9dc4aa796 100644 --- a/lib/puppet/type/sensu_check.rb +++ b/lib/puppet/type/sensu_check.rb @@ -1,3 +1,4 @@ +require 'puppet_x/sensu/to_type' Puppet::Type.newtype(:sensu_check) do @doc = "" @@ -52,6 +53,7 @@ def initialize(*args) newproperty(:custom) do desc "Custom check variables" + include Puppet_X::Sensu::Totype def is_to_s(hash = @is) hash.keys.sort.map {|key| "#{key} => #{hash[key]}"}.join(", ") @@ -73,19 +75,6 @@ def insync?(is) end end - def to_type(value) - case value - when true, 'true', 'True', :true - true - when false, 'false', 'False', :false - false - when /^([0-9])+$/ - value.to_i - else - value - end - end - defaultto {} end diff --git a/lib/puppet/type/sensu_client_config.rb b/lib/puppet/type/sensu_client_config.rb index 2f09208072..74b0460ae3 100644 --- a/lib/puppet/type/sensu_client_config.rb +++ b/lib/puppet/type/sensu_client_config.rb @@ -1,3 +1,4 @@ +require 'puppet_x/sensu/to_type' Puppet::Type.newtype(:sensu_client_config) do @doc = "" @@ -45,6 +46,8 @@ def initialize(*args) 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 @@ -53,6 +56,18 @@ 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 diff --git a/lib/puppet_x/sensu/to_type.rb b/lib/puppet_x/sensu/to_type.rb new file mode 100644 index 0000000000..fb5ae0b5cc --- /dev/null +++ b/lib/puppet_x/sensu/to_type.rb @@ -0,0 +1,27 @@ +module Puppet_X + module Sensu + module Totype + def to_type(value) + if value.is_a?(Hash) + new = Hash.new + value.each { |k,v| new[k] = to_type v } + new + elsif value.is_a?(Array) + value.collect { |v| to_type v } + else + case value + when true, 'true', 'True', :true + true + when false, 'false', 'False', :false + false + when /^([0-9])+$/ + value.to_i + else + value + end + end + end + end + end +end + diff --git a/spec/unit/puppet_x_sensu_totype_spec.rb b/spec/unit/puppet_x_sensu_totype_spec.rb new file mode 100644 index 0000000000..39f177e3e1 --- /dev/null +++ b/spec/unit/puppet_x_sensu_totype_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' +require 'puppet_x/sensu/to_type' + +class TotypeFixtureClass + include Puppet_X::Sensu::Totype +end + +describe TotypeFixtureClass do + let(:helper) { TotypeFixtureClass.new } + it 'should be callable with int, and return int' do + helper.to_type(1).should == 1 + end + it 'should be callable with string int, and return int' do + helper.to_type('1').should == 1 + end + it 'should be callable with string, and return string' do + helper.to_type('1 foo').should == '1 foo' + end + it 'should be callable with false' do + helper.to_type(false).should == false + end + it 'should be callable with true' do + helper.to_type(true).should == true + end + it 'should be callable with nil' do + helper.to_type(nil).should == nil + end + it 'should be callable with array and return munged array' do + helper.to_type([1, '1', '1 foo', false, true, nil]).should == [1, 1, '1 foo', false, true, nil] + end + it 'should be callable with hash and return munged hash' do + helper.to_type({:a => 1, :b => '1', :c => '1 foo', :d => false, :e => true, :f => nil}).should == {:a => 1, :b => 1, :c => '1 foo', :d => false, :e => true, :f => nil} + end + it 'should be able to recurse' do + helper.to_type({:a => 1, :b => '1', :c => '1 foo', :d => false, :e => true, :f => nil, :g => {:a => 1, :b => '1', :c => '1 foo', :d => false, :e => true, :f => nil}, :h => [1, '1', '1 foo', false, true, nil]}).should == {:a => 1, :b => 1, :c => '1 foo', :d => false, :e => true, :f => nil, :g => {:a => 1, :b => 1, :c => '1 foo', :d => false, :e => true, :f => nil}, :h => [1, 1, '1 foo', false, true, nil]} + end +end +