[icinga-checkins] icinga.org: chef-icinga2/master: Add function to format ruby data

git at icinga.org git at icinga.org
Tue Jul 19 18:01:45 CEST 2016


Module: chef-icinga2
Branch: master
Commit: 7a98befeacf9d487bb6d17c816f3a661be5dd5a2
URL:    https://git.icinga.org/?p=chef-icinga2.git;a=commit;h=7a98befeacf9d487bb6d17c816f3a661be5dd5a2

Author: Henry Finucane <henry.finucane at illumio.com>
Date:   Thu Mar 24 11:04:49 2016 -0700

Add function to format ruby data

I've been taking advantage of `add_node_vars` to dump as much Chef
metadata as possible into objects, but unfortunately the only
transformation being done is `.inspect`, which Icinga2 2.4.4 no
longer tolerates.

---

 libraries/icinga2.rb |   39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/libraries/icinga2.rb b/libraries/icinga2.rb
index f628bbe..191695e 100644
--- a/libraries/icinga2.rb
+++ b/libraries/icinga2.rb
@@ -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



More information about the icinga-checkins mailing list