4 Puppet::Parser::Functions.newfunction(
9 Rotates an array or string a random number of times, combining the `$fqdn` fact
10 and an optional seed for repeatable randomness.
13 rotated array or string
15 @example Example Usage:
16 fqdn_rotate(['a', 'b', 'c', 'd'])
18 fqdn_rotate([1, 2, 3], 'custom seed')
22 raise(Puppet::ParseError, "fqdn_rotate(): Wrong number of arguments given (#{args.size} for 1)") if args.empty?
27 unless value.is_a?(Array) || value.is_a?(String)
28 raise(Puppet::ParseError, 'fqdn_rotate(): Requires either array or string to work with')
33 string = value.is_a?(String) ? true : false
35 # Check whether it makes sense to rotate ...
36 return result if result.size <= 1
38 # We turn any string value into an array to be able to rotate ...
39 result = string ? result.split('') : result
41 elements = result.size
43 seed = Digest::MD5.hexdigest([lookupvar('::fqdn'), args].join(':')).hex
44 # deterministic_rand() was added in Puppet 3.2.0; reimplement if necessary
45 if Puppet::Util.respond_to?(:deterministic_rand)
46 offset = Puppet::Util.deterministic_rand(seed, elements).to_i
48 return offset = Random.new(seed).rand(elements) if defined?(Random) == 'constant' && Random.class == Class
50 old_seed = srand(seed)
51 offset = rand(elements)
55 result.push result.shift
58 result = string ? result.join : result
63 # vim: set ts=2 sw=2 et :