Skip to content

Commit d7c8460

Browse files
committed
fqdn_rotate: Improve documentation
1 parent 601f681 commit d7c8460

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

README.markdown

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,20 @@ fqdn_rand_string(10, '', 'custom seed')
259259

260260
#### `fqdn_rotate`
261261

262-
Rotates an array a random number of times, based on a node's fqdn. *Type*: rvalue.
262+
Rotates an array or string a random number of times, combining the `$fqdn` fact and an optional seed for repeatable randomness.
263+
264+
*Usage:*
265+
~~~
266+
fqdn_rotate(VALUE, [SEED])
267+
~~~
268+
*Examples:*
269+
~~~
270+
fqdn_rotate(['a', 'b', 'c', 'd'])
271+
fqdn_rotate('abcd')
272+
fqdn_rotate([1, 2, 3], 'custom seed')
273+
~~~
274+
275+
*Type*: rvalue.
263276

264277
#### `get_module_path`
265278

lib/puppet/parser/functions/fqdn_rotate.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@
22
# fqdn_rotate.rb
33
#
44

5-
module Puppet::Parser::Functions
6-
newfunction(:fqdn_rotate, :type => :rvalue, :doc => <<-EOS
7-
Rotates an array a random number of times based on a nodes fqdn.
8-
EOS
9-
) do |arguments|
5+
Puppet::Parser::Functions.newfunction(
6+
:fqdn_rotate,
7+
:type => :rvalue,
8+
:doc => "Usage: `fqdn_rotate(VALUE, [SEED])`. VALUE is required and
9+
must be an array or a string. SEED is optional and may be any number
10+
or string.
11+
12+
Rotates VALUE a random number of times, combining the `$fqdn` fact and
13+
the value of SEED for repeatable randomness. (That is, each node will
14+
get a different random rotation from this function, but a given node's
15+
result will be the same every time unless its hostname changes.) Adding
16+
a SEED can be useful if you need more than one unrelated rotation.") do |args|
1017

1118
raise(Puppet::ParseError, "fqdn_rotate(): Wrong number of arguments " +
12-
"given (#{arguments.size} for 1)") if arguments.size < 1
19+
"given (#{args.size} for 1)") if args.size < 1
1320

14-
value = arguments.shift
21+
value = args.shift
1522
require 'digest/md5'
1623

1724
unless value.is_a?(Array) || value.is_a?(String)
@@ -31,7 +38,7 @@ module Puppet::Parser::Functions
3138

3239
elements = result.size
3340

34-
seed = Digest::MD5.hexdigest([lookupvar('::fqdn'),arguments].join(':')).hex
41+
seed = Digest::MD5.hexdigest([lookupvar('::fqdn'),args].join(':')).hex
3542
# deterministic_rand() was added in Puppet 3.2.0; reimplement if necessary
3643
if Puppet::Util.respond_to?(:deterministic_rand)
3744
offset = Puppet::Util.deterministic_rand(seed, elements).to_i
@@ -51,7 +58,6 @@ module Puppet::Parser::Functions
5158
result = string ? result.join : result
5259

5360
return result
54-
end
5561
end
5662

5763
# vim: set ts=2 sw=2 et :

0 commit comments

Comments
 (0)