Puppet Function: stdlib::fqdn_rand_string

Defined in:
lib/puppet/functions/stdlib/fqdn_rand_string.rb
Function type:
Ruby 4.x API

Summary

Generates a random alphanumeric string. Combining the `$fqdn` fact and an optional seed for repeatable randomness.

Overview

stdlib::fqdn_rand_string(Integer[1] $length, Optional[Optional[String]] $charset, Optional[Any] *$seed)String

Optionally, you can specify a character set for the function (defaults to alphanumeric).

Examples:

Example Usage:

stdlib::fqdn_rand_string(10)
stdlib::fqdn_rand_string(10, 'ABCDEF!@$%^')
stdlib::fqdn_rand_string(10, undef, 'custom seed')

Parameters:

  • length (Integer[1])

    The length of the resulting string.

  • charset (Optional[Optional[String]])

    The character set to use.

  • *seed (Optional[Any])

    The seed for repeatable randomness.

Returns:

  • (String)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/puppet/functions/stdlib/fqdn_rand_string.rb', line 8

Puppet::Functions.create_function(:'stdlib::fqdn_rand_string') do
  # @param length The length of the resulting string.
  # @param charset The character set to use.
  # @param seed The seed for repeatable randomness.
  #
  # @return [String]
  #
  # @example Example Usage:
  #   stdlib::fqdn_rand_string(10)
  #   stdlib::fqdn_rand_string(10, 'ABCDEF!@$%^')
  #   stdlib::fqdn_rand_string(10, undef, 'custom seed')
  dispatch :fqdn_rand_string do
    param 'Integer[1]', :length
    optional_param 'Optional[String]', :charset
    optional_repeated_param 'Any', :seed
  end

  def fqdn_rand_string(length, charset = nil, *seed)
    charset = if charset && !charset.empty?
                charset.chars.to_a
              else
                (0..9).map(&:to_s) + ('A'..'Z').to_a + ('a'..'z').to_a
              end

    rand_string = ''
    length.times do |current|
      rand_string += charset[call_function('fqdn_rand', charset.size, (seed + [current + 1]).join(':'))]
    end

    rand_string
  end
end