Skip to content

Puppet 4 functions #1079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions lib/puppet/functions/stdlib/abs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# This is an autogenerated function, ported from the original legacy version.
# It /should work/ as is, but will not have all the benefits of the modern
# function API. You should see the function docs to learn how to add function
# signatures for type safety and to document this function using puppet-strings.
#
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
#
# ---- original file header ----
#
# abs.rb
#
# ---- original file header ----
#
# @summary
# @summary
# **Deprecated:** Returns the absolute value of a number
#
# For example -34.56 becomes 34.56.
# Takes a single integer or float value as an argument.
#
# > *Note:*
# **Deprected** from Puppet 6.0.0, the built-in
# ['abs'](https://puppet.com/docs/puppet/6.4/function.html#abs)function will be used instead.
#
# @return The absolute value of the given number if it was an Integer
#
#
#
Puppet::Functions.create_function(:'stdlib::abs') do
# @param arguments
# The original array of arguments. Port this to individually managed params
# to get the full benefit of the modern function API.
#
# @return [Data type]
# Describe what the function returns here
#
dispatch :default_impl do
# Call the method named 'default_impl' when this is matched
# Port this to match individual params for better type safety
repeated_param 'Any', :arguments
end

def default_impl(*arguments)
raise(Puppet::ParseError, "abs(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?

value = arguments[0]

# Numbers in Puppet are often string-encoded which is troublesome ...
if value.is_a?(String)
if value =~ %r{^-?(?:\d+)(?:\.\d+){1}$}
value = value.to_f
elsif value =~ %r{^-?\d+$}
value = value.to_i
else
raise(Puppet::ParseError, 'abs(): Requires float or integer to work with')
end
end

# We have numeric value to handle ...
result = value.abs

result
end
end
77 changes: 77 additions & 0 deletions lib/puppet/functions/stdlib/any2array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# This is an autogenerated function, ported from the original legacy version.
# It /should work/ as is, but will not have all the benefits of the modern
# function API. You should see the function docs to learn how to add function
# signatures for type safety and to document this function using puppet-strings.
#
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
#
# ---- original file header ----
#
# any2array.rb
#
# ---- original file header ----
#
# @summary
# @summary
# This converts any object to an array containing that object.
#
# Empty argument lists are converted to an empty array. Arrays are left
# untouched. Hashes are converted to arrays of alternating keys and values.
#
# > *Note:*
# since Puppet 5.0.0 it is possible to create new data types for almost any
# datatype using the type system and the built-in
# [`Array.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-array-and-tuple)
# function is used to create a new Array..
#
# ```
# $hsh = {'key' => 42, 'another-key' => 100}
# notice(Array($hsh))
# ```
#
# Would notice `[['key', 42], ['another-key', 100]]`
#
# The Array data type also has a special mode to "create an array if not already an array"
#
# ```
# notice(Array({'key' => 42, 'another-key' => 100}, true))
# ```
#
# Would notice `[{'key' => 42, 'another-key' => 100}]`, as the `true` flag prevents the hash from being
# transformed into an array.
#
# @return [Array] The new array containing the given object
#
#
Puppet::Functions.create_function(:'stdlib::any2array') do
# @param arguments
# The original array of arguments. Port this to individually managed params
# to get the full benefit of the modern function API.
#
# @return [Data type]
# Describe what the function returns here
#
dispatch :default_impl do
# Call the method named 'default_impl' when this is matched
# Port this to match individual params for better type safety
repeated_param 'Any', :arguments
end

def default_impl(*arguments)
if arguments.empty?
return []
end

return arguments unless arguments.length == 1
return arguments[0] if arguments[0].is_a?(Array)
return [] if arguments == ['']
if arguments[0].is_a?(Hash)
result = []
arguments[0].each do |key, value|
result << key << value
end
return result
end
arguments
end
end
81 changes: 81 additions & 0 deletions lib/puppet/functions/stdlib/any2bool.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# This is an autogenerated function, ported from the original legacy version.
# It /should work/ as is, but will not have all the benefits of the modern
# function API. You should see the function docs to learn how to add function
# signatures for type safety and to document this function using puppet-strings.
#
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
#
# ---- original file header ----
#
# any2bool.rb
#
# ---- original file header ----
#
# @summary
# @summary
# Converts 'anything' to a boolean.
#
# In practise it does the following:
# * Strings such as Y,y,1,T,t,TRUE,yes,'true' will return true
# * Strings such as 0,F,f,N,n,FALSE,no,'false' will return false
# * Booleans will just return their original value
# * Number (or a string representation of a number) > 0 will return true, otherwise false
# * undef will return false
# * Anything else will return true
#
# Also see the built-in [`Boolean.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-boolean)
# function.
#
# @return [Boolean] The boolean value of the object that was given
#
#
Puppet::Functions.create_function(:'stdlib::any2bool') do
# @param arguments
# The original array of arguments. Port this to individually managed params
# to get the full benefit of the modern function API.
#
# @return [Data type]
# Describe what the function returns here
#
dispatch :default_impl do
# Call the method named 'default_impl' when this is matched
# Port this to match individual params for better type safety
repeated_param 'Any', :arguments
end

def default_impl(*arguments)
raise(Puppet::ParseError, "any2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?

# If argument is already Boolean, return it
if !!arguments[0] == arguments[0] # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean
return arguments[0]
end

arg = arguments[0]

if arg.nil?
return false
end

if arg == :undef
return false
end

valid_float = begin
!!Float(arg) # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean
rescue
false
end

if arg.is_a?(Numeric)
return function_num2bool([arguments[0]])
end

if arg.is_a?(String)
return function_num2bool([arguments[0]]) if valid_float
return function_str2bool([arguments[0]])
end

true
end
end
55 changes: 55 additions & 0 deletions lib/puppet/functions/stdlib/assert_private.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# This is an autogenerated function, ported from the original legacy version.
# It /should work/ as is, but will not have all the benefits of the modern
# function API. You should see the function docs to learn how to add function
# signatures for type safety and to document this function using puppet-strings.
#
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
#
# ---- original file header ----
#
# assert_private.rb
#
# ---- original file header ----
#
# @summary
# @summary
# Sets the current class or definition as private.
#
# @return
# set the current class or definition as private.
#
# Calling the class or definition from outside the current module will fail.
#
#
Puppet::Functions.create_function(:'stdlib::assert_private') do
# @param args
# The original array of arguments. Port this to individually managed params
# to get the full benefit of the modern function API.
#
# @return [Data type]
# Describe what the function returns here
#
dispatch :default_impl do
# Call the method named 'default_impl' when this is matched
# Port this to match individual params for better type safety
repeated_param 'Any', :args
end

def default_impl(*args)
raise(Puppet::ParseError, "assert_private(): Wrong number of arguments given (#{args.size}}) for 0 or 1)") if args.size > 1

scope = self
return unless scope.lookupvar('module_name') != scope.lookupvar('caller_module_name')

message = nil
if args[0] && args[0].is_a?(String)
message = args[0]
else
manifest_name = scope.source.name
manifest_type = scope.source.type
message = (manifest_type.to_s == 'hostclass') ? 'Class' : 'Definition'
message += " #{manifest_name} is private"
end
raise(Puppet::ParseError, message)
end
end
109 changes: 109 additions & 0 deletions lib/puppet/functions/stdlib/base64.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# This is an autogenerated function, ported from the original legacy version.
# It /should work/ as is, but will not have all the benefits of the modern
# function API. You should see the function docs to learn how to add function
# signatures for type safety and to document this function using puppet-strings.
#
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
#
# ---- original file header ----
# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
# ---- original file header ----
#
# @summary
# @summary
# Base64 encode or decode a string based on the command and the string submitted
#
# @example Example usage
#
# Encode and decode a string
#
# $encodestring = base64('encode', 'thestring')
# $decodestring = base64('decode', 'dGhlc3RyaW5n')
#
# Explicitly define encode/decode method: default, strict, urlsafe
#
# $method = 'default'
# $encodestring = base64('encode', 'thestring', $method)
# $decodestring = base64('decode', 'dGhlc3RyaW5n', $method)
#
# Encode a string as if it was binary
#
# $encodestring = String(Binary('thestring', '%s'))
#
# Decode a Binary assuming it is an UTF-8 String
#
# $decodestring = String(Binary("dGhlc3RyaW5n"), "%s")
#
# > **Note:*
# Since Puppet 4.8.0, the Binary data type can be used to produce base 64 encoded strings.
# See the `new()` function for the Binary and String types for documentation. Also see `binary_file()`
# function for reading a file with binary (non UTF-8) content.
#
# @return [String] The encoded/decoded value
#
#
Puppet::Functions.create_function(:'stdlib::base64') do
# @param args
# The original array of arguments. Port this to individually managed params
# to get the full benefit of the modern function API.
#
# @return [Data type]
# Describe what the function returns here
#
dispatch :default_impl do
# Call the method named 'default_impl' when this is matched
# Port this to match individual params for better type safety
repeated_param 'Any', :args
end

def default_impl(*args)
require 'base64'

raise Puppet::ParseError, "base64(): Wrong number of arguments (#{args.length}; must be >= 2)" unless args.length >= 2

actions = ['encode', 'decode']

unless actions.include?(args[0])
raise Puppet::ParseError, "base64(): the first argument must be one of 'encode' or 'decode'"
end

unless args[1].is_a?(String)
raise Puppet::ParseError, 'base64(): the second argument must be a string to base64'
end

method = ['default', 'strict', 'urlsafe']

chosen_method = if args.length <= 2
'default'
else
args[2]
end

unless method.include?(chosen_method)
raise Puppet::ParseError, "base64(): the third argument must be one of 'default', 'strict', or 'urlsafe'"
end

case args[0]
when 'encode'
case chosen_method
when 'default'
result = Base64.encode64(args[1])
when 'strict'
result = Base64.strict_encode64(args[1])
when 'urlsafe'
result = Base64.urlsafe_encode64(args[1])
end
when 'decode'
case chosen_method
when 'default'
result = Base64.decode64(args[1])
when 'strict'
result = Base64.strict_decode64(args[1])
when 'urlsafe'
result = Base64.urlsafe_decode64(args[1])
end
end

result
end
end
Loading