Files
puppet-nova/lib/puppet/parser/functions/to_array_of_json_strings.rb
Oliver Walsh 1cd349f893 Fix handling of nova pci MultiStrOpt params
The changes in Ie27dbbc510c73c685b239a9be4af2700a0eb42f0 did not appear to fix
the handling of the nova pci_* params. The config being written to nova.conf
remains in the ListOpt format.

As support for MultiStrOpt was added to nova_config in
I6be7bb4cea1906bd98c513bd2d01153e4643e3ac we just need to pass an array of
strings to nova_config to set these MultiStrOpt params.

Nova expects the string values to be JSON encoded so that is what the parser
function now returns.  This function has also been renamed from
'check_array_of_hash' (which it never did) to 'to_array_of_json_strings'.

The acceptable input formats are a JSON array of objects or a puppet Array of
Hashes. Perviously a "JSON with single quotes" format was used but should now
be considered deprecated as it could corrupt data.

This also removes any pci_alias entries from nova.conf when the param is not
set.

Change-Id: Ida3ecab717bc3113ba23553c559263f35c49c46a
Closes-bug: #1696955
2017-08-14 12:52:20 +01:00

39 lines
1.2 KiB
Ruby

require 'json'
def array_of_hash?(list)
return false unless list.class == Array
list.each do |e|
return false unless e.class == Hash
end
true
end
module Puppet::Parser::Functions
newfunction(:to_array_of_json_strings, :arity =>1, :type => :rvalue, :doc => "Convert
input array of hashes (optionally JSON encoded) to a puppet Array of JSON encoded Strings") do |arg|
list = arg[0]
if list.class == String
begin
begin
list = JSON.load(list)
rescue JSON::ParserError
# If parsing failed it could be a legacy format that uses single quotes.
# NB This will corrupt valid JSON data, e.g {"foo": "\'"} => {"foo": "\""}
list = JSON.load(list.gsub("'","\""))
Puppet.warning("#{arg[0]} is not valid JSON. Support for this format is deprecated and may be removed in future.")
end
rescue JSON::ParserError
raise Puppet::ParseError, "Syntax error: #{arg[0]} is not valid"
end
end
unless array_of_hash?(list)
raise Puppet::ParseError, "Syntax error: #{arg[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