272
|
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.",
|
|
16 ) do |args|
|
|
17 raise(ArgumentError, 'fqdn_rand_string(): wrong number of arguments (0 for 1)') if args.empty?
|
|
18 Puppet::Parser::Functions.function('is_integer')
|
|
19 raise(ArgumentError, 'fqdn_rand_string(): first argument must be a positive integer') unless function_is_integer([args[0]]) && args[0].to_i > 0
|
|
20 raise(ArgumentError, 'fqdn_rand_string(): second argument must be undef or a string') unless args[1].nil? || args[1].is_a?(String)
|
|
21
|
|
22 Puppet::Parser::Functions.function('fqdn_rand')
|
|
23
|
|
24 length = args.shift.to_i
|
|
25 charset = args.shift.to_s.chars.to_a
|
|
26
|
|
27 charset = (0..9).map { |i| i.to_s } + ('A'..'Z').to_a + ('a'..'z').to_a if charset.empty?
|
|
28
|
|
29 rand_string = ''
|
|
30 for current in 1..length # rubocop:disable Style/For : An each loop would not work correctly in this circumstance
|
|
31 rand_string << charset[function_fqdn_rand([charset.size, (args + [current.to_s]).join(':')]).to_i]
|
|
32 end
|
|
33
|
|
34 rand_string
|
|
35 end
|