From 30e994fb6eb91e25a77e60a849f795d60c11af4b Mon Sep 17 00:00:00 2001 From: Leonardo Rodrigues de Mello Date: Fri, 6 Sep 2013 10:53:47 -0300 Subject: enhanced the error message of pick function. When pick function fail return a better error message like the other stdlib functions, indicating that the error is on function pick. This would help people that see the error to identity it is related to a incorrect use of stdlib function pick, instead of having to grep all puppet libraries and manifests source for the old message. I had also changed the spec test. pick function change spec as suggested GH-179 Fix the spec test to use expect {}.to instead of lambda {}.should as explained by Adrienthebo. "Using expect { }.to is preferred over lambda { }.should. In addition it's best practice to do a string match against the error message to ensure that we're catching the right error, instead of any error of the right type." Also fixed a typo on the error message, it was missing one space. pick function stylish fix as suggested on GH179 --- lib/puppet/parser/functions/pick.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/pick.rb b/lib/puppet/parser/functions/pick.rb index cbc0300..e9e5d66 100644 --- a/lib/puppet/parser/functions/pick.rb +++ b/lib/puppet/parser/functions/pick.rb @@ -21,7 +21,7 @@ EOS args.delete(:undefined) args.delete("") if args[0].to_s.empty? then - fail "Must provide non empty value." + fail Puppet::ParseError, "pick(): must receive at last one non empty value" else return args[0] end -- cgit v1.2.3 From c14cbf31e26b5749623947727ffac64817b5bb5f Mon Sep 17 00:00:00 2001 From: Leonardo Rodrigues de Mello Date: Mon, 9 Sep 2013 10:27:54 -0300 Subject: bug # 20681 delete() function should not remove elements from original list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The setup: list with 3 elements, delete one: $test_list = [‘a’, ‘b’, ‘c’] $test_deleted = delete($test_list, ‘a’) Print out the elements in ‘test_deleted’: notify { ‘group_output2’: withpath => true, name => “$cfeng::test_deleted”, } Notice: /Stage[main]/Syslog/Notify[group_output2]/message: bc Good! Run-on output shows that ‘a’ was deleted Print out the elements in ‘test_list’: notify { ‘group_output1’: withpath => true, name => “$cfeng::test_list”, } Notice: /Stage[main]/Syslog/Notify[group_output1]/message: bc WHAT!? 'a' was deleted from ‘test_list’ as well! Expected abc as output! This behaviour is confirmed for string, hash and array. This is fixed on this commit, I had added two spec tests to cover that cases. bug #20681 spec test for delete() function. I had forgot in the last commit the spec test for hash in the delete function. bug # 20681 delete() function change aproach. Instead of rejecting elements from the original list, we use collection = arguments[0].dup . then latter we could continue to use delete and gsub! on collection without impact on original argument. this is a better solution than the previous one, and works on ruby 1.8.7, 1.9.3 and 2.0.0. The previous solution does not work on ruby 1.8.7. delete function remove typo whitespace. fix typo whitespaces. --- lib/puppet/parser/functions/delete.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb index f814344..d03a293 100644 --- a/lib/puppet/parser/functions/delete.rb +++ b/lib/puppet/parser/functions/delete.rb @@ -27,7 +27,7 @@ string, or key from a hash. "given #{arguments.size} for 2.") end - collection = arguments[0] + collection = arguments[0].dup item = arguments[1] case collection -- cgit v1.2.3 From 51d96088c1d6bde8dae511d6a93bc6775e716f60 Mon Sep 17 00:00:00 2001 From: Leonardo Rodrigues de Mello Date: Mon, 16 Sep 2013 10:33:58 -0300 Subject: (#20681) fix behaviour of delete_values The issue #20681 describe the error of delete() function removing the elements from the origin array/hash/string. This issue affected other delete functions. Because ruby delete and delete_if functions make destructive changes to the origin array/hash. The delete_undef_values removed elements from the origin hash and this is not the desired behaviour. To solve this, we should dup or clone the hash before using the delete or delete_if ruby functions. This fix the problem and add unit tests, so we could enforce this behaviour and prevent regressions. --- lib/puppet/parser/functions/delete_values.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/delete_values.rb b/lib/puppet/parser/functions/delete_values.rb index 17b9d37..ca8eef5 100644 --- a/lib/puppet/parser/functions/delete_values.rb +++ b/lib/puppet/parser/functions/delete_values.rb @@ -21,6 +21,6 @@ Would return: {'a'=>'A','c'=>'C','B'=>'D'} raise(TypeError, "delete_values(): First argument must be a Hash. " + \ "Given an argument of class #{hash.class}.") end - hash.delete_if { |key, val| item == val } + hash.dup.delete_if { |key, val| item == val } end end -- cgit v1.2.3 From 80a8b7bd1fdb3aadd53d112ce9a20b86d8ea9a62 Mon Sep 17 00:00:00 2001 From: Leonardo Rodrigues de Mello Date: Mon, 16 Sep 2013 11:08:13 -0300 Subject: (#20681) fix behaviour of delete_undef_values The issue #20681 describe the error of delete() function removing the elements from the origin array/hash/string. This issue affected other delete functions. Because ruby delete and delete_if functions make destructive changes to the origin array/hash. The delete_undef_values removed elements from the origin array/hash and this is not the desired behaviour. To solve this, we should dup or clone the array/hash before using the delete or delete_if ruby functions. We should also check if args[0] is not nil before using dup, since dup on nil raises exception. This fix the problem and add unit tests, so we could enforce this behaviour and prevent regressions. --- lib/puppet/parser/functions/delete_undef_values.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/delete_undef_values.rb b/lib/puppet/parser/functions/delete_undef_values.rb index 4eecab2..532639e 100644 --- a/lib/puppet/parser/functions/delete_undef_values.rb +++ b/lib/puppet/parser/functions/delete_undef_values.rb @@ -18,16 +18,16 @@ Would return: ['A','',false] raise(Puppet::ParseError, "delete_undef_values(): Wrong number of arguments given " + "(#{args.size})") if args.size < 1 - - result = args[0] + + unless args[0].is_a? Array or args[0].is_a? Hash + raise(Puppet::ParseError, + "delete_undef_values(): expected an array or hash, got #{args[0]} type #{args[0].class} ") + end + result = args[0].dup if result.is_a?(Hash) result.delete_if {|key, val| val.equal? :undef} elsif result.is_a?(Array) result.delete :undef - else - raise(Puppet::ParseError, - "delete_undef_values(): Wrong argument type #{args[0].class} " + - "for first argument") end result end -- cgit v1.2.3 From 9e0d8a8e0a83e1659fdb289078fd15862dca028b Mon Sep 17 00:00:00 2001 From: sgzijl Date: Mon, 9 Sep 2013 16:20:36 +0200 Subject: (#22214): close content file before executing checkscript Right now validation seems to be done against zero byte generated temp files. We need to close the file before executing validation against it. --- lib/puppet/parser/functions/validate_cmd.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index 344a80c..2ebe91c 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -32,6 +32,7 @@ module Puppet::Parser::Functions tmpfile = Tempfile.new("validate_cmd") begin tmpfile.write(content) + tmpfile.close if Puppet::Util::Execution.respond_to?('execute') Puppet::Util::Execution.execute("#{checkscript} #{tmpfile.path}") else -- cgit v1.2.3 From 753801537f2e791d36cdded2e022878049a86119 Mon Sep 17 00:00:00 2001 From: Tehmasp Chaudhri Date: Fri, 25 Oct 2013 15:26:21 -0600 Subject: Fixed 'separator' typos --- lib/puppet/parser/functions/join.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/join.rb b/lib/puppet/parser/functions/join.rb index 005a46e..6c0a6ba 100644 --- a/lib/puppet/parser/functions/join.rb +++ b/lib/puppet/parser/functions/join.rb @@ -4,7 +4,7 @@ module Puppet::Parser::Functions newfunction(:join, :type => :rvalue, :doc => <<-EOS -This function joins an array into a string using a seperator. +This function joins an array into a string using a separator. *Examples:* -- cgit v1.2.3 From 57a5c0b3e36e1c01016865c264ca3642f64b5034 Mon Sep 17 00:00:00 2001 From: Matthew Haughton <3flex@users.noreply.github.com> Date: Tue, 29 Oct 2013 14:21:03 -0400 Subject: (Main) fix typo in pick error message Update pick error message "at least one non empty value" --- lib/puppet/parser/functions/pick.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/pick.rb b/lib/puppet/parser/functions/pick.rb index e9e5d66..fdd0aef 100644 --- a/lib/puppet/parser/functions/pick.rb +++ b/lib/puppet/parser/functions/pick.rb @@ -21,7 +21,7 @@ EOS args.delete(:undefined) args.delete("") if args[0].to_s.empty? then - fail Puppet::ParseError, "pick(): must receive at last one non empty value" + fail Puppet::ParseError, "pick(): must receive at least one non empty value" else return args[0] end -- cgit v1.2.3 From 199ca9c78be3ceb7d8d49329ea28ff75e53d979e Mon Sep 17 00:00:00 2001 From: Justin Burnham Date: Tue, 24 Sep 2013 10:57:15 -0700 Subject: (#20200) Add a recursive merge function. Issue #20200 notes that the merge function does not support nested hashes. To prevent unintended side effects with changing merge, add a deep_merge function instead. --- lib/puppet/parser/functions/deep_merge.rb | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lib/puppet/parser/functions/deep_merge.rb (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/deep_merge.rb b/lib/puppet/parser/functions/deep_merge.rb new file mode 100644 index 0000000..94677b8 --- /dev/null +++ b/lib/puppet/parser/functions/deep_merge.rb @@ -0,0 +1,44 @@ +module Puppet::Parser::Functions + newfunction(:deep_merge, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| + Recursively merges two or more hashes together and returns the resulting hash. + + For example: + + $hash1 = {'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } } + $hash2 = {'two' => 'dos', 'three' => { 'five' => 5 } } + $merged_hash = deep_merge($hash1, $hash2) + # The resulting hash is equivalent to: + # $merged_hash = { 'one' => 1, 'two' => 'dos', 'three' => { 'four' => 4, 'five' => 5 } } + + When there is a duplicate key that is a hash, they are recursively merged. + When there is a duplicate key that is not a hash, the key in the rightmost hash will "win." + + ENDHEREDOC + + if args.length < 2 + raise Puppet::ParseError, ("deep_merge(): wrong number of arguments (#{args.length}; must be at least 2)") + end + + deep_merge = Proc.new do |hash1,hash2| + hash1.merge!(hash2) do |key,old_value,new_value| + if old_value.is_a?(Hash) && new_value.is_a?(Hash) + deep_merge.call(old_value, new_value) + else + new_value + end + end + end + + result = Hash.new + args.each do |arg| + next if arg.is_a? String and arg.empty? # empty string is synonym for puppet's undef + # If the argument was not a hash, skip it. + unless arg.is_a?(Hash) + raise Puppet::ParseError, "deep_merge: unexpected argument type #{arg.class}, only expects hash arguments" + end + + deep_merge.call(result, arg) + end + return( result ) + end +end -- cgit v1.2.3 From 1077881873024a5d2f53d3b4c4159ee49ae38c7a Mon Sep 17 00:00:00 2001 From: Joshua Hoblitt Date: Mon, 9 Dec 2013 11:45:50 -0700 Subject: (#23381) add is_bool() function --- lib/puppet/parser/functions/is_bool.rb | 22 ++++++++++++++++++++++ lib/puppet/parser/functions/validate_bool.rb | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 lib/puppet/parser/functions/is_bool.rb (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/is_bool.rb b/lib/puppet/parser/functions/is_bool.rb new file mode 100644 index 0000000..8bbdbc8 --- /dev/null +++ b/lib/puppet/parser/functions/is_bool.rb @@ -0,0 +1,22 @@ +# +# is_bool.rb +# + +module Puppet::Parser::Functions + newfunction(:is_bool, :type => :rvalue, :doc => <<-EOS +Returns true if the variable passed to this function is a boolean. + EOS + ) do |arguments| + + raise(Puppet::ParseError, "is_bool(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size != 1 + + type = arguments[0] + + result = type.is_a?(TrueClass) || type.is_a?(FalseClass) + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/validate_bool.rb b/lib/puppet/parser/functions/validate_bool.rb index 62c1d88..59a0805 100644 --- a/lib/puppet/parser/functions/validate_bool.rb +++ b/lib/puppet/parser/functions/validate_bool.rb @@ -24,7 +24,7 @@ module Puppet::Parser::Functions end args.each do |arg| - unless (arg.is_a?(TrueClass) || arg.is_a?(FalseClass)) + unless function_is_bool([arg]) raise Puppet::ParseError, ("#{arg.inspect} is not a boolean. It looks to be a #{arg.class}") end end -- cgit v1.2.3 From 7991dd20738209ab86a657ba62f2600ba39ca8ef Mon Sep 17 00:00:00 2001 From: Franco Catena Date: Thu, 5 Dec 2013 16:11:59 -0300 Subject: Fix prefix exception message (Closes #23364) --- lib/puppet/parser/functions/prefix.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb index 62211ae..d02286a 100644 --- a/lib/puppet/parser/functions/prefix.rb +++ b/lib/puppet/parser/functions/prefix.rb @@ -28,7 +28,7 @@ Will return: ['pa','pb','pc'] if prefix unless prefix.is_a?(String) - raise Puppet::ParseError, "prefix(): expected second argument to be a String, got #{suffix.inspect}" + raise Puppet::ParseError, "prefix(): expected second argument to be a String, got #{prefix.inspect}" end end -- cgit v1.2.3 From 735db82bef56bf939c971ab76a66647269d6ae35 Mon Sep 17 00:00:00 2001 From: Tomas Doran Date: Tue, 19 Nov 2013 18:38:12 +0000 Subject: Allow a single argument, rather than an array --- lib/puppet/parser/functions/ensure_packages.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/ensure_packages.rb b/lib/puppet/parser/functions/ensure_packages.rb index 450ea02..371d63a 100644 --- a/lib/puppet/parser/functions/ensure_packages.rb +++ b/lib/puppet/parser/functions/ensure_packages.rb @@ -11,8 +11,7 @@ Takes a list of packages and only installs them if they don't already exist. raise(Puppet::ParseError, "ensure_packages(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size != 1 - raise(Puppet::ParseError, "ensure_packages(): Requires array " + - "given (#{arguments[0].class})") if !arguments[0].kind_of?(Array) + arguments[0] = [ arguments[0] ] unless arguments[0].kind_of?(Array) Puppet::Parser::Functions.function(:ensure_resource) arguments[0].each { |package_name| -- cgit v1.2.3 From 686a05aea20eb45560698caaad5c9f2f01821ae6 Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Wed, 15 Jan 2014 11:03:49 -0800 Subject: (maint) refactor ensure_packages for clarity --- lib/puppet/parser/functions/ensure_packages.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/ensure_packages.rb b/lib/puppet/parser/functions/ensure_packages.rb index 371d63a..1e0f225 100644 --- a/lib/puppet/parser/functions/ensure_packages.rb +++ b/lib/puppet/parser/functions/ensure_packages.rb @@ -1,7 +1,6 @@ # # ensure_packages.rb # -require 'puppet/parser/functions' module Puppet::Parser::Functions newfunction(:ensure_packages, :type => :statement, :doc => <<-EOS @@ -9,12 +8,15 @@ Takes a list of packages and only installs them if they don't already exist. EOS ) do |arguments| - raise(Puppet::ParseError, "ensure_packages(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size != 1 - arguments[0] = [ arguments[0] ] unless arguments[0].kind_of?(Array) + if arguments.size != 1 + raise(Puppet::ParseError, "ensure_packages(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") + end + + packages = Array(arguments[0]) Puppet::Parser::Functions.function(:ensure_resource) - arguments[0].each { |package_name| + packages.each { |package_name| function_ensure_resource(['package', package_name, {'ensure' => 'present' } ]) } end -- cgit v1.2.3 From 264dc9bbdee8f04b78f8193d62d17b2ba096d671 Mon Sep 17 00:00:00 2001 From: Simon Effenberg Date: Fri, 20 Dec 2013 00:13:39 +0100 Subject: (PUP-1195) Fix is_numeric/is_integer when checking non-string parameters I expect a function called "is_numeric" or "is_integer" to check if a variable is an integer or a number even if the variable passed by isn't a string nor a number at all. Otherwise we should call them is_string_a_number and is_string_an_integer and we have then to remove the check for .is_a?(Number) and .is_a?(FixNum) now checking also if it is a hex or octal number improved/corrected checking for integer * checking against Integer instead of Fixnum so that also Bignum is matching * now .is_a? Integer is done first so this is quiet fast Now many types of numerics are recognized. 1. Float/Integer values (signed or unsigned, with exponent or without) 2. octal and hex check 3. except hex numbers and the "0." in a float lower than 1 can be prefixed with a '0'. whitespaces shouldn't be allowed as prefix/suffix string representation of numbers should not contain any type of whitespace.. the user is responsible to clean a string before checking it.. fix documentation and added more checks tried to be 99.9% backward compatible * for now the decission is post poned if hex and octal numbers should be allowed or not (is_numeric) * native Bignum is now also a valid integer class fix problem with old 1.8 ruby and Hash.to_s/Array.to_s In ruby < 1.9 array and hashes would be recognized as numeric if they have a special format: 1.8: [1,2,3,4].to_s = "1234" {1=>2}.to_s = "12" 1.9: [1,2,3,4].to_s = "[1, 2, 3, 4]" {1=>2}.to_s = "{1=>2}" --- lib/puppet/parser/functions/is_integer.rb | 26 ++++++++++++---- lib/puppet/parser/functions/is_numeric.rb | 50 +++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 7 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/is_integer.rb b/lib/puppet/parser/functions/is_integer.rb index 6b29e98..3c4874e 100644 --- a/lib/puppet/parser/functions/is_integer.rb +++ b/lib/puppet/parser/functions/is_integer.rb @@ -4,7 +4,10 @@ module Puppet::Parser::Functions newfunction(:is_integer, :type => :rvalue, :doc => <<-EOS -Returns true if the variable returned to this string is an integer. +Returns true if the variable passed to this function is an integer. + +If the variable is a string it has to be in the correct format of an +integer. EOS ) do |arguments| @@ -15,12 +18,25 @@ Returns true if the variable returned to this string is an integer. value = arguments[0] - if value != value.to_i.to_s and !value.is_a? Fixnum then - return false - else + # Regex is taken from the lexer of puppet + # puppet/pops/parser/lexer.rb but modified to match also + # negative values and disallow numbers prefixed with multiple + # 0's + # + # TODO these parameter should be a constant but I'm not sure + # if there is no risk to declare it inside of the module + # Puppet::Parser::Functions + + # Integer numbers like + # -1234568981273 + # 47291 + numeric = %r{^-?(?:(?:[1-9]\d*)|0)$} + + if value.is_a? Integer or (value.is_a? String and value.match numeric) return true + else + return false end - end end diff --git a/lib/puppet/parser/functions/is_numeric.rb b/lib/puppet/parser/functions/is_numeric.rb index abf0321..f2417f3 100644 --- a/lib/puppet/parser/functions/is_numeric.rb +++ b/lib/puppet/parser/functions/is_numeric.rb @@ -5,6 +5,20 @@ module Puppet::Parser::Functions newfunction(:is_numeric, :type => :rvalue, :doc => <<-EOS Returns true if the variable passed to this function is a number. + +The function recognizes only integer and float but not hex or octal +numbers (for now) until a decision is made how to handle these types. + +The parameter can be in the native format or given as string representation +of a number. + +Valid examples: + + 77435 + 10e-12 + -8475 + 0.2343 + -23.561e3 EOS ) do |arguments| @@ -15,12 +29,44 @@ Returns true if the variable passed to this function is a number. value = arguments[0] - if value == value.to_f.to_s or value == value.to_i.to_s or value.is_a? Numeric then + # Regex is taken from the lexer of puppet + # puppet/pops/parser/lexer.rb but modified to match also + # negative values and disallow invalid octal numbers or + # numbers prefixed with multiple 0's (except in hex numbers) + # + # TODO these parameters should be constants but I'm not sure + # if there is no risk to declare them inside of the module + # Puppet::Parser::Functions + + # TODO decide if this should be used + # HEX numbers like + # 0xaa230F + # 0X1234009C + # 0x0012 + # -12FcD + #numeric_hex = %r{^-?0[xX][0-9A-Fa-f]+$} + + # TODO decide if this should be used + # OCTAL numbers like + # 01234567 + # -045372 + #numeric_oct = %r{^-?0[1-7][0-7]*$} + + # Integer/Float numbers like + # -0.1234568981273 + # 47291 + # 42.12345e-12 + numeric = %r{^-?(?:(?:[1-9]\d*)|0)(?:\.\d+)?(?:[eE]-?\d+)?$} + + if value.is_a? Numeric or (value.is_a? String and ( + value.match(numeric) #or + # value.match(numeric_hex) or + # value.match(numeric_oct) + )) return true else return false end - end end -- cgit v1.2.3 From 2c8450d830453e452b819bfb05678768336c3031 Mon Sep 17 00:00:00 2001 From: Henrik Lindberg Date: Fri, 24 Jan 2014 00:22:09 +0100 Subject: (PUP-1195) Rephrase documentation for is_integer and is_numeric The documentation contained references to future decisions about functionality. Text rephrased for clarity. --- lib/puppet/parser/functions/is_integer.rb | 8 +++++--- lib/puppet/parser/functions/is_numeric.rb | 12 +++++++----- 2 files changed, 12 insertions(+), 8 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/is_integer.rb b/lib/puppet/parser/functions/is_integer.rb index 3c4874e..c03d28d 100644 --- a/lib/puppet/parser/functions/is_integer.rb +++ b/lib/puppet/parser/functions/is_integer.rb @@ -4,10 +4,12 @@ module Puppet::Parser::Functions newfunction(:is_integer, :type => :rvalue, :doc => <<-EOS -Returns true if the variable passed to this function is an integer. +Returns true if the variable passed to this function is an Integer or +a decimal (base 10) integer in String form. The string may +start with a '-' (minus). A value of '0' is allowed, but a leading '0' digit may not +be followed by other digits as this indicates that the value is octal (base 8). -If the variable is a string it has to be in the correct format of an -integer. +If given any other argument `false` is returned. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_numeric.rb b/lib/puppet/parser/functions/is_numeric.rb index f2417f3..e7e1d2a 100644 --- a/lib/puppet/parser/functions/is_numeric.rb +++ b/lib/puppet/parser/functions/is_numeric.rb @@ -4,13 +4,15 @@ module Puppet::Parser::Functions newfunction(:is_numeric, :type => :rvalue, :doc => <<-EOS -Returns true if the variable passed to this function is a number. +Returns true if the given argument is a Numeric (Integer or Float), +or a String containing either a valid integer in decimal base 10 form, or +a valid floating point string representation. -The function recognizes only integer and float but not hex or octal -numbers (for now) until a decision is made how to handle these types. +The function recognizes only decimal (base 10) integers and float but not +integers in hex (base 16) or octal (base 8) form. -The parameter can be in the native format or given as string representation -of a number. +The string representation may start with a '-' (minus). If a decimal '.' is used, +it must be followed by at least one digit. Valid examples: -- cgit v1.2.3 From 52fcef573f206630df007caf2979fb4c404e3dbc Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Thu, 23 Jan 2014 15:22:27 +0100 Subject: (PUP-638) Add a pick_default() function that always returns a value. This version of pick() does not error out, instead always returning at least the last argument, even if that too has no "real" value. --- lib/puppet/parser/functions/pick_default.rb | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lib/puppet/parser/functions/pick_default.rb (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/pick_default.rb b/lib/puppet/parser/functions/pick_default.rb new file mode 100644 index 0000000..36e33ab --- /dev/null +++ b/lib/puppet/parser/functions/pick_default.rb @@ -0,0 +1,35 @@ +module Puppet::Parser::Functions + newfunction(:pick_default, :type => :rvalue, :doc => <<-EOS + +This function is similar to a coalesce function in SQL in that it will return +the first value in a list of values that is not undefined or an empty string +(two things in Puppet that will return a boolean false value). If no value is +found, it will return the last argument. + +Typically, this function is used to check for a value in the Puppet +Dashboard/Enterprise Console, and failover to a default value like the +following: + + $real_jenkins_version = pick_default($::jenkins_version, '1.449') + +The value of $real_jenkins_version will first look for a top-scope variable +called 'jenkins_version' (note that parameters set in the Puppet Dashboard/ +Enterprise Console are brought into Puppet as top-scope variables), and, +failing that, will use a default value of 1.449. + +Note that, contrary to the pick() function, the pick_default does not fail if +all arguments are empty. This allows pick_default to use an empty value as +default. + +EOS +) do |args| + fail "Must receive at least one argument." if args.empty? + default = args.last + args = args[0..-2].compact + args.delete(:undef) + args.delete(:undefined) + args.delete("") + args << default + return args[0] + end +end -- cgit v1.2.3 From a972e0645b31cf1eb89874cb08b403bdc0fad3a4 Mon Sep 17 00:00:00 2001 From: Sharif Nassar Date: Wed, 5 Feb 2014 15:01:45 -0800 Subject: Remove trailing whitespace --- lib/puppet/parser/functions/base64.rb | 12 ++++++------ lib/puppet/parser/functions/delete_undef_values.rb | 10 +++++----- lib/puppet/parser/functions/delete_values.rb | 2 +- lib/puppet/parser/functions/range.rb | 2 +- lib/puppet/parser/functions/str2bool.rb | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/base64.rb b/lib/puppet/parser/functions/base64.rb index d9a590a..617ba31 100644 --- a/lib/puppet/parser/functions/base64.rb +++ b/lib/puppet/parser/functions/base64.rb @@ -1,5 +1,5 @@ module Puppet::Parser::Functions - + newfunction(:base64, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| Base64 encode or decode a string based on the command and the string submitted @@ -10,9 +10,9 @@ module Puppet::Parser::Functions $decodestring = base64('decode','dGhlc3RyaW5n') ENDHEREDOC - + require 'base64' - + raise Puppet::ParseError, ("base64(): Wrong number of arguments (#{args.length}; must be = 2)") unless args.length == 2 actions = ['encode','decode'] @@ -20,18 +20,18 @@ module Puppet::Parser::Functions 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 - + case args[0] when 'encode' result = Base64.encode64(args[1]) when 'decode' result = Base64.decode64(args[1]) end - + return result end end diff --git a/lib/puppet/parser/functions/delete_undef_values.rb b/lib/puppet/parser/functions/delete_undef_values.rb index 532639e..f94d4da 100644 --- a/lib/puppet/parser/functions/delete_undef_values.rb +++ b/lib/puppet/parser/functions/delete_undef_values.rb @@ -3,7 +3,7 @@ module Puppet::Parser::Functions Returns a copy of input hash or array with all undefs deleted. *Examples:* - + $hash = delete_undef_values({a=>'A', b=>'', c=>undef, d => false}) Would return: {a => 'A', b => '', d => false} @@ -11,19 +11,19 @@ Would return: {a => 'A', b => '', d => false} $array = delete_undef_values(['A','',undef,false]) Would return: ['A','',false] - + EOS ) do |args| raise(Puppet::ParseError, "delete_undef_values(): Wrong number of arguments given " + "(#{args.size})") if args.size < 1 - - unless args[0].is_a? Array or args[0].is_a? Hash + + unless args[0].is_a? Array or args[0].is_a? Hash raise(Puppet::ParseError, "delete_undef_values(): expected an array or hash, got #{args[0]} type #{args[0].class} ") end - result = args[0].dup + result = args[0].dup if result.is_a?(Hash) result.delete_if {|key, val| val.equal? :undef} elsif result.is_a?(Array) diff --git a/lib/puppet/parser/functions/delete_values.rb b/lib/puppet/parser/functions/delete_values.rb index ca8eef5..f6c8c0e 100644 --- a/lib/puppet/parser/functions/delete_values.rb +++ b/lib/puppet/parser/functions/delete_values.rb @@ -19,7 +19,7 @@ Would return: {'a'=>'A','c'=>'C','B'=>'D'} if not hash.is_a?(Hash) raise(TypeError, "delete_values(): First argument must be a Hash. " + \ - "Given an argument of class #{hash.class}.") + "Given an argument of class #{hash.class}.") end hash.dup.delete_if { |key, val| item == val } end diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb index 0849491..ffbdf84 100644 --- a/lib/puppet/parser/functions/range.rb +++ b/lib/puppet/parser/functions/range.rb @@ -28,7 +28,7 @@ Will return: ["a","b","c"] Will return: ["host01", "host02", ..., "host09", "host10"] -Passing a third argument will cause the generated range to step by that +Passing a third argument will cause the generated range to step by that interval, e.g. range("0", "9", "2") diff --git a/lib/puppet/parser/functions/str2bool.rb b/lib/puppet/parser/functions/str2bool.rb index fece7a6..446732e 100644 --- a/lib/puppet/parser/functions/str2bool.rb +++ b/lib/puppet/parser/functions/str2bool.rb @@ -14,7 +14,7 @@ like: 0, f, n, false, no to 'false'. "given (#{arguments.size} for 1)") if arguments.size < 1 string = arguments[0] - + # If string is already Boolean, return it if !!string == string return string -- cgit v1.2.3 From c12e9afc97f4356bc20554d32861889680c5fdc1 Mon Sep 17 00:00:00 2001 From: Justin Burnham Date: Mon, 17 Feb 2014 11:46:55 -0800 Subject: PUP-1724 Don't modify the paramaters to deep_merge Instead of modifying the first paramater of deep_merge due to the use of the merge! function, instead use merge to return a copy of the merged object. This allows one to continue to use the original first parameter after the call to deep_merge. --- lib/puppet/parser/functions/deep_merge.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/deep_merge.rb b/lib/puppet/parser/functions/deep_merge.rb index 94677b8..6df32e9 100644 --- a/lib/puppet/parser/functions/deep_merge.rb +++ b/lib/puppet/parser/functions/deep_merge.rb @@ -20,7 +20,7 @@ module Puppet::Parser::Functions end deep_merge = Proc.new do |hash1,hash2| - hash1.merge!(hash2) do |key,old_value,new_value| + hash1.merge(hash2) do |key,old_value,new_value| if old_value.is_a?(Hash) && new_value.is_a?(Hash) deep_merge.call(old_value, new_value) else @@ -37,7 +37,7 @@ module Puppet::Parser::Functions raise Puppet::ParseError, "deep_merge: unexpected argument type #{arg.class}, only expects hash arguments" end - deep_merge.call(result, arg) + result = deep_merge.call(result, arg) end return( result ) end -- cgit v1.2.3 From 35bf5fd8c93d5052ecf5284ed3194a92cab838d5 Mon Sep 17 00:00:00 2001 From: Martin Foot Date: Fri, 21 Feb 2014 14:32:32 +0000 Subject: Allow concat to take non-array second parameters Also improve and extend concat tests to lock down functionality --- lib/puppet/parser/functions/concat.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb index c86aa00..6c86382 100644 --- a/lib/puppet/parser/functions/concat.rb +++ b/lib/puppet/parser/functions/concat.rb @@ -23,12 +23,16 @@ Would result in: a = arguments[0] b = arguments[1] - # Check that both args are arrays. - unless a.is_a?(Array) and b.is_a?(Array) + # Check that the first parameter is an array + unless a.is_a?(Array) raise(Puppet::ParseError, 'concat(): Requires array to work with') end - result = a.concat(b) + if b.is_a?(Array) + result = a.concat(b) + else + result = a << b + end return result end -- cgit v1.2.3 From d9b5e912bbb6dffff01e03a0b040fd78888f2578 Mon Sep 17 00:00:00 2001 From: Yanis Guenane Date: Sun, 30 Mar 2014 18:47:36 -0400 Subject: (MODULES-603) Add defaults arguments to ensure_packages() Without this patch one can not specify package resource specific parameters. All the ensure_packages() function does it makes sure the named packages are installed. This patch allows one to pass default as a second argument and allow greater flexibility on packages installations. Use case like the following are now possible : * ensure_packages(['r10k', 'serverspec'], {'provider' => 'gem'}) * ensure_packages(['ntp'], {'require' => 'Exec[foobar]'}) --- lib/puppet/parser/functions/ensure_packages.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/ensure_packages.rb b/lib/puppet/parser/functions/ensure_packages.rb index 1e0f225..f1da4aa 100644 --- a/lib/puppet/parser/functions/ensure_packages.rb +++ b/lib/puppet/parser/functions/ensure_packages.rb @@ -5,19 +5,29 @@ module Puppet::Parser::Functions newfunction(:ensure_packages, :type => :statement, :doc => <<-EOS Takes a list of packages and only installs them if they don't already exist. +It optionally takes a hash as a second parameter that will be passed as the +third argument to the ensure_resource() function. EOS ) do |arguments| - if arguments.size != 1 + if arguments.size > 2 or arguments.size == 0 raise(Puppet::ParseError, "ensure_packages(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") + "given (#{arguments.size} for 1 or 2)") + elsif arguments.size == 2 and !arguments[1].is_a?(Hash) + raise(Puppet::ParseError, 'ensure_packages(): Requires second argument to be a Hash') end packages = Array(arguments[0]) + if arguments[1] + defaults = { 'ensure' => 'present' }.merge(arguments[1]) + else + defaults = { 'ensure' => 'present' } + end + Puppet::Parser::Functions.function(:ensure_resource) packages.each { |package_name| - function_ensure_resource(['package', package_name, {'ensure' => 'present' } ]) + function_ensure_resource(['package', package_name, defaults ]) } end end -- cgit v1.2.3 From 0804121719e4bde856723e19e8ff8bf6f9c2d55d Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Thu, 8 May 2014 14:43:06 -0700 Subject: Fix the stdlib functions that fail tests --- lib/puppet/parser/functions/floor.rb | 7 ++++++- lib/puppet/parser/functions/is_domain_name.rb | 3 +++ lib/puppet/parser/functions/is_float.rb | 3 +++ lib/puppet/parser/functions/is_function_available.rb | 3 +++ 4 files changed, 15 insertions(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/floor.rb b/lib/puppet/parser/functions/floor.rb index a401923..9a6f014 100644 --- a/lib/puppet/parser/functions/floor.rb +++ b/lib/puppet/parser/functions/floor.rb @@ -8,7 +8,12 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "floor(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size != 1 - arg = arguments[0] + begin + arg = Float(arguments[0]) + rescue TypeError, ArgumentError => e + raise(Puppet::ParseError, "floor(): Wrong argument type " + + "given (#{arguments[0]} for Numeric)") + end raise(Puppet::ParseError, "floor(): Wrong argument type " + "given (#{arg.class} for Numeric)") if arg.is_a?(Numeric) == false diff --git a/lib/puppet/parser/functions/is_domain_name.rb b/lib/puppet/parser/functions/is_domain_name.rb index 5826dc0..b3fee96 100644 --- a/lib/puppet/parser/functions/is_domain_name.rb +++ b/lib/puppet/parser/functions/is_domain_name.rb @@ -20,6 +20,9 @@ Returns true if the string passed to this function is a syntactically correct do label_min_length=1 label_max_length=63 + # Only allow string types + return false unless domain.is_a?(String) + # Allow ".", it is the top level domain return true if domain == '.' diff --git a/lib/puppet/parser/functions/is_float.rb b/lib/puppet/parser/functions/is_float.rb index 911f3c2..a2da943 100644 --- a/lib/puppet/parser/functions/is_float.rb +++ b/lib/puppet/parser/functions/is_float.rb @@ -15,6 +15,9 @@ Returns true if the variable passed to this function is a float. value = arguments[0] + # Only allow Numeric or String types + return false unless value.is_a?(Numeric) or value.is_a?(String) + if value != value.to_f.to_s and !value.is_a? Float then return false else diff --git a/lib/puppet/parser/functions/is_function_available.rb b/lib/puppet/parser/functions/is_function_available.rb index 6cbd35c..6da82c8 100644 --- a/lib/puppet/parser/functions/is_function_available.rb +++ b/lib/puppet/parser/functions/is_function_available.rb @@ -15,6 +15,9 @@ true if the function exists, false if not. "given #{arguments.size} for 1") end + # Only allow String types + return false unless arguments[0].is_a?(String) + function = Puppet::Parser::Functions.function(arguments[0].to_sym) function.is_a?(String) and not function.empty? end -- cgit v1.2.3 From 42743614cb13541a2973f28251b1f79a33019d77 Mon Sep 17 00:00:00 2001 From: Ryan McKern Date: Tue, 13 May 2014 15:01:44 -0700 Subject: (MODULES-905) Add bool2str() and camelcase() for string manipulation Python likes to have its constants Capitalized, and the capitalize function only understands strings... so I shave a yak. bool2str will convert a boolean to its equivalent string value, and camelcase extends on uppercase & downcase to convert an underscore delimited string into a camelcased string. --- lib/puppet/parser/functions/bool2str.rb | 30 +++++++++++++++++++++++++++++ lib/puppet/parser/functions/camelcase.rb | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 lib/puppet/parser/functions/bool2str.rb create mode 100644 lib/puppet/parser/functions/camelcase.rb (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/bool2str.rb b/lib/puppet/parser/functions/bool2str.rb new file mode 100644 index 0000000..a97d356 --- /dev/null +++ b/lib/puppet/parser/functions/bool2str.rb @@ -0,0 +1,30 @@ +# +# bool2str.rb +# + +module Puppet::Parser::Functions + newfunction(:bool2str, :type => :rvalue, :doc => <<-EOS + Converts a boolean to a string. + Requires a single boolean or string as an input. + EOS + ) do |arguments| + + raise(Puppet::ParseError, "bool2str(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + # We can have either true or false, or string which resembles boolean ... + unless [FalseClass, TrueClass, String].include?(klass) + raise(Puppet::ParseError, 'bool2str(): Requires either ' + + 'boolean or string to work with') + end + + result = value.is_a?(String) ? value : value.to_s + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/camelcase.rb b/lib/puppet/parser/functions/camelcase.rb new file mode 100644 index 0000000..d7f43f7 --- /dev/null +++ b/lib/puppet/parser/functions/camelcase.rb @@ -0,0 +1,33 @@ +# +# camelcase.rb +# + +module Puppet::Parser::Functions + newfunction(:camelcase, :type => :rvalue, :doc => <<-EOS +Converts the case of a string or all strings in an array to camel case. + EOS + ) do |arguments| + + raise(Puppet::ParseError, "camelcase(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'camelcase(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + # Numbers in Puppet are often string-encoded which is troublesome ... + result = value.collect { |i| i.is_a?(String) ? i.split('_').map{|e| e.capitalize}.join : i } + else + result = value.split('_').map{|e| e.capitalize}.join + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 93c4151edfedb28a0cafa60011c57eb6d76ca6be Mon Sep 17 00:00:00 2001 From: Ryan McKern Date: Thu, 15 May 2014 15:01:14 -0700 Subject: (MODULES-905) Narrow the confinement in bool2str Previously, bool2str() accepted a broad array of boolean values and bare strings, without any attempt to validate that the strings in any way resembled "true" or "false" (or any of the other values bool2num() accepts). This commit narrows the input confinement to TrueClass and FalseClass, which means that bool2str() will only interpolate strict boolean values now. --- lib/puppet/parser/functions/bool2str.rb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/bool2str.rb b/lib/puppet/parser/functions/bool2str.rb index a97d356..fcd3791 100644 --- a/lib/puppet/parser/functions/bool2str.rb +++ b/lib/puppet/parser/functions/bool2str.rb @@ -5,7 +5,7 @@ module Puppet::Parser::Functions newfunction(:bool2str, :type => :rvalue, :doc => <<-EOS Converts a boolean to a string. - Requires a single boolean or string as an input. + Requires a single boolean as an input. EOS ) do |arguments| @@ -15,15 +15,12 @@ module Puppet::Parser::Functions value = arguments[0] klass = value.class - # We can have either true or false, or string which resembles boolean ... - unless [FalseClass, TrueClass, String].include?(klass) - raise(Puppet::ParseError, 'bool2str(): Requires either ' + - 'boolean or string to work with') + # We can have either true or false, and nothing else + unless [FalseClass, TrueClass].include?(klass) + raise(Puppet::ParseError, 'bool2str(): Requires a boolean to work with') end - result = value.is_a?(String) ? value : value.to_s - - return result + return value.to_s end end -- cgit v1.2.3 From 2062f9734bc955be896183790afc355aab428747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Tue, 10 Jun 2014 17:23:42 +0200 Subject: Add private() function --- lib/puppet/parser/functions/private.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lib/puppet/parser/functions/private.rb (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/private.rb b/lib/puppet/parser/functions/private.rb new file mode 100644 index 0000000..60210d3 --- /dev/null +++ b/lib/puppet/parser/functions/private.rb @@ -0,0 +1,29 @@ +# +# private.rb +# + +module Puppet::Parser::Functions + newfunction(:private, :doc => <<-'EOS' + Sets the current class or definition as private. + Calling the class or definition from outside the current module will fail. + EOS + ) do |args| + + raise(Puppet::ParseError, "private(): Wrong number of arguments "+ + "given (#{args.size}}) for 0 or 1)") if args.size > 1 + + scope = self + if scope.lookupvar('module_name') != scope.lookupvar('caller_module_name') + message = nil + if args[0] and 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 +end -- cgit v1.2.3 From 64f0ae2269be8b98d2d028dcb6a732be67b0f3a6 Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Mon, 23 Jun 2014 22:47:03 -0700 Subject: Increase resilience if lookup var comes back with nil object --- lib/puppet/parser/functions/has_interface_with.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/has_interface_with.rb b/lib/puppet/parser/functions/has_interface_with.rb index 7f150a7..927b0df 100644 --- a/lib/puppet/parser/functions/has_interface_with.rb +++ b/lib/puppet/parser/functions/has_interface_with.rb @@ -25,7 +25,7 @@ has_interface_with("lo") => true interfaces = lookupvar('interfaces') # If we do not have any interfaces, then there are no requested attributes - return false if (interfaces == :undefined) + return false if (interfaces == :undefined || interfaces.nil?) interfaces = interfaces.split(',') -- cgit v1.2.3 From 85d5eadbab273e838eb2de33e78054b8d3867f41 Mon Sep 17 00:00:00 2001 From: Colleen Murphy Date: Tue, 19 Nov 2013 20:24:46 -0800 Subject: Concatenate arrays without modifying the first array --- lib/puppet/parser/functions/concat.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb index 6c86382..0d35b07 100644 --- a/lib/puppet/parser/functions/concat.rb +++ b/lib/puppet/parser/functions/concat.rb @@ -28,11 +28,7 @@ Would result in: raise(Puppet::ParseError, 'concat(): Requires array to work with') end - if b.is_a?(Array) - result = a.concat(b) - else - result = a << b - end + result = a + Array(b) return result end -- cgit v1.2.3 From 448e66b8bb3bd5a2b413436b21c2c480511223c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20P=C3=A1nek?= Date: Tue, 16 Sep 2014 17:55:26 +0200 Subject: Updated docs of validate_string to reflect bug See: https://tickets.puppetlabs.com/browse/MODULES-457 --- lib/puppet/parser/functions/validate_string.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/validate_string.rb b/lib/puppet/parser/functions/validate_string.rb index e667794..0bab21e 100644 --- a/lib/puppet/parser/functions/validate_string.rb +++ b/lib/puppet/parser/functions/validate_string.rb @@ -13,6 +13,9 @@ module Puppet::Parser::Functions validate_string(true) validate_string([ 'some', 'array' ]) + + NOTE: undef will only fail when using the future parser (See: https://tickets.puppetlabs.com/browse/MODULES-457) + $undefined = undef validate_string($undefined) -- cgit v1.2.3 From 6631934df8775305bc6c92514b5dec3624f5ba4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20P=C3=A1nek?= Date: Tue, 16 Sep 2014 19:03:02 +0200 Subject: Note that also future parser does not work --- lib/puppet/parser/functions/validate_string.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/validate_string.rb b/lib/puppet/parser/functions/validate_string.rb index 0bab21e..c841f6a 100644 --- a/lib/puppet/parser/functions/validate_string.rb +++ b/lib/puppet/parser/functions/validate_string.rb @@ -14,11 +14,13 @@ module Puppet::Parser::Functions validate_string(true) validate_string([ 'some', 'array' ]) - NOTE: undef will only fail when using the future parser (See: https://tickets.puppetlabs.com/browse/MODULES-457) + 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('...') + } - $undefined = undef - validate_string($undefined) - ENDHEREDOC unless args.length > 0 then -- cgit v1.2.3 From e2d7f3bb89a91d3aff6f9810d69bd84bc82ffb29 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Tue, 22 Apr 2014 09:36:28 +0200 Subject: (MODULES-707) chomp() fails because generate() no longer returns a string We need to use unless value.is_a?(String) || value.is_a?(Array) rather than klass = value.class unless [String, Array].include?(klass) because the klass version enforces type checking which is too strict, and does not allow us to accept objects wich have extended String (or Array). For example, generate() function now returns Puppet::Util::Execution::ProcessOutput which is just a very simple extension of String. While this in it's self was not intentional (PUP-2306) it is not unreasonable to cope with objects which extend Strings --- lib/puppet/parser/functions/bool2num.rb | 3 +-- lib/puppet/parser/functions/capitalize.rb | 3 +-- lib/puppet/parser/functions/chomp.rb | 3 +-- lib/puppet/parser/functions/chop.rb | 3 +-- lib/puppet/parser/functions/downcase.rb | 3 +-- lib/puppet/parser/functions/empty.rb | 3 +-- lib/puppet/parser/functions/fqdn_rotate.rb | 3 +-- lib/puppet/parser/functions/lstrip.rb | 3 +-- lib/puppet/parser/functions/reverse.rb | 3 +-- lib/puppet/parser/functions/rstrip.rb | 3 +-- lib/puppet/parser/functions/shuffle.rb | 3 +-- lib/puppet/parser/functions/strip.rb | 3 +-- lib/puppet/parser/functions/swapcase.rb | 3 +-- lib/puppet/parser/functions/unique.rb | 3 +-- lib/puppet/parser/functions/upcase.rb | 3 +-- lib/puppet/parser/functions/uriescape.rb | 3 +-- lib/puppet/parser/functions/zip.rb | 4 +--- 17 files changed, 17 insertions(+), 35 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/bool2num.rb b/lib/puppet/parser/functions/bool2num.rb index 9a07a8a..b32a4e8 100644 --- a/lib/puppet/parser/functions/bool2num.rb +++ b/lib/puppet/parser/functions/bool2num.rb @@ -15,10 +15,9 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class # We can have either true or false, or string which resembles boolean ... - unless [FalseClass, TrueClass, String].include?(klass) + unless value.is_a?(String) || value.is_a?(FalseClass) || value.is_a?(TrueClass) raise(Puppet::ParseError, 'bool2num(): Requires either ' + 'boolean or string to work with') end diff --git a/lib/puppet/parser/functions/capitalize.rb b/lib/puppet/parser/functions/capitalize.rb index 640d00b..98b2d16 100644 --- a/lib/puppet/parser/functions/capitalize.rb +++ b/lib/puppet/parser/functions/capitalize.rb @@ -13,9 +13,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'capitalize(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/chomp.rb b/lib/puppet/parser/functions/chomp.rb index 4564a00..c55841e 100644 --- a/lib/puppet/parser/functions/chomp.rb +++ b/lib/puppet/parser/functions/chomp.rb @@ -14,9 +14,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'chomp(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/chop.rb b/lib/puppet/parser/functions/chop.rb index f242af3..b24ab78 100644 --- a/lib/puppet/parser/functions/chop.rb +++ b/lib/puppet/parser/functions/chop.rb @@ -16,9 +16,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'chop(): Requires either an ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/downcase.rb b/lib/puppet/parser/functions/downcase.rb index 4066d21..040b84f 100644 --- a/lib/puppet/parser/functions/downcase.rb +++ b/lib/puppet/parser/functions/downcase.rb @@ -12,9 +12,8 @@ Converts the case of a string or all strings in an array to lower case. "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'downcase(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/empty.rb b/lib/puppet/parser/functions/empty.rb index 80ebb86..cca620f 100644 --- a/lib/puppet/parser/functions/empty.rb +++ b/lib/puppet/parser/functions/empty.rb @@ -12,9 +12,8 @@ Returns true if the variable is empty. "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, Hash, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(Hash) || value.is_a?(String) raise(Puppet::ParseError, 'empty(): Requires either ' + 'array, hash or string to work with') end diff --git a/lib/puppet/parser/functions/fqdn_rotate.rb b/lib/puppet/parser/functions/fqdn_rotate.rb index 6558206..7f4d37d 100644 --- a/lib/puppet/parser/functions/fqdn_rotate.rb +++ b/lib/puppet/parser/functions/fqdn_rotate.rb @@ -12,10 +12,9 @@ Rotates an array a random number of times based on a nodes fqdn. "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class require 'digest/md5' - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'fqdn_rotate(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/lstrip.rb b/lib/puppet/parser/functions/lstrip.rb index 3a64de3..624e4c8 100644 --- a/lib/puppet/parser/functions/lstrip.rb +++ b/lib/puppet/parser/functions/lstrip.rb @@ -12,9 +12,8 @@ Strips leading spaces to the left of a string. "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'lstrip(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/reverse.rb b/lib/puppet/parser/functions/reverse.rb index fe04869..7f1018f 100644 --- a/lib/puppet/parser/functions/reverse.rb +++ b/lib/puppet/parser/functions/reverse.rb @@ -12,9 +12,8 @@ Reverses the order of a string or array. "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'reverse(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/rstrip.rb b/lib/puppet/parser/functions/rstrip.rb index 29b0998..0cf8d22 100644 --- a/lib/puppet/parser/functions/rstrip.rb +++ b/lib/puppet/parser/functions/rstrip.rb @@ -12,9 +12,8 @@ Strips leading spaces to the right of the string. "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'rstrip(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/shuffle.rb b/lib/puppet/parser/functions/shuffle.rb index 18134ab..30c663d 100644 --- a/lib/puppet/parser/functions/shuffle.rb +++ b/lib/puppet/parser/functions/shuffle.rb @@ -12,9 +12,8 @@ Randomizes the order of a string or array elements. "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'shuffle(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/strip.rb b/lib/puppet/parser/functions/strip.rb index 5f4630d..3fac47d 100644 --- a/lib/puppet/parser/functions/strip.rb +++ b/lib/puppet/parser/functions/strip.rb @@ -19,9 +19,8 @@ Would result in: "aaa" "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'strip(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/swapcase.rb b/lib/puppet/parser/functions/swapcase.rb index b9e6632..eb7fe13 100644 --- a/lib/puppet/parser/functions/swapcase.rb +++ b/lib/puppet/parser/functions/swapcase.rb @@ -18,9 +18,8 @@ Would result in: "AbCd" "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'swapcase(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/unique.rb b/lib/puppet/parser/functions/unique.rb index 8844a74..cf770f3 100644 --- a/lib/puppet/parser/functions/unique.rb +++ b/lib/puppet/parser/functions/unique.rb @@ -28,9 +28,8 @@ This returns: "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'unique(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/upcase.rb b/lib/puppet/parser/functions/upcase.rb index fe6cadc..4302b29 100644 --- a/lib/puppet/parser/functions/upcase.rb +++ b/lib/puppet/parser/functions/upcase.rb @@ -20,9 +20,8 @@ Will return: "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'upcase(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/uriescape.rb b/lib/puppet/parser/functions/uriescape.rb index 0d81de5..a486eee 100644 --- a/lib/puppet/parser/functions/uriescape.rb +++ b/lib/puppet/parser/functions/uriescape.rb @@ -14,9 +14,8 @@ module Puppet::Parser::Functions "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] - klass = value.class - unless [Array, String].include?(klass) + unless value.is_a?(Array) || value.is_a?(String) raise(Puppet::ParseError, 'uriescape(): Requires either ' + 'array or string to work with') end diff --git a/lib/puppet/parser/functions/zip.rb b/lib/puppet/parser/functions/zip.rb index 2b56e9c..00266a4 100644 --- a/lib/puppet/parser/functions/zip.rb +++ b/lib/puppet/parser/functions/zip.rb @@ -30,10 +30,8 @@ Would result in: flatten = arguments[2] if arguments[2] if flatten - klass = flatten.class - # We can have either true or false, or string which resembles boolean ... - unless [FalseClass, TrueClass, String].include?(klass) + unless flatten.is_a?(String) || flatten.is_a?(FalseClass) || flatten.is_a?(TrueClass) raise(Puppet::ParseError, 'zip(): Requires either ' + 'boolean or string to work with') end -- cgit v1.2.3 From 23bc7d51bd3aca0c3a3391bb628a797dc768422a Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Fri, 23 May 2014 08:44:50 +0200 Subject: Re-use existing str2bool code rather than doing a copy and paste --- lib/puppet/parser/functions/bool2num.rb | 24 +----------------------- lib/puppet/parser/functions/zip.rb | 26 +------------------------- 2 files changed, 2 insertions(+), 48 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/bool2num.rb b/lib/puppet/parser/functions/bool2num.rb index b32a4e8..6ad6cf4 100644 --- a/lib/puppet/parser/functions/bool2num.rb +++ b/lib/puppet/parser/functions/bool2num.rb @@ -14,29 +14,7 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "bool2num(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size < 1 - value = arguments[0] - - # We can have either true or false, or string which resembles boolean ... - unless value.is_a?(String) || value.is_a?(FalseClass) || value.is_a?(TrueClass) - raise(Puppet::ParseError, 'bool2num(): Requires either ' + - 'boolean or string to work with') - end - - if value.is_a?(String) - # We consider all the yes, no, y, n and so on too ... - value = case value - # - # This is how undef looks like in Puppet ... - # We yield 0 (or false if you wish) in this case. - # - when /^$/, '' then false # Empty string will be false ... - when /^(1|t|y|true|yes)$/ then true - when /^(0|f|n|false|no)$/ then false - when /^(undef|undefined)$/ then false # This is not likely to happen ... - else - raise(Puppet::ParseError, 'bool2num(): Unknown type of boolean given') - end - end + value = function_str2bool([arguments[0]]) # We have real boolean values as well ... result = value ? 1 : 0 diff --git a/lib/puppet/parser/functions/zip.rb b/lib/puppet/parser/functions/zip.rb index 00266a4..3074f28 100644 --- a/lib/puppet/parser/functions/zip.rb +++ b/lib/puppet/parser/functions/zip.rb @@ -27,31 +27,7 @@ Would result in: raise(Puppet::ParseError, 'zip(): Requires array to work with') end - flatten = arguments[2] if arguments[2] - - if flatten - # We can have either true or false, or string which resembles boolean ... - unless flatten.is_a?(String) || flatten.is_a?(FalseClass) || flatten.is_a?(TrueClass) - raise(Puppet::ParseError, 'zip(): Requires either ' + - 'boolean or string to work with') - end - - if flatten.is_a?(String) - # We consider all the yes, no, y, n and so on too ... - flatten = case flatten - # - # This is how undef looks like in Puppet ... - # We yield false in this case. - # - when /^$/, '' then false # Empty string will be false ... - when /^(1|t|y|true|yes)$/ then true - when /^(0|f|n|false|no)$/ then false - when /^(undef|undefined)$/ then false # This is not likely to happen ... - else - raise(Puppet::ParseError, 'zip(): Unknown type of boolean given') - end - end - end + flatten = function_str2bool([arguments[2]]) if arguments[2] result = a.zip(b) result = flatten ? result.flatten : result -- cgit v1.2.3 From 032f93af1d9704dc218ef411e9e81befbe1c70a3 Mon Sep 17 00:00:00 2001 From: Tomas Doran Date: Fri, 18 Jul 2014 14:36:09 -0700 Subject: Fix strict_variables = true --- lib/puppet/parser/functions/getvar.rb | 5 ++++- lib/puppet/parser/functions/has_interface_with.rb | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/getvar.rb b/lib/puppet/parser/functions/getvar.rb index 1621149..fb336b6 100644 --- a/lib/puppet/parser/functions/getvar.rb +++ b/lib/puppet/parser/functions/getvar.rb @@ -19,7 +19,10 @@ module Puppet::Parser::Functions raise Puppet::ParseError, ("getvar(): wrong number of arguments (#{args.length}; must be 1)") end - self.lookupvar("#{args[0]}") + begin + self.lookupvar("#{args[0]}") + rescue Puppet::ParseError # Eat the exception if strict_variables = true is set + end end diff --git a/lib/puppet/parser/functions/has_interface_with.rb b/lib/puppet/parser/functions/has_interface_with.rb index 927b0df..10ad542 100644 --- a/lib/puppet/parser/functions/has_interface_with.rb +++ b/lib/puppet/parser/functions/has_interface_with.rb @@ -41,7 +41,12 @@ has_interface_with("lo") => true result = false interfaces.each do |iface| - if value == lookupvar("#{kind}_#{iface}") + factval = nil + begin + factval = lookupvar("#{kind}_#{iface}") + rescue Puppet::ParseError # Eat the exception if strict_variables = true is set + end + if value == factval result = true break end -- cgit v1.2.3 From 8ad7f68ecaf444b39c4ca89e2e62790bc00ad89a Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Wed, 8 Oct 2014 10:14:10 -0700 Subject: ENTERPRISE-281 fixes issue with has_interfaces and case mismatch causing us not to return some interfaces --- lib/puppet/parser/functions/has_interface_with.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/has_interface_with.rb b/lib/puppet/parser/functions/has_interface_with.rb index 927b0df..d5dbe47 100644 --- a/lib/puppet/parser/functions/has_interface_with.rb +++ b/lib/puppet/parser/functions/has_interface_with.rb @@ -34,13 +34,15 @@ has_interface_with("lo") => true end kind, value = args - + kind.downcase! + if lookupvar(kind) == value return true end result = false interfaces.each do |iface| + iface.downcase! if value == lookupvar("#{kind}_#{iface}") result = true break -- cgit v1.2.3 From 0d11bde5073d2976e9f5d9625757a7a71ddab6c4 Mon Sep 17 00:00:00 2001 From: Mathias Klette Date: Fri, 10 Oct 2014 22:54:22 +0200 Subject: ensure_resource: be more verbose in debug mode helps discovering duplication issues, especially when figthing boolean vs. string arguments --- lib/puppet/parser/functions/ensure_resource.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/ensure_resource.rb b/lib/puppet/parser/functions/ensure_resource.rb index 05e5593..1ba6a44 100644 --- a/lib/puppet/parser/functions/ensure_resource.rb +++ b/lib/puppet/parser/functions/ensure_resource.rb @@ -36,8 +36,9 @@ ENDOFDOC items.each do |item| Puppet::Parser::Functions.function(:defined_with_params) if function_defined_with_params(["#{type}[#{item}]", params]) - Puppet.debug("Resource #{type}[#{item}] not created because it already exists") + Puppet.debug("Resource #{type}[#{item}] with params #{params} not created because it already exists") else + Puppet.debug("Create new resource #{type}[#{item}] with params #{params}") Puppet::Parser::Functions.function(:create_resources) function_create_resources([type.capitalize, { item => params }]) end -- cgit v1.2.3 From 624ccbd22c845a042e8128d44c5299c0732e81de Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Fri, 24 Oct 2014 16:35:34 -0700 Subject: add require 'tempfile' to resolve a previously autorequired resource --- lib/puppet/parser/functions/validate_augeas.rb | 2 ++ lib/puppet/parser/functions/validate_cmd.rb | 1 + 2 files changed, 3 insertions(+) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/validate_augeas.rb b/lib/puppet/parser/functions/validate_augeas.rb index 154d660..4ea4fe0 100644 --- a/lib/puppet/parser/functions/validate_augeas.rb +++ b/lib/puppet/parser/functions/validate_augeas.rb @@ -1,3 +1,5 @@ +require 'tempfile' + module Puppet::Parser::Functions newfunction(:validate_augeas, :doc => <<-'ENDHEREDOC') do |args| Perform validation of a string using an Augeas lens diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index 2ebe91c..652aa55 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -1,4 +1,5 @@ require 'puppet/util/execution' +require 'tempfile' module Puppet::Parser::Functions newfunction(:validate_cmd, :doc => <<-'ENDHEREDOC') do |args| -- cgit v1.2.3 From 9295d0d5cae91f8c9b3b4b4fd891661666ab9e13 Mon Sep 17 00:00:00 2001 From: Zachary Alex Stern Date: Tue, 28 Oct 2014 13:14:06 -0700 Subject: Added correct converstions for PB and EB. * We were converting Exabytes to bytes as Petabytes. * Updated tests to cover ever unit. * Added note that we're going by the old, inaccurate definitions of Kilobytes, Megabytes, etc, in that we treat them as powers of 2. --- lib/puppet/parser/functions/to_bytes.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/to_bytes.rb b/lib/puppet/parser/functions/to_bytes.rb index 8ff73d1..df490ea 100644 --- a/lib/puppet/parser/functions/to_bytes.rb +++ b/lib/puppet/parser/functions/to_bytes.rb @@ -2,6 +2,8 @@ module Puppet::Parser::Functions newfunction(:to_bytes, :type => :rvalue, :doc => <<-EOS Converts the argument into bytes, for example 4 kB becomes 4096. Takes a single string value as an argument. + These conversions reflect a layperson's understanding of + 1 MB = 1024 KB, when in fact 1 MB = 1000 KB, and 1 MiB = 1024 KiB. EOS ) do |arguments| @@ -21,7 +23,8 @@ module Puppet::Parser::Functions when 'M' then return (value*(1<<20)).to_i when 'G' then return (value*(1<<30)).to_i when 'T' then return (value*(1<<40)).to_i - when 'E' then return (value*(1<<50)).to_i + when 'P' then return (value*(1<<50)).to_i + when 'E' then return (value*(1<<60)).to_i else raise Puppet::ParseError, "to_bytes(): Unknown prefix #{prefix}" end end -- cgit v1.2.3 From 51f1d574d92cf686ac79e57e69d8aff31caf2925 Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Tue, 28 Oct 2014 15:27:24 -0700 Subject: Fix testcases for Future Parser and resolve issue with values_at in assuming that it was dealing with a string --- lib/puppet/parser/functions/values_at.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/values_at.rb b/lib/puppet/parser/functions/values_at.rb index d3e69d9..f350f53 100644 --- a/lib/puppet/parser/functions/values_at.rb +++ b/lib/puppet/parser/functions/values_at.rb @@ -49,6 +49,7 @@ Would return ['a','c','d']. indices_list = [] indices.each do |i| + i = i.to_s if m = i.match(/^(\d+)(\.\.\.?|\-)(\d+)$/) start = m[1].to_i stop = m[3].to_i -- cgit v1.2.3 From 328aae223f28881746f45d76c778ddbfa3adce39 Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Tue, 28 Oct 2014 16:46:16 -0700 Subject: Add proper exception catching of Windows errors when CreateProcess does not succeed --- lib/puppet/parser/functions/validate_cmd.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index 652aa55..c855c7e 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -42,6 +42,9 @@ module Puppet::Parser::Functions rescue Puppet::ExecutionFailure => detail msg += "\n#{detail}" raise Puppet::ParseError, msg + rescue SystemCallError => detail + msg += "\nWin32::Process::SystemCallError #{detail}" + raise Puppet::ParseError, msg ensure tmpfile.unlink end -- cgit v1.2.3 From 6c7da72c0fa6b5e0ed3e5e2d5c80ff27f1e99dee Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Wed, 29 Oct 2014 20:03:07 -0700 Subject: Fix validate_cmd, previous addition of SystemCallError only works for Puppet 3.7, previous version throw different exception. Wrapping in generic Exception catch all --- lib/puppet/parser/functions/validate_cmd.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index c855c7e..c6136a5 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -42,8 +42,8 @@ module Puppet::Parser::Functions rescue Puppet::ExecutionFailure => detail msg += "\n#{detail}" raise Puppet::ParseError, msg - rescue SystemCallError => detail - msg += "\nWin32::Process::SystemCallError #{detail}" + rescue Exception => detail + msg += "\n#{detail.class.name} #{detail}" raise Puppet::ParseError, msg ensure tmpfile.unlink -- cgit v1.2.3 From f19aea5a75998da95617fadce7a50aee65022d3b Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Wed, 29 Oct 2014 22:02:13 -0700 Subject: MODULES-1413 Ability to for future parser to use member with FixNum types --- lib/puppet/parser/functions/member.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/member.rb b/lib/puppet/parser/functions/member.rb index 43d76af..11a1d24 100644 --- a/lib/puppet/parser/functions/member.rb +++ b/lib/puppet/parser/functions/member.rb @@ -32,8 +32,9 @@ Would return: false item = arguments[1] + raise(Puppet::ParseError, 'member(): You must provide item ' + - 'to search for within array given') if item.empty? + 'to search for within array given') if item.respond_to?('empty?') && item.empty? result = array.include?(item) -- cgit v1.2.3 From c52e262a17d9defbd59bfed4761ab887d9e7840d Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Thu, 30 Oct 2014 23:37:00 -0700 Subject: Catch :undefined_variable thrown when Future Parser is enabled with 3.7.x --- lib/puppet/parser/functions/has_interface_with.rb | 26 +++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/has_interface_with.rb b/lib/puppet/parser/functions/has_interface_with.rb index 00e405d..1e91026 100644 --- a/lib/puppet/parser/functions/has_interface_with.rb +++ b/lib/puppet/parser/functions/has_interface_with.rb @@ -16,11 +16,11 @@ etc. If no "kind" is given, then the presence of the interface is checked: has_interface_with("lo") => true - EOS + EOS ) do |args| raise(Puppet::ParseError, "has_interface_with(): Wrong number of arguments " + - "given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2 + "given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2 interfaces = lookupvar('interfaces') @@ -35,7 +35,13 @@ has_interface_with("lo") => true kind, value = args - if lookupvar(kind) == value + # Bug with 3.7.1 - 3.7.3 when using future parser throws :undefined_variable + # https://tickets.puppetlabs.com/browse/PUP-3597 + factval = nil + catch :undefined_variable do + factval = lookupvar(kind) + end + if factval == value return true end @@ -44,15 +50,17 @@ has_interface_with("lo") => true iface.downcase! factval = nil begin - factval = lookupvar("#{kind}_#{iface}") + # Bug with 3.7.1 - 3.7.3 when using future parser throws :undefined_variable + # https://tickets.puppetlabs.com/browse/PUP-3597 + catch :undefined_variable do + factval = lookupvar("#{kind}_#{iface}") + end + if value == factval + result = true + end rescue Puppet::ParseError # Eat the exception if strict_variables = true is set end - if value == factval - result = true - break - end end - result end end -- cgit v1.2.3 From 4949cfd21cb97b17006d82f2f192ec9d01b0d1ee Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Mon, 10 Nov 2014 16:37:53 -0800 Subject: Fix breaking out of .each loop And some other small formatting fixes that don't belong in this patch. --- lib/puppet/parser/functions/has_interface_with.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/has_interface_with.rb b/lib/puppet/parser/functions/has_interface_with.rb index 1e91026..3691524 100644 --- a/lib/puppet/parser/functions/has_interface_with.rb +++ b/lib/puppet/parser/functions/has_interface_with.rb @@ -16,11 +16,11 @@ etc. If no "kind" is given, then the presence of the interface is checked: has_interface_with("lo") => true - EOS + EOS ) do |args| raise(Puppet::ParseError, "has_interface_with(): Wrong number of arguments " + - "given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2 + "given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2 interfaces = lookupvar('interfaces') @@ -55,12 +55,14 @@ has_interface_with("lo") => true catch :undefined_variable do factval = lookupvar("#{kind}_#{iface}") end - if value == factval - result = true - end rescue Puppet::ParseError # Eat the exception if strict_variables = true is set end + if value == factval + result = true + break + end end + result end end -- cgit v1.2.3 From e61f402283774883d7b7c7d0f04dec7c457c968c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Dal=C3=A9n?= Date: Wed, 12 Nov 2014 15:52:33 +0100 Subject: (maint) Fix indentation of range function --- lib/puppet/parser/functions/range.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb index ffbdf84..06d75d4 100644 --- a/lib/puppet/parser/functions/range.rb +++ b/lib/puppet/parser/functions/range.rb @@ -65,21 +65,21 @@ Will return: [0,2,4,6,8] end end - # Check whether we have integer value if so then make it so ... - if start.match(/^\d+$/) - start = start.to_i - stop = stop.to_i - else - start = start.to_s - stop = stop.to_s - end + # Check whether we have integer value if so then make it so ... + if start.match(/^\d+$/) + start = start.to_i + stop = stop.to_i + else + start = start.to_s + stop = stop.to_s + end - range = case type - when /^(\.\.|\-)$/ then (start .. stop) - when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... - end + range = case type + when /^(\.\.|\-)$/ then (start .. stop) + when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... + end - result = range.step(step).collect { |i| i } # Get them all ... Pokemon ... + result = range.step(step).collect { |i| i } # Get them all ... Pokemon ... return result end -- cgit v1.2.3 From ce995e15d5c266fd6d7fa781284771a5a5d5b00e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Dal=C3=A9n?= Date: Wed, 12 Nov 2014 16:02:05 +0100 Subject: Make the range function work with integers This is needed for the future parser which actually treats numbers as numbers and strings as strings. With this patch you can use range(1,5) instead of having to quote them like range('1','5'). --- lib/puppet/parser/functions/range.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb index 06d75d4..49fba21 100644 --- a/lib/puppet/parser/functions/range.rb +++ b/lib/puppet/parser/functions/range.rb @@ -66,7 +66,7 @@ Will return: [0,2,4,6,8] end # Check whether we have integer value if so then make it so ... - if start.match(/^\d+$/) + if start.to_s.match(/^\d+$/) start = start.to_i stop = stop.to_i else -- cgit v1.2.3 From c9f906f80325a12a6509633d87472bd3cbaf0364 Mon Sep 17 00:00:00 2001 From: Yanis Guenane Date: Mon, 15 Sep 2014 14:16:52 -0400 Subject: (MODULES-1329) Allow member function to look for array Currently, the member function allows one to only find if a variable is part of an array. Sometimes it is useful to find if an array is part of a bigger array for validation purpose. --- lib/puppet/parser/functions/member.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/member.rb b/lib/puppet/parser/functions/member.rb index 11a1d24..bb19a86 100644 --- a/lib/puppet/parser/functions/member.rb +++ b/lib/puppet/parser/functions/member.rb @@ -8,6 +8,7 @@ module Puppet::Parser::Functions newfunction(:member, :type => :rvalue, :doc => <<-EOS This function determines if a variable is a member of an array. +The variable can either be a string or an array. *Examples:* @@ -15,9 +16,17 @@ This function determines if a variable is a member of an array. Would return: true + member(['a', 'b', 'c'], ['a', 'b']) + +would return: true + member(['a','b'], 'c') Would return: false + + member(['a', 'b', 'c'], ['d', 'b']) + +would return: false EOS ) do |arguments| @@ -30,13 +39,17 @@ Would return: false raise(Puppet::ParseError, 'member(): Requires array to work with') end - item = arguments[1] + if arguments[1].is_a? String + item = Array(arguments[1]) + else + item = arguments[1] + end raise(Puppet::ParseError, 'member(): You must provide item ' + 'to search for within array given') if item.respond_to?('empty?') && item.empty? - result = array.include?(item) + result = (item - array).empty? return result end -- cgit v1.2.3 From c5467cc507c004d75037d07c70633d1e743825af Mon Sep 17 00:00:00 2001 From: Morgan Haskel Date: Fri, 14 Nov 2014 14:33:59 -0800 Subject: Need to convert strings and fixnums to arrays --- lib/puppet/parser/functions/member.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/member.rb b/lib/puppet/parser/functions/member.rb index bb19a86..88609ce 100644 --- a/lib/puppet/parser/functions/member.rb +++ b/lib/puppet/parser/functions/member.rb @@ -8,7 +8,7 @@ module Puppet::Parser::Functions newfunction(:member, :type => :rvalue, :doc => <<-EOS This function determines if a variable is a member of an array. -The variable can either be a string or an array. +The variable can be a string, fixnum, or array. *Examples:* @@ -39,7 +39,11 @@ would return: false raise(Puppet::ParseError, 'member(): Requires array to work with') end - if arguments[1].is_a? String + unless arguments[1].is_a? String or arguments[1].is_a? Fixnum or arguments[1].is_a? Array + raise(Puppet::ParseError, 'member(): Item to search for must be a string, fixnum, or array') + end + + if arguments[1].is_a? String or arguments[1].is_a? Fixnum item = Array(arguments[1]) else item = arguments[1] -- cgit v1.2.3 From 89995e4db0eacf55260cf5ca85e715e2e45dfce5 Mon Sep 17 00:00:00 2001 From: Oliver Bertuch Date: Tue, 25 Nov 2014 12:45:23 +0100 Subject: Allow array of pathes in validate_absolute_path --- .../parser/functions/validate_absolute_path.rb | 61 +++++++++++++--------- 1 file changed, 37 insertions(+), 24 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/validate_absolute_path.rb b/lib/puppet/parser/functions/validate_absolute_path.rb index fe27974..b696680 100644 --- a/lib/puppet/parser/functions/validate_absolute_path.rb +++ b/lib/puppet/parser/functions/validate_absolute_path.rb @@ -5,15 +5,20 @@ module Puppet::Parser::Functions The following values will pass: - $my_path = "C:/Program Files (x86)/Puppet Labs/Puppet" + $my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet' validate_absolute_path($my_path) - $my_path2 = "/var/lib/puppet" + $my_path2 = '/var/lib/puppet' validate_absolute_path($my_path2) - + $my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet','C:/Program Files/Puppet Labs/Puppet'] + validate_absolute_path($my_path3) + $my_path4 = ['/var/lib/puppet','/usr/share/puppet'] + validate_absolute_path($my_path4) The following values will fail, causing compilation to abort: validate_absolute_path(true) + validate_absolute_path('../var/lib/puppet') + validate_absolute_path('var/lib/puppet') validate_absolute_path([ 'var/lib/puppet', '/var/foo' ]) validate_absolute_path([ '/var/lib/puppet', 'var/foo' ]) $undefined = undef @@ -28,28 +33,36 @@ module Puppet::Parser::Functions end args.each do |arg| - # This logic was borrowed from - # [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb) - - # Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise. - if Puppet::Util.respond_to?(:absolute_path?) then - unless Puppet::Util.absolute_path?(arg, :posix) or Puppet::Util.absolute_path?(arg, :windows) - raise Puppet::ParseError, ("#{arg.inspect} is not an absolute path.") + # put arg to candidate var to be able to replace it + candidates = arg + # if arg is just a string with a path to test, convert it to an array + # to avoid test code duplication + unless arg.is_a?(Array) then + candidates = Array.new(1,arg) + end + # iterate over all pathes within the candidates array + candidates.each do |path| + # This logic was borrowed from + # [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb) + # Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise. + if Puppet::Util.respond_to?(:absolute_path?) then + unless Puppet::Util.absolute_path?(path, :posix) or Puppet::Util.absolute_path?(path, :windows) + raise Puppet::ParseError, ("#{path.inspect} is not an absolute path.") + end + else + # This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path? + # Determine in a platform-specific way whether a path is absolute. This + # defaults to the local platform if none is specified. + # Escape once for the string literal, and once for the regex. + slash = '[\\\\/]' + name = '[^\\\\/]+' + regexes = { + :windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i, + :posix => %r!^/!, + } + rval = (!!(path =~ regexes[:posix])) || (!!(path =~ regexes[:windows])) + rval or raise Puppet::ParseError, ("#{path.inspect} is not an absolute path.") end - else - # This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path? - # Determine in a platform-specific way whether a path is absolute. This - # defaults to the local platform if none is specified. - # Escape once for the string literal, and once for the regex. - slash = '[\\\\/]' - name = '[^\\\\/]+' - regexes = { - :windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i, - :posix => %r!^/!, - } - - rval = (!!(arg =~ regexes[:posix])) || (!!(arg =~ regexes[:windows])) - rval or raise Puppet::ParseError, ("#{arg.inspect} is not an absolute path.") end end end -- cgit v1.2.3 From 75a6186512b0d4d02593453fcc9ffe5e1b253107 Mon Sep 17 00:00:00 2001 From: Peter Souter Date: Thu, 4 Dec 2014 14:32:23 +0000 Subject: (MODULES-444) Update docs with new functionality --- lib/puppet/parser/functions/concat.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb index 0d35b07..4b53f59 100644 --- a/lib/puppet/parser/functions/concat.rb +++ b/lib/puppet/parser/functions/concat.rb @@ -4,15 +4,15 @@ module Puppet::Parser::Functions newfunction(:concat, :type => :rvalue, :doc => <<-EOS -Appends the contents of array 2 onto array 1. +Appends the contents of multiple arrays into array 1. *Example:* - concat(['1','2','3'],['4','5','6']) + concat(['1','2','3'],['4','5','6'],['7','8','9']) Would result in: - ['1','2','3','4','5','6'] + ['1','2','3','4','5','6','7','8','9'] EOS ) do |arguments| -- cgit v1.2.3 From 594c2dd38dc35a4f458ce511be9b7dd875915b44 Mon Sep 17 00:00:00 2001 From: Peter Souter Date: Thu, 4 Dec 2014 14:33:23 +0000 Subject: (MODULES-444) Change argument restriction to < 2 --- lib/puppet/parser/functions/concat.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb index 4b53f59..8400f7b 100644 --- a/lib/puppet/parser/functions/concat.rb +++ b/lib/puppet/parser/functions/concat.rb @@ -16,9 +16,9 @@ Would result in: EOS ) do |arguments| - # Check that 2 arguments have been given ... + # Check that more than 2 arguments have been given ... raise(Puppet::ParseError, "concat(): Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size != 2 + "given (#{arguments.size} for < 2)") if arguments.size < 2 a = arguments[0] b = arguments[1] -- cgit v1.2.3 From 84bd98645f248a1dc3e0ed791e3af6f2ba9996fa Mon Sep 17 00:00:00 2001 From: Peter Souter Date: Thu, 4 Dec 2014 14:34:25 +0000 Subject: (MODULES-444) - Real meat of the change This is the core change, we now go through the array and add it to the first element, instead of just two arguments. --- lib/puppet/parser/functions/concat.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb index 8400f7b..618e62d 100644 --- a/lib/puppet/parser/functions/concat.rb +++ b/lib/puppet/parser/functions/concat.rb @@ -21,14 +21,18 @@ Would result in: "given (#{arguments.size} for < 2)") if arguments.size < 2 a = arguments[0] - b = arguments[1] # Check that the first parameter is an array unless a.is_a?(Array) raise(Puppet::ParseError, 'concat(): Requires array to work with') end - result = a + Array(b) + result = a + arguments.shift + + arguments.each do |x| + result = result + Array(x) + end return result end -- cgit v1.2.3 From ef3d42f7bbdf95b21f46e580de309298cad300ea Mon Sep 17 00:00:00 2001 From: Rob Fugina Date: Mon, 17 Nov 2014 16:01:42 -0600 Subject: Added basename() based on Ruby's File.basename Based on dirname code. Includes RSpec tests and docs. --- lib/puppet/parser/functions/basename.rb | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 lib/puppet/parser/functions/basename.rb (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/basename.rb b/lib/puppet/parser/functions/basename.rb new file mode 100644 index 0000000..f7e4438 --- /dev/null +++ b/lib/puppet/parser/functions/basename.rb @@ -0,0 +1,34 @@ +module Puppet::Parser::Functions + newfunction(:basename, :type => :rvalue, :doc => <<-EOS + Strips directory (and optional suffix) from a filename + EOS + ) do |arguments| + + if arguments.size < 1 then + raise(Puppet::ParseError, "basename(): No arguments given") + elsif arguments.size > 2 then + raise(Puppet::ParseError, "basename(): Too many arguments given (#{arguments.size})") + else + + unless arguments[0].is_a?(String) + raise(Puppet::ParseError, 'basename(): Requires string as first argument') + end + + if arguments.size == 1 then + rv = File.basename(arguments[0]) + elsif arguments.size == 2 then + + unless arguments[1].is_a?(String) + raise(Puppet::ParseError, 'basename(): Requires string as second argument') + end + + rv = File.basename(arguments[0], arguments[1]) + end + + end + + return rv + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 165caa8be195590bfd445b87db5fe2d0227d99ad Mon Sep 17 00:00:00 2001 From: Peter Souter Date: Tue, 9 Dec 2014 14:20:31 +0000 Subject: (MODULES-1582) Initial spike for % placeholder This simply `gsub`'s the file path into where the % placeholder is. --- lib/puppet/parser/functions/validate_cmd.rb | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index c6136a5..7290265 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -6,9 +6,9 @@ module Puppet::Parser::Functions Perform validation of a string with an external command. The first argument of this function should be a string to test, and the second argument should be a path to a test command - taking a file as last argument. If the command, launched against - a tempfile containing the passed string, returns a non-null value, - compilation will abort with a parse error. + taking a % as a placeholder for the file path (will default to the end). + If the command, launched against a tempfile containing the passed string, + returns a non-null value, compilation will abort with a parse error. If a third argument is specified, this will be the error message raised and seen by the user. @@ -17,8 +17,12 @@ module Puppet::Parser::Functions Example: + # Defaults to end of path validate_cmd($sudoerscontent, '/usr/sbin/visudo -c -f', 'Visudo failed to validate sudoers content') + # % as file location + validate_cmd($haproxycontent, '/usr/sbin/haproxy -f % -c', 'Haproxy failed to validate config content') + ENDHEREDOC if (args.length < 2) or (args.length > 3) then raise Puppet::ParseError, ("validate_cmd(): wrong number of arguments (#{args.length}; must be 2 or 3)") @@ -34,10 +38,17 @@ module Puppet::Parser::Functions begin tmpfile.write(content) tmpfile.close + + if checkscript.include?('%') + check_with_correct_location = checkscript.gsub(/%/,tmpfile.path) + else + check_with_correct_location = "#{checkscript} #{tmpfile.path}" + end + if Puppet::Util::Execution.respond_to?('execute') - Puppet::Util::Execution.execute("#{checkscript} #{tmpfile.path}") + Puppet::Util::Execution.execute(check_with_correct_location) else - Puppet::Util.execute("#{checkscript} #{tmpfile.path}") + Puppet::Util.execute(check_with_correct_location) end rescue Puppet::ExecutionFailure => detail msg += "\n#{detail}" -- cgit v1.2.3 From b3d007f1daa9bd7c8c02f372494df3a6e6ff6acf Mon Sep 17 00:00:00 2001 From: Peter Souter Date: Thu, 18 Dec 2014 23:08:13 +0000 Subject: (MODULES-1582) Improve % detection Avoids any validate commands that have %'s in them other than "... % ..." --- lib/puppet/parser/functions/validate_cmd.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index 7290265..5df3c60 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -39,7 +39,7 @@ module Puppet::Parser::Functions tmpfile.write(content) tmpfile.close - if checkscript.include?('%') + if checkscript =~ /\s%(\s|$)/ check_with_correct_location = checkscript.gsub(/%/,tmpfile.path) else check_with_correct_location = "#{checkscript} #{tmpfile.path}" -- cgit v1.2.3 From 8ec6f8dbfdaaa4b34030cbe1f4764c42c629240e Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Fri, 19 Dec 2014 10:26:01 -0800 Subject: MODULES-1606 add ability to pass array to delete for items to delete --- lib/puppet/parser/functions/delete.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb index d03a293..a1e39ed 100644 --- a/lib/puppet/parser/functions/delete.rb +++ b/lib/puppet/parser/functions/delete.rb @@ -19,25 +19,25 @@ string, or key from a hash. delete('abracadabra', 'bra') Would return: 'acada' - EOS + EOS ) do |arguments| if (arguments.size != 2) then raise(Puppet::ParseError, "delete(): Wrong number of arguments "+ - "given #{arguments.size} for 2.") + "given #{arguments.size} for 2.") end collection = arguments[0].dup - item = arguments[1] - - case collection - when Array, Hash - collection.delete item - when String - collection.gsub! item, '' - else - raise(TypeError, "delete(): First argument must be an Array, " + - "String, or Hash. Given an argument of class #{collection.class}.") + Array(arguments[1]).each do |item| + case collection + when Array, Hash + collection.delete item + when String + collection.gsub! item, '' + else + raise(TypeError, "delete(): First argument must be an Array, " + + "String, or Hash. Given an argument of class #{collection.class}.") + end end collection end -- cgit v1.2.3 From f6e20d20684f315d746e99ff554a0bdc714f96a3 Mon Sep 17 00:00:00 2001 From: Travis Fields Date: Fri, 19 Dec 2014 10:41:07 -0800 Subject: Update docs to reflect new behavior of delete function taking array in second argument --- lib/puppet/parser/functions/delete.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb index a1e39ed..f548b44 100644 --- a/lib/puppet/parser/functions/delete.rb +++ b/lib/puppet/parser/functions/delete.rb @@ -17,6 +17,9 @@ string, or key from a hash. delete({'a'=>1,'b'=>2,'c'=>3}, 'b') Would return: {'a'=>1,'c'=>3} + delete({'a'=>1,'b'=>2,'c'=>3}, ['b','c']) + Would return: {'a'=>1} + delete('abracadabra', 'bra') Would return: 'acada' EOS -- cgit v1.2.3 From 7c8ae311cade65e84df1054779a039ff906e630c Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Mon, 15 Dec 2014 16:11:10 -0800 Subject: (MODULES-1473) Deprecate type() function for new parser The `type()` function will cease to work on the new parser because 'type' is a reserved keyword. The `type3x()` function may be used to continue similar functionality, but will be deprecated in favor of the built-in typing system. The `type_of()` function has been included to introspect types in the new parser. --- lib/puppet/parser/functions/type.rb | 43 +++++------------------------ lib/puppet/parser/functions/type3x.rb | 51 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 37 deletions(-) create mode 100644 lib/puppet/parser/functions/type3x.rb (limited to 'lib/puppet/parser/functions') diff --git a/lib/puppet/parser/functions/type.rb b/lib/puppet/parser/functions/type.rb index 8d85f11..016529b 100644 --- a/lib/puppet/parser/functions/type.rb +++ b/lib/puppet/parser/functions/type.rb @@ -4,46 +4,15 @@ module Puppet::Parser::Functions newfunction(:type, :type => :rvalue, :doc => <<-EOS -Returns the type when passed a variable. Type can be one of: - -* string -* array -* hash -* float -* integer -* boolean + DEPRECATED: This function will cease to function on Puppet 4; please use type3x() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system. EOS - ) do |arguments| - - raise(Puppet::ParseError, "type(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - - klass = value.class - - if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass) - raise(Puppet::ParseError, 'type(): Unknown type') - end - - klass = klass.to_s # Ugly ... + ) do |args| - # We note that Integer is the parent to Bignum and Fixnum ... - result = case klass - when /^(?:Big|Fix)num$/ then 'integer' - when /^(?:True|False)Class$/ then 'boolean' - else klass + warning("type() DEPRECATED: This function will cease to function on Puppet 4; please use type3x() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.") + if ! Puppet::Parser::Functions.autoloader.loaded?(:type3x) + Puppet::Parser::Functions.autoloader.load(:type3x) end - - if result == "String" then - if value == value.to_i.to_s then - result = "Integer" - elsif value == value.to_f.to_s then - result = "Float" - end - end - - return result.downcase + function_type3x(args + [false]) end end diff --git a/lib/puppet/parser/functions/type3x.rb b/lib/puppet/parser/functions/type3x.rb new file mode 100644 index 0000000..0800b4a --- /dev/null +++ b/lib/puppet/parser/functions/type3x.rb @@ -0,0 +1,51 @@ +# +# type3x.rb +# + +module Puppet::Parser::Functions + newfunction(:type3x, :type => :rvalue, :doc => <<-EOS +DEPRECATED: This function will be removed when puppet 3 support is dropped; please migrate to the new parser's typing system. + +Returns the type when passed a value. Type can be one of: + +* string +* array +* hash +* float +* integer +* boolean + EOS + ) do |args| + raise(Puppet::ParseError, "type3x(): Wrong number of arguments " + + "given (#{args.size} for 1)") if args.size < 1 + + value = args[0] + + klass = value.class + + if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass) + raise(Puppet::ParseError, 'type3x(): Unknown type') + end + + klass = klass.to_s # Ugly ... + + # We note that Integer is the parent to Bignum and Fixnum ... + result = case klass + when /^(?:Big|Fix)num$/ then 'integer' + when /^(?:True|False)Class$/ then 'boolean' + else klass + end + + if result == "String" then + if value == value.to_i.to_s then + result = "Integer" + elsif value == value.to_f.to_s then + result = "Float" + end + end + + return result.downcase + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3