summaryrefslogtreecommitdiff
path: root/lib/puppet/parser
diff options
context:
space:
mode:
authorvarac <varacanero@zeromail.org>2013-08-27 18:03:56 +0200
committervarac <varacanero@zeromail.org>2013-08-27 18:03:56 +0200
commit473896f2ca22f5003ea47542ad8026819d2d8613 (patch)
tree297936a37a0cf217a76a5cee368f7f91430d9f02 /lib/puppet/parser
parent966f3b349a60b3997e58af1095bbd96671952fac (diff)
parent2a78cbfaad2acc61d6d8bab715db00c9890adbd7 (diff)
Merge branch 'master' of git://github.com/puppetlabs/puppetlabs-stdlib into leap_master
Diffstat (limited to 'lib/puppet/parser')
-rw-r--r--lib/puppet/parser/functions/delete_undef_values.rb34
-rw-r--r--lib/puppet/parser/functions/delete_values.rb26
-rw-r--r--lib/puppet/parser/functions/ensure_resource.rb2
-rw-r--r--lib/puppet/parser/functions/range.rb10
-rw-r--r--lib/puppet/parser/functions/uriescape.rb3
-rw-r--r--lib/puppet/parser/functions/validate_ipv4_address.rb48
-rw-r--r--lib/puppet/parser/functions/validate_ipv6_address.rb49
-rw-r--r--lib/puppet/parser/functions/validate_slength.rb71
8 files changed, 213 insertions, 30 deletions
diff --git a/lib/puppet/parser/functions/delete_undef_values.rb b/lib/puppet/parser/functions/delete_undef_values.rb
new file mode 100644
index 0000000..4eecab2
--- /dev/null
+++ b/lib/puppet/parser/functions/delete_undef_values.rb
@@ -0,0 +1,34 @@
+module Puppet::Parser::Functions
+ newfunction(:delete_undef_values, :type => :rvalue, :doc => <<-EOS
+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}
+
+ $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
+
+ result = args[0]
+ 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
+end
diff --git a/lib/puppet/parser/functions/delete_values.rb b/lib/puppet/parser/functions/delete_values.rb
new file mode 100644
index 0000000..17b9d37
--- /dev/null
+++ b/lib/puppet/parser/functions/delete_values.rb
@@ -0,0 +1,26 @@
+module Puppet::Parser::Functions
+ newfunction(:delete_values, :type => :rvalue, :doc => <<-EOS
+Deletes all instances of a given value from a hash.
+
+*Examples:*
+
+ delete_values({'a'=>'A','b'=>'B','c'=>'C','B'=>'D'}, 'B')
+
+Would return: {'a'=>'A','c'=>'C','B'=>'D'}
+
+ EOS
+ ) do |arguments|
+
+ raise(Puppet::ParseError,
+ "delete_values(): Wrong number of arguments given " +
+ "(#{arguments.size} of 2)") if arguments.size != 2
+
+ hash, item = arguments
+
+ if not hash.is_a?(Hash)
+ 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 }
+ end
+end
diff --git a/lib/puppet/parser/functions/ensure_resource.rb b/lib/puppet/parser/functions/ensure_resource.rb
index a9a1733..05e5593 100644
--- a/lib/puppet/parser/functions/ensure_resource.rb
+++ b/lib/puppet/parser/functions/ensure_resource.rb
@@ -13,7 +13,7 @@ resource.
This example only creates the resource if it does not already exist:
- ensure_resource('user, 'dan', {'ensure' => 'present' })
+ ensure_resource('user', 'dan', {'ensure' => 'present' })
If the resource already exists but does not match the specified parameters,
this function will attempt to recreate the resource leading to a duplicate
diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb
index 825617b..0849491 100644
--- a/lib/puppet/parser/functions/range.rb
+++ b/lib/puppet/parser/functions/range.rb
@@ -27,6 +27,13 @@ Will return: ["a","b","c"]
range("host01", "host10")
Will return: ["host01", "host02", ..., "host09", "host10"]
+
+Passing a third argument will cause the generated range to step by that
+interval, e.g.
+
+ range("0", "9", "2")
+
+Will return: [0,2,4,6,8]
EOS
) do |arguments|
@@ -37,6 +44,7 @@ Will return: ["host01", "host02", ..., "host09", "host10"]
if arguments.size > 1
start = arguments[0]
stop = arguments[1]
+ step = arguments[2].nil? ? 1 : arguments[2].to_i.abs
type = '..' # We select simplest type for Range available in Ruby ...
@@ -71,7 +79,7 @@ Will return: ["host01", "host02", ..., "host09", "host10"]
when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ...
end
- result = range.collect { |i| i } # Get them all ... Pokemon ...
+ result = range.step(step).collect { |i| i } # Get them all ... Pokemon ...
return result
end
diff --git a/lib/puppet/parser/functions/uriescape.rb b/lib/puppet/parser/functions/uriescape.rb
index 67b93a6..0d81de5 100644
--- a/lib/puppet/parser/functions/uriescape.rb
+++ b/lib/puppet/parser/functions/uriescape.rb
@@ -15,7 +15,6 @@ module Puppet::Parser::Functions
value = arguments[0]
klass = value.class
- unsafe = ":/?#[]@!$&'()*+,;= "
unless [Array, String].include?(klass)
raise(Puppet::ParseError, 'uriescape(): Requires either ' +
@@ -26,7 +25,7 @@ module Puppet::Parser::Functions
# Numbers in Puppet are often string-encoded which is troublesome ...
result = value.collect { |i| i.is_a?(String) ? URI.escape(i,unsafe) : i }
else
- result = URI.escape(value,unsafe)
+ result = URI.escape(value)
end
return result
diff --git a/lib/puppet/parser/functions/validate_ipv4_address.rb b/lib/puppet/parser/functions/validate_ipv4_address.rb
new file mode 100644
index 0000000..fc02748
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_ipv4_address.rb
@@ -0,0 +1,48 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_ipv4_address, :doc => <<-ENDHEREDOC
+ Validate that all values passed are valid IPv4 addresses.
+ Fail compilation if any value fails this check.
+
+ The following values will pass:
+
+ $my_ip = "1.2.3.4"
+ validate_ipv4_address($my_ip)
+ validate_bool("8.8.8.8", "172.16.0.1", $my_ip)
+
+ The following values will fail, causing compilation to abort:
+
+ $some_array = [ 1, true, false, "garbage string", "3ffe:505:2" ]
+ validate_ipv4_address($some_array)
+
+ ENDHEREDOC
+ ) do |args|
+
+ require "ipaddr"
+ rescuable_exceptions = [ ArgumentError ]
+
+ if defined?(IPAddr::InvalidAddressError)
+ rescuable_exceptions << IPAddr::InvalidAddressError
+ end
+
+ unless args.length > 0 then
+ raise Puppet::ParseError, ("validate_ipv4_address(): wrong number of arguments (#{args.length}; must be > 0)")
+ end
+
+ args.each do |arg|
+ unless arg.is_a?(String)
+ raise Puppet::ParseError, "#{arg.inspect} is not a string."
+ end
+
+ begin
+ unless IPAddr.new(arg).ipv4?
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
+ end
+ rescue *rescuable_exceptions
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
+ end
+ end
+
+ end
+
+end
diff --git a/lib/puppet/parser/functions/validate_ipv6_address.rb b/lib/puppet/parser/functions/validate_ipv6_address.rb
new file mode 100644
index 0000000..b0f2558
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_ipv6_address.rb
@@ -0,0 +1,49 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_ipv6_address, :doc => <<-ENDHEREDOC
+ Validate that all values passed are valid IPv6 addresses.
+ Fail compilation if any value fails this check.
+
+ The following values will pass:
+
+ $my_ip = "3ffe:505:2"
+ validate_ipv6_address(1)
+ validate_ipv6_address($my_ip)
+ validate_bool("fe80::baf6:b1ff:fe19:7507", $my_ip)
+
+ The following values will fail, causing compilation to abort:
+
+ $some_array = [ true, false, "garbage string", "1.2.3.4" ]
+ validate_ipv6_address($some_array)
+
+ ENDHEREDOC
+ ) do |args|
+
+ require "ipaddr"
+ rescuable_exceptions = [ ArgumentError ]
+
+ if defined?(IPAddr::InvalidAddressError)
+ rescuable_exceptions << IPAddr::InvalidAddressError
+ end
+
+ unless args.length > 0 then
+ raise Puppet::ParseError, ("validate_ipv6_address(): wrong number of arguments (#{args.length}; must be > 0)")
+ end
+
+ args.each do |arg|
+ unless arg.is_a?(String)
+ raise Puppet::ParseError, "#{arg.inspect} is not a string."
+ end
+
+ begin
+ unless IPAddr.new(arg).ipv6?
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
+ end
+ rescue *rescuable_exceptions
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
+ end
+ end
+
+ end
+
+end
diff --git a/lib/puppet/parser/functions/validate_slength.rb b/lib/puppet/parser/functions/validate_slength.rb
index fdcc0a2..7d534f3 100644
--- a/lib/puppet/parser/functions/validate_slength.rb
+++ b/lib/puppet/parser/functions/validate_slength.rb
@@ -2,51 +2,70 @@ module Puppet::Parser::Functions
newfunction(:validate_slength, :doc => <<-'ENDHEREDOC') do |args|
Validate that the first argument is a string (or an array of strings), and
- less/equal to than the length of the second argument. It fails if the first
- argument is not a string or array of strings, and if arg 2 is not convertable
- to a number.
+ less/equal to than the length of the second argument. An optional third
+ parameter can be given a the minimum length. It fails if the first
+ argument is not a string or array of strings, and if arg 2 and arg 3 are
+ not convertable to a number.
The following values will pass:
validate_slength("discombobulate",17)
validate_slength(["discombobulate","moo"],17)
+ validate_slength(["discombobulate","moo"],17,3)
The following valueis will not:
validate_slength("discombobulate",1)
validate_slength(["discombobulate","thermometer"],5)
+ validate_slength(["discombobulate","moo"],17,10)
ENDHEREDOC
- raise Puppet::ParseError, ("validate_slength(): Wrong number of arguments (#{args.length}; must be = 2)") unless args.length == 2
+ raise Puppet::ParseError, "validate_slength(): Wrong number of arguments (#{args.length}; must be 2 or 3)" unless args.length == 2 or args.length == 3
- unless (args[0].is_a?(String) or args[0].is_a?(Array))
- raise Puppet::ParseError, ("validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}")
- end
+ input, max_length, min_length = *args
begin
- max_length = args[1].to_i
- rescue NoMethodError => e
- raise Puppet::ParseError, ("validate_slength(): Couldn't convert whatever you passed as the length parameter to an integer - sorry: " + e.message )
+ max_length = Integer(max_length)
+ raise ArgumentError if max_length <= 0
+ rescue ArgumentError, TypeError
+ raise Puppet::ParseError, "validate_slength(): Expected second argument to be a positive Numeric, got #{max_length}:#{max_length.class}"
+ end
+
+ if min_length
+ begin
+ min_length = Integer(min_length)
+ raise ArgumentError if min_length < 0
+ rescue ArgumentError, TypeError
+ raise Puppet::ParseError, "validate_slength(): Expected third argument to be unset or a positive Numeric, got #{min_length}:#{min_length.class}"
+ end
+ else
+ min_length = 0
+ end
+
+ if min_length > max_length
+ raise Puppet::ParseError, "validate_slength(): Expected second argument to be larger than third argument"
+ end
+
+ validator = lambda do |str|
+ unless str.length <= max_length and str.length >= min_length
+ raise Puppet::ParseError, "validate_slength(): Expected length of #{input.inspect} to be between #{min_length} and #{max_length}, was #{input.length}"
+ end
end
- raise Puppet::ParseError, ("validate_slength(): please pass a positive number as max_length") unless max_length > 0
-
- case args[0]
- when String
- raise Puppet::ParseError, ("validate_slength(): #{args[0].inspect} is #{args[0].length} characters. It should have been less than or equal to #{max_length} characters") unless args[0].length <= max_length
- when Array
- args[0].each do |arg|
- if arg.is_a?(String)
- unless ( arg.is_a?(String) and arg.length <= max_length )
- raise Puppet::ParseError, ("validate_slength(): #{arg.inspect} is #{arg.length} characters. It should have been less than or equal to #{max_length} characters")
- end
- else
- raise Puppet::ParseError, ("validate_slength(): #{arg.inspect} is not a string, it's a #{arg.class}")
- end
+ case input
+ when String
+ validator.call(input)
+ when Array
+ input.each_with_index do |arg, pos|
+ if arg.is_a? String
+ validator.call(arg)
+ else
+ raise Puppet::ParseError, "validate_slength(): Expected element at array position #{pos} to be a String, got #{arg.class}"
end
- else
- raise Puppet::ParseError, ("validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}")
+ end
+ else
+ raise Puppet::ParseError, "validate_slength(): Expected first argument to be a String or Array, got #{input.class}"
end
end
end