-
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.
Make integers come out in client config JSON as integers.
There are places (for example if you pass keepalive/threshold values to the keepalive check in custom_client) where it matters about the type of the variable coming from the JSON - Sensu looks for .is_a?(Integer) and so if you quote the integers in the client config then it breaks. There is already a to_type method which was replicated in a number of places - however it wasn't recursive, and (at least in the case of the client config) you can have a hash of hashes. I've pulled this method out into a common mixin, and reused it from all the places which use it currently + added it to the custom= methods in providers so that things are munged on the way in, and also to the client_config type to make data comparisions work as expected
- Loading branch information
Showing
6 changed files
with
90 additions
and
29 deletions.
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
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
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,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 | ||
|
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,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 | ||
|