Skip to content

Commit dc97fe3

Browse files
committed
Porting functions to the modern Puppet 4.x API
1 parent f1e2e0b commit dc97fe3

File tree

272 files changed

+15403
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

272 files changed

+15403
-0
lines changed

lib/puppet/functions/stdlib/abs.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# This is an autogenerated function, ported from the original legacy version.
2+
# It /should work/ as is, but will not have all the benefits of the modern
3+
# function API. You should see the function docs to learn how to add function
4+
# signatures for type safety and to document this function using puppet-strings.
5+
#
6+
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
7+
#
8+
# ---- original file header ----
9+
#
10+
# abs.rb
11+
#
12+
# ---- original file header ----
13+
#
14+
# @summary
15+
# @summary
16+
# **Deprecated:** Returns the absolute value of a number
17+
#
18+
# For example -34.56 becomes 34.56.
19+
# Takes a single integer or float value as an argument.
20+
#
21+
# > *Note:*
22+
# **Deprected** from Puppet 6.0.0, the built-in
23+
# ['abs'](https://puppet.com/docs/puppet/6.4/function.html#abs)function will be used instead.
24+
#
25+
# @return The absolute value of the given number if it was an Integer
26+
#
27+
#
28+
#
29+
Puppet::Functions.create_function(:'stdlib::abs') do
30+
# @param arguments
31+
# The original array of arguments. Port this to individually managed params
32+
# to get the full benefit of the modern function API.
33+
#
34+
# @return [Data type]
35+
# Describe what the function returns here
36+
#
37+
dispatch :default_impl do
38+
# Call the method named 'default_impl' when this is matched
39+
# Port this to match individual params for better type safety
40+
repeated_param 'Any', :arguments
41+
end
42+
43+
44+
def default_impl(*arguments)
45+
46+
47+
raise(Puppet::ParseError, "abs(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
48+
49+
value = arguments[0]
50+
51+
# Numbers in Puppet are often string-encoded which is troublesome ...
52+
if value.is_a?(String)
53+
if value =~ %r{^-?(?:\d+)(?:\.\d+){1}$}
54+
value = value.to_f
55+
elsif value =~ %r{^-?\d+$}
56+
value = value.to_i
57+
else
58+
raise(Puppet::ParseError, 'abs(): Requires float or integer to work with')
59+
end
60+
end
61+
62+
# We have numeric value to handle ...
63+
result = value.abs
64+
65+
return result
66+
67+
end
68+
end
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# This is an autogenerated function, ported from the original legacy version.
2+
# It /should work/ as is, but will not have all the benefits of the modern
3+
# function API. You should see the function docs to learn how to add function
4+
# signatures for type safety and to document this function using puppet-strings.
5+
#
6+
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
7+
#
8+
# ---- original file header ----
9+
#
10+
# any2array.rb
11+
#
12+
# ---- original file header ----
13+
#
14+
# @summary
15+
# @summary
16+
# This converts any object to an array containing that object.
17+
#
18+
# Empty argument lists are converted to an empty array. Arrays are left
19+
# untouched. Hashes are converted to arrays of alternating keys and values.
20+
#
21+
# > *Note:*
22+
# since Puppet 5.0.0 it is possible to create new data types for almost any
23+
# datatype using the type system and the built-in
24+
# [`Array.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-array-and-tuple)
25+
# function is used to create a new Array..
26+
#
27+
# ```
28+
# $hsh = {'key' => 42, 'another-key' => 100}
29+
# notice(Array($hsh))
30+
# ```
31+
#
32+
# Would notice `[['key', 42], ['another-key', 100]]`
33+
#
34+
# The Array data type also has a special mode to "create an array if not already an array"
35+
#
36+
# ```
37+
# notice(Array({'key' => 42, 'another-key' => 100}, true))
38+
# ```
39+
#
40+
# Would notice `[{'key' => 42, 'another-key' => 100}]`, as the `true` flag prevents the hash from being
41+
# transformed into an array.
42+
#
43+
# @return [Array] The new array containing the given object
44+
#
45+
#
46+
Puppet::Functions.create_function(:'stdlib::any2array') do
47+
# @param arguments
48+
# The original array of arguments. Port this to individually managed params
49+
# to get the full benefit of the modern function API.
50+
#
51+
# @return [Data type]
52+
# Describe what the function returns here
53+
#
54+
dispatch :default_impl do
55+
# Call the method named 'default_impl' when this is matched
56+
# Port this to match individual params for better type safety
57+
repeated_param 'Any', :arguments
58+
end
59+
60+
61+
def default_impl(*arguments)
62+
63+
64+
if arguments.empty?
65+
return []
66+
end
67+
68+
return arguments unless arguments.length == 1
69+
return arguments[0] if arguments[0].is_a?(Array)
70+
return [] if arguments == ['']
71+
if arguments[0].is_a?(Hash)
72+
result = []
73+
arguments[0].each do |key, value|
74+
result << key << value
75+
end
76+
return result
77+
end
78+
return arguments
79+
80+
end
81+
end
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# This is an autogenerated function, ported from the original legacy version.
2+
# It /should work/ as is, but will not have all the benefits of the modern
3+
# function API. You should see the function docs to learn how to add function
4+
# signatures for type safety and to document this function using puppet-strings.
5+
#
6+
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
7+
#
8+
# ---- original file header ----
9+
#
10+
# any2bool.rb
11+
#
12+
# ---- original file header ----
13+
#
14+
# @summary
15+
# @summary
16+
# Converts 'anything' to a boolean.
17+
#
18+
# In practise it does the following:
19+
# * Strings such as Y,y,1,T,t,TRUE,yes,'true' will return true
20+
# * Strings such as 0,F,f,N,n,FALSE,no,'false' will return false
21+
# * Booleans will just return their original value
22+
# * Number (or a string representation of a number) > 0 will return true, otherwise false
23+
# * undef will return false
24+
# * Anything else will return true
25+
#
26+
# Also see the built-in [`Boolean.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-boolean)
27+
# function.
28+
#
29+
# @return [Boolean] The boolean value of the object that was given
30+
#
31+
#
32+
Puppet::Functions.create_function(:'stdlib::any2bool') do
33+
# @param arguments
34+
# The original array of arguments. Port this to individually managed params
35+
# to get the full benefit of the modern function API.
36+
#
37+
# @return [Data type]
38+
# Describe what the function returns here
39+
#
40+
dispatch :default_impl do
41+
# Call the method named 'default_impl' when this is matched
42+
# Port this to match individual params for better type safety
43+
repeated_param 'Any', :arguments
44+
end
45+
46+
47+
def default_impl(*arguments)
48+
49+
50+
raise(Puppet::ParseError, "any2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
51+
52+
# If argument is already Boolean, return it
53+
if !!arguments[0] == arguments[0] # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean
54+
return arguments[0]
55+
end
56+
57+
arg = arguments[0]
58+
59+
if arg.nil?
60+
return false
61+
end
62+
63+
if arg == :undef
64+
return false
65+
end
66+
67+
valid_float = begin
68+
!!Float(arg) # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean
69+
rescue
70+
false
71+
end
72+
73+
if arg.is_a?(Numeric)
74+
return function_num2bool([arguments[0]])
75+
end
76+
77+
if arg.is_a?(String)
78+
return function_num2bool([arguments[0]]) if valid_float
79+
return function_str2bool([arguments[0]])
80+
end
81+
82+
return true
83+
84+
end
85+
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# This is an autogenerated function, ported from the original legacy version.
2+
# It /should work/ as is, but will not have all the benefits of the modern
3+
# function API. You should see the function docs to learn how to add function
4+
# signatures for type safety and to document this function using puppet-strings.
5+
#
6+
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
7+
#
8+
# ---- original file header ----
9+
#
10+
# assert_private.rb
11+
#
12+
# ---- original file header ----
13+
#
14+
# @summary
15+
# @summary
16+
# Sets the current class or definition as private.
17+
#
18+
# @return
19+
# set the current class or definition as private.
20+
#
21+
# Calling the class or definition from outside the current module will fail.
22+
#
23+
#
24+
Puppet::Functions.create_function(:'stdlib::assert_private') do
25+
# @param args
26+
# The original array of arguments. Port this to individually managed params
27+
# to get the full benefit of the modern function API.
28+
#
29+
# @return [Data type]
30+
# Describe what the function returns here
31+
#
32+
dispatch :default_impl do
33+
# Call the method named 'default_impl' when this is matched
34+
# Port this to match individual params for better type safety
35+
repeated_param 'Any', :args
36+
end
37+
38+
39+
def default_impl(*args)
40+
41+
42+
raise(Puppet::ParseError, "assert_private(): Wrong number of arguments given (#{args.size}}) for 0 or 1)") if args.size > 1
43+
44+
scope = self
45+
if scope.lookupvar('module_name') != scope.lookupvar('caller_module_name')
46+
message = nil
47+
if args[0] && args[0].is_a?(String)
48+
message = args[0]
49+
else
50+
manifest_name = scope.source.name
51+
manifest_type = scope.source.type
52+
message = (manifest_type.to_s == 'hostclass') ? 'Class' : 'Definition'
53+
message += " #{manifest_name} is private"
54+
end
55+
raise(Puppet::ParseError, message)
56+
end
57+
58+
end
59+
end

0 commit comments

Comments
 (0)