5 module Puppet::Parser::Functions
6 newfunction(:fqdn_rotate, :type => :rvalue, :doc => <<-EOS
7 Rotates an array a random number of times based on a nodes fqdn.
11 raise(Puppet::ParseError, "fqdn_rotate(): Wrong number of arguments " +
12 "given (#{arguments.size} for 1)") if arguments.size < 1
17 unless value.is_a?(Array) || value.is_a?(String)
18 raise(Puppet::ParseError, 'fqdn_rotate(): Requires either ' +
19 'array or string to work with')
24 string = value.is_a?(String) ? true : false
26 # Check whether it makes sense to rotate ...
27 return result if result.size <= 1
29 # We turn any string value into an array to be able to rotate ...
30 result = string ? result.split('') : result
32 elements = result.size
34 seed = Digest::MD5.hexdigest([lookupvar('::fqdn'),arguments].join(':')).hex
35 # deterministic_rand() was added in Puppet 3.2.0; reimplement if necessary
36 if Puppet::Util.respond_to?(:deterministic_rand)
37 offset = Puppet::Util.deterministic_rand(seed, elements).to_i
39 if defined?(Random) == 'constant' && Random.class == Class
40 offset = Random.new(seed).rand(elements)
43 offset = rand(elements)
48 result.push result.shift
51 result = string ? result.join : result
57 # vim: set ts=2 sw=2 et :