Skip to content

Commit

Permalink
Merge pull request #121 from bobtfish/fix_integers_in_output
Browse files Browse the repository at this point in the history
Make integers come out in JSON as integers.
  • Loading branch information
jamtur01 committed Dec 30, 2013
2 parents 3cf0363 + 4f736d3 commit 2f79d3b
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 29 deletions.
19 changes: 4 additions & 15 deletions lib/puppet/provider/sensu_check/json.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/puppet/provider/sensu_client_config/json.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 2 additions & 13 deletions lib/puppet/type/sensu_check.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'puppet_x/sensu/to_type'
Puppet::Type.newtype(:sensu_check) do
@doc = ""

Expand Down Expand Up @@ -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(", ")
Expand All @@ -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

Expand Down
15 changes: 15 additions & 0 deletions lib/puppet/type/sensu_client_config.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'puppet_x/sensu/to_type'
Puppet::Type.newtype(:sensu_client_config) do
@doc = ""

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
27 changes: 27 additions & 0 deletions lib/puppet_x/sensu/to_type.rb
Original file line number Diff line number Diff line change
@@ -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

38 changes: 38 additions & 0 deletions spec/unit/puppet_x_sensu_totype_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2f79d3b

Please sign in to comment.