diff options
author | Helen <helen@puppetlabs.com> | 2016-04-15 13:29:23 +0100 |
---|---|---|
committer | Helen <helen@puppetlabs.com> | 2016-04-15 13:29:23 +0100 |
commit | d9f65387c99c99177f94075fc840e7c574e07417 (patch) | |
tree | e2b1bf7362973650efdcac0a3be68df7d9bae8f6 /lib/puppet/parser/functions | |
parent | 29961218a74bfdae96371c42a3c336e714c79a93 (diff) | |
parent | 085035dccebbf27cf2bfd7f1d9101c746f5178a2 (diff) |
Merge pull request #595 from tphoney/4.12.0_release
master to 4.12.x
Diffstat (limited to 'lib/puppet/parser/functions')
-rw-r--r-- | lib/puppet/parser/functions/concat.rb | 2 | ||||
-rw-r--r-- | lib/puppet/parser/functions/delete.rb | 4 | ||||
-rw-r--r-- | lib/puppet/parser/functions/hash.rb | 2 | ||||
-rw-r--r-- | lib/puppet/parser/functions/is_email_address.rb | 21 | ||||
-rw-r--r-- | lib/puppet/parser/functions/parsejson.rb | 2 | ||||
-rw-r--r-- | lib/puppet/parser/functions/parseyaml.rb | 5 | ||||
-rw-r--r-- | lib/puppet/parser/functions/validate_cmd.rb | 2 | ||||
-rw-r--r-- | lib/puppet/parser/functions/validate_email_address.rb | 31 |
8 files changed, 61 insertions, 8 deletions
diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb index 618e62d..91edb4e 100644 --- a/lib/puppet/parser/functions/concat.rb +++ b/lib/puppet/parser/functions/concat.rb @@ -31,7 +31,7 @@ Would result in: arguments.shift arguments.each do |x| - result = result + Array(x) + result = result + (x.is_a?(Array) ? x : [x]) end return result diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb index f548b44..8435163 100644 --- a/lib/puppet/parser/functions/delete.rb +++ b/lib/puppet/parser/functions/delete.rb @@ -2,8 +2,6 @@ # delete.rb # -# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... - module Puppet::Parser::Functions newfunction(:delete, :type => :rvalue, :doc => <<-EOS Deletes all instances of a given element from an array, substring from a @@ -34,7 +32,7 @@ string, or key from a hash. Array(arguments[1]).each do |item| case collection when Array, Hash - collection.delete item + collection.delete_if { |coll_item| coll_item =~ %r{#{item}} } when String collection.gsub! item, '' else diff --git a/lib/puppet/parser/functions/hash.rb b/lib/puppet/parser/functions/hash.rb index 8cc4823..89d0e07 100644 --- a/lib/puppet/parser/functions/hash.rb +++ b/lib/puppet/parser/functions/hash.rb @@ -29,7 +29,7 @@ Would return: {'a'=>1,'b'=>2,'c'=>3} # This is to make it compatible with older version of Ruby ... array = array.flatten result = Hash[*array] - rescue Exception + rescue StandardError raise(Puppet::ParseError, 'hash(): Unable to compute ' + 'hash from array given') end diff --git a/lib/puppet/parser/functions/is_email_address.rb b/lib/puppet/parser/functions/is_email_address.rb new file mode 100644 index 0000000..ab8d075 --- /dev/null +++ b/lib/puppet/parser/functions/is_email_address.rb @@ -0,0 +1,21 @@ +# +# is_email_address.rb +# + +module Puppet::Parser::Functions + newfunction(:is_email_address, type: :rvalue, doc: <<-EOS +Returns true if the string passed to this function is a valid email address. + EOS + ) do |arguments| + if arguments.size != 1 + raise(Puppet::ParseError, 'is_email_address(): Wrong number of arguments '\ + "given #{arguments.size} for 1") + end + + # Taken from http://emailregex.com/ (simpler regex) + valid_email_regex = %r{\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z} + return (arguments[0] =~ valid_email_regex) == 0 + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/parsejson.rb b/lib/puppet/parser/functions/parsejson.rb index b4af40e..f7c2896 100644 --- a/lib/puppet/parser/functions/parsejson.rb +++ b/lib/puppet/parser/functions/parsejson.rb @@ -15,7 +15,7 @@ be returned if the parsing of YAML string have failed. begin PSON::load(arguments[0]) || arguments[1] - rescue Exception => e + rescue StandardError => e if arguments[1] arguments[1] else diff --git a/lib/puppet/parser/functions/parseyaml.rb b/lib/puppet/parser/functions/parseyaml.rb index 66d0413..ba9d98a 100644 --- a/lib/puppet/parser/functions/parseyaml.rb +++ b/lib/puppet/parser/functions/parseyaml.rb @@ -16,7 +16,10 @@ be returned if the parsing of YAML string have failed. begin YAML::load(arguments[0]) || arguments[1] - rescue Exception => e + # in ruby 1.9.3 Psych::SyntaxError is a RuntimeException + # this still needs to catch that and work also on rubies that + # do not have Psych available. + rescue StandardError, Psych::SyntaxError => e if arguments[1] arguments[1] else diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index 5df3c60..685162b 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -53,7 +53,7 @@ module Puppet::Parser::Functions rescue Puppet::ExecutionFailure => detail msg += "\n#{detail}" raise Puppet::ParseError, msg - rescue Exception => detail + rescue StandardError => detail msg += "\n#{detail.class.name} #{detail}" raise Puppet::ParseError, msg ensure diff --git a/lib/puppet/parser/functions/validate_email_address.rb b/lib/puppet/parser/functions/validate_email_address.rb new file mode 100644 index 0000000..63f59a7 --- /dev/null +++ b/lib/puppet/parser/functions/validate_email_address.rb @@ -0,0 +1,31 @@ +module Puppet::Parser::Functions + newfunction(:validate_email_address, doc: <<-ENDHEREDOC + Validate that all values passed are valid email addresses. + Fail compilation if any value fails this check. + The following values will pass: + $my_email = "waldo@gmail.com" + validate_email_address($my_email) + validate_email_address("bob@gmail.com", "alice@gmail.com", $my_email) + + The following values will fail, causing compilation to abort: + $some_array = [ 'bad_email@/d/efdf.com' ] + validate_email_address($some_array) + ENDHEREDOC + ) do |args| + rescuable_exceptions = [ArgumentError] + + if args.empty? + raise Puppet::ParseError, "validate_email_address(): wrong number of arguments (#{args.length}; must be > 0)" + end + + args.each do |arg| + raise Puppet::ParseError, "#{arg.inspect} is not a string." unless arg.is_a?(String) + + begin + raise Puppet::ParseError, "#{arg.inspect} is not a valid email address" unless function_is_email_address([arg]) + rescue *rescuable_exceptions + raise Puppet::ParseError, "#{arg.inspect} is not a valid email address" + end + end + end +end |