From 99a93d366f2e1efb977fcc8fe300d3d8357c8214 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Apr 2011 09:27:10 +0200 Subject: Convert to module format. --- lib/puppet/parser/functions/abs.rb | 34 ++++++++++ lib/puppet/parser/functions/bool2num.rb | 45 ++++++++++++ lib/puppet/parser/functions/count.rb | 36 ++++++++++ lib/puppet/parser/functions/delete_at.rb | 42 ++++++++++++ lib/puppet/parser/functions/empty.rb | 27 ++++++++ lib/puppet/parser/functions/fact.rb | 36 ++++++++++ lib/puppet/parser/functions/is_array.rb | 21 ++++++ lib/puppet/parser/functions/is_hash.rb | 21 ++++++ lib/puppet/parser/functions/is_string.rb | 21 ++++++ lib/puppet/parser/functions/join.rb | 34 ++++++++++ lib/puppet/parser/functions/join_with_prefix.rb | 38 +++++++++++ lib/puppet/parser/functions/keys.rb | 25 +++++++ lib/puppet/parser/functions/load_variables.rb | 77 +++++++++++++++++++++ lib/puppet/parser/functions/member.rb | 33 +++++++++ lib/puppet/parser/functions/num2bool.rb | 38 +++++++++++ .../parser/functions/persistent_crontab_minutes.rb | 63 +++++++++++++++++ lib/puppet/parser/functions/prefix.rb | 38 +++++++++++ .../parser/functions/random_crontab_minutes.rb | 31 +++++++++ lib/puppet/parser/functions/range.rb | 59 ++++++++++++++++ lib/puppet/parser/functions/reverse.rb | 27 ++++++++ lib/puppet/parser/functions/shuffle.rb | 45 ++++++++++++ lib/puppet/parser/functions/size.rb | 47 +++++++++++++ lib/puppet/parser/functions/strftime.rb | 44 ++++++++++++ lib/puppet/parser/functions/time.rb | 37 ++++++++++ lib/puppet/parser/functions/unique.rb | 34 ++++++++++ lib/puppet/parser/functions/values.rb | 25 +++++++ lib/puppet/parser/functions/values_at.rb | 79 ++++++++++++++++++++++ 27 files changed, 1057 insertions(+) create mode 100644 lib/puppet/parser/functions/abs.rb create mode 100644 lib/puppet/parser/functions/bool2num.rb create mode 100644 lib/puppet/parser/functions/count.rb create mode 100644 lib/puppet/parser/functions/delete_at.rb create mode 100644 lib/puppet/parser/functions/empty.rb create mode 100644 lib/puppet/parser/functions/fact.rb create mode 100644 lib/puppet/parser/functions/is_array.rb create mode 100644 lib/puppet/parser/functions/is_hash.rb create mode 100644 lib/puppet/parser/functions/is_string.rb create mode 100644 lib/puppet/parser/functions/join.rb create mode 100644 lib/puppet/parser/functions/join_with_prefix.rb create mode 100644 lib/puppet/parser/functions/keys.rb create mode 100644 lib/puppet/parser/functions/load_variables.rb create mode 100644 lib/puppet/parser/functions/member.rb create mode 100644 lib/puppet/parser/functions/num2bool.rb create mode 100644 lib/puppet/parser/functions/persistent_crontab_minutes.rb create mode 100644 lib/puppet/parser/functions/prefix.rb create mode 100644 lib/puppet/parser/functions/random_crontab_minutes.rb create mode 100644 lib/puppet/parser/functions/range.rb create mode 100644 lib/puppet/parser/functions/reverse.rb create mode 100644 lib/puppet/parser/functions/shuffle.rb create mode 100644 lib/puppet/parser/functions/size.rb create mode 100644 lib/puppet/parser/functions/strftime.rb create mode 100644 lib/puppet/parser/functions/time.rb create mode 100644 lib/puppet/parser/functions/unique.rb create mode 100644 lib/puppet/parser/functions/values.rb create mode 100644 lib/puppet/parser/functions/values_at.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/abs.rb b/lib/puppet/parser/functions/abs.rb new file mode 100644 index 0000000..0a554e4 --- /dev/null +++ b/lib/puppet/parser/functions/abs.rb @@ -0,0 +1,34 @@ +# +# abs.rb +# + +module Puppet::Parser::Functions + newfunction(:abs, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "abs(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + + # Numbers in Puppet are often string-encoded which is troublesome ... + if value.is_a?(String) + if value.match(/^-?(?:\d+)(?:\.\d+){1}$/) + value = value.to_f + elsif value.match(/^-?\d+$/) + value = value.to_i + else + raise(Puppet::ParseError, 'abs(): Requires float or ' + + 'integer to work with') + end + end + + # We have numeric value to handle ... + result = value.abs + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/bool2num.rb b/lib/puppet/parser/functions/bool2num.rb new file mode 100644 index 0000000..b2989d0 --- /dev/null +++ b/lib/puppet/parser/functions/bool2num.rb @@ -0,0 +1,45 @@ +# +# bool2num.rb +# + +module Puppet::Parser::Functions + newfunction(:bool2num, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "bool2num(): 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, '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 + + # We have real boolean values as well ... + result = value ? 1 : 0 + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/count.rb b/lib/puppet/parser/functions/count.rb new file mode 100644 index 0000000..c4e2283 --- /dev/null +++ b/lib/puppet/parser/functions/count.rb @@ -0,0 +1,36 @@ +# +# count.rb +# + +# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... +# TODO(Krzysztof Wilczynski): Support for hash values would be nice too ... + +module Puppet::Parser::Functions + newfunction(:count, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support two arguments but only first is mandatory ... + raise(Puppet::ParseError, "count(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, Hash, String].include?(klass) + raise(Puppet::ParseError, 'count(): Requires either ' + + 'array, hash or string to work with') + end + + item = arguments[1] if arguments[1] + + value = value.is_a?(Hash) ? value.keys : value + + # No item to look for and count? Then just return current size ... + result = item ? value.count(item) : value.size + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/delete_at.rb b/lib/puppet/parser/functions/delete_at.rb new file mode 100644 index 0000000..10190ba --- /dev/null +++ b/lib/puppet/parser/functions/delete_at.rb @@ -0,0 +1,42 @@ +# +# delete_at.rb +# + +module Puppet::Parser::Functions + newfunction(:delete_at, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "delete_at(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'delete_at(): Requires array to work with') + end + + index = arguments[1] + + if index.is_a?(String) and not index.match(/^\d+$/) + raise(Puppet::ParseError, 'delete_at(): You must provide ' + + 'non-negative numeric index') + end + + result = array.clone + + # Numbers in Puppet are often string-encoded which is troublesome ... + index = index.to_i + + if index > result.size - 1 # First element is at index 0 is it not? + raise(Puppet::ParseError, 'delete_at(): Given index ' + + 'exceeds size of array given') + end + + result.delete_at(index) # We ignore the element that got deleted ... + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/empty.rb b/lib/puppet/parser/functions/empty.rb new file mode 100644 index 0000000..e78ebf0 --- /dev/null +++ b/lib/puppet/parser/functions/empty.rb @@ -0,0 +1,27 @@ +# +# empty.rb +# + +module Puppet::Parser::Functions + newfunction(:empty, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "empty(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, Hash, String].include?(klass) + raise(Puppet::ParseError, 'empty(): Requires either ' + + 'array, hash or string to work with') + end + + result = value.empty? + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/fact.rb b/lib/puppet/parser/functions/fact.rb new file mode 100644 index 0000000..27b7bb2 --- /dev/null +++ b/lib/puppet/parser/functions/fact.rb @@ -0,0 +1,36 @@ +# +# fact.rb +# + +module Puppet::Parser::Functions + newfunction(:fact, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "fact(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + fact = arguments[0] + + unless fact.is_a?(String) + raise(Puppet::ParseError, 'fact(): Requires fact name to be a string') + end + + raise(Puppet::ParseError, 'fact(): You must provide ' + + 'fact name') if fact.empty? + + result = lookupvar(fact) # Get the value of interest from Facter ... + + # + # Now this is a funny one ... Puppet does not have a concept of + # returning neither undef nor nil back for use within the Puppet DSL + # and empty string is as closest to actual undef as you we can get + # at this point in time ... + # + result = result.empty? ? '' : result + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_array.rb b/lib/puppet/parser/functions/is_array.rb new file mode 100644 index 0000000..0b508fd --- /dev/null +++ b/lib/puppet/parser/functions/is_array.rb @@ -0,0 +1,21 @@ +# +# is_array.rb +# + +module Puppet::Parser::Functions + newfunction(:is_array, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "is_array(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + type = arguments[0] + + result = type.is_a?(Array) + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_hash.rb b/lib/puppet/parser/functions/is_hash.rb new file mode 100644 index 0000000..259809c --- /dev/null +++ b/lib/puppet/parser/functions/is_hash.rb @@ -0,0 +1,21 @@ +# +# is_hash.rb +# + +module Puppet::Parser::Functions + newfunction(:is_array, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "is_hash(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + type = arguments[0] + + result = type.is_a?(Hash) + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_string.rb b/lib/puppet/parser/functions/is_string.rb new file mode 100644 index 0000000..61037e3 --- /dev/null +++ b/lib/puppet/parser/functions/is_string.rb @@ -0,0 +1,21 @@ +# +# is_string.rb +# + +module Puppet::Parser::Functions + newfunction(:is_string, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "is_string(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + type = arguments[0] + + result = type.is_a?(String) + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/join.rb b/lib/puppet/parser/functions/join.rb new file mode 100644 index 0000000..945556a --- /dev/null +++ b/lib/puppet/parser/functions/join.rb @@ -0,0 +1,34 @@ +# +# join.rb +# + +module Puppet::Parser::Functions + newfunction(:join, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support two arguments but only first is mandatory ... + raise(Puppet::ParseError, "join(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'join(): Requires array to work with') + end + + suffix = arguments[1] if arguments[1] + + if suffix + unless suffix.is_a?(String) + raise(Puppet::ParseError, 'join(): Requires string to work with') + end + end + + result = suffix ? array.join(suffix) : array.join + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/join_with_prefix.rb b/lib/puppet/parser/functions/join_with_prefix.rb new file mode 100644 index 0000000..8bf96d9 --- /dev/null +++ b/lib/puppet/parser/functions/join_with_prefix.rb @@ -0,0 +1,38 @@ +# +# join_with_prefix.rb +# + +module Puppet::Parser::Functions + newfunction(:join_with_prefix, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support three arguments but only first is mandatory ... + raise(Puppet::ParseError, "join(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'join_with_prefix(): Requires ' + + 'array to work with') + end + + prefix = arguments[1] if arguments[1] + suffix = arguments[2] if arguments[2] + + if prefix and suffix + result = prefix + array.join(suffix + prefix) + elsif prefix and not suffix + result = array.collect { |i| prefix ? prefix + i : i } + elsif suffix and not prefix + result = array.join(suffix) + else + result = array.join + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/keys.rb b/lib/puppet/parser/functions/keys.rb new file mode 100644 index 0000000..3a92a47 --- /dev/null +++ b/lib/puppet/parser/functions/keys.rb @@ -0,0 +1,25 @@ +# +# keys.rb +# + +module Puppet::Parser::Functions + newfunction(:keys, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "keys(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + hash = arguments[0] + + unless hash.is_a?(Hash) + raise(Puppet::ParseError, 'keys(): Requires hash to work with') + end + + result = hash.keys + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/load_variables.rb b/lib/puppet/parser/functions/load_variables.rb new file mode 100644 index 0000000..a28c64b --- /dev/null +++ b/lib/puppet/parser/functions/load_variables.rb @@ -0,0 +1,77 @@ +# +# load_variables.rb +# + +module Puppet::Parser::Functions + newfunction(:load_variables, :type => :statement, :doc => <<-EOS +This function will allow for loading variables from an external YAML +file and expose them for further use inside the Puppet manifest file ... + +For example: + +Given following content of the data.yaml file: + + --- + host1.example.com: + foo: bar + baz: quux + question: 42 + host2.example.com: + abc: def + this: that + darth: vader + +Then calling load_variables in Puppet manifest file as follows: + + load_variables("/etc/puppet/data.yaml", $fqdn) + +Will result in addition of variables $foo, $baz and $question +for matching host name as per the variable $fqdn ... + +Another example which uses per-host file: + +Given following content of the file data-host1.example.com.yaml: + + --- + foo: bar + +Then when we call load_variables like this: + + load_variables("/etc/puppet/data-${fqdn}.yaml") + +This will result in a variable $foo being added and ready for use. + EOS + ) do |arguments| + + raise(Puppet::ParseError, "load_variables(): Wrong number of " + + "arguments given (#{arguments.size} for 2)") if arguments.size < 2 + + data = {} + + file = arguments[0] + key = arguments[1] if arguments[1] + + if File.exists?(file) + + begin + data = YAML.load_file(file) + rescue => error + raise(Puppet::ParseError, "load_variables(): Unable to load data " + + "from the file `%s': %s" % file, error.to_s) + end + + raise(Puppet::ParseError, "load_variables(): Data in the file `%s' " + + "is not a hash" % file) unless data.is_a?(Hash) + + data = ((data[key] and data[key].is_a?(Hash)) ? data[key] : {}) if key + end + + data.each do |param, value| + value = strinterp(value) # Evaluate any interpolated variable names ... + + setvar(param, value) + end + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/member.rb b/lib/puppet/parser/functions/member.rb new file mode 100644 index 0000000..a491a76 --- /dev/null +++ b/lib/puppet/parser/functions/member.rb @@ -0,0 +1,33 @@ +# +# include.rb +# + +# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... +# TODO(Krzysztof Wilczynski): Support for strings and hashes too ... + +module Puppet::Parser::Functions + newfunction(:includes, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "includes(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + array = arguments[0] + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'includes(): Requires an array to work with') + end + + item = arguments[1] + + raise(Puppet::ParseError, 'includes(): You must provide item ' + + 'to search for within given array') if item.empty? + + result = array.include?(item) + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb new file mode 100644 index 0000000..2baef62 --- /dev/null +++ b/lib/puppet/parser/functions/num2bool.rb @@ -0,0 +1,38 @@ +# +# num2bool.rb +# + +# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... + +module Puppet::Parser::Functions + newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + number = arguments[0] + + # Only numbers allowed ... + unless number.match(/^\-?\d+$/) + raise(Puppet::ParseError, 'num2bool(): Requires integer to work with') + end + + result = case number + when /^0$/ + false + when /^\-?\d+$/ + # Numbers in Puppet are often string-encoded which is troublesome ... + number = number.to_i + # We yield true for any positive number and false otherwise ... + number > 0 ? true : false + else + raise(Puppet::ParseError, 'num2bool(): Unknown numeric format given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/persistent_crontab_minutes.rb b/lib/puppet/parser/functions/persistent_crontab_minutes.rb new file mode 100644 index 0000000..cd80094 --- /dev/null +++ b/lib/puppet/parser/functions/persistent_crontab_minutes.rb @@ -0,0 +1,63 @@ +# +# persistent_crontab_minutes.rb +# + +module Puppet::Parser::Functions + newfunction(:persistent_crontab_minutes, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + require 'md5' + + value = 0 + + job = arguments[0] + host = arguments[1] + + environment = Puppet[:environment] + + # We select first directory that exists. This might not be the best idea ... + modules = Puppet[:modulepath].split(':').select { |i| File.exists?(i) }.first + + raise(Puppet::ParseError, "Unable to determine the storage " + + "directory for Puppet modules") unless modules + + # Prepare the file where we store current value ... + file = "/puppet/state/crontab/#{host}-#{job}.minutes" + file = File.join(modules, file) + + # Get the directory portion from the file name ... + directory = File.dirname(file) + + FileUtils.mkdir_p(directory) unless File.directory?(directory) + + if FileTest.exists?(file) + File.open(file, 'r') { |f| value = f.read.to_i } + + raise(Puppet::ParseError, "The value for minutes in the file `%s' " + + "is out of the range from 0 to 59 inclusive") unless value < 60 + else + # + # Pick a random number based on the job and host name. This will yield + # the same value for exactly the same combination of the job and host name. + # + value = MD5.new(job_name + host).to_s.hex % 60 + + # Minutes are from 0 to 59 inclusive ... + value = value < 60 ? value : 59 + + File.open(file, 'w') { |f| f.write(value) } + end + + # Tell Puppet to keep an eye on this file ... + parser = Puppet::Parser::Parser.new(environment) + parser.watch_file(file) if File.exists?(file) + + return value + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb new file mode 100644 index 0000000..0e0cee2 --- /dev/null +++ b/lib/puppet/parser/functions/prefix.rb @@ -0,0 +1,38 @@ +# +# prefix.rb +# + +module Puppet::Parser::Functions + newfunction(:prefix, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support two arguments but only first is mandatory ... + raise(Puppet::ParseError, "prefix(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'prefix(): Requires array to work with') + end + + prefix = arguments[1] if arguments[1] + + if prefix + unless prefix.is_a?(String) + raise(Puppet::ParseError, 'prefix(): Requires string to work with') + end + end + + # Turn everything into string same as join would do ... + result = array.collect do |i| + i = i.to_s + prefix ? prefix + i : i + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/random_crontab_minutes.rb b/lib/puppet/parser/functions/random_crontab_minutes.rb new file mode 100644 index 0000000..8ab29e1 --- /dev/null +++ b/lib/puppet/parser/functions/random_crontab_minutes.rb @@ -0,0 +1,31 @@ +# +# random_crontab_minutes.rb +# + +module Puppet::Parser::Functions + newfunction(:random_crontab_minutes, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + require 'md5' + + job_name = arguments[0] + host = arguments[1] + + # + # Pick a random number based on the job and host name. This will yield + # the same value for exactly the same combination of the job and host name. + # + value = MD5.new(job_name + host).to_s.hex % 60 + + # Minutes are from 0 to 59 inclusive ... + value = value < 60 ? value : 59 + + return value + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb new file mode 100644 index 0000000..6afb50c --- /dev/null +++ b/lib/puppet/parser/functions/range.rb @@ -0,0 +1,59 @@ +# +# range.rb +# + +# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... + +module Puppet::Parser::Functions + newfunction(:range, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # We support more than one argument but at least one is mandatory ... + raise(Puppet::ParseError, "range(): Wrong number of " + + "arguments given (#{arguments.size} for 1)") if arguments.size < 1 + + if arguments.size > 1 + start = arguments[0] + stop = arguments[1] + + type = '..' # We select simplest type for Range available in Ruby ... + + elsif arguments.size > 0 + value = arguments[0] + + if m = value.match(/^(\w+)(\.\.\.?|\-)(\w+)$/) + start = m[1] + stop = m[3] + + type = m[2] + + elsif value.match(/^.+$/) + raise(Puppet::ParseError, 'range(): Unable to compute range ' + + 'from the value given') + else + raise(Puppet::ParseError, 'range(): Unknown format of range given') + 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 + + range = case type + when /^(\.\.|\-)$/ then (start .. stop) + when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... + end + + result = range.collect { |i| i } # Get them all ... Pokemon ... + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/reverse.rb b/lib/puppet/parser/functions/reverse.rb new file mode 100644 index 0000000..79e9b93 --- /dev/null +++ b/lib/puppet/parser/functions/reverse.rb @@ -0,0 +1,27 @@ +# +# reverse.rb +# + +module Puppet::Parser::Functions + newfunction(:reverse, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "reverse(): 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, 'reverse(): Requires either ' + + 'array or string to work with') + end + + result = value.reverse + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/shuffle.rb b/lib/puppet/parser/functions/shuffle.rb new file mode 100644 index 0000000..73e798c --- /dev/null +++ b/lib/puppet/parser/functions/shuffle.rb @@ -0,0 +1,45 @@ +# +# shuffle.rb +# + +module Puppet::Parser::Functions + newfunction(:shuffle, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "shuffle(): 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, 'shuffle(): Requires either ' + + 'array or string to work with') + end + + result = value.clone + + string = value.is_a?(String) ? true : false + + # Check whether it makes sense to shuffle ... + return result if result.size <= 1 + + # We turn any string value into an array to be able to shuffle ... + result = string ? result.split('') : result + + elements = result.size + + # Simple implementation of Fisher–Yates in-place shuffle ... + elements.times do |i| + j = rand(elements - i) + i + result[j], result[i] = result[i], result[j] + end + + result = string ? result.join : result + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/size.rb b/lib/puppet/parser/functions/size.rb new file mode 100644 index 0000000..aa4f4ad --- /dev/null +++ b/lib/puppet/parser/functions/size.rb @@ -0,0 +1,47 @@ +# +# size.rb +# + +# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ... + +module Puppet::Parser::Functions + newfunction(:size, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "size(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + item = arguments[0] + + if item.is_a?(String) + + begin + # + # Check whether your item is a numeric value or not ... + # This will take care about positive and/or negative numbers + # for both integer and floating-point values ... + # + # Please note that Puppet has no notion of hexadecimal + # nor octal numbers for its DSL at this point in time ... + # + Float(item) + + raise(Puppet::ParseError, 'size(): Requires either ' + + 'string or array to work with') + + rescue ArgumentError + result = item.size + end + + elsif item.is_a?(Array) + result = item.size + else + raise(Puppet::ParseError, 'size(): Unknown type given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/strftime.rb b/lib/puppet/parser/functions/strftime.rb new file mode 100644 index 0000000..c919320 --- /dev/null +++ b/lib/puppet/parser/functions/strftime.rb @@ -0,0 +1,44 @@ +# +# strftime.rb +# + +module Puppet::Parser::Functions + newfunction(:strftime, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support two arguments but only first is mandatory ... + raise(Puppet::ParseError, "strftime(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + format = arguments[0] + + raise(Puppet::ParseError, 'strftime(): You must provide ' + + 'format for evaluation') if format.empty? + + # The Time Zone argument is optional ... + time_zone = arguments[1] if arguments[1] + + time = Time.new + + # There is probably a better way to handle Time Zone ... + if time_zone and not time_zone.empty? + original_zone = ENV['TZ'] + + local_time = time.clone + local_time = local_time.utc + + ENV['TZ'] = time_zone + + time = local_time.localtime + + ENV['TZ'] = original_zone + end + + result = time.strftime(format) + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/time.rb b/lib/puppet/parser/functions/time.rb new file mode 100644 index 0000000..f7c1041 --- /dev/null +++ b/lib/puppet/parser/functions/time.rb @@ -0,0 +1,37 @@ +# +# time.rb +# + +module Puppet::Parser::Functions + newfunction(:time, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # The Time Zone argument is optional ... + time_zone = arguments[0] if arguments[0] + + time = Time.new + + # There is probably a better way to handle Time Zone ... + if time_zone and not time_zone.empty? + original_zone = ENV['TZ'] + + local_time = time.clone + local_time = local_time.utc + + ENV['TZ'] = time_zone + + time = local_time.localtime + + ENV['TZ'] = original_zone + end + + # Calling Time#to_i on a receiver changes it. Trust me I am the Doctor. + result = time.strftime('%s') + result = result.to_i + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/unique.rb b/lib/puppet/parser/functions/unique.rb new file mode 100644 index 0000000..a922c94 --- /dev/null +++ b/lib/puppet/parser/functions/unique.rb @@ -0,0 +1,34 @@ +# +# unique.rb +# + +module Puppet::Parser::Functions + newfunction(:unique, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "unique(): 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, 'unique(): Requires either ' + + 'array or string to work with') + end + + result = value.clone + + string = value.is_a?(String) ? true : false + + # We turn any string value into an array to be able to shuffle ... + result = string ? result.split('') : result + result = result.uniq # Remove duplicates ... + result = string ? result.join : result + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/values.rb b/lib/puppet/parser/functions/values.rb new file mode 100644 index 0000000..c1c4a77 --- /dev/null +++ b/lib/puppet/parser/functions/values.rb @@ -0,0 +1,25 @@ +# +# values.rb +# + +module Puppet::Parser::Functions + newfunction(:values, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "values(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + hash = arguments[0] + + unless hash.is_a?(Hash) + raise(Puppet::ParseError, 'values(): Requires hash to work with') + end + + result = hash.values + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/values_at.rb b/lib/puppet/parser/functions/values_at.rb new file mode 100644 index 0000000..331af6a --- /dev/null +++ b/lib/puppet/parser/functions/values_at.rb @@ -0,0 +1,79 @@ +# +# values_at.rb +# + +# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ... +# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... + +module Puppet::Parser::Functions + newfunction(:values_at, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "values_at(): Wrong number of " + + "arguments given (#{arguments.size} for 2)") if arguments.size < 2 + + array = arguments.shift + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'values_at(): Requires array to work with') + end + + indices = *arguments # Get them all ... Pokemon ... + + if not indices or indices.empty? + raise(Puppet::ParseError, 'values_at(): You must provide ' + + 'at least one positive index to collect') + end + + result = [] + indices_list = [] + + indices.each do |i| + if m = i.match(/^(\d+)(\.\.\.?|\-)(\d+)$/) + start = m[1].to_i + stop = m[3].to_i + + type = m[2] + + if start > stop + raise(Puppet::ParseError, 'values_at(): Stop index in ' + + 'given indices range is smaller than the start index') + elsif stop > array.size - 1 # First element is at index 0 is it not? + raise(Puppet::ParseError, 'values_at(): Stop index in ' + + 'given indices range exceeds array size') + end + + range = case type + when /^(\.\.|\-)$/ then (start .. stop) + when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... + end + + range.each { |i| indices_list << i.to_i } + else + # Only positive numbers allowed in this case ... + if not i.match(/^\d+$/) + raise(Puppet::ParseError, 'values_at(): Unknown format ' + + 'of given index') + end + + # In Puppet numbers are often string-encoded ... + i = i.to_i + + if i > array.size - 1 # Same story. First element is at index 0 ... + raise(Puppet::ParseError, 'values_at(): Given index ' + + 'exceeds array size') + end + + indices_list << i + end + end + + # We remove nil values as they make no sense in Puppet DSL ... + result = indices_list.collect { |i| array[i] }.compact + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 09abea2d47b09b9e8933a1c27c7a454c77760e83 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Apr 2011 16:52:14 +0200 Subject: Moved type.rb --- lib/puppet/parser/functions/type.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lib/puppet/parser/functions/type.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/type.rb b/lib/puppet/parser/functions/type.rb new file mode 100644 index 0000000..389a056 --- /dev/null +++ b/lib/puppet/parser/functions/type.rb @@ -0,0 +1,33 @@ +# +# type.rb +# + +module Puppet::Parser::Functions + newfunction(:type, :type => :rvalue, :doc => <<-EOS + 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 [Array, Bignum, Fixnum, Float, Hash, String].include?(klass) + raise(Puppet::ParseError, 'type(): 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' + else klass + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From aafce9c99b779855e1e10e5a337848fa9f676a01 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Sat, 30 Apr 2011 16:00:49 +0200 Subject: Moved more functions into lib/puppet/parser/functions/ --- lib/puppet/parser/functions/capitalize.rb | 32 +++++++++++++ lib/puppet/parser/functions/chomp.rb | 32 +++++++++++++ lib/puppet/parser/functions/chop.rb | 32 +++++++++++++ lib/puppet/parser/functions/date.rb | 12 +++++ lib/puppet/parser/functions/delete.rb | 15 ++++++ lib/puppet/parser/functions/downcase.rb | 32 +++++++++++++ lib/puppet/parser/functions/flatten.rb | 25 ++++++++++ lib/puppet/parser/functions/grep.rb | 12 +++++ lib/puppet/parser/functions/hash.rb | 34 +++++++++++++ lib/puppet/parser/functions/is_float.rb | 12 +++++ lib/puppet/parser/functions/is_integer.rb | 12 +++++ lib/puppet/parser/functions/is_numeric.rb | 12 +++++ .../parser/functions/is_valid_domain_name.rb | 12 +++++ lib/puppet/parser/functions/is_valid_ip_address.rb | 12 +++++ .../parser/functions/is_valid_mac_address.rb | 12 +++++ lib/puppet/parser/functions/is_valid_netmask.rb | 12 +++++ lib/puppet/parser/functions/load_json.rb | 12 +++++ lib/puppet/parser/functions/load_yaml.rb | 12 +++++ lib/puppet/parser/functions/lstrip.rb | 32 +++++++++++++ lib/puppet/parser/functions/member.rb | 14 +++--- lib/puppet/parser/functions/rand.rb | 12 +++++ lib/puppet/parser/functions/rstrip.rb | 31 ++++++++++++ lib/puppet/parser/functions/sort.rb | 12 +++++ lib/puppet/parser/functions/squeeze.rb | 12 +++++ lib/puppet/parser/functions/str2bool.rb | 38 +++++++++++++++ lib/puppet/parser/functions/strip.rb | 31 ++++++++++++ lib/puppet/parser/functions/swapcase.rb | 32 +++++++++++++ lib/puppet/parser/functions/upcase.rb | 32 +++++++++++++ lib/puppet/parser/functions/zip.rb | 56 ++++++++++++++++++++++ 29 files changed, 629 insertions(+), 7 deletions(-) create mode 100644 lib/puppet/parser/functions/capitalize.rb create mode 100644 lib/puppet/parser/functions/chomp.rb create mode 100644 lib/puppet/parser/functions/chop.rb create mode 100644 lib/puppet/parser/functions/date.rb create mode 100644 lib/puppet/parser/functions/delete.rb create mode 100644 lib/puppet/parser/functions/downcase.rb create mode 100644 lib/puppet/parser/functions/flatten.rb create mode 100644 lib/puppet/parser/functions/grep.rb create mode 100644 lib/puppet/parser/functions/hash.rb create mode 100644 lib/puppet/parser/functions/is_float.rb create mode 100644 lib/puppet/parser/functions/is_integer.rb create mode 100644 lib/puppet/parser/functions/is_numeric.rb create mode 100644 lib/puppet/parser/functions/is_valid_domain_name.rb create mode 100644 lib/puppet/parser/functions/is_valid_ip_address.rb create mode 100644 lib/puppet/parser/functions/is_valid_mac_address.rb create mode 100644 lib/puppet/parser/functions/is_valid_netmask.rb create mode 100644 lib/puppet/parser/functions/load_json.rb create mode 100644 lib/puppet/parser/functions/load_yaml.rb create mode 100644 lib/puppet/parser/functions/lstrip.rb create mode 100644 lib/puppet/parser/functions/rand.rb create mode 100644 lib/puppet/parser/functions/rstrip.rb create mode 100644 lib/puppet/parser/functions/sort.rb create mode 100644 lib/puppet/parser/functions/squeeze.rb create mode 100644 lib/puppet/parser/functions/str2bool.rb create mode 100644 lib/puppet/parser/functions/strip.rb create mode 100644 lib/puppet/parser/functions/swapcase.rb create mode 100644 lib/puppet/parser/functions/upcase.rb create mode 100644 lib/puppet/parser/functions/zip.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/capitalize.rb b/lib/puppet/parser/functions/capitalize.rb new file mode 100644 index 0000000..f902cb3 --- /dev/null +++ b/lib/puppet/parser/functions/capitalize.rb @@ -0,0 +1,32 @@ +# +# capitalize.rb +# + +module Puppet::Parser::Functions + newfunction(:capitalize, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "capitalize(): 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, 'capitalize(): 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.capitalize : i } + else + result = value.capitalize + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/chomp.rb b/lib/puppet/parser/functions/chomp.rb new file mode 100644 index 0000000..e1d788a --- /dev/null +++ b/lib/puppet/parser/functions/chomp.rb @@ -0,0 +1,32 @@ +# +# chomp.rb +# + +module Puppet::Parser::Functions + newfunction(:chomp, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "chomp(): 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, 'chomp(): 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.chomp : i } + else + result = value.chomp + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/chop.rb b/lib/puppet/parser/functions/chop.rb new file mode 100644 index 0000000..0f9af86 --- /dev/null +++ b/lib/puppet/parser/functions/chop.rb @@ -0,0 +1,32 @@ +# +# chop.rb +# + +module Puppet::Parser::Functions + newfunction(:chop, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "chop(): 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, 'chop(): Requires either an ' + + '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.chop : i } + else + result = value.chop + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/date.rb b/lib/puppet/parser/functions/date.rb new file mode 100644 index 0000000..ea11265 --- /dev/null +++ b/lib/puppet/parser/functions/date.rb @@ -0,0 +1,12 @@ +# +# date.rb +# + +module Puppet::Parser::Functions + newfunction(:date, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb new file mode 100644 index 0000000..b8376d1 --- /dev/null +++ b/lib/puppet/parser/functions/delete.rb @@ -0,0 +1,15 @@ +# +# delete.rb +# + +# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... +# TODO(Krzysztof Wilczynski): Support for strings and hashes too ... + +module Puppet::Parser::Functions + newfunction(:delete, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/downcase.rb b/lib/puppet/parser/functions/downcase.rb new file mode 100644 index 0000000..71f8480 --- /dev/null +++ b/lib/puppet/parser/functions/downcase.rb @@ -0,0 +1,32 @@ +# +# downcase.rb +# + +module Puppet::Parser::Functions + newfunction(:downcase, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "downcase(): 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, 'downcase(): 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.downcase : i } + else + result = value.downcase + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/flatten.rb b/lib/puppet/parser/functions/flatten.rb new file mode 100644 index 0000000..6036f72 --- /dev/null +++ b/lib/puppet/parser/functions/flatten.rb @@ -0,0 +1,25 @@ +# +# flatten.rb +# + +module Puppet::Parser::Functions + newfunction(:flatten, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "flatten(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'flatten(): Requires array to work with') + end + + result = array.flatten + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/grep.rb b/lib/puppet/parser/functions/grep.rb new file mode 100644 index 0000000..1663fe7 --- /dev/null +++ b/lib/puppet/parser/functions/grep.rb @@ -0,0 +1,12 @@ +# +# grep.rb +# + +module Puppet::Parser::Functions + newfunction(:grep, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/hash.rb b/lib/puppet/parser/functions/hash.rb new file mode 100644 index 0000000..f0c01d4 --- /dev/null +++ b/lib/puppet/parser/functions/hash.rb @@ -0,0 +1,34 @@ +# +# hash.rb +# + +module Puppet::Parser::Functions + newfunction(:hash, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "hash(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'hash(): Requires array to work with') + end + + result = {} + + begin + # This is to make it compatible with older version of Ruby ... + array = array.flatten + result = Hash[*array] + rescue Exception + raise(Puppet::ParseError, 'hash(): Unable to compute ' + + 'hash from array given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_float.rb b/lib/puppet/parser/functions/is_float.rb new file mode 100644 index 0000000..2a5a923 --- /dev/null +++ b/lib/puppet/parser/functions/is_float.rb @@ -0,0 +1,12 @@ +# +# is_float.rb +# + +module Puppet::Parser::Functions + newfunction(:is_float, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_integer.rb b/lib/puppet/parser/functions/is_integer.rb new file mode 100644 index 0000000..44337f0 --- /dev/null +++ b/lib/puppet/parser/functions/is_integer.rb @@ -0,0 +1,12 @@ +# +# is_integer.rb +# + +module Puppet::Parser::Functions + newfunction(:is_integer, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_numeric.rb b/lib/puppet/parser/functions/is_numeric.rb new file mode 100644 index 0000000..7a64091 --- /dev/null +++ b/lib/puppet/parser/functions/is_numeric.rb @@ -0,0 +1,12 @@ +# +# is_numeric.rb +# + +module Puppet::Parser::Functions + newfunction(:is_numeric, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_domain_name.rb b/lib/puppet/parser/functions/is_valid_domain_name.rb new file mode 100644 index 0000000..05d64be --- /dev/null +++ b/lib/puppet/parser/functions/is_valid_domain_name.rb @@ -0,0 +1,12 @@ +# +# is_valid_doman_name.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_doman_name, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_ip_address.rb b/lib/puppet/parser/functions/is_valid_ip_address.rb new file mode 100644 index 0000000..e6b68a8 --- /dev/null +++ b/lib/puppet/parser/functions/is_valid_ip_address.rb @@ -0,0 +1,12 @@ +# +# is_valid_ip_address.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_ip_address, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_mac_address.rb b/lib/puppet/parser/functions/is_valid_mac_address.rb new file mode 100644 index 0000000..5e354c9 --- /dev/null +++ b/lib/puppet/parser/functions/is_valid_mac_address.rb @@ -0,0 +1,12 @@ +# +# is_valid_mac_address.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_mac_address, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_netmask.rb b/lib/puppet/parser/functions/is_valid_netmask.rb new file mode 100644 index 0000000..2aeb374 --- /dev/null +++ b/lib/puppet/parser/functions/is_valid_netmask.rb @@ -0,0 +1,12 @@ +# +# is_valid_netmask.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_netmask, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/load_json.rb b/lib/puppet/parser/functions/load_json.rb new file mode 100644 index 0000000..9ec8c78 --- /dev/null +++ b/lib/puppet/parser/functions/load_json.rb @@ -0,0 +1,12 @@ +# +# load_json.rb +# + +module Puppet::Parser::Functions + newfunction(:load_json, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/load_yaml.rb b/lib/puppet/parser/functions/load_yaml.rb new file mode 100644 index 0000000..684a721 --- /dev/null +++ b/lib/puppet/parser/functions/load_yaml.rb @@ -0,0 +1,12 @@ +# +# load_yaml.rb +# + +module Puppet::Parser::Functions + newfunction(:load_yaml, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/lstrip.rb b/lib/puppet/parser/functions/lstrip.rb new file mode 100644 index 0000000..a7b2656 --- /dev/null +++ b/lib/puppet/parser/functions/lstrip.rb @@ -0,0 +1,32 @@ +# +# lstrip.rb +# + +module Puppet::Parser::Functions + newfunction(:lstrip, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "lstrip(): 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, 'lstrip(): 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.lstrip : i } + else + result = value.lstrip + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/member.rb b/lib/puppet/parser/functions/member.rb index a491a76..bb43a37 100644 --- a/lib/puppet/parser/functions/member.rb +++ b/lib/puppet/parser/functions/member.rb @@ -1,28 +1,28 @@ # -# include.rb +# member.rb # # TODO(Krzysztof Wilczynski): We need to add support for regular expression ... # TODO(Krzysztof Wilczynski): Support for strings and hashes too ... module Puppet::Parser::Functions - newfunction(:includes, :type => :rvalue, :doc => <<-EOS + newfunction(:member, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| - raise(Puppet::ParseError, "includes(): Wrong number of arguments " + + raise(Puppet::ParseError, "member(): Wrong number of arguments " + "given (#{arguments.size} for 2)") if arguments.size < 2 array = arguments[0] - if not array.is_a?(Array) - raise(Puppet::ParseError, 'includes(): Requires an array to work with') + unless array.is_a?(Array) + raise(Puppet::ParseError, 'member(): Requires array to work with') end item = arguments[1] - raise(Puppet::ParseError, 'includes(): You must provide item ' + - 'to search for within given array') if item.empty? + raise(Puppet::ParseError, 'member(): You must provide item ' + + 'to search for within array given') if item.empty? result = array.include?(item) diff --git a/lib/puppet/parser/functions/rand.rb b/lib/puppet/parser/functions/rand.rb new file mode 100644 index 0000000..2cb9acb --- /dev/null +++ b/lib/puppet/parser/functions/rand.rb @@ -0,0 +1,12 @@ +# +# rand.rb +# + +module Puppet::Parser::Functions + newfunction(:rand, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/rstrip.rb b/lib/puppet/parser/functions/rstrip.rb new file mode 100644 index 0000000..56849d3 --- /dev/null +++ b/lib/puppet/parser/functions/rstrip.rb @@ -0,0 +1,31 @@ +# +# rstrip.rb +# + +module Puppet::Parser::Functions + newfunction(:rstrip, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "rstrip(): 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, 'rstrip(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + result = value.collect { |i| i.is_a?(String) ? i.rstrip : i } + else + result = value.rstrip + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb new file mode 100644 index 0000000..85e5ba0 --- /dev/null +++ b/lib/puppet/parser/functions/sort.rb @@ -0,0 +1,12 @@ +# +# sort.rb +# + +module Puppet::Parser::Functions + newfunction(:sort, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/squeeze.rb b/lib/puppet/parser/functions/squeeze.rb new file mode 100644 index 0000000..a135bd3 --- /dev/null +++ b/lib/puppet/parser/functions/squeeze.rb @@ -0,0 +1,12 @@ +# +# squeeze.rb +# + +module Puppet::Parser::Functions + newfunction(:squeeze, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/str2bool.rb b/lib/puppet/parser/functions/str2bool.rb new file mode 100644 index 0000000..f3a6d6c --- /dev/null +++ b/lib/puppet/parser/functions/str2bool.rb @@ -0,0 +1,38 @@ +# +# str2bool.rb +# + +module Puppet::Parser::Functions + newfunction(:str2bool, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "str2bool(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + string = arguments[0] + + unless string.is_a?(String) + raise(Puppet::ParseError, 'str2bool(): Requires either ' + + 'string to work with') + end + + # We consider all the yes, no, y, n and so on too ... + result = case string + # + # 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, 'str2bool(): Unknown type of boolean given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/strip.rb b/lib/puppet/parser/functions/strip.rb new file mode 100644 index 0000000..ebab20e --- /dev/null +++ b/lib/puppet/parser/functions/strip.rb @@ -0,0 +1,31 @@ +# +# strip.rb +# + +module Puppet::Parser::Functions + newfunction(:strip, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "strip(): 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, 'strip(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + result = value.collect { |i| i.is_a?(String) ? i.strip : i } + else + result = value.strip + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/swapcase.rb b/lib/puppet/parser/functions/swapcase.rb new file mode 100644 index 0000000..a0002a9 --- /dev/null +++ b/lib/puppet/parser/functions/swapcase.rb @@ -0,0 +1,32 @@ +# +# swapcase.rb +# + +module Puppet::Parser::Functions + newfunction(:swapcase, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "swapcase(): 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, 'swapcase(): 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.swapcase : i } + else + result = value.swapcase + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/upcase.rb b/lib/puppet/parser/functions/upcase.rb new file mode 100644 index 0000000..8a9769d --- /dev/null +++ b/lib/puppet/parser/functions/upcase.rb @@ -0,0 +1,32 @@ +# +# upcase.rb +# + +module Puppet::Parser::Functions + newfunction(:upcase, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "upcase(): 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, 'upcase(): 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.upcase : i } + else + result = value.upcase + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/zip.rb b/lib/puppet/parser/functions/zip.rb new file mode 100644 index 0000000..024d64a --- /dev/null +++ b/lib/puppet/parser/functions/zip.rb @@ -0,0 +1,56 @@ +# +# zip.rb +# + +module Puppet::Parser::Functions + newfunction(:zip, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support three arguments but only first is mandatory ... + raise(Puppet::ParseError, "zip(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + a = arguments[0] + b = arguments[1] + + unless a.is_a?(Array) and b.is_a?(Array) + raise(Puppet::ParseError, 'zip(): Requires array to work with') + end + + 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) + 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 + + result = a.zip(b) + result = flatten ? result.flatten : result + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From e6b5a6dd0252f5491753abb3b71859644036f2fe Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Sun, 26 Jun 2011 14:33:53 +0200 Subject: Removed duplicate - is_hash is really now is_hash instead of is_array. --- lib/puppet/parser/functions/is_hash.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/is_hash.rb b/lib/puppet/parser/functions/is_hash.rb index 259809c..000306e 100644 --- a/lib/puppet/parser/functions/is_hash.rb +++ b/lib/puppet/parser/functions/is_hash.rb @@ -3,12 +3,12 @@ # module Puppet::Parser::Functions - newfunction(:is_array, :type => :rvalue, :doc => <<-EOS + newfunction(:is_hash, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| raise(Puppet::ParseError, "is_hash(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + "given (#{arguments.size} for 1)") if arguments.size != 1 type = arguments[0] -- cgit v1.2.3 From e071b05ab631f4b73fed7178c52d0416f7629cb8 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Wed, 29 Jun 2011 12:30:07 +0100 Subject: Added kwalify function. --- lib/puppet/parser/functions/kwalify.rb | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lib/puppet/parser/functions/kwalify.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/kwalify.rb b/lib/puppet/parser/functions/kwalify.rb new file mode 100644 index 0000000..7238f84 --- /dev/null +++ b/lib/puppet/parser/functions/kwalify.rb @@ -0,0 +1,35 @@ +# +# kwalify.rb +# + +require 'kwalify' + +module Puppet::Parser::Functions + newfunction(:kwalify, :type => :statement, :doc => <<-EOS +This function uses kwalify to validate Puppet data structures against Kwalify +schemas. + EOS + ) do |args| + + raise(Puppet::ParseError, "kwalify(): Wrong number of arguments " + + "given (#{args.size} for 2)") if args.size != 2 + + schema = args[0] + document = args[1] + + validator = Kwalify::Validator.new(schema) + + errors = validator.validate(document) + + if errors && !errors.empty? + error_out = [] + for e in errors + error_out << "[#{e.path}] #{e.message}" + end + raise(Puppet::ParseError, "Failed kwalify schema validation:\n" + error_out.join("\n")) + end + + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 790818116e953118ba9eab5e5bef6d63f7bbc1fa Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Wed, 29 Jun 2011 21:21:55 +0100 Subject: Added tests for each function, fixing functions as we hit bugs. --- lib/puppet/parser/functions/date.rb | 6 ++++++ lib/puppet/parser/functions/delete.rb | 6 ++++++ lib/puppet/parser/functions/grep.rb | 6 ++++++ lib/puppet/parser/functions/is_float.rb | 6 ++++++ lib/puppet/parser/functions/is_integer.rb | 6 ++++++ lib/puppet/parser/functions/is_numeric.rb | 6 ++++++ lib/puppet/parser/functions/is_valid_domain_name.rb | 10 ++++++++-- lib/puppet/parser/functions/is_valid_ip_address.rb | 6 ++++++ lib/puppet/parser/functions/is_valid_mac_address.rb | 6 ++++++ lib/puppet/parser/functions/is_valid_netmask.rb | 6 ++++++ lib/puppet/parser/functions/load_json.rb | 12 ++++++++++++ lib/puppet/parser/functions/load_yaml.rb | 10 ++++++++++ lib/puppet/parser/functions/rand.rb | 6 ++++++ lib/puppet/parser/functions/sort.rb | 6 ++++++ lib/puppet/parser/functions/squeeze.rb | 6 ++++++ lib/puppet/parser/functions/time.rb | 5 +++++ 16 files changed, 107 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/date.rb b/lib/puppet/parser/functions/date.rb index ea11265..4d0543e 100644 --- a/lib/puppet/parser/functions/date.rb +++ b/lib/puppet/parser/functions/date.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:date, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_valid_netmask(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb index b8376d1..88f3448 100644 --- a/lib/puppet/parser/functions/delete.rb +++ b/lib/puppet/parser/functions/delete.rb @@ -9,6 +9,12 @@ module Puppet::Parser::Functions newfunction(:delete, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 2) then + raise(Puppet::ParseError, "is_valid_netmask(): Wrong number of arguments "+ + "given #{arguments.size} for 2") + end + end end diff --git a/lib/puppet/parser/functions/grep.rb b/lib/puppet/parser/functions/grep.rb index 1663fe7..8549218 100644 --- a/lib/puppet/parser/functions/grep.rb +++ b/lib/puppet/parser/functions/grep.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:grep, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 2) then + raise(Puppet::ParseError, "grep(): Wrong number of arguments "+ + "given #{arguments.size} for 2") + end + end end diff --git a/lib/puppet/parser/functions/is_float.rb b/lib/puppet/parser/functions/is_float.rb index 2a5a923..39d097f 100644 --- a/lib/puppet/parser/functions/is_float.rb +++ b/lib/puppet/parser/functions/is_float.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:is_float, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_float(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/is_integer.rb b/lib/puppet/parser/functions/is_integer.rb index 44337f0..9813cf1 100644 --- a/lib/puppet/parser/functions/is_integer.rb +++ b/lib/puppet/parser/functions/is_integer.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:is_integer, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_integer(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/is_numeric.rb b/lib/puppet/parser/functions/is_numeric.rb index 7a64091..96e8674 100644 --- a/lib/puppet/parser/functions/is_numeric.rb +++ b/lib/puppet/parser/functions/is_numeric.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:is_numeric, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_numeric(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/is_valid_domain_name.rb b/lib/puppet/parser/functions/is_valid_domain_name.rb index 05d64be..c0b319c 100644 --- a/lib/puppet/parser/functions/is_valid_domain_name.rb +++ b/lib/puppet/parser/functions/is_valid_domain_name.rb @@ -1,11 +1,17 @@ # -# is_valid_doman_name.rb +# is_valid_domain_name.rb # module Puppet::Parser::Functions - newfunction(:is_valid_doman_name, :type => :rvalue, :doc => <<-EOS + newfunction(:is_valid_domain_name, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_valid_domain_name(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/is_valid_ip_address.rb b/lib/puppet/parser/functions/is_valid_ip_address.rb index e6b68a8..e91dda8 100644 --- a/lib/puppet/parser/functions/is_valid_ip_address.rb +++ b/lib/puppet/parser/functions/is_valid_ip_address.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:is_valid_ip_address, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_valid_ip_address(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/is_valid_mac_address.rb b/lib/puppet/parser/functions/is_valid_mac_address.rb index 5e354c9..0b91d0d 100644 --- a/lib/puppet/parser/functions/is_valid_mac_address.rb +++ b/lib/puppet/parser/functions/is_valid_mac_address.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:is_valid_mac_address, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_valid_mac_address(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/is_valid_netmask.rb b/lib/puppet/parser/functions/is_valid_netmask.rb index 2aeb374..41e4843 100644 --- a/lib/puppet/parser/functions/is_valid_netmask.rb +++ b/lib/puppet/parser/functions/is_valid_netmask.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:is_valid_netmask, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_valid_netmask(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/load_json.rb b/lib/puppet/parser/functions/load_json.rb index 9ec8c78..7c3f187 100644 --- a/lib/puppet/parser/functions/load_json.rb +++ b/lib/puppet/parser/functions/load_json.rb @@ -6,6 +6,18 @@ module Puppet::Parser::Functions newfunction(:load_json, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "load_json(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + json = arguments[0] + + require 'json' + + JSON.load(json) + end end diff --git a/lib/puppet/parser/functions/load_yaml.rb b/lib/puppet/parser/functions/load_yaml.rb index 684a721..1bc2f36 100644 --- a/lib/puppet/parser/functions/load_yaml.rb +++ b/lib/puppet/parser/functions/load_yaml.rb @@ -6,6 +6,16 @@ module Puppet::Parser::Functions newfunction(:load_yaml, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "load_yaml(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + require 'yaml' + + YAML::load(arguments[0]) + end end diff --git a/lib/puppet/parser/functions/rand.rb b/lib/puppet/parser/functions/rand.rb index 2cb9acb..6d870dc 100644 --- a/lib/puppet/parser/functions/rand.rb +++ b/lib/puppet/parser/functions/rand.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:rand, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 0) and (arguments.size != 1) then + raise(Puppet::ParseError, "rand(): Wrong number of arguments "+ + "given #{arguments.size} for 0 or 1") + end + end end diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb index 85e5ba0..974141c 100644 --- a/lib/puppet/parser/functions/sort.rb +++ b/lib/puppet/parser/functions/sort.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:sort, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 0) then + raise(Puppet::ParseError, "sort(): Wrong number of arguments "+ + "given #{arguments.size} for 0") + end + end end diff --git a/lib/puppet/parser/functions/squeeze.rb b/lib/puppet/parser/functions/squeeze.rb index a135bd3..02eb00c 100644 --- a/lib/puppet/parser/functions/squeeze.rb +++ b/lib/puppet/parser/functions/squeeze.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:squeeze, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 2) then + raise(Puppet::ParseError, "squeeze(): Wrong number of arguments "+ + "given #{arguments.size} for 2") + end + end end diff --git a/lib/puppet/parser/functions/time.rb b/lib/puppet/parser/functions/time.rb index f7c1041..e1f5b6f 100644 --- a/lib/puppet/parser/functions/time.rb +++ b/lib/puppet/parser/functions/time.rb @@ -10,6 +10,11 @@ module Puppet::Parser::Functions # The Time Zone argument is optional ... time_zone = arguments[0] if arguments[0] + if (arguments.size != 0) and (arguments.size != 1) then + raise(Puppet::ParseError, "time(): Wrong number of arguments "+ + "given #{arguments.size} for 0 or 1") + end + time = Time.new # There is probably a better way to handle Time Zone ... -- cgit v1.2.3 From 464fb1f41b9c7197fcaade6831b80d6390829ec7 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Wed, 29 Jun 2011 23:37:37 +0100 Subject: Add some more functional tests. --- lib/puppet/parser/functions/date.rb | 2 ++ lib/puppet/parser/functions/delete.rb | 6 ++++++ lib/puppet/parser/functions/fact.rb | 36 ----------------------------------- lib/puppet/parser/functions/grep.rb | 5 +++++ 4 files changed, 13 insertions(+), 36 deletions(-) delete mode 100644 lib/puppet/parser/functions/fact.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/date.rb b/lib/puppet/parser/functions/date.rb index 4d0543e..bc62e60 100644 --- a/lib/puppet/parser/functions/date.rb +++ b/lib/puppet/parser/functions/date.rb @@ -12,6 +12,8 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end + # TODO: stubbed + end end diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb index 88f3448..0d208b5 100644 --- a/lib/puppet/parser/functions/delete.rb +++ b/lib/puppet/parser/functions/delete.rb @@ -15,6 +15,12 @@ module Puppet::Parser::Functions "given #{arguments.size} for 2") end + a = arguments[0] + item = arguments[1] + + a.delete(item) + a + end end diff --git a/lib/puppet/parser/functions/fact.rb b/lib/puppet/parser/functions/fact.rb deleted file mode 100644 index 27b7bb2..0000000 --- a/lib/puppet/parser/functions/fact.rb +++ /dev/null @@ -1,36 +0,0 @@ -# -# fact.rb -# - -module Puppet::Parser::Functions - newfunction(:fact, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "fact(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - fact = arguments[0] - - unless fact.is_a?(String) - raise(Puppet::ParseError, 'fact(): Requires fact name to be a string') - end - - raise(Puppet::ParseError, 'fact(): You must provide ' + - 'fact name') if fact.empty? - - result = lookupvar(fact) # Get the value of interest from Facter ... - - # - # Now this is a funny one ... Puppet does not have a concept of - # returning neither undef nor nil back for use within the Puppet DSL - # and empty string is as closest to actual undef as you we can get - # at this point in time ... - # - result = result.empty? ? '' : result - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/grep.rb b/lib/puppet/parser/functions/grep.rb index 8549218..2caaa6f 100644 --- a/lib/puppet/parser/functions/grep.rb +++ b/lib/puppet/parser/functions/grep.rb @@ -12,6 +12,11 @@ module Puppet::Parser::Functions "given #{arguments.size} for 2") end + a = arguments[0] + pattern = Regexp.new(arguments[1]) + + a.grep(pattern) + end end -- cgit v1.2.3 From c7c8647634df07f0de0e662360eb4567f7c20770 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Wed, 29 Jun 2011 23:39:23 +0100 Subject: Move require inside function for kwalify. --- lib/puppet/parser/functions/kwalify.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/kwalify.rb b/lib/puppet/parser/functions/kwalify.rb index 7238f84..49b9aeb 100644 --- a/lib/puppet/parser/functions/kwalify.rb +++ b/lib/puppet/parser/functions/kwalify.rb @@ -2,8 +2,6 @@ # kwalify.rb # -require 'kwalify' - module Puppet::Parser::Functions newfunction(:kwalify, :type => :statement, :doc => <<-EOS This function uses kwalify to validate Puppet data structures against Kwalify @@ -17,6 +15,8 @@ schemas. schema = args[0] document = args[1] + require 'kwalify' + validator = Kwalify::Validator.new(schema) errors = validator.validate(document) -- cgit v1.2.3 From 1abf4b62fc8e97c7096c9da3d350e3e6268c5c72 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 30 Jun 2011 01:00:32 +0200 Subject: Few more tests. --- lib/puppet/parser/functions/date.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/date.rb b/lib/puppet/parser/functions/date.rb index bc62e60..0439bd1 100644 --- a/lib/puppet/parser/functions/date.rb +++ b/lib/puppet/parser/functions/date.rb @@ -8,7 +8,7 @@ module Puppet::Parser::Functions ) do |arguments| if (arguments.size != 1) then - raise(Puppet::ParseError, "is_valid_netmask(): Wrong number of arguments "+ + raise(Puppet::ParseError, "date(): Wrong number of arguments "+ "given #{arguments.size} for 1") end -- cgit v1.2.3 From 07ee33455439d960b9996e8110e011437181b42a Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 1 Jul 2011 21:09:02 +0200 Subject: Added validate_resource function and examples on how to use it (and kwalify as well) --- lib/puppet/parser/functions/validate_resource.rb | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 lib/puppet/parser/functions/validate_resource.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/validate_resource.rb b/lib/puppet/parser/functions/validate_resource.rb new file mode 100644 index 0000000..bbb5a54 --- /dev/null +++ b/lib/puppet/parser/functions/validate_resource.rb @@ -0,0 +1,36 @@ +# +# validate_resource +# + +module Puppet::Parser::Functions + newfunction(:validate_resource, :type => :statement, :doc => <<-EOS + EOS + ) do |arguments| + + require 'kwalify' + + if (arguments.size != 0) then + raise(Puppet::ParseError, "validate_resource(): Wrong number of arguments "+ + "given #{arguments.size} for 0") + end + + + classhash = to_hash(recursive=false) + sourcepath = source.file + schemapath = sourcepath.gsub(/\.(rb|pp)$/, ".schema") + schema = Kwalify::Yaml.load_file(schemapath) + validator = Kwalify::Validator.new(schema) + errors = validator.validate(classhash) + + if errors && !errors.empty? + error_output = "Resource validation failed:\n" + for e in errors + error_output += "[#{e.path}] #{e.message}\n" + end + raise(Puppet::ParseError, error_output) + end + + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From fde64f37c98291f4a5b9ee1fcf634288e42b730a Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Sun, 24 Jul 2011 00:39:17 +0100 Subject: (#1) - fleshed out some more tests. --- lib/puppet/parser/functions/sort.rb | 6 ++++-- lib/puppet/parser/functions/squeeze.rb | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb index 974141c..54a4018 100644 --- a/lib/puppet/parser/functions/sort.rb +++ b/lib/puppet/parser/functions/sort.rb @@ -7,11 +7,13 @@ module Puppet::Parser::Functions EOS ) do |arguments| - if (arguments.size != 0) then + if (arguments.size != 1) then raise(Puppet::ParseError, "sort(): Wrong number of arguments "+ - "given #{arguments.size} for 0") + "given #{arguments.size} for 1") end + arguments[0].sort + end end diff --git a/lib/puppet/parser/functions/squeeze.rb b/lib/puppet/parser/functions/squeeze.rb index 02eb00c..9a1dd20 100644 --- a/lib/puppet/parser/functions/squeeze.rb +++ b/lib/puppet/parser/functions/squeeze.rb @@ -7,9 +7,26 @@ module Puppet::Parser::Functions EOS ) do |arguments| - if (arguments.size != 2) then + if ((arguments.size != 2) and (arguments.size != 1)) then raise(Puppet::ParseError, "squeeze(): Wrong number of arguments "+ - "given #{arguments.size} for 2") + "given #{arguments.size} for 2 or 1") + end + + item = arguments[0] + squeezeval = arguments[1] + + if item.is_a?(Array) then + if squeezeval then + item.collect { |i| i.squeeze(squeezeval) } + else + item.collect { |i| i.squeeze } + end + else + if squeezeval then + item.squeeze(squeezeval) + else + item.squeeze + end end end -- cgit v1.2.3 From a55930368afe2e8177608472653c4696eccec92f Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 28 Jul 2011 15:38:19 +0100 Subject: (#2) - Added is_float and is_integer functionality. --- lib/puppet/parser/functions/is_float.rb | 8 ++++++++ lib/puppet/parser/functions/is_integer.rb | 8 ++++++++ 2 files changed, 16 insertions(+) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/is_float.rb b/lib/puppet/parser/functions/is_float.rb index 39d097f..8aed848 100644 --- a/lib/puppet/parser/functions/is_float.rb +++ b/lib/puppet/parser/functions/is_float.rb @@ -12,6 +12,14 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end + value = arguments[0] + + if value != value.to_f.to_s then + return false + else + return true + end + end end diff --git a/lib/puppet/parser/functions/is_integer.rb b/lib/puppet/parser/functions/is_integer.rb index 9813cf1..9dd1cee 100644 --- a/lib/puppet/parser/functions/is_integer.rb +++ b/lib/puppet/parser/functions/is_integer.rb @@ -12,6 +12,14 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end + value = arguments[0] + + if value != value.to_i.to_s then + return false + else + return true + end + end end -- cgit v1.2.3 From 635ed82e5cae38b0ba82098c340cbeed70d483c3 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 28 Jul 2011 18:10:16 +0100 Subject: (#2) - unstubbed is_valid_ip_address --- lib/puppet/parser/functions/is_valid_ip_address.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/is_valid_ip_address.rb b/lib/puppet/parser/functions/is_valid_ip_address.rb index e91dda8..4f45890 100644 --- a/lib/puppet/parser/functions/is_valid_ip_address.rb +++ b/lib/puppet/parser/functions/is_valid_ip_address.rb @@ -7,11 +7,24 @@ module Puppet::Parser::Functions EOS ) do |arguments| + require 'ipaddr' + if (arguments.size != 1) then raise(Puppet::ParseError, "is_valid_ip_address(): Wrong number of arguments "+ "given #{arguments.size} for 1") end + begin + ip = IPAddr.new(arguments[0]) + rescue ArgumentError + return false + end + + if ip.ipv4? or ip.ipv6? then + return true + else + return false + end end end -- cgit v1.2.3 From 313df566bf5cfcbef73fc8182ccb07ddf2f13feb Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 28 Jul 2011 21:03:33 +0100 Subject: (#2) unstub is_numeric function. --- lib/puppet/parser/functions/is_numeric.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/is_numeric.rb b/lib/puppet/parser/functions/is_numeric.rb index 96e8674..c2c0fb5 100644 --- a/lib/puppet/parser/functions/is_numeric.rb +++ b/lib/puppet/parser/functions/is_numeric.rb @@ -12,6 +12,14 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end + value = arguments[0] + + if value == value.to_f.to_s or value == value.to_i.to_s then + return true + else + return false + end + end end -- cgit v1.2.3 From 1a7bd1ae8321bfc3d2ae87f1d5184828abf56916 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 28 Jul 2011 21:10:33 +0100 Subject: Remove is_valid_netmask instead of unstubbing. Doesn't seem like a sensible function on its own. --- lib/puppet/parser/functions/is_valid_netmask.rb | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 lib/puppet/parser/functions/is_valid_netmask.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/is_valid_netmask.rb b/lib/puppet/parser/functions/is_valid_netmask.rb deleted file mode 100644 index 41e4843..0000000 --- a/lib/puppet/parser/functions/is_valid_netmask.rb +++ /dev/null @@ -1,18 +0,0 @@ -# -# is_valid_netmask.rb -# - -module Puppet::Parser::Functions - newfunction(:is_valid_netmask, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "is_valid_netmask(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - end -end - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From a47853502d7681edd8173e05249ce43d44bede7c Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 28 Jul 2011 21:15:43 +0100 Subject: Removed load_variables. load_yaml is sufficient to solve this problem on its own. --- lib/puppet/parser/functions/load_variables.rb | 77 --------------------------- 1 file changed, 77 deletions(-) delete mode 100644 lib/puppet/parser/functions/load_variables.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/load_variables.rb b/lib/puppet/parser/functions/load_variables.rb deleted file mode 100644 index a28c64b..0000000 --- a/lib/puppet/parser/functions/load_variables.rb +++ /dev/null @@ -1,77 +0,0 @@ -# -# load_variables.rb -# - -module Puppet::Parser::Functions - newfunction(:load_variables, :type => :statement, :doc => <<-EOS -This function will allow for loading variables from an external YAML -file and expose them for further use inside the Puppet manifest file ... - -For example: - -Given following content of the data.yaml file: - - --- - host1.example.com: - foo: bar - baz: quux - question: 42 - host2.example.com: - abc: def - this: that - darth: vader - -Then calling load_variables in Puppet manifest file as follows: - - load_variables("/etc/puppet/data.yaml", $fqdn) - -Will result in addition of variables $foo, $baz and $question -for matching host name as per the variable $fqdn ... - -Another example which uses per-host file: - -Given following content of the file data-host1.example.com.yaml: - - --- - foo: bar - -Then when we call load_variables like this: - - load_variables("/etc/puppet/data-${fqdn}.yaml") - -This will result in a variable $foo being added and ready for use. - EOS - ) do |arguments| - - raise(Puppet::ParseError, "load_variables(): Wrong number of " + - "arguments given (#{arguments.size} for 2)") if arguments.size < 2 - - data = {} - - file = arguments[0] - key = arguments[1] if arguments[1] - - if File.exists?(file) - - begin - data = YAML.load_file(file) - rescue => error - raise(Puppet::ParseError, "load_variables(): Unable to load data " + - "from the file `%s': %s" % file, error.to_s) - end - - raise(Puppet::ParseError, "load_variables(): Data in the file `%s' " + - "is not a hash" % file) unless data.is_a?(Hash) - - data = ((data[key] and data[key].is_a?(Hash)) ? data[key] : {}) if key - end - - data.each do |param, value| - value = strinterp(value) # Evaluate any interpolated variable names ... - - setvar(param, value) - end - end -end - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 4915eff575801e73ab15a77b500eb2e0d42b579c Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 28 Jul 2011 21:23:53 +0100 Subject: Removed crontab functions instead of unstubbing them. --- .../parser/functions/persistent_crontab_minutes.rb | 63 ---------------------- .../parser/functions/random_crontab_minutes.rb | 31 ----------- 2 files changed, 94 deletions(-) delete mode 100644 lib/puppet/parser/functions/persistent_crontab_minutes.rb delete mode 100644 lib/puppet/parser/functions/random_crontab_minutes.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/persistent_crontab_minutes.rb b/lib/puppet/parser/functions/persistent_crontab_minutes.rb deleted file mode 100644 index cd80094..0000000 --- a/lib/puppet/parser/functions/persistent_crontab_minutes.rb +++ /dev/null @@ -1,63 +0,0 @@ -# -# persistent_crontab_minutes.rb -# - -module Puppet::Parser::Functions - newfunction(:persistent_crontab_minutes, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 - - require 'md5' - - value = 0 - - job = arguments[0] - host = arguments[1] - - environment = Puppet[:environment] - - # We select first directory that exists. This might not be the best idea ... - modules = Puppet[:modulepath].split(':').select { |i| File.exists?(i) }.first - - raise(Puppet::ParseError, "Unable to determine the storage " + - "directory for Puppet modules") unless modules - - # Prepare the file where we store current value ... - file = "/puppet/state/crontab/#{host}-#{job}.minutes" - file = File.join(modules, file) - - # Get the directory portion from the file name ... - directory = File.dirname(file) - - FileUtils.mkdir_p(directory) unless File.directory?(directory) - - if FileTest.exists?(file) - File.open(file, 'r') { |f| value = f.read.to_i } - - raise(Puppet::ParseError, "The value for minutes in the file `%s' " + - "is out of the range from 0 to 59 inclusive") unless value < 60 - else - # - # Pick a random number based on the job and host name. This will yield - # the same value for exactly the same combination of the job and host name. - # - value = MD5.new(job_name + host).to_s.hex % 60 - - # Minutes are from 0 to 59 inclusive ... - value = value < 60 ? value : 59 - - File.open(file, 'w') { |f| f.write(value) } - end - - # Tell Puppet to keep an eye on this file ... - parser = Puppet::Parser::Parser.new(environment) - parser.watch_file(file) if File.exists?(file) - - return value - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/random_crontab_minutes.rb b/lib/puppet/parser/functions/random_crontab_minutes.rb deleted file mode 100644 index 8ab29e1..0000000 --- a/lib/puppet/parser/functions/random_crontab_minutes.rb +++ /dev/null @@ -1,31 +0,0 @@ -# -# random_crontab_minutes.rb -# - -module Puppet::Parser::Functions - newfunction(:random_crontab_minutes, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 - - require 'md5' - - job_name = arguments[0] - host = arguments[1] - - # - # Pick a random number based on the job and host name. This will yield - # the same value for exactly the same combination of the job and host name. - # - value = MD5.new(job_name + host).to_s.hex % 60 - - # Minutes are from 0 to 59 inclusive ... - value = value < 60 ? value : 59 - - return value - end -end - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 7d6ae5d57ce45ff1293f02b90c816a7f938a3af6 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 28 Jul 2011 21:30:02 +0100 Subject: Count functionality overlaps with size - so removing it. --- lib/puppet/parser/functions/count.rb | 36 ------------------------------------ 1 file changed, 36 deletions(-) delete mode 100644 lib/puppet/parser/functions/count.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/count.rb b/lib/puppet/parser/functions/count.rb deleted file mode 100644 index c4e2283..0000000 --- a/lib/puppet/parser/functions/count.rb +++ /dev/null @@ -1,36 +0,0 @@ -# -# count.rb -# - -# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... -# TODO(Krzysztof Wilczynski): Support for hash values would be nice too ... - -module Puppet::Parser::Functions - newfunction(:count, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - # Technically we support two arguments but only first is mandatory ... - raise(Puppet::ParseError, "count(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, Hash, String].include?(klass) - raise(Puppet::ParseError, 'count(): Requires either ' + - 'array, hash or string to work with') - end - - item = arguments[1] if arguments[1] - - value = value.is_a?(Hash) ? value.keys : value - - # No item to look for and count? Then just return current size ... - result = item ? value.count(item) : value.size - - return result - end -end - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From ce48eb6e7a76f74e1f61c76ac3b185031c40772b Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 00:10:31 +0100 Subject: Allow sort for strings. --- lib/puppet/parser/functions/sort.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb index 54a4018..49ca20a 100644 --- a/lib/puppet/parser/functions/sort.rb +++ b/lib/puppet/parser/functions/sort.rb @@ -12,7 +12,13 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end - arguments[0].sort + value = arguments[0] + + if value.is_a?(Array) then + value.sort + elsif value.is_a?(String) then + value.split("").sort.to_s + end end end -- cgit v1.2.3 From 4080c0534e76cf68b935344c75b2382f0184a226 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 17:55:45 +0100 Subject: (#2) unstub is_valid_mac_address. --- lib/puppet/parser/functions/is_valid_mac_address.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/is_valid_mac_address.rb b/lib/puppet/parser/functions/is_valid_mac_address.rb index 0b91d0d..a00d874 100644 --- a/lib/puppet/parser/functions/is_valid_mac_address.rb +++ b/lib/puppet/parser/functions/is_valid_mac_address.rb @@ -12,6 +12,14 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end + mac = arguments[0] + + if /^[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}$/.match(mac) then + return true + else + return false + end + end end -- cgit v1.2.3 From db7e06e301b689efcd421fd56fb1c23e3595e173 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 18:00:32 +0100 Subject: Removed join_with_prefix. --- lib/puppet/parser/functions/join_with_prefix.rb | 38 ------------------------- 1 file changed, 38 deletions(-) delete mode 100644 lib/puppet/parser/functions/join_with_prefix.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/join_with_prefix.rb b/lib/puppet/parser/functions/join_with_prefix.rb deleted file mode 100644 index 8bf96d9..0000000 --- a/lib/puppet/parser/functions/join_with_prefix.rb +++ /dev/null @@ -1,38 +0,0 @@ -# -# join_with_prefix.rb -# - -module Puppet::Parser::Functions - newfunction(:join_with_prefix, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - # Technically we support three arguments but only first is mandatory ... - raise(Puppet::ParseError, "join(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - array = arguments[0] - - unless array.is_a?(Array) - raise(Puppet::ParseError, 'join_with_prefix(): Requires ' + - 'array to work with') - end - - prefix = arguments[1] if arguments[1] - suffix = arguments[2] if arguments[2] - - if prefix and suffix - result = prefix + array.join(suffix + prefix) - elsif prefix and not suffix - result = array.collect { |i| prefix ? prefix + i : i } - elsif suffix and not prefix - result = array.join(suffix) - else - result = array.join - end - - return result - end -end - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 62520a2df03acde975d8d7bd96805e767a9611f4 Mon Sep 17 00:00:00 2001 From: James Turnbull Date: Sat, 30 Jul 2011 04:22:30 +1000 Subject: Added doc strings for first five functions --- lib/puppet/parser/functions/abs.rb | 2 ++ lib/puppet/parser/functions/bool2num.rb | 4 ++++ lib/puppet/parser/functions/capitalize.rb | 2 ++ lib/puppet/parser/functions/chomp.rb | 5 ++++- lib/puppet/parser/functions/chop.rb | 7 ++++++- 5 files changed, 18 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/abs.rb b/lib/puppet/parser/functions/abs.rb index 0a554e4..ade5462 100644 --- a/lib/puppet/parser/functions/abs.rb +++ b/lib/puppet/parser/functions/abs.rb @@ -4,6 +4,8 @@ module Puppet::Parser::Functions newfunction(:abs, :type => :rvalue, :doc => <<-EOS + Returns the absolute value of a number, for example -34.56 becomes + 34.56. Takes a single integer and float value as an argument. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/bool2num.rb b/lib/puppet/parser/functions/bool2num.rb index b2989d0..9a07a8a 100644 --- a/lib/puppet/parser/functions/bool2num.rb +++ b/lib/puppet/parser/functions/bool2num.rb @@ -4,6 +4,10 @@ module Puppet::Parser::Functions newfunction(:bool2num, :type => :rvalue, :doc => <<-EOS + Converts a boolean to a number. Converts the values: + false, f, 0, n, and no to 0 + true, t, 1, y, and yes to 1 + Requires a single boolean or string as an input. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/capitalize.rb b/lib/puppet/parser/functions/capitalize.rb index f902cb3..640d00b 100644 --- a/lib/puppet/parser/functions/capitalize.rb +++ b/lib/puppet/parser/functions/capitalize.rb @@ -4,6 +4,8 @@ module Puppet::Parser::Functions newfunction(:capitalize, :type => :rvalue, :doc => <<-EOS + Capitalizes the first letter of a string or array of strings. + Requires either a single string or an array as an input. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/chomp.rb b/lib/puppet/parser/functions/chomp.rb index e1d788a..c99d139 100644 --- a/lib/puppet/parser/functions/chomp.rb +++ b/lib/puppet/parser/functions/chomp.rb @@ -3,7 +3,10 @@ # module Puppet::Parser::Functions - newfunction(:chomp, :type => :rvalue, :doc => <<-EOS + newfunction(:chomp, :type => :rvalue, :doc => <<-'EOS' + Removes the record separator from the end of a string or an array of + strings, for example `hello\n` becomes `hello`. + Requires a single string or array as an input. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/chop.rb b/lib/puppet/parser/functions/chop.rb index 0f9af86..636b990 100644 --- a/lib/puppet/parser/functions/chop.rb +++ b/lib/puppet/parser/functions/chop.rb @@ -3,7 +3,12 @@ # module Puppet::Parser::Functions - newfunction(:chop, :type => :rvalue, :doc => <<-EOS + newfunction(:chop, :type => :rvalue, :doc => <<-'EOS' + Returns a new string with the last character removed. If the string ends + with `\r\n`, both characters are removed. Applying chop to an empty + string returns an empty string. If you wish to merely remove record + separators then you should use the `chomp` function. + Requires a string or array of strings as input. EOS ) do |arguments| -- cgit v1.2.3 From 56a402e6542105ec63c5071e685d5af93fd4d0f3 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 20:08:31 +0100 Subject: (#2) unstub is_valid_domain_name --- lib/puppet/parser/functions/is_valid_domain_name.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/is_valid_domain_name.rb b/lib/puppet/parser/functions/is_valid_domain_name.rb index c0b319c..99d6f86 100644 --- a/lib/puppet/parser/functions/is_valid_domain_name.rb +++ b/lib/puppet/parser/functions/is_valid_domain_name.rb @@ -12,6 +12,14 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end + domain = arguments[0] + + if domain =~ /^(([a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\.?$/ then + return true + else + return false + end + end end -- cgit v1.2.3 From 18e5302614bce168ac07e35dc23b058064e9e41c Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 20:11:47 +0100 Subject: (#2) fix is_string finally so it also makes sure numbers return false. --- lib/puppet/parser/functions/is_string.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/is_string.rb b/lib/puppet/parser/functions/is_string.rb index 61037e3..8a02a10 100644 --- a/lib/puppet/parser/functions/is_string.rb +++ b/lib/puppet/parser/functions/is_string.rb @@ -14,6 +14,10 @@ module Puppet::Parser::Functions result = type.is_a?(String) + if result and (type == type.to_f.to_s or type == type.to_i.to_s) then + return false + end + return result end end -- cgit v1.2.3 From aa023c1e5d23b2a5a66dbe9a36d0e8765a8d8cff Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 20:56:40 +0100 Subject: Removed date stub since this functinality is available in strftime anyway. --- lib/puppet/parser/functions/date.rb | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 lib/puppet/parser/functions/date.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/date.rb b/lib/puppet/parser/functions/date.rb deleted file mode 100644 index 0439bd1..0000000 --- a/lib/puppet/parser/functions/date.rb +++ /dev/null @@ -1,20 +0,0 @@ -# -# date.rb -# - -module Puppet::Parser::Functions - newfunction(:date, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "date(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - # TODO: stubbed - - end -end - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From f9634b7f9b03be86e25dc740cdcda754a0d2bf7d Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 22:08:09 +0100 Subject: Remove rand. --- lib/puppet/parser/functions/rand.rb | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 lib/puppet/parser/functions/rand.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/rand.rb b/lib/puppet/parser/functions/rand.rb deleted file mode 100644 index 6d870dc..0000000 --- a/lib/puppet/parser/functions/rand.rb +++ /dev/null @@ -1,18 +0,0 @@ -# -# rand.rb -# - -module Puppet::Parser::Functions - newfunction(:rand, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - if (arguments.size != 0) and (arguments.size != 1) then - raise(Puppet::ParseError, "rand(): Wrong number of arguments "+ - "given #{arguments.size} for 0 or 1") - end - - end -end - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 19313b43ea04066a88a0b78e83650ac52785e2e9 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 22:18:56 +0100 Subject: (#3) Apply missing documentation to more functions. --- lib/puppet/parser/functions/num2bool.rb | 2 + lib/puppet/parser/functions/prefix.rb | 7 +++ lib/puppet/parser/functions/range.rb | 12 +++++ lib/puppet/parser/functions/reverse.rb | 1 + lib/puppet/parser/functions/rstrip.rb | 1 + lib/puppet/parser/functions/shuffle.rb | 1 + lib/puppet/parser/functions/size.rb | 1 + lib/puppet/parser/functions/sort.rb | 1 + lib/puppet/parser/functions/squeeze.rb | 1 + lib/puppet/parser/functions/str2bool.rb | 3 ++ lib/puppet/parser/functions/strftime.rb | 63 ++++++++++++++++++++++++ lib/puppet/parser/functions/strip.rb | 8 +++ lib/puppet/parser/functions/swapcase.rb | 7 +++ lib/puppet/parser/functions/time.rb | 7 +++ lib/puppet/parser/functions/type.rb | 19 ++++++- lib/puppet/parser/functions/unique.rb | 17 +++++++ lib/puppet/parser/functions/upcase.rb | 9 ++++ lib/puppet/parser/functions/validate_resource.rb | 5 ++ lib/puppet/parser/functions/values.rb | 14 ++++++ lib/puppet/parser/functions/values_at.rb | 25 ++++++++-- lib/puppet/parser/functions/zip.rb | 9 ++++ 21 files changed, 208 insertions(+), 5 deletions(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index 2baef62..874db22 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -6,6 +6,8 @@ module Puppet::Parser::Functions newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS +This function converts a number into a true boolean. Zero becomes false. Numbers +higher then 0 become true. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb index 0e0cee2..4593976 100644 --- a/lib/puppet/parser/functions/prefix.rb +++ b/lib/puppet/parser/functions/prefix.rb @@ -4,6 +4,13 @@ module Puppet::Parser::Functions newfunction(:prefix, :type => :rvalue, :doc => <<-EOS +This function applies a prefix to all elements in an array. + +*Examles:* + + prefix(['a','b','c'], 'p') + +Will return: ['pa','pb','pc'] EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb index 6afb50c..6e85422 100644 --- a/lib/puppet/parser/functions/range.rb +++ b/lib/puppet/parser/functions/range.rb @@ -6,6 +6,18 @@ module Puppet::Parser::Functions newfunction(:range, :type => :rvalue, :doc => <<-EOS +When given range in the form of (start, stop) it will extrapolate a range as +an array. + +*Examples:* + + range("0", "9") + +Will return: [0,1,2,3,4,5,6,7,8,9] + + range("a", "c") + +Will return: ["a","b","c"] EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/reverse.rb b/lib/puppet/parser/functions/reverse.rb index 79e9b93..fe04869 100644 --- a/lib/puppet/parser/functions/reverse.rb +++ b/lib/puppet/parser/functions/reverse.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:reverse, :type => :rvalue, :doc => <<-EOS +Reverses the order of a string or array. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/rstrip.rb b/lib/puppet/parser/functions/rstrip.rb index 56849d3..29b0998 100644 --- a/lib/puppet/parser/functions/rstrip.rb +++ b/lib/puppet/parser/functions/rstrip.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:rstrip, :type => :rvalue, :doc => <<-EOS +Strips leading spaces to the right of the string. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/shuffle.rb b/lib/puppet/parser/functions/shuffle.rb index 73e798c..18134ab 100644 --- a/lib/puppet/parser/functions/shuffle.rb +++ b/lib/puppet/parser/functions/shuffle.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:shuffle, :type => :rvalue, :doc => <<-EOS +Randomizes the order of a string or array elements. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/size.rb b/lib/puppet/parser/functions/size.rb index aa4f4ad..cc207e3 100644 --- a/lib/puppet/parser/functions/size.rb +++ b/lib/puppet/parser/functions/size.rb @@ -6,6 +6,7 @@ module Puppet::Parser::Functions newfunction(:size, :type => :rvalue, :doc => <<-EOS +Returns the number of elements in a string or array. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb index 49ca20a..3785496 100644 --- a/lib/puppet/parser/functions/sort.rb +++ b/lib/puppet/parser/functions/sort.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:sort, :type => :rvalue, :doc => <<-EOS +Sorts strings and arrays lexically. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/squeeze.rb b/lib/puppet/parser/functions/squeeze.rb index 9a1dd20..65c174a 100644 --- a/lib/puppet/parser/functions/squeeze.rb +++ b/lib/puppet/parser/functions/squeeze.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:squeeze, :type => :rvalue, :doc => <<-EOS +Returns a new string where runs of the same character that occur in this set are replaced by a single character. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/str2bool.rb b/lib/puppet/parser/functions/str2bool.rb index f3a6d6c..c320da6 100644 --- a/lib/puppet/parser/functions/str2bool.rb +++ b/lib/puppet/parser/functions/str2bool.rb @@ -4,6 +4,9 @@ module Puppet::Parser::Functions newfunction(:str2bool, :type => :rvalue, :doc => <<-EOS +This converts a string to a boolean. This attempt to convert strings that +contain things like: y, 1, t, true to 'true' and strings that contain things +like: 0, f, n, false, no to 'false'. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/strftime.rb b/lib/puppet/parser/functions/strftime.rb index c919320..0b52ade 100644 --- a/lib/puppet/parser/functions/strftime.rb +++ b/lib/puppet/parser/functions/strftime.rb @@ -4,6 +4,69 @@ module Puppet::Parser::Functions newfunction(:strftime, :type => :rvalue, :doc => <<-EOS +This function returns formatted time. + +*Examples:* + +To return the time since epoch: + + strftime("%s") + +To return the date: + + strftime("%Y-%m-%d") + +*Format meaning:* + + %a - The abbreviated weekday name (``Sun'') + %A - The full weekday name (``Sunday'') + %b - The abbreviated month name (``Jan'') + %B - The full month name (``January'') + %c - The preferred local date and time representation + %C - Century (20 in 2009) + %d - Day of the month (01..31) + %D - Date (%m/%d/%y) + %e - Day of the month, blank-padded ( 1..31) + %F - Equivalent to %Y-%m-%d (the ISO 8601 date format) + %h - Equivalent to %b + %H - Hour of the day, 24-hour clock (00..23) + %I - Hour of the day, 12-hour clock (01..12) + %j - Day of the year (001..366) + %k - hour, 24-hour clock, blank-padded ( 0..23) + %l - hour, 12-hour clock, blank-padded ( 0..12) + %L - Millisecond of the second (000..999) + %m - Month of the year (01..12) + %M - Minute of the hour (00..59) + %n - Newline (\n) + %N - Fractional seconds digits, default is 9 digits (nanosecond) + %3N millisecond (3 digits) + %6N microsecond (6 digits) + %9N nanosecond (9 digits) + %p - Meridian indicator (``AM'' or ``PM'') + %P - Meridian indicator (``am'' or ``pm'') + %r - time, 12-hour (same as %I:%M:%S %p) + %R - time, 24-hour (%H:%M) + %s - Number of seconds since 1970-01-01 00:00:00 UTC. + %S - Second of the minute (00..60) + %t - Tab character (\t) + %T - time, 24-hour (%H:%M:%S) + %u - Day of the week as a decimal, Monday being 1. (1..7) + %U - Week number of the current year, + starting with the first Sunday as the first + day of the first week (00..53) + %v - VMS date (%e-%b-%Y) + %V - Week number of year according to ISO 8601 (01..53) + %W - Week number of the current year, + starting with the first Monday as the first + day of the first week (00..53) + %w - Day of the week (Sunday is 0, 0..6) + %x - Preferred representation for the date alone, no time + %X - Preferred representation for the time alone, no date + %y - Year without a century (00..99) + %Y - Year with century + %z - Time zone as hour offset from UTC (e.g. +0900) + %Z - Time zone name + %% - Literal ``%'' character EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/strip.rb b/lib/puppet/parser/functions/strip.rb index ebab20e..5f4630d 100644 --- a/lib/puppet/parser/functions/strip.rb +++ b/lib/puppet/parser/functions/strip.rb @@ -4,6 +4,14 @@ module Puppet::Parser::Functions newfunction(:strip, :type => :rvalue, :doc => <<-EOS +This function removes leading and trailing whitespace from a string or from +every string inside an array. + +*Examples:* + + strip(" aaa ") + +Would result in: "aaa" EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/swapcase.rb b/lib/puppet/parser/functions/swapcase.rb index a0002a9..b9e6632 100644 --- a/lib/puppet/parser/functions/swapcase.rb +++ b/lib/puppet/parser/functions/swapcase.rb @@ -4,6 +4,13 @@ module Puppet::Parser::Functions newfunction(:swapcase, :type => :rvalue, :doc => <<-EOS +This function will swap the existing case of a string. + +*Examples:* + + swapcase("aBcD") + +Would result in: "AbCd" EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/time.rb b/lib/puppet/parser/functions/time.rb index e1f5b6f..0cddaf8 100644 --- a/lib/puppet/parser/functions/time.rb +++ b/lib/puppet/parser/functions/time.rb @@ -4,6 +4,13 @@ module Puppet::Parser::Functions newfunction(:time, :type => :rvalue, :doc => <<-EOS +This function will return the current time since epoch as an integer. + +*Examples:* + + time() + +Will return something like: 1311972653 EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/type.rb b/lib/puppet/parser/functions/type.rb index 389a056..de6087e 100644 --- a/lib/puppet/parser/functions/type.rb +++ b/lib/puppet/parser/functions/type.rb @@ -4,6 +4,13 @@ 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 EOS ) do |arguments| @@ -22,11 +29,19 @@ module Puppet::Parser::Functions # We note that Integer is the parent to Bignum and Fixnum ... result = case klass - when /^(?:Big|Fix)num$/ then 'Integer' + when /^(?:Big|Fix)num$/ then 'integer' else klass end - return result + 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 diff --git a/lib/puppet/parser/functions/unique.rb b/lib/puppet/parser/functions/unique.rb index a922c94..8844a74 100644 --- a/lib/puppet/parser/functions/unique.rb +++ b/lib/puppet/parser/functions/unique.rb @@ -4,6 +4,23 @@ module Puppet::Parser::Functions newfunction(:unique, :type => :rvalue, :doc => <<-EOS +This function will remove duplicates from strings and arrays. + +*Examples:* + + unique("aabbcc") + +Will return: + + abc + +You can also use this with arrays: + + unique(["a","a","b","b","c","c"]) + +This returns: + + ["a","b","c"] EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/upcase.rb b/lib/puppet/parser/functions/upcase.rb index 8a9769d..fe6cadc 100644 --- a/lib/puppet/parser/functions/upcase.rb +++ b/lib/puppet/parser/functions/upcase.rb @@ -4,6 +4,15 @@ module Puppet::Parser::Functions newfunction(:upcase, :type => :rvalue, :doc => <<-EOS +Converts a string or an array of strings to uppercase. + +*Examples:* + + upcase("abcd") + +Will return: + + ASDF EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/validate_resource.rb b/lib/puppet/parser/functions/validate_resource.rb index bbb5a54..d71ef3f 100644 --- a/lib/puppet/parser/functions/validate_resource.rb +++ b/lib/puppet/parser/functions/validate_resource.rb @@ -4,6 +4,11 @@ module Puppet::Parser::Functions newfunction(:validate_resource, :type => :statement, :doc => <<-EOS +This function when placed at the beginning of a class, will go looking for a +valid kwalify schema by replacing the extension of the file with '.schema'. + +It will then validate the arguments passed to the function using that kwalify +schema. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/values.rb b/lib/puppet/parser/functions/values.rb index c1c4a77..1606756 100644 --- a/lib/puppet/parser/functions/values.rb +++ b/lib/puppet/parser/functions/values.rb @@ -4,6 +4,20 @@ module Puppet::Parser::Functions newfunction(:values, :type => :rvalue, :doc => <<-EOS +When given a hash this function will return the values of that hash. + +*Examples:* + + $hash = { + 'a' => 1, + 'b' => 2, + 'c' => 3, + } + values($hash) + +This example would return: + + [1,2,3] EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/values_at.rb b/lib/puppet/parser/functions/values_at.rb index 331af6a..7f1de8e 100644 --- a/lib/puppet/parser/functions/values_at.rb +++ b/lib/puppet/parser/functions/values_at.rb @@ -2,11 +2,30 @@ # values_at.rb # -# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ... -# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... - module Puppet::Parser::Functions newfunction(:values_at, :type => :rvalue, :doc => <<-EOS +Finds value inside an array based on location. + +The first argument is the array you want to analyze, and the second element can +be a combination of: + +* A single numeric index +* A range in the form of 'start-stop' (eg. 4-9) +* An array combining the above + +*Examples*: + + values_at(['a','b','c'], 2) + +Would return ['c']. + + values_at(['a','b','c'], ["0-1"]) + +Would return ['a','b']. + + values_at(['a','b','c','d','e'], [0, "2-3"]) + +Would return ['a','c','d']. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/zip.rb b/lib/puppet/parser/functions/zip.rb index 024d64a..2b56e9c 100644 --- a/lib/puppet/parser/functions/zip.rb +++ b/lib/puppet/parser/functions/zip.rb @@ -4,6 +4,15 @@ module Puppet::Parser::Functions newfunction(:zip, :type => :rvalue, :doc => <<-EOS +Takes one element from first array and merges corresponding elements from second array. This generates a sequence of n-element arrays, where n is one more than the count of arguments. + +*Example:* + + zip(['1','2','3'],['4','5','6']) + +Would result in: + + ["1", "4"], ["2", "5"], ["3", "6"] EOS ) do |arguments| -- cgit v1.2.3 From a1cae426f1d6b8f2c19184ec8aac3ebc47d97744 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 23:09:30 +0100 Subject: (#3) Provide documentation for remaining functions. --- lib/puppet/parser/functions/delete.rb | 9 ++++++++- lib/puppet/parser/functions/delete_at.rb | 7 +++++++ lib/puppet/parser/functions/downcase.rb | 1 + lib/puppet/parser/functions/empty.rb | 1 + lib/puppet/parser/functions/flatten.rb | 8 ++++++++ lib/puppet/parser/functions/grep.rb | 10 ++++++++++ lib/puppet/parser/functions/hash.rb | 7 +++++++ lib/puppet/parser/functions/is_array.rb | 1 + lib/puppet/parser/functions/is_float.rb | 1 + lib/puppet/parser/functions/is_hash.rb | 1 + lib/puppet/parser/functions/is_integer.rb | 1 + lib/puppet/parser/functions/is_numeric.rb | 1 + lib/puppet/parser/functions/is_string.rb | 1 + lib/puppet/parser/functions/is_valid_domain_name.rb | 1 + lib/puppet/parser/functions/is_valid_ip_address.rb | 1 + lib/puppet/parser/functions/is_valid_mac_address.rb | 1 + lib/puppet/parser/functions/join.rb | 7 +++++++ lib/puppet/parser/functions/keys.rb | 1 + lib/puppet/parser/functions/load_json.rb | 2 ++ lib/puppet/parser/functions/load_yaml.rb | 2 ++ lib/puppet/parser/functions/lstrip.rb | 1 + lib/puppet/parser/functions/member.rb | 11 +++++++++++ lib/puppet/parser/functions/type.rb | 4 +++- 23 files changed, 78 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb index 0d208b5..ab8f75b 100644 --- a/lib/puppet/parser/functions/delete.rb +++ b/lib/puppet/parser/functions/delete.rb @@ -7,11 +7,18 @@ module Puppet::Parser::Functions newfunction(:delete, :type => :rvalue, :doc => <<-EOS +Deletes a selected element from an array. + +*Examples:* + + delete(['a','b','c'], 'b') + +Would return: ['a','c'] EOS ) do |arguments| if (arguments.size != 2) then - raise(Puppet::ParseError, "is_valid_netmask(): Wrong number of arguments "+ + raise(Puppet::ParseError, "delete(): Wrong number of arguments "+ "given #{arguments.size} for 2") end diff --git a/lib/puppet/parser/functions/delete_at.rb b/lib/puppet/parser/functions/delete_at.rb index 10190ba..3eb4b53 100644 --- a/lib/puppet/parser/functions/delete_at.rb +++ b/lib/puppet/parser/functions/delete_at.rb @@ -4,6 +4,13 @@ module Puppet::Parser::Functions newfunction(:delete_at, :type => :rvalue, :doc => <<-EOS +Deletes a determined indexed value from an array. + +*Examples:* + + delete_at(['a','b','c'], 1) + +Would return: ['a','c'] EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/downcase.rb b/lib/puppet/parser/functions/downcase.rb index 71f8480..4066d21 100644 --- a/lib/puppet/parser/functions/downcase.rb +++ b/lib/puppet/parser/functions/downcase.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:downcase, :type => :rvalue, :doc => <<-EOS +Converts the case of a string or all strings in an array to lower case. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/empty.rb b/lib/puppet/parser/functions/empty.rb index e78ebf0..80ebb86 100644 --- a/lib/puppet/parser/functions/empty.rb +++ b/lib/puppet/parser/functions/empty.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:empty, :type => :rvalue, :doc => <<-EOS +Returns true if the variable is empty. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/flatten.rb b/lib/puppet/parser/functions/flatten.rb index 6036f72..781da78 100644 --- a/lib/puppet/parser/functions/flatten.rb +++ b/lib/puppet/parser/functions/flatten.rb @@ -4,6 +4,14 @@ module Puppet::Parser::Functions newfunction(:flatten, :type => :rvalue, :doc => <<-EOS +This function flattens any deeply nested arrays and returns a single flat array +as a result. + +*Examples:* + + flatten(['a', ['b', ['c']]]) + +Would return: ['a','b','c'] EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/grep.rb b/lib/puppet/parser/functions/grep.rb index 2caaa6f..ceba9ec 100644 --- a/lib/puppet/parser/functions/grep.rb +++ b/lib/puppet/parser/functions/grep.rb @@ -4,6 +4,16 @@ module Puppet::Parser::Functions newfunction(:grep, :type => :rvalue, :doc => <<-EOS +This function searches through an array and returns any elements that match +the provided regular expression. + +*Examples:* + + grep(['aaa','bbb','ccc','aaaddd'], 'aaa') + +Would return: + + ['aaa','aaaddd'] EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/hash.rb b/lib/puppet/parser/functions/hash.rb index f0c01d4..453ba1e 100644 --- a/lib/puppet/parser/functions/hash.rb +++ b/lib/puppet/parser/functions/hash.rb @@ -4,6 +4,13 @@ module Puppet::Parser::Functions newfunction(:hash, :type => :rvalue, :doc => <<-EOS +This function converts and array into a hash. + +*Examples:* + + hash(['a',1,'b',2,'c',3]) + +Would return: {'a'=>1,'b'=>2,'c'=>3} EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_array.rb b/lib/puppet/parser/functions/is_array.rb index 0b508fd..b39e184 100644 --- a/lib/puppet/parser/functions/is_array.rb +++ b/lib/puppet/parser/functions/is_array.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_array, :type => :rvalue, :doc => <<-EOS +Returns true if the variable passed to this function is an array. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_float.rb b/lib/puppet/parser/functions/is_float.rb index 8aed848..2fc05ba 100644 --- a/lib/puppet/parser/functions/is_float.rb +++ b/lib/puppet/parser/functions/is_float.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_float, :type => :rvalue, :doc => <<-EOS +Returns true if the variable passed to this function is a float. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_hash.rb b/lib/puppet/parser/functions/is_hash.rb index 000306e..ad907f0 100644 --- a/lib/puppet/parser/functions/is_hash.rb +++ b/lib/puppet/parser/functions/is_hash.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_hash, :type => :rvalue, :doc => <<-EOS +Returns true if the variable passed to this function is a hash. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_integer.rb b/lib/puppet/parser/functions/is_integer.rb index 9dd1cee..8ee34f6 100644 --- a/lib/puppet/parser/functions/is_integer.rb +++ b/lib/puppet/parser/functions/is_integer.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_integer, :type => :rvalue, :doc => <<-EOS +Returns true if the variable returned to this string is an integer. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_numeric.rb b/lib/puppet/parser/functions/is_numeric.rb index c2c0fb5..ce13ece 100644 --- a/lib/puppet/parser/functions/is_numeric.rb +++ b/lib/puppet/parser/functions/is_numeric.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_numeric, :type => :rvalue, :doc => <<-EOS +Returns true if the variable passed to this function is a number. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_string.rb b/lib/puppet/parser/functions/is_string.rb index 8a02a10..f5bef04 100644 --- a/lib/puppet/parser/functions/is_string.rb +++ b/lib/puppet/parser/functions/is_string.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_string, :type => :rvalue, :doc => <<-EOS +Returns true if the variable passed to this function is a string. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_valid_domain_name.rb b/lib/puppet/parser/functions/is_valid_domain_name.rb index 99d6f86..7dd9c0b 100644 --- a/lib/puppet/parser/functions/is_valid_domain_name.rb +++ b/lib/puppet/parser/functions/is_valid_domain_name.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_valid_domain_name, :type => :rvalue, :doc => <<-EOS +Returns true if the string passed to this function is a valid IP address. Support for IPv4 and IPv6 address types is included. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_valid_ip_address.rb b/lib/puppet/parser/functions/is_valid_ip_address.rb index 4f45890..604d938 100644 --- a/lib/puppet/parser/functions/is_valid_ip_address.rb +++ b/lib/puppet/parser/functions/is_valid_ip_address.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_valid_ip_address, :type => :rvalue, :doc => <<-EOS +Returns true if the string passed to this function is a valid IP address. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_valid_mac_address.rb b/lib/puppet/parser/functions/is_valid_mac_address.rb index a00d874..53199b6 100644 --- a/lib/puppet/parser/functions/is_valid_mac_address.rb +++ b/lib/puppet/parser/functions/is_valid_mac_address.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_valid_mac_address, :type => :rvalue, :doc => <<-EOS +Returns true if the string passed to this function is a valid mac address. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/join.rb b/lib/puppet/parser/functions/join.rb index 945556a..005a46e 100644 --- a/lib/puppet/parser/functions/join.rb +++ b/lib/puppet/parser/functions/join.rb @@ -4,6 +4,13 @@ module Puppet::Parser::Functions newfunction(:join, :type => :rvalue, :doc => <<-EOS +This function joins an array into a string using a seperator. + +*Examples:* + + join(['a','b','c'], ",") + +Would result in: "a,b,c" EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/keys.rb b/lib/puppet/parser/functions/keys.rb index 3a92a47..f0d13b6 100644 --- a/lib/puppet/parser/functions/keys.rb +++ b/lib/puppet/parser/functions/keys.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:keys, :type => :rvalue, :doc => <<-EOS +Returns the keys of a hash as an array. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/load_json.rb b/lib/puppet/parser/functions/load_json.rb index 7c3f187..333cf11 100644 --- a/lib/puppet/parser/functions/load_json.rb +++ b/lib/puppet/parser/functions/load_json.rb @@ -4,6 +4,8 @@ module Puppet::Parser::Functions newfunction(:load_json, :type => :rvalue, :doc => <<-EOS +This function accepts JSON as a string and converts into the correct Puppet +structure. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/load_yaml.rb b/lib/puppet/parser/functions/load_yaml.rb index 1bc2f36..2683277 100644 --- a/lib/puppet/parser/functions/load_yaml.rb +++ b/lib/puppet/parser/functions/load_yaml.rb @@ -4,6 +4,8 @@ module Puppet::Parser::Functions newfunction(:load_yaml, :type => :rvalue, :doc => <<-EOS +This function accepts YAML as a string and converts it into the correct +Puppet structure. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/lstrip.rb b/lib/puppet/parser/functions/lstrip.rb index a7b2656..3a64de3 100644 --- a/lib/puppet/parser/functions/lstrip.rb +++ b/lib/puppet/parser/functions/lstrip.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:lstrip, :type => :rvalue, :doc => <<-EOS +Strips leading spaces to the left of a string. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/member.rb b/lib/puppet/parser/functions/member.rb index bb43a37..43d76af 100644 --- a/lib/puppet/parser/functions/member.rb +++ b/lib/puppet/parser/functions/member.rb @@ -7,6 +7,17 @@ module Puppet::Parser::Functions newfunction(:member, :type => :rvalue, :doc => <<-EOS +This function determines if a variable is a member of an array. + +*Examples:* + + member(['a','b'], 'b') + +Would return: true + + member(['a','b'], 'c') + +Would return: false EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/type.rb b/lib/puppet/parser/functions/type.rb index de6087e..8d85f11 100644 --- a/lib/puppet/parser/functions/type.rb +++ b/lib/puppet/parser/functions/type.rb @@ -11,6 +11,7 @@ Returns the type when passed a variable. Type can be one of: * hash * float * integer +* boolean EOS ) do |arguments| @@ -21,7 +22,7 @@ Returns the type when passed a variable. Type can be one of: klass = value.class - if not [Array, Bignum, Fixnum, Float, Hash, String].include?(klass) + if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass) raise(Puppet::ParseError, 'type(): Unknown type') end @@ -30,6 +31,7 @@ Returns the type when passed a variable. Type can be one of: # 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 -- cgit v1.2.3 From 35fefe186546427963d8c8a446fa98875d65ccad Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Sat, 30 Jul 2011 00:44:02 +0100 Subject: Fix some ruby 1.9.2 issues. --- lib/puppet/parser/functions/sort.rb | 2 +- lib/puppet/parser/functions/values_at.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb index 3785496..cefbe54 100644 --- a/lib/puppet/parser/functions/sort.rb +++ b/lib/puppet/parser/functions/sort.rb @@ -18,7 +18,7 @@ Sorts strings and arrays lexically. if value.is_a?(Array) then value.sort elsif value.is_a?(String) then - value.split("").sort.to_s + value.split("").sort.join("") end end diff --git a/lib/puppet/parser/functions/values_at.rb b/lib/puppet/parser/functions/values_at.rb index 7f1de8e..d3e69d9 100644 --- a/lib/puppet/parser/functions/values_at.rb +++ b/lib/puppet/parser/functions/values_at.rb @@ -38,7 +38,7 @@ Would return ['a','c','d']. raise(Puppet::ParseError, 'values_at(): Requires array to work with') end - indices = *arguments # Get them all ... Pokemon ... + indices = [arguments.shift].flatten() # Get them all ... Pokemon ... if not indices or indices.empty? raise(Puppet::ParseError, 'values_at(): You must provide ' + -- cgit v1.2.3 From 681a1c7971d78c53dc9a0747ae4d983ff6b0d670 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 5 Aug 2011 08:25:03 +0100 Subject: Prep for stdlib merge * Renamed load_yaml & load_json to parseyaml & parsejson * Renamed is_valid_* functions and remove the 'valid_' --- lib/puppet/parser/functions/is_domain_name.rb | 27 ++++++++++++++++++ lib/puppet/parser/functions/is_ip_address.rb | 32 ++++++++++++++++++++++ lib/puppet/parser/functions/is_mac_address.rb | 27 ++++++++++++++++++ .../parser/functions/is_valid_domain_name.rb | 27 ------------------ lib/puppet/parser/functions/is_valid_ip_address.rb | 32 ---------------------- .../parser/functions/is_valid_mac_address.rb | 27 ------------------ lib/puppet/parser/functions/load_json.rb | 26 ------------------ lib/puppet/parser/functions/load_yaml.rb | 24 ---------------- lib/puppet/parser/functions/parsejson.rb | 26 ++++++++++++++++++ lib/puppet/parser/functions/parseyaml.rb | 24 ++++++++++++++++ 10 files changed, 136 insertions(+), 136 deletions(-) create mode 100644 lib/puppet/parser/functions/is_domain_name.rb create mode 100644 lib/puppet/parser/functions/is_ip_address.rb create mode 100644 lib/puppet/parser/functions/is_mac_address.rb delete mode 100644 lib/puppet/parser/functions/is_valid_domain_name.rb delete mode 100644 lib/puppet/parser/functions/is_valid_ip_address.rb delete mode 100644 lib/puppet/parser/functions/is_valid_mac_address.rb delete mode 100644 lib/puppet/parser/functions/load_json.rb delete mode 100644 lib/puppet/parser/functions/load_yaml.rb create mode 100644 lib/puppet/parser/functions/parsejson.rb create mode 100644 lib/puppet/parser/functions/parseyaml.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/is_domain_name.rb b/lib/puppet/parser/functions/is_domain_name.rb new file mode 100644 index 0000000..4e92939 --- /dev/null +++ b/lib/puppet/parser/functions/is_domain_name.rb @@ -0,0 +1,27 @@ +# +# is_domain_name.rb +# + +module Puppet::Parser::Functions + newfunction(:is_domain_name, :type => :rvalue, :doc => <<-EOS +Returns true if the string passed to this function is a valid IP address. Support for IPv4 and IPv6 address types is included. + EOS + ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_domain_name(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + domain = arguments[0] + + if domain =~ /^(([a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\.?$/ then + return true + else + return false + end + + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_ip_address.rb b/lib/puppet/parser/functions/is_ip_address.rb new file mode 100644 index 0000000..b4a9a15 --- /dev/null +++ b/lib/puppet/parser/functions/is_ip_address.rb @@ -0,0 +1,32 @@ +# +# is_ip_address.rb +# + +module Puppet::Parser::Functions + newfunction(:is_ip_address, :type => :rvalue, :doc => <<-EOS +Returns true if the string passed to this function is a valid IP address. + EOS + ) do |arguments| + + require 'ipaddr' + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_ip_address(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + begin + ip = IPAddr.new(arguments[0]) + rescue ArgumentError + return false + end + + if ip.ipv4? or ip.ipv6? then + return true + else + return false + end + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_mac_address.rb b/lib/puppet/parser/functions/is_mac_address.rb new file mode 100644 index 0000000..1b3088a --- /dev/null +++ b/lib/puppet/parser/functions/is_mac_address.rb @@ -0,0 +1,27 @@ +# +# is_mac_address.rb +# + +module Puppet::Parser::Functions + newfunction(:is_mac_address, :type => :rvalue, :doc => <<-EOS +Returns true if the string passed to this function is a valid mac address. + EOS + ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_mac_address(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + mac = arguments[0] + + if /^[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}$/.match(mac) then + return true + else + return false + end + + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_domain_name.rb b/lib/puppet/parser/functions/is_valid_domain_name.rb deleted file mode 100644 index 7dd9c0b..0000000 --- a/lib/puppet/parser/functions/is_valid_domain_name.rb +++ /dev/null @@ -1,27 +0,0 @@ -# -# is_valid_domain_name.rb -# - -module Puppet::Parser::Functions - newfunction(:is_valid_domain_name, :type => :rvalue, :doc => <<-EOS -Returns true if the string passed to this function is a valid IP address. Support for IPv4 and IPv6 address types is included. - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "is_valid_domain_name(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - domain = arguments[0] - - if domain =~ /^(([a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\.?$/ then - return true - else - return false - end - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_ip_address.rb b/lib/puppet/parser/functions/is_valid_ip_address.rb deleted file mode 100644 index 604d938..0000000 --- a/lib/puppet/parser/functions/is_valid_ip_address.rb +++ /dev/null @@ -1,32 +0,0 @@ -# -# is_valid_ip_address.rb -# - -module Puppet::Parser::Functions - newfunction(:is_valid_ip_address, :type => :rvalue, :doc => <<-EOS -Returns true if the string passed to this function is a valid IP address. - EOS - ) do |arguments| - - require 'ipaddr' - - if (arguments.size != 1) then - raise(Puppet::ParseError, "is_valid_ip_address(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - begin - ip = IPAddr.new(arguments[0]) - rescue ArgumentError - return false - end - - if ip.ipv4? or ip.ipv6? then - return true - else - return false - end - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_mac_address.rb b/lib/puppet/parser/functions/is_valid_mac_address.rb deleted file mode 100644 index 53199b6..0000000 --- a/lib/puppet/parser/functions/is_valid_mac_address.rb +++ /dev/null @@ -1,27 +0,0 @@ -# -# is_valid_mac_address.rb -# - -module Puppet::Parser::Functions - newfunction(:is_valid_mac_address, :type => :rvalue, :doc => <<-EOS -Returns true if the string passed to this function is a valid mac address. - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "is_valid_mac_address(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - mac = arguments[0] - - if /^[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}$/.match(mac) then - return true - else - return false - end - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/load_json.rb b/lib/puppet/parser/functions/load_json.rb deleted file mode 100644 index 333cf11..0000000 --- a/lib/puppet/parser/functions/load_json.rb +++ /dev/null @@ -1,26 +0,0 @@ -# -# load_json.rb -# - -module Puppet::Parser::Functions - newfunction(:load_json, :type => :rvalue, :doc => <<-EOS -This function accepts JSON as a string and converts into the correct Puppet -structure. - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "load_json(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - json = arguments[0] - - require 'json' - - JSON.load(json) - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/load_yaml.rb b/lib/puppet/parser/functions/load_yaml.rb deleted file mode 100644 index 2683277..0000000 --- a/lib/puppet/parser/functions/load_yaml.rb +++ /dev/null @@ -1,24 +0,0 @@ -# -# load_yaml.rb -# - -module Puppet::Parser::Functions - newfunction(:load_yaml, :type => :rvalue, :doc => <<-EOS -This function accepts YAML as a string and converts it into the correct -Puppet structure. - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "load_yaml(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - require 'yaml' - - YAML::load(arguments[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 new file mode 100644 index 0000000..cf3b12c --- /dev/null +++ b/lib/puppet/parser/functions/parsejson.rb @@ -0,0 +1,26 @@ +# +# parsejson.rb +# + +module Puppet::Parser::Functions + newfunction(:parsejson, :type => :rvalue, :doc => <<-EOS +This function accepts JSON as a string and converts into the correct Puppet +structure. + EOS + ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "parsejson(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + json = arguments[0] + + require 'json' + + JSON.load(json) + + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/parseyaml.rb b/lib/puppet/parser/functions/parseyaml.rb new file mode 100644 index 0000000..e8ac8a4 --- /dev/null +++ b/lib/puppet/parser/functions/parseyaml.rb @@ -0,0 +1,24 @@ +# +# parseyaml.rb +# + +module Puppet::Parser::Functions + newfunction(:parseyaml, :type => :rvalue, :doc => <<-EOS +This function accepts YAML as a string and converts it into the correct +Puppet structure. + EOS + ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "parseyaml(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + require 'yaml' + + YAML::load(arguments[0]) + + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 1b73a66fc67af0e33fa41aacf50654d4a7a4903c Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 5 Aug 2011 08:46:38 +0100 Subject: * Moved kwalify to puppetlabs-kwalify project * Re-arranged tests in line with puppetlabs-stdlib --- lib/puppet/parser/functions/kwalify.rb | 35 -------------------- lib/puppet/parser/functions/validate_resource.rb | 41 ------------------------ 2 files changed, 76 deletions(-) delete mode 100644 lib/puppet/parser/functions/kwalify.rb delete mode 100644 lib/puppet/parser/functions/validate_resource.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/kwalify.rb b/lib/puppet/parser/functions/kwalify.rb deleted file mode 100644 index 49b9aeb..0000000 --- a/lib/puppet/parser/functions/kwalify.rb +++ /dev/null @@ -1,35 +0,0 @@ -# -# kwalify.rb -# - -module Puppet::Parser::Functions - newfunction(:kwalify, :type => :statement, :doc => <<-EOS -This function uses kwalify to validate Puppet data structures against Kwalify -schemas. - EOS - ) do |args| - - raise(Puppet::ParseError, "kwalify(): Wrong number of arguments " + - "given (#{args.size} for 2)") if args.size != 2 - - schema = args[0] - document = args[1] - - require 'kwalify' - - validator = Kwalify::Validator.new(schema) - - errors = validator.validate(document) - - if errors && !errors.empty? - error_out = [] - for e in errors - error_out << "[#{e.path}] #{e.message}" - end - raise(Puppet::ParseError, "Failed kwalify schema validation:\n" + error_out.join("\n")) - end - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/validate_resource.rb b/lib/puppet/parser/functions/validate_resource.rb deleted file mode 100644 index d71ef3f..0000000 --- a/lib/puppet/parser/functions/validate_resource.rb +++ /dev/null @@ -1,41 +0,0 @@ -# -# validate_resource -# - -module Puppet::Parser::Functions - newfunction(:validate_resource, :type => :statement, :doc => <<-EOS -This function when placed at the beginning of a class, will go looking for a -valid kwalify schema by replacing the extension of the file with '.schema'. - -It will then validate the arguments passed to the function using that kwalify -schema. - EOS - ) do |arguments| - - require 'kwalify' - - if (arguments.size != 0) then - raise(Puppet::ParseError, "validate_resource(): Wrong number of arguments "+ - "given #{arguments.size} for 0") - end - - - classhash = to_hash(recursive=false) - sourcepath = source.file - schemapath = sourcepath.gsub(/\.(rb|pp)$/, ".schema") - schema = Kwalify::Yaml.load_file(schemapath) - validator = Kwalify::Validator.new(schema) - errors = validator.validate(classhash) - - if errors && !errors.empty? - error_output = "Resource validation failed:\n" - for e in errors - error_output += "[#{e.path}] #{e.message}\n" - end - raise(Puppet::ParseError, error_output) - end - - end -end - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3