Files
puppet-nova/lib/puppet/functions/to_array_of_json_strings.rb
Takashi Kajinami f8bde51891 pci: Drop support for "broken" json
JSON does not allow usage of single quotes and require double quotes.
We have been converting single quotes to support such invalid usage but
that was deprecated a long ago[1].

This also simplifies the logic to handle aliases. Using undef results
in the value set to service default fact.

[1] 1cd349f893

Change-Id: If1328531f33a9fe778091ce38b5c1f8072b473b8
2023-11-03 23:11:29 +09:00

37 lines
998 B
Ruby

# Convert input array of hashes (optionally JSON encoded)
# to a puppet Array of JSON encoded Strings.
Puppet::Functions.create_function(:to_array_of_json_strings) do
def _array_of_hash?(list)
return false unless list.class == Array
list.each do |e|
return false unless e.class == Hash
end
true
end
def to_array_of_json_strings(*args)
require 'json'
if (args.size != 1) then
raise Puppet::ParseError, 'to_array_of_json_strings(): Wrong number of arguments'
end
list = args[0]
if list.class == String
begin
list = JSON.load(list)
rescue JSON::ParserError
raise Puppet::ParseError, "Syntax error: #{args[0]} is not valid"
end
list = [list] unless list.class == Array
end
unless _array_of_hash?(list)
raise Puppet::ParseError, "Syntax error: #{args[0]} is not an Array or JSON encoded String"
end
rv = []
list.each do |e|
rv.push(e.to_json)
end
return rv
end
end