1 | Puppet::Parser::Functions::newfunction(
|
---|
2 | :fqdn_rand_string,
|
---|
3 | :arity => -2,
|
---|
4 | :type => :rvalue,
|
---|
5 | :doc => "Usage: `fqdn_rand_string(LENGTH, [CHARSET], [SEED])`. LENGTH is
|
---|
6 | required and must be a positive integer. CHARSET is optional and may be
|
---|
7 | `undef` or a string. SEED is optional and may be any number or string.
|
---|
8 |
|
---|
9 | Generates a random string LENGTH characters long using the character set
|
---|
10 | provided by CHARSET, combining the `$fqdn` fact and the value of SEED for
|
---|
11 | repeatable randomness. (That is, each node will get a different random
|
---|
12 | string from this function, but a given node's result will be the same every
|
---|
13 | time unless its hostname changes.) Adding a SEED can be useful if you need
|
---|
14 | more than one unrelated string. CHARSET will default to alphanumeric if
|
---|
15 | `undef` or an empty string.") do |args|
|
---|
16 | raise(ArgumentError, "fqdn_rand_string(): wrong number of arguments (0 for 1)") if args.size == 0
|
---|
17 | Puppet::Parser::Functions.function('is_integer')
|
---|
18 | raise(ArgumentError, "fqdn_rand_string(): first argument must be a positive integer") unless function_is_integer([args[0]]) and args[0].to_i > 0
|
---|
19 | raise(ArgumentError, "fqdn_rand_string(): second argument must be undef or a string") unless args[1].nil? or args[1].is_a? String
|
---|
20 |
|
---|
21 | Puppet::Parser::Functions.function('fqdn_rand')
|
---|
22 |
|
---|
23 | length = args.shift.to_i
|
---|
24 | charset = args.shift.to_s.chars.to_a
|
---|
25 |
|
---|
26 | charset = (0..9).map { |i| i.to_s } + ('A'..'Z').to_a + ('a'..'z').to_a if charset.empty?
|
---|
27 |
|
---|
28 | rand_string = ''
|
---|
29 | for current in 1..length
|
---|
30 | rand_string << charset[function_fqdn_rand([charset.size, (args + [current.to_s]).join(':')]).to_i]
|
---|
31 | end
|
---|
32 |
|
---|
33 | rand_string
|
---|
34 | end
|
---|