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