-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert the function to the modern function API as a namespaced function and use the `networking` fact instead of legacy facts. A non-namespaced shim is also created (but marked deprecated), to preserve compatibility. Fixes #1381
- Loading branch information
1 parent
1e3884d
commit cc0454e
Showing
4 changed files
with
85 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims` | ||
|
||
# @summary DEPRECATED. Use the namespaced function [`stdlib::fqdn_rotate`](#stdlibfqdn_rotate) instead. | ||
Puppet::Functions.create_function(:fqdn_rotate) do | ||
dispatch :deprecation_gen do | ||
repeated_param 'Any', :args | ||
end | ||
def deprecation_gen(*args) | ||
call_function('deprecation', 'fqdn_rotate', 'This function is deprecated, please use stdlib::fqdn_rotate instead.', false) | ||
call_function('stdlib::fqdn_rotate', *args) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
# @summary Rotates an array or string a random number of times, combining the `fqdn` fact and an optional seed for repeatable randomness. | ||
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters