Puppet Function: stdlib::fqdn_rotate

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

Summary

Rotates an array or string a random number of times, combining the `fqdn` fact and an optional seed for repeatable randomness.

Overview

Signatures:

  • stdlib::fqdn_rotate(String $input, Optional[Variant[Integer,String]] *$seeds)String

    Returns the rotated String

    Examples:

    Rotating a String

    stdlib::fqdn_rotate('abcd')

    Using a custom seed

    stdlib::fqdn_rotate('abcd', 'custom seed')

    Parameters:

    • input (String)

      The String you want rotated a random number of times

    • *seeds (Optional[Variant[Integer,String]])

      One of more values to use as a custom seed. These will be combined with the host’s FQDN

    Returns:

    • (String)

      Returns the rotated String

  • stdlib::fqdn_rotate(Array $input, Optional[Variant[Integer,String]] *$seeds)Array

    Returns the rotated Array

    Examples:

    Rotating an Array

    stdlib::fqdn_rotate(['a', 'b', 'c', 'd'])

    Using custom seeds

    stdlib::fqdn_rotate([1, 2, 3], 'custom', 'seed', 1)

    Parameters:

    • input (Array)

      The Array you want rotated a random number of times

    • *seeds (Optional[Variant[Integer,String]])

      One of more values to use as a custom seed. These will be combined with the host’s FQDN

    Returns:

    • (Array)

      Returns the rotated Array



4
5
6
7
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/puppet/functions/stdlib/fqdn_rotate.rb', line 4

Puppet::Functions.create_function(:'stdlib::fqdn_rotate') do
  # @param input
  #   The String you want rotated a random number of times
  # @param seeds
  #   One of more values to use as a custom seed. These will be combined with the host's FQDN
  #
  # @return [String] Returns the rotated String
  #
  # @example Rotating a String
  #   stdlib::fqdn_rotate('abcd')
  # @example Using a custom seed
  #   stdlib::fqdn_rotate('abcd', 'custom seed')
  dispatch :fqdn_rotate_string do
    param 'String', :input
    optional_repeated_param 'Variant[Integer,String]', :seeds
    return_type 'String'
  end

  # @param input
  #   The Array you want rotated a random number of times
  # @param seeds
  #   One of more values to use as a custom seed. These will be combined with the host's FQDN
  #
  # @return [String] Returns the rotated Array
  #
  # @example Rotating an Array
  #   stdlib::fqdn_rotate(['a', 'b', 'c', 'd'])
  # @example Using custom seeds
  #   stdlib::fqdn_rotate([1, 2, 3], 'custom', 'seed', 1)
  dispatch :fqdn_rotate_array do
    param 'Array', :input
    optional_repeated_param 'Variant[Integer,String]', :seeds
    return_type 'Array'
  end

  def fqdn_rotate_array(input, *seeds)
    # Check whether it makes sense to rotate ...
    return input if input.size <= 1

    result = input.clone

    require 'digest/md5'
    seed = Digest::MD5.hexdigest([fqdn_fact, seeds].join(':')).hex

    offset = Puppet::Util.deterministic_rand(seed, result.size).to_i

    offset.times do
      result.push result.shift
    end

    result
  end

  def fqdn_rotate_string(input, *seeds)
    fqdn_rotate_array(input.chars, seeds).join
  end

  private

  def fqdn_fact
    closure_scope['facts']['networking']['fqdn']
  end
end