2011-04-30 15:36:25 +02:00
|
|
|
#
|
2011-08-05 09:25:03 +02:00
|
|
|
# parsejson.rb
|
2011-04-30 15:36:25 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
module Puppet::Parser::Functions
|
2015-09-21 19:56:08 +02:00
|
|
|
newfunction(:parsejson, :type => :rvalue, :doc => <<-EOS
|
2015-08-24 21:00:18 +02:00
|
|
|
This function accepts JSON as a string and converts it into the correct
|
|
|
|
Puppet structure.
|
|
|
|
|
|
|
|
The optional second argument can be used to pass a default value that will
|
|
|
|
be returned if the parsing of YAML string have failed.
|
|
|
|
EOS
|
2011-04-30 15:36:25 +02:00
|
|
|
) do |arguments|
|
2015-08-24 21:00:18 +02:00
|
|
|
raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless arguments.length >= 1
|
2011-06-29 22:21:55 +02:00
|
|
|
|
2015-08-24 21:00:18 +02:00
|
|
|
begin
|
|
|
|
PSON::load(arguments[0]) || arguments[1]
|
2015-09-21 19:56:08 +02:00
|
|
|
rescue Exception => e
|
|
|
|
if arguments[1]
|
|
|
|
arguments[1]
|
|
|
|
else
|
|
|
|
raise e
|
|
|
|
end
|
2011-06-29 22:21:55 +02:00
|
|
|
end
|
|
|
|
|
2011-04-30 15:36:25 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# vim: set ts=2 sw=2 et :
|