Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
Merge branch 'escaping' of git://github.com/hfinucane/chef-icinga2
Browse files Browse the repository at this point in the history
  • Loading branch information
vkhatri committed Jul 17, 2016
2 parents b10d5f5 + fe5b40c commit 3a4a872
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
39 changes: 39 additions & 0 deletions libraries/icinga2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,42 @@ def icinga2_resource_create?(a)
false
end
end

def icinga_format(toplevel)
case toplevel
when Hash
rval = "{ "
when Array
rval = "[ "
when NilClass
return 'null'
when String, Float, Fixnum
return toplevel.inspect
when Symbol
return toplevel.to_s.inspect
else
return toplevel.inspect.to_s.inspect
end

rval += toplevel.collect do |k,v|
prefix = ''

target = k
case toplevel
when Hash
prefix += "#{icinga_format(k)} = "
target = v
end

prefix += icinga_format(target)
end.join(', ')

case toplevel
when Hash
rval += " }"
when Array
rval += " ]"
end

return rval
end
44 changes: 44 additions & 0 deletions spec/unit/helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

# This seems wrong, but I don't actually know how to do this
require_relative '../../libraries/icinga2.rb'

describe "#icinga_format" do
subject { icinga_format }

it "handles hashes" do
expect(icinga_format({:foo => :bar})).to eq('{ "foo" = "bar" }')
end

it "handles arrays" do
expect(icinga_format([1,3,5])).to eq('[ 1, 3, 5 ]')
end

it "handles strings" do
expect(icinga_format("foo")).to eq('"foo"')
end

it "handles floats" do
expect(icinga_format(1.2)).to eq('1.2')
end

it "handles fixnums" do
expect(icinga_format(10)).to eq('10')
end

it "handles nulls" do
expect(icinga_format(nil)).to eq("null")
end

it "handles nesting" do
expect(icinga_format({:foo => [:bar, {1=>2}, {1=>[:a,:b,:c]}]})).to eq('{ "foo" = [ "bar", { 1 = 2 }, { 1 = [ "a", "b", "c" ] } ] }')
end

it "handles arbitrary objects by stringifying them" do
expect(icinga_format(StandardError.new)).to eq('"#<StandardError: StandardError>"')
end

it "Arbitrary stringifying works with nesting" do
expect(icinga_format({"err"=>StandardError.new, "list"=>[{"err"=>IOError.new}]})).to eq('{ "err" = "#<StandardError: StandardError>", "list" = [ { "err" = "#<IOError: IOError>" } ] }')
end

end
8 changes: 4 additions & 4 deletions templates/default/object.environment.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@ object Host <%= node_hash['fqdn'].inspect %> {
<% if var && value && value.is_a?(Hash) -%>
<% value.each do |a, v|-%>
<% if a && v && v.is_a?(Hash) && !v.empty?-%>
vars.<%= var -%><%= [a].inspect -%> = {
vars["<%= var -%>"][<%= a.inspect -%>] = {
<% v.each do |a1, v1|%>
<% if a1 && v1 %>
<%= a1 -%> = <%= v1.inspect %>
"<%= a1 -%>" = <%= icinga_format(v1) %>
<% end -%>
<% end -%>
}
<% else -%>
vars.<%= var -%><%= [a].inspect -%> = <%= v.inspect %>
vars["<%= var -%>"][<%= a.inspect -%>] = <%= icinga_format(v) %>
<% end -%>
<% end -%>
<% elsif var && value -%>
vars.<%= var -%> = <%= value.inspect %>
vars["<%= var -%>"] = <%= icinga_format(value) %>
<% end -%>
<% end -%>
<% end -%>
Expand Down

0 comments on commit 3a4a872

Please sign in to comment.