diff options
Diffstat (limited to 'lib/puppet/parser')
27 files changed, 109 insertions, 30 deletions
diff --git a/lib/puppet/parser/functions/deprecation.rb b/lib/puppet/parser/functions/deprecation.rb new file mode 100644 index 0000000..e30f3a0 --- /dev/null +++ b/lib/puppet/parser/functions/deprecation.rb @@ -0,0 +1,17 @@ +module Puppet::Parser::Functions + newfunction(:deprecation, :type => :rvalue, :doc => <<-EOS + Function to print deprecation warnings (this is the 3.X version of it), The uniqueness key - can appear once. The msg is the message text including any positional information that is formatted by the user/caller of the method.). +EOS + ) do |arguments| + + raise(Puppet::ParseError, "deprecation: Wrong number of arguments " + + "given (#{arguments.size} for 2)") unless arguments.size == 2 + + key = arguments[0] + message = arguments[1] + + if ENV['STDLIB_LOG_DEPRECATIONS'] == "true" + warning("deprecation. #{key}. #{message}") + end + end +end diff --git a/lib/puppet/parser/functions/dig44.rb b/lib/puppet/parser/functions/dig44.rb index a7de363..1e7c318 100644 --- a/lib/puppet/parser/functions/dig44.rb +++ b/lib/puppet/parser/functions/dig44.rb @@ -3,21 +3,28 @@ # module Puppet::Parser::Functions - newfunction(:dig44, :type => :rvalue, :doc => <<-EOS + newfunction( + :dig44, + :type => :rvalue, + :arity => -2, + :doc => <<-eos DEPRECATED: This function has been replaced in puppet 4.5.0. -Looks up into a complex structure of arrays and hashes and returns nil +Looks up into a complex structure of arrays and hashes and returns a value or the default value if nothing was found. -Path is an array of keys to be looked up in data argument. The function -will go down the structure and try to extract the required value. +Key can contain slashes to describe path components. The function will go down +the structure and try to extract the required value. $data = { 'a' => { 'b' => [ 'b1', 'b2', - 'b3' ]}} + 'b3', + ] + } +} $value = dig44($data, ['a', 'b', '2'], 'not_found') => $value = 'b3' @@ -29,18 +36,18 @@ b -> second hash key not_found -> (optional) will be returned if there is no value or the path did not match. Defaults to nil. -In addition to the required "path" argument, "dig44" accepts default +In addition to the required "key" argument, the function accepts a default argument. It will be returned if no value was found or a path component is missing. And the fourth argument can set a variable path separator. - EOS - ) do |arguments| + eos + ) do |arguments| # Two arguments are required raise(Puppet::ParseError, "dig44(): Wrong number of arguments " + "given (#{arguments.size} for at least 2)") if arguments.size < 2 data, path, default = *arguments - if !(data.is_a?(Hash) || data.is_a?(Array)) + unless data.is_a?(Hash) or data.is_a?(Array) raise(Puppet::ParseError, "dig44(): first argument must be a hash or an array, " << "given #{data.class.name}") end @@ -50,7 +57,17 @@ missing. And the fourth argument can set a variable path separator. "given #{path.class.name}") end - value = path.reduce(data) { |h, k| (h.is_a?(Hash) || h.is_a?(Array)) ? h[k] : break } + value = path.reduce(data) do |structure, key| + if structure.is_a? Hash or structure.is_a? Array + if structure.is_a? Array + key = Integer key rescue break + end + break if structure[key].nil? or structure[key] == :undef + structure[key] + else + break + end + end value.nil? ? default : value end end diff --git a/lib/puppet/parser/functions/ensure_resources.rb b/lib/puppet/parser/functions/ensure_resources.rb index 30d57a8..b3c51e6 100644 --- a/lib/puppet/parser/functions/ensure_resources.rb +++ b/lib/puppet/parser/functions/ensure_resources.rb @@ -36,7 +36,7 @@ ENDOFDOC params ||= {} if title.is_a?(Hash) - resource_hash = Hash(title) + resource_hash = title.dup resources = resource_hash.keys Puppet::Parser::Functions.function(:ensure_resource) diff --git a/lib/puppet/parser/functions/getparam.rb b/lib/puppet/parser/functions/getparam.rb index 6d51006..0a5cbe0 100644 --- a/lib/puppet/parser/functions/getparam.rb +++ b/lib/puppet/parser/functions/getparam.rb @@ -28,7 +28,7 @@ ENDOFDOC return '' if param.empty? if resource = findresource(reference.to_s) - return resource[param] if resource[param] + return resource[param] unless resource[param].nil? end return '' diff --git a/lib/puppet/parser/functions/getvar.rb b/lib/puppet/parser/functions/getvar.rb index ae9c869..3af8d48 100644 --- a/lib/puppet/parser/functions/getvar.rb +++ b/lib/puppet/parser/functions/getvar.rb @@ -20,9 +20,13 @@ module Puppet::Parser::Functions end begin + result = nil catch(:undefined_variable) do - self.lookupvar("#{args[0]}") + result = self.lookupvar("#{args[0]}") end + + # avoid relying on incosistent behaviour around ruby return values from catch + result rescue Puppet::ParseError # Eat the exception if strict_variables = true is set end diff --git a/lib/puppet/parser/functions/is_absolute_path.rb b/lib/puppet/parser/functions/is_absolute_path.rb index 53a5445..e64777f 100644 --- a/lib/puppet/parser/functions/is_absolute_path.rb +++ b/lib/puppet/parser/functions/is_absolute_path.rb @@ -23,7 +23,7 @@ module Puppet::Parser::Functions is_absolute_path($undefined) ENDHEREDOC - + function_deprecation([:is_absolute_path, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Absolute_path. There is further documentation for validate_legacy function in the README.']) require 'puppet/util' path = args[0] @@ -47,4 +47,4 @@ module Puppet::Parser::Functions end value end -end
\ No newline at end of file +end diff --git a/lib/puppet/parser/functions/is_array.rb b/lib/puppet/parser/functions/is_array.rb index b39e184..1d2c0fa 100644 --- a/lib/puppet/parser/functions/is_array.rb +++ b/lib/puppet/parser/functions/is_array.rb @@ -8,6 +8,8 @@ Returns true if the variable passed to this function is an array. EOS ) do |arguments| + function_deprecation([:is_array, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Array. There is further documentation for validate_legacy function in the README.']) + raise(Puppet::ParseError, "is_array(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size < 1 @@ -18,5 +20,3 @@ Returns true if the variable passed to this function is an array. return result end end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_bool.rb b/lib/puppet/parser/functions/is_bool.rb index 8bbdbc8..83d2ebe 100644 --- a/lib/puppet/parser/functions/is_bool.rb +++ b/lib/puppet/parser/functions/is_bool.rb @@ -8,6 +8,8 @@ Returns true if the variable passed to this function is a boolean. EOS ) do |arguments| + function_deprecation([:is_bool, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Bool. There is further documentation for validate_legacy function in the README.']) + raise(Puppet::ParseError, "is_bool(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size != 1 @@ -18,5 +20,3 @@ Returns true if the variable passed to this function is a boolean. return result end end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_float.rb b/lib/puppet/parser/functions/is_float.rb index a2da943..1186458 100644 --- a/lib/puppet/parser/functions/is_float.rb +++ b/lib/puppet/parser/functions/is_float.rb @@ -8,6 +8,8 @@ Returns true if the variable passed to this function is a float. EOS ) do |arguments| + function_deprecation([:is_float, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Float. There is further documentation for validate_legacy function in the README.']) + if (arguments.size != 1) then raise(Puppet::ParseError, "is_float(): Wrong number of arguments "+ "given #{arguments.size} for 1") diff --git a/lib/puppet/parser/functions/is_integer.rb b/lib/puppet/parser/functions/is_integer.rb index c03d28d..e04fd1f 100644 --- a/lib/puppet/parser/functions/is_integer.rb +++ b/lib/puppet/parser/functions/is_integer.rb @@ -13,6 +13,8 @@ If given any other argument `false` is returned. EOS ) do |arguments| + function_deprecation([:is_integer, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Integer. There is further documentation for validate_legacy function in the README.']) + if (arguments.size != 1) then raise(Puppet::ParseError, "is_integer(): Wrong number of arguments "+ "given #{arguments.size} for 1") diff --git a/lib/puppet/parser/functions/is_ip_address.rb b/lib/puppet/parser/functions/is_ip_address.rb index a90adab..5f1d765 100644 --- a/lib/puppet/parser/functions/is_ip_address.rb +++ b/lib/puppet/parser/functions/is_ip_address.rb @@ -10,6 +10,8 @@ Returns true if the string passed to this function is a valid IP address. require 'ipaddr' + function_deprecation([:is_ip_address, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Ip_address. There is further documentation for validate_legacy function in the README.']) + if (arguments.size != 1) then raise(Puppet::ParseError, "is_ip_address(): Wrong number of arguments "+ "given #{arguments.size} for 1") diff --git a/lib/puppet/parser/functions/is_ipv4_address.rb b/lib/puppet/parser/functions/is_ipv4_address.rb index b4861d5..1764e61 100644 --- a/lib/puppet/parser/functions/is_ipv4_address.rb +++ b/lib/puppet/parser/functions/is_ipv4_address.rb @@ -10,6 +10,8 @@ Returns true if the string passed to this function is a valid IPv4 address. require 'ipaddr' + function_deprecation([:is_ipv4_address, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Ipv4. There is further documentation for validate_legacy function in the README.']) + if (arguments.size != 1) then raise(Puppet::ParseError, "is_ipv4_address(): Wrong number of arguments "+ "given #{arguments.size} for 1") diff --git a/lib/puppet/parser/functions/is_ipv6_address.rb b/lib/puppet/parser/functions/is_ipv6_address.rb index 475ad50..7ca4997 100644 --- a/lib/puppet/parser/functions/is_ipv6_address.rb +++ b/lib/puppet/parser/functions/is_ipv6_address.rb @@ -8,6 +8,8 @@ Returns true if the string passed to this function is a valid IPv6 address. EOS ) do |arguments| + function_deprecation([:is_ipv6_address, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Ipv6. There is further documentation for validate_legacy function in the README.']) + require 'ipaddr' if (arguments.size != 1) then diff --git a/lib/puppet/parser/functions/is_numeric.rb b/lib/puppet/parser/functions/is_numeric.rb index e7e1d2a..4a55225 100644 --- a/lib/puppet/parser/functions/is_numeric.rb +++ b/lib/puppet/parser/functions/is_numeric.rb @@ -24,6 +24,8 @@ Valid examples: EOS ) do |arguments| + function_deprecation([:is_numeric, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Numeric. There is further documentation for validate_legacy function in the README.']) + if (arguments.size != 1) then raise(Puppet::ParseError, "is_numeric(): Wrong number of arguments "+ "given #{arguments.size} for 1") @@ -71,5 +73,3 @@ Valid examples: end end end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_string.rb b/lib/puppet/parser/functions/is_string.rb index f5bef04..31ee91e 100644 --- a/lib/puppet/parser/functions/is_string.rb +++ b/lib/puppet/parser/functions/is_string.rb @@ -8,12 +8,15 @@ Returns true if the variable passed to this function is a string. EOS ) do |arguments| + function_deprecation([:is_string, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::String. There is further documentation for validate_legacy function in the README.']) + raise(Puppet::ParseError, "is_string(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size < 1 type = arguments[0] - result = type.is_a?(String) + # when called through the v4 API shim, undef gets translated to nil + result = type.is_a?(String) || type.nil? if result and (type == type.to_f.to_s or type == type.to_i.to_s) then return false diff --git a/lib/puppet/parser/functions/validate_absolute_path.rb b/lib/puppet/parser/functions/validate_absolute_path.rb index 5f85f72..c73f3df 100644 --- a/lib/puppet/parser/functions/validate_absolute_path.rb +++ b/lib/puppet/parser/functions/validate_absolute_path.rb @@ -26,6 +26,9 @@ module Puppet::Parser::Functions ENDHEREDOC + # The deprecation function was being called twice, as validate_absolute_path calls is_absolute_path. I have removed it from here so it only calls deprecation once within is_absolute_path. + # function_deprecation([:validate_absolute_path, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Absolute_path. There is further documentation for validate_legacy function in the README.']) + require 'puppet/util' unless args.length > 0 then diff --git a/lib/puppet/parser/functions/validate_array.rb b/lib/puppet/parser/functions/validate_array.rb index 34b5118..3bf3983 100644 --- a/lib/puppet/parser/functions/validate_array.rb +++ b/lib/puppet/parser/functions/validate_array.rb @@ -18,6 +18,8 @@ module Puppet::Parser::Functions ENDHEREDOC + function_deprecation([:validate_array, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Array. There is further documentation for validate_legacy function in the README.']) + unless args.length > 0 then raise Puppet::ParseError, ("validate_array(): wrong number of arguments (#{args.length}; must be > 0)") end diff --git a/lib/puppet/parser/functions/validate_bool.rb b/lib/puppet/parser/functions/validate_bool.rb index 59a0805..49075b8 100644 --- a/lib/puppet/parser/functions/validate_bool.rb +++ b/lib/puppet/parser/functions/validate_bool.rb @@ -19,6 +19,9 @@ module Puppet::Parser::Functions ENDHEREDOC + # The deprecation function was being called twice, as validate_bool calls is_bool. I have removed it from here so it only calls deprecation once within is_bool. + # function_deprecation([:validate_bool, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Bool. There is further documentation for validate_legacy function in the README.']) + unless args.length > 0 then raise Puppet::ParseError, ("validate_bool(): wrong number of arguments (#{args.length}; must be > 0)") end diff --git a/lib/puppet/parser/functions/validate_hash.rb b/lib/puppet/parser/functions/validate_hash.rb index 9bdd543..fcdc7e1 100644 --- a/lib/puppet/parser/functions/validate_hash.rb +++ b/lib/puppet/parser/functions/validate_hash.rb @@ -18,6 +18,8 @@ module Puppet::Parser::Functions ENDHEREDOC + function_deprecation([:validate_hash, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Hash. There is further documentation for validate_legacy function in the README.']) + unless args.length > 0 then raise Puppet::ParseError, ("validate_hash(): wrong number of arguments (#{args.length}; must be > 0)") end diff --git a/lib/puppet/parser/functions/validate_integer.rb b/lib/puppet/parser/functions/validate_integer.rb index a950916..2ae0293 100644 --- a/lib/puppet/parser/functions/validate_integer.rb +++ b/lib/puppet/parser/functions/validate_integer.rb @@ -2,7 +2,7 @@ module Puppet::Parser::Functions newfunction(:validate_integer, :doc => <<-'ENDHEREDOC') do |args| Validate that the first argument is an integer (or an array of integers). Abort catalog compilation if any of the checks fail. - + The second argument is optional and passes a maximum. (All elements of) the first argument has to be less or equal to this max. The third argument is optional and passes a minimum. (All elements of) the first argument has to be greater or equal to this min. @@ -53,6 +53,8 @@ module Puppet::Parser::Functions ENDHEREDOC + function_deprecation([:validate_integer, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Integer. There is further documentation for validate_legacy function in the README.']) + # tell the user we need at least one, and optionally up to two other parameters raise Puppet::ParseError, "validate_integer(): Wrong number of arguments; must be 1, 2 or 3, got #{args.length}" unless args.length > 0 and args.length < 4 diff --git a/lib/puppet/parser/functions/validate_ip_address.rb b/lib/puppet/parser/functions/validate_ip_address.rb index 64fbd75..5d80cfb 100644 --- a/lib/puppet/parser/functions/validate_ip_address.rb +++ b/lib/puppet/parser/functions/validate_ip_address.rb @@ -8,12 +8,12 @@ module Puppet::Parser::Functions $my_ip = "1.2.3.4" validate_ip_address($my_ip) validate_ip_address("8.8.8.8", "172.16.0.1", $my_ip) - + $my_ip = "3ffe:505:2" validate_ip_address(1) validate_ip_address($my_ip) validate_ip_address("fe80::baf6:b1ff:fe19:7507", $my_ip) - + The following values will fail, causing compilation to abort: $some_array = [ 1, true, false, "garbage string", "3ffe:505:2" ] validate_ip_address($some_array) @@ -23,6 +23,8 @@ module Puppet::Parser::Functions require "ipaddr" rescuable_exceptions = [ ArgumentError ] + function_deprecation([:validate_ip_address, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Ip_address. There is further documentation for validate_legacy function in the README.']) + if defined?(IPAddr::InvalidAddressError) rescuable_exceptions << IPAddr::InvalidAddressError end diff --git a/lib/puppet/parser/functions/validate_ipv4_address.rb b/lib/puppet/parser/functions/validate_ipv4_address.rb index 97faa57..0660abd 100644 --- a/lib/puppet/parser/functions/validate_ipv4_address.rb +++ b/lib/puppet/parser/functions/validate_ipv4_address.rb @@ -18,6 +18,8 @@ module Puppet::Parser::Functions ENDHEREDOC ) do |args| + function_deprecation([:validate_ipv4_address, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Ipv4. There is further documentation for validate_legacy function in the README.']) + require "ipaddr" rescuable_exceptions = [ ArgumentError ] diff --git a/lib/puppet/parser/functions/validate_ipv6_address.rb b/lib/puppet/parser/functions/validate_ipv6_address.rb index b0f2558..f5dd9e5 100644 --- a/lib/puppet/parser/functions/validate_ipv6_address.rb +++ b/lib/puppet/parser/functions/validate_ipv6_address.rb @@ -19,6 +19,8 @@ module Puppet::Parser::Functions ENDHEREDOC ) do |args| + function_deprecation([:validate_ipv6_address, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Ipv6. There is further documentation for validate_legacy function in the README.']) + require "ipaddr" rescuable_exceptions = [ ArgumentError ] diff --git a/lib/puppet/parser/functions/validate_numeric.rb b/lib/puppet/parser/functions/validate_numeric.rb index 3a14443..4205b30 100644 --- a/lib/puppet/parser/functions/validate_numeric.rb +++ b/lib/puppet/parser/functions/validate_numeric.rb @@ -2,7 +2,7 @@ module Puppet::Parser::Functions newfunction(:validate_numeric, :doc => <<-'ENDHEREDOC') do |args| Validate that the first argument is a numeric value (or an array of numeric values). Abort catalog compilation if any of the checks fail. - + The second argument is optional and passes a maximum. (All elements of) the first argument has to be less or equal to this max. The third argument is optional and passes a minimum. (All elements of) the first argument has to be greater or equal to this min. @@ -15,6 +15,8 @@ module Puppet::Parser::Functions ENDHEREDOC + function_deprecation([:validate_numeric, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Numeric. There is further documentation for validate_legacy function in the README.']) + # tell the user we need at least one, and optionally up to two other parameters raise Puppet::ParseError, "validate_numeric(): Wrong number of arguments; must be 1, 2 or 3, got #{args.length}" unless args.length > 0 and args.length < 4 diff --git a/lib/puppet/parser/functions/validate_re.rb b/lib/puppet/parser/functions/validate_re.rb index efee7f8..0ac83dd 100644 --- a/lib/puppet/parser/functions/validate_re.rb +++ b/lib/puppet/parser/functions/validate_re.rb @@ -29,6 +29,9 @@ module Puppet::Parser::Functions validate_re("${::operatingsystemmajrelease}", '^[57]$') ENDHEREDOC + + function_deprecation([:validate_re, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Re. There is further documentation for validate_legacy function in the README.']) + if (args.length < 2) or (args.length > 3) then raise Puppet::ParseError, "validate_re(): wrong number of arguments (#{args.length}; must be 2 or 3)" end diff --git a/lib/puppet/parser/functions/validate_slength.rb b/lib/puppet/parser/functions/validate_slength.rb index 47c7d4a..383855c 100644 --- a/lib/puppet/parser/functions/validate_slength.rb +++ b/lib/puppet/parser/functions/validate_slength.rb @@ -21,6 +21,8 @@ module Puppet::Parser::Functions ENDHEREDOC + function_deprecation([:validate_slength, 'This method is deprecated, please use the stdlib validate_legacy function, with String[]. There is further documentation for validate_legacy function in the README.']) + raise Puppet::ParseError, "validate_slength(): Wrong number of arguments (#{args.length}; must be 2 or 3)" unless args.length == 2 or args.length == 3 input, max_length, min_length = *args diff --git a/lib/puppet/parser/functions/validate_string.rb b/lib/puppet/parser/functions/validate_string.rb index c841f6a..6675d86 100644 --- a/lib/puppet/parser/functions/validate_string.rb +++ b/lib/puppet/parser/functions/validate_string.rb @@ -13,22 +13,25 @@ module Puppet::Parser::Functions validate_string(true) validate_string([ 'some', 'array' ]) - + Note: validate_string(undef) will not fail in this version of the functions API (incl. current and future parser). Instead, use: - + if $var == undef { fail('...') } - + ENDHEREDOC + function_deprecation([:validate_string, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::String. There is further documentation for validate_legacy function in the README.']) + unless args.length > 0 then raise Puppet::ParseError, ("validate_string(): wrong number of arguments (#{args.length}; must be > 0)") end args.each do |arg| - unless arg.is_a?(String) + # when called through the v4 API shim, undef gets translated to nil + unless arg.is_a?(String) || arg.nil? raise Puppet::ParseError, ("#{arg.inspect} is not a string. It looks to be a #{arg.class}") end end |