diff options
Diffstat (limited to 'lib/puppet/parser')
-rw-r--r-- | lib/puppet/parser/functions/getvar.rb | 23 | ||||
-rw-r--r-- | lib/puppet/parser/functions/has_key.rb | 27 | ||||
-rw-r--r-- | lib/puppet/parser/functions/loadyaml.rb | 20 | ||||
-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_bool.rb | 39 | ||||
-rw-r--r-- | lib/puppet/parser/functions/validate_hash.rb | 37 | ||||
-rw-r--r-- | lib/puppet/parser/functions/validate_re.rb | 35 | ||||
-rw-r--r-- | lib/puppet/parser/functions/validate_string.rb | 35 |
9 files changed, 281 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/getvar.rb b/lib/puppet/parser/functions/getvar.rb new file mode 100644 index 0000000..ffd774d --- /dev/null +++ b/lib/puppet/parser/functions/getvar.rb @@ -0,0 +1,23 @@ +module Puppet::Parser::Functions + + newfunction(:getvar, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| + Lookup a variable in a remote namespace. + + For example: + + $foo = getvar('site::data::foo') + + This is useful if the namespace itself is stored in a string: + + $bar = getvar("${datalocation}::bar") + ENDHEREDOC + + unless args.length == 1 + raise Puppet::ParseError, ("getvar(): wrong number of arguments (#{args.length}; must be 1)") + end + + self.lookupvar("#{args[0]}") + + end + +end 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/loadyaml.rb b/lib/puppet/parser/functions/loadyaml.rb new file mode 100644 index 0000000..0f16f69 --- /dev/null +++ b/lib/puppet/parser/functions/loadyaml.rb @@ -0,0 +1,20 @@ +module Puppet::Parser::Functions + + newfunction(:loadyaml, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| + Load a YAML file and return the data if it contains an Array, String, or Hash + as a Puppet variable. + + For example: + + $myhash = loadyaml('/etc/puppet/data/myhash.yaml') + ENDHEREDOC + + unless args.length == 1 + raise Puppet::ParseError, ("loadyaml(): wrong number of arguments (#{args.length}; must be 1)") + end + + YAML.load_file(args[0]) + + 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_bool.rb b/lib/puppet/parser/functions/validate_bool.rb new file mode 100644 index 0000000..49e6378 --- /dev/null +++ b/lib/puppet/parser/functions/validate_bool.rb @@ -0,0 +1,39 @@ +module Puppet::Parser::Functions + + newfunction(:validate_bool, :doc => <<-'ENDHEREDOC') do |args| + Validate all passed values are true or false. Abort catalog compilation if the + value does not pass the check. + + Example: + + These booleans validate + + $iamtrue = true + validate_bool(true) + validate_bool(true, true, false, $iamtrue) + + These strings do NOT validate and will abort catalog compilation + + $some_array = [ true ] + validate_bool("false") + validate_bool("true") + validate_bool($some_array) + + * Jeff McCune <jeff@puppetlabs.com> + * Dan Bode <dan@puppetlabs.com> + + ENDHEREDOC + + unless args.length > 0 then + raise Puppet::ParseError, ("validate_bool(): wrong number of arguments (#{args.length}; must be > 0)") + end + + args.each do |arg| + unless (arg.is_a?(TrueClass) || arg.is_a?(FalseClass)) + raise Puppet::ParseError, ("#{arg.inspect} is not a boolean. It looks to be a #{arg.class}") + end + end + + end + +end diff --git a/lib/puppet/parser/functions/validate_hash.rb b/lib/puppet/parser/functions/validate_hash.rb new file mode 100644 index 0000000..1443318 --- /dev/null +++ b/lib/puppet/parser/functions/validate_hash.rb @@ -0,0 +1,37 @@ +module Puppet::Parser::Functions + + newfunction(:validate_hash, :doc => <<-'ENDHEREDOC') do |args| + Validate all passed values are a Hash data structure + value does not pass the check. + + Example: + + These values validate + + $my_hash = { 'one' => 'two' } + validate_hash($my_hash) + + These values do NOT validate + + validate_hash(true) + validate_hash('some_string') + $undefined = undef + validate_hash($undefined) + + * Jeff McCune <jeff@puppetlabs.com> + + ENDHEREDOC + + unless args.length > 0 then + raise Puppet::ParseError, ("validate_hash(): wrong number of arguments (#{args.length}; must be > 0)") + end + + args.each do |arg| + unless arg.is_a?(Hash) + raise Puppet::ParseError, ("#{arg.inspect} is not a Hash. It looks to be a #{arg.class}") + end + end + + end + +end diff --git a/lib/puppet/parser/functions/validate_re.rb b/lib/puppet/parser/functions/validate_re.rb new file mode 100644 index 0000000..583f26a --- /dev/null +++ b/lib/puppet/parser/functions/validate_re.rb @@ -0,0 +1,35 @@ +module Puppet::Parser::Functions + + newfunction(:validate_re, :doc => <<-'ENDHEREDOC') do |args| + Perform simple validation of a string against a regular expression. The second + argument of the function should be a string regular expression (without the //'s) + or an array of regular expressions. If none of the regular expressions in the array + match the string passed in, then an exception will be raised. + + Example: + + These strings validate against the regular expressions + + validate_re('one', '^one$') + validate_re('one', [ '^one', '^two' ]) + + These strings do NOT validate + + validate_re('one', [ '^two', '^three' ]) + + Jeff McCune <jeff@puppetlabs.com> + + ENDHEREDOC + if args.length != 2 then + raise Puppet::ParseError, ("validate_re(): wrong number of arguments (#{args.length}; must be 2)") + end + + msg = "validate_re(): #{args[0].inspect} does not match #{args[1].inspect}" + + raise Puppet::ParseError, (msg) unless args[1].any? do |re_str| + args[0] =~ Regexp.compile(re_str) + 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 |