Use puppet4 functions-api
Change-Id: I3aad89ddc53d08c4357dd278f8dc6560137ab0ae
This commit is contained in:
parent
54f25f2abf
commit
d65396b7b4
43
lib/puppet/functions/to_array_of_json_strings.rb
Normal file
43
lib/puppet/functions/to_array_of_json_strings.rb
Normal file
@ -0,0 +1,43 @@
|
||||
# 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
|
||||
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("#{args[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: #{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
|
@ -1,30 +0,0 @@
|
||||
require 'json'
|
||||
|
||||
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 list.class == Array or (list.each { |e| return false unless e.class == Hash })
|
||||
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
|
51
spec/functions/to_array_of_json_strings_spec.rb
Normal file
51
spec/functions/to_array_of_json_strings_spec.rb
Normal file
@ -0,0 +1,51 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe 'to_array_of_json_strings' do
|
||||
it 'exists' do
|
||||
is_expected.not_to eq(nil)
|
||||
end
|
||||
|
||||
it 'fails with no arguments' do
|
||||
is_expected.to run.with_params.and_raise_error(Puppet::ParseError)
|
||||
end
|
||||
|
||||
it 'fails with too many arguments' do
|
||||
is_expected.to run.with_params('arg1', 'arg2').and_raise_error(Puppet::ParseError)
|
||||
end
|
||||
|
||||
it 'fails with invalid json string' do
|
||||
data = 'invalid json'
|
||||
is_expected.to run.with_params(data).and_raise_error(Puppet::ParseError)
|
||||
end
|
||||
|
||||
it 'fails with array of json string' do
|
||||
data = ['{"valid": "json", "syntax": "here"}', '{"some": "data"}']
|
||||
is_expected.to run.with_params(data).and_raise_error(Puppet::ParseError)
|
||||
end
|
||||
|
||||
it 'works with valid json string' do
|
||||
data = '{"valid": "json", "syntax": "here"}'
|
||||
retval = ['{"valid":"json","syntax":"here"}']
|
||||
is_expected.to run.with_params(data).and_return(retval)
|
||||
end
|
||||
|
||||
it 'fails unless its an array or string' do
|
||||
is_expected.to run.with_params(1234).and_raise_error(Puppet::ParseError)
|
||||
end
|
||||
|
||||
it 'fails unless array doesnt have hashes' do
|
||||
data = [12, 23]
|
||||
is_expected.to run.with_params(data).and_raise_error(Puppet::ParseError)
|
||||
end
|
||||
|
||||
it 'fails if array but only some entries are valid' do
|
||||
data = [{:some => "entry"}, 23]
|
||||
is_expected.to run.with_params(data).and_raise_error(Puppet::ParseError)
|
||||
end
|
||||
|
||||
it 'works with an array of hashes' do
|
||||
data = [{:some => "entry"}, {:with => "data"}]
|
||||
retval = ['{"some":"entry"}','{"with":"data"}']
|
||||
is_expected.to run.with_params(data).and_return(retval)
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user