diff options
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/parser/functions/has_key.rb | 27 | ||||
-rw-r--r-- | lib/puppet/parser/functions/merge.rb | 30 | ||||
-rw-r--r-- | lib/puppet/parser/functions/validate_array.rb | 35 | ||||
-rw-r--r-- | lib/puppet/parser/functions/validate_string.rb | 35 |
4 files changed, 127 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/has_key.rb b/lib/puppet/parser/functions/has_key.rb new file mode 100644 index 0000000..9c1c4c3 --- /dev/null +++ b/lib/puppet/parser/functions/has_key.rb @@ -0,0 +1,27 @@ +module Puppet::Parser::Functions + + newfunction(:has_key, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| + determine if a hash has a certain key value. + + Example: + $my_hash = {'key_one' => 'value_one'} + if has_key($my_hash, 'key_two') { + notice('we will not reach here') + } + if has_key($my_hash, 'key_one') { + notice('this will be printed') + } + + ENDHEREDOC + + unless args.length == 2 + raise Puppet::ParseError, ("has_key(): wrong number of arguments (#{args.length}; must be 2)") + end + unless args[0].is_a?(Hash) + raise Puppet::ParseError, "has_key(): expects the first argument to be a hash, got #{args[0].inspect} which is of type #{args[0].class}" + end + args[0].has_key?(args[1]) + + end + +end diff --git a/lib/puppet/parser/functions/merge.rb b/lib/puppet/parser/functions/merge.rb new file mode 100644 index 0000000..d2dc0f9 --- /dev/null +++ b/lib/puppet/parser/functions/merge.rb @@ -0,0 +1,30 @@ +module Puppet::Parser::Functions + newfunction(:merge, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| + Merges two or more hashes together and returns the resulting hash. + + For example: + + $hash1 = {'one' => 1, 'two', => 2} + $hash1 = {'two' => 2, 'three', => 2} + $merged_hash = merge($hash1, $hash2) + # merged_hash = {'one' => 1, 'two' => 2, 'three' => 2} + + ENDHEREDOC + + if args.length < 2 + raise Puppet::ParseError, ("merge(): wrong number of arguments (#{args.length}; must be at least 2)") + end + + # The hash we accumulate into + accumulator = Hash.new + # Merge into the accumulator hash + args.each do |arg| + unless arg.is_a?(Hash) + raise Puppet::ParseError, "merge: unexpected argument type #{arg.class}, only expects hash arguments" + end + accumulator.merge!(arg) + end + # Return the fully merged hash + accumulator + end +end diff --git a/lib/puppet/parser/functions/validate_array.rb b/lib/puppet/parser/functions/validate_array.rb new file mode 100644 index 0000000..a7a7165 --- /dev/null +++ b/lib/puppet/parser/functions/validate_array.rb @@ -0,0 +1,35 @@ +module Puppet::Parser::Functions + + newfunction(:validate_array, :doc => <<-'ENDHEREDOC') do |args| + Validate all passed values are a Array data structure + value does not pass the check. + + Example: + + These values validate + + $my_array = [ 'one', 'two' ] + validate_array($my_array) + + These values do NOT validate + + validate_array(true) + validate_array('some_string') + $undefined = undef + validate_array($undefined) + + ENDHEREDOC + + unless args.length > 0 then + raise Puppet::ParseError, ("validate_array(): wrong number of arguments (#{args.length}; must be > 0)") + end + + args.each do |arg| + unless arg.is_a?(Array) + raise Puppet::ParseError, ("#{arg.inspect} is not an Array. It looks to be a #{arg.class}") + end + end + + end + +end diff --git a/lib/puppet/parser/functions/validate_string.rb b/lib/puppet/parser/functions/validate_string.rb new file mode 100644 index 0000000..d0e1376 --- /dev/null +++ b/lib/puppet/parser/functions/validate_string.rb @@ -0,0 +1,35 @@ +module Puppet::Parser::Functions + + newfunction(:validate_string, :doc => <<-'ENDHEREDOC') do |args| + Validate all passed values are a string data structure + value does not pass the check. + + Example: + + These values validate + + $my_string = "one two" + validate_string($my_string) + + These values do NOT validate + + validate_string(true) + validate_string([ 'some', 'array' ]) + $undefined = undef + validate_string($undefined) + + ENDHEREDOC + + unless args.length > 0 then + raise Puppet::ParseError, ("validate_string(): wrong number of arguments (#{args.length}; must be > 0)") + end + + args.each do |arg| + unless arg.is_a?(String) + raise Puppet::ParseError, ("#{arg.inspect} is not a string. It looks to be a #{arg.class}") + end + end + + end + +end |