From 190b9438c58eab2322bdf216be1aaae109d14072 Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 3 Jan 2013 13:53:03 +0000 Subject: Add test/validation for is_numeric if created from an arithmetical operation --- lib/puppet/parser/functions/is_numeric.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/is_numeric.rb b/lib/puppet/parser/functions/is_numeric.rb index ce13ece..abf0321 100644 --- a/lib/puppet/parser/functions/is_numeric.rb +++ b/lib/puppet/parser/functions/is_numeric.rb @@ -15,7 +15,7 @@ Returns true if the variable passed to this function is a number. value = arguments[0] - if value == value.to_f.to_s or value == value.to_i.to_s then + if value == value.to_f.to_s or value == value.to_i.to_s or value.is_a? Numeric then return true else return false -- cgit v1.2.3 From b86f5dc1293ad431581afbb8f294560f3cf34d43 Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 3 Jan 2013 14:02:58 +0000 Subject: Add test/validation for is_integer if created from an arithmetical operation --- lib/puppet/parser/functions/is_integer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/is_integer.rb b/lib/puppet/parser/functions/is_integer.rb index 8ee34f6..6b29e98 100644 --- a/lib/puppet/parser/functions/is_integer.rb +++ b/lib/puppet/parser/functions/is_integer.rb @@ -15,7 +15,7 @@ Returns true if the variable returned to this string is an integer. value = arguments[0] - if value != value.to_i.to_s then + if value != value.to_i.to_s and !value.is_a? Fixnum then return false else return true -- cgit v1.2.3 From a773281760469f2b104c89b2bfb760c0e3013422 Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 3 Jan 2013 14:05:29 +0000 Subject: Add test/validation for is_float if created from an arithmetical operation --- lib/puppet/parser/functions/is_float.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/is_float.rb b/lib/puppet/parser/functions/is_float.rb index 2fc05ba..911f3c2 100644 --- a/lib/puppet/parser/functions/is_float.rb +++ b/lib/puppet/parser/functions/is_float.rb @@ -15,7 +15,7 @@ Returns true if the variable passed to this function is a float. value = arguments[0] - if value != value.to_f.to_s then + if value != value.to_f.to_s and !value.is_a? Float then return false else return true -- cgit v1.2.3 From 20e0e0709021b7ba4a819f1324526d98bcb5dcc3 Mon Sep 17 00:00:00 2001 From: Jaka Hudoklin Date: Wed, 2 Jan 2013 12:10:43 +0000 Subject: Add getparam function to get defined resource parameters As far as i know there's no other puppet-dsl-like way to get parameter of defined resource, so that's why i implemented getparam function, which takes resource reference and parameter name and returns parameter value. Here's another example why this function is really useful: define config($path, $config_param1, $config_param2) { } define example_resource($config) { $path = getparam($config, "path") notice("Path is $path") } define example_resource2($example_resource, $config = getparam($example_resource, "config")) { $config_param1 = getparam($config, "config_param1") notice("Config parameter is $config_param1") } define example_resource3($example_resource, $config = getparam($example_resource, "config")) { $config_param2 = getparam($config, "config_param2") notice("Config parameter is $config_param2") } class test_getparam { config { "config_instance": path => "/some/config/path", config_param1 => "someconfigtext1", config_param2 => "someconfigtext2", } example_resource { "example_resource_instance": config => Config["config_instance"] } example_resource2 { "example_resource_instance": example_resource => Example_resource["example_resource_instance"] } example_resource3 { "example_resource_instance": example_resource => Example_resource2["example_resource_instance"] } } class { "test_getparam": } --- lib/puppet/parser/functions/getparam.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lib/puppet/parser/functions/getparam.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/getparam.rb b/lib/puppet/parser/functions/getparam.rb new file mode 100644 index 0000000..0962656 --- /dev/null +++ b/lib/puppet/parser/functions/getparam.rb @@ -0,0 +1,33 @@ +# Test whether a given class or definition is defined +require 'puppet/parser/functions' + +Puppet::Parser::Functions.newfunction(:getparam, + :type => :rvalue, + :doc => <<-'ENDOFDOC' +Takes a resource reference and name of the parameter and +returns value of resource's parameter. + +*Examples:* + + define example_resource($param) { + } + + example_resource { "example_resource_instance": + param => "param_value" + } + + getparam(Example_resource["example_resource_instance"], "param") + +Would return: param_value +ENDOFDOC +) do |vals| + reference, param = vals + raise(ArgumentError, 'Must specify a reference') unless reference + raise(ArgumentError, 'Must specify name of a parameter') unless param and param.instance_of? String + + if resource = findresource(reference.to_s) + return resource[param] if resource[param] + end + + return '' +end -- cgit v1.2.3 From 2a28ece233262e2c94f8f608bf900b7801c06c9a Mon Sep 17 00:00:00 2001 From: Jeff McCune Date: Mon, 14 Jan 2013 18:37:16 -0800 Subject: (maint) Fix getparam() spec failure on MRI 1.8 Without this patch applied we're getting the following spec failure, but only in the MRI 1.8 matrix cells. Failures: 1) getparam when compared against a resource with params Failure/Error: should run.with_params('User[dan]', '').and_return('') ArgumentError: interning empty string # ./vendor/ruby/1.8/gems/puppet-3.0.2/lib/puppet/parser/resource.rb:42:in `intern' # ./vendor/ruby/1.8/gems/puppet-3.0.2/lib/puppet/parser/resource.rb:42:in `[]' # ./lib/puppet/parser/functions/getparam.rb:29:in `real_function_getparam' # ./vendor/ruby/1.8/gems/puppet-3.0.2/lib/puppet/parser/functions.rb:63:in `send' # ./vendor/ruby/1.8/gems/puppet-3.0.2/lib/puppet/parser/functions.rb:63:in `function_getparam' # ./vendor/ruby/1.8/gems/rspec-puppet-0.1.5/lib/rspec-puppet/matchers/run.rb:8:in `call' # ./vendor/ruby/1.8/gems/rspec-puppet-0.1.5/lib/rspec-puppet/matchers/run.rb:8 # ./vendor/ruby/1.8/gems/rspec-puppet-0.1.5/lib/rspec-puppet/matchers/run.rb:24:in `call' # ./vendor/ruby/1.8/gems/rspec-puppet-0.1.5/lib/rspec-puppet/matchers/run.rb:24 # ./vendor/ruby/1.8/gems/rspec-expectations-2.11.3/lib/rspec/matchers/extensions/instance_eval_with_args.rb:11:in `instance_exec' # ./vendor/ruby/1.8/gems/rspec-expectations-2.11.3/lib/rspec/matchers/extensions/instance_eval_with_args.rb:11:in `instance_eval_with_args' # ./vendor/ruby/1.8/gems/rspec-expectations-2.11.3/lib/rspec/matchers/matcher.rb:60:in `matches?' # ./vendor/ruby/1.8/gems/rspec-expectations-2.11.3/lib/rspec/expectations/handler.rb:9:in `handle_matcher' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/subject.rb:64:in `should' # ./spec/functions/getparam_spec.rb:29 # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example.rb:113:in `instance_eval' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example.rb:113:in `run' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example.rb:253:in `with_around_each_hooks' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example.rb:110:in `run' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:378:in `run_examples' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:374:in `map' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:374:in `run_examples' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:360:in `run' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:361:in `run' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:361:in `map' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:361:in `run' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:28:in `run' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:28:in `map' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:28:in `run' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/reporter.rb:34:in `report' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:25:in `run' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run' # ./vendor/ruby/1.8/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `autorun' # ./vendor/ruby/1.8/bin/rspec:23 This patch addresses the problem by explicitly returning an empty string if the string itself is empty. This avoids trying to convert an empty string to a symbol which is the root cause of the problem. --- lib/puppet/parser/functions/getparam.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/getparam.rb b/lib/puppet/parser/functions/getparam.rb index 0962656..6d51006 100644 --- a/lib/puppet/parser/functions/getparam.rb +++ b/lib/puppet/parser/functions/getparam.rb @@ -25,6 +25,8 @@ ENDOFDOC raise(ArgumentError, 'Must specify a reference') unless reference raise(ArgumentError, 'Must specify name of a parameter') unless param and param.instance_of? String + return '' if param.empty? + if resource = findresource(reference.to_s) return resource[param] if resource[param] end -- cgit v1.2.3 From 6902cc582eef6eb59cd5252208eca5ac608995bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Thu, 6 Dec 2012 11:01:19 +0100 Subject: Add validate_cmd function --- lib/puppet/parser/functions/validate_cmd.rb | 41 +++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 lib/puppet/parser/functions/validate_cmd.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb new file mode 100644 index 0000000..e7793c3 --- /dev/null +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -0,0 +1,41 @@ +module Puppet::Parser::Functions + newfunction(:validate_cmd, :doc => <<-'ENDHEREDOC') do |args| + Perform validation of a string with an external command. + The first argument of this function should be a string to + test, and the second argument should be a path to a test command + taking a file as last argument. If the command, launched against + a tempfile containing the passed string, returns a non-null value, + compilation will abort with a parse error. + + If a third argument is specified, this will be the error message raised and + seen by the user. + + A helpful error message can be returned like this: + + Example: + + validate_cmd($sudoerscontent, '/usr/sbin/visudo -c -f', 'Visudo failed to validate sudoers content') + + ENDHEREDOC + if (args.length < 2) or (args.length > 3) then + raise Puppet::ParseError, ("validate_cmd(): wrong number of arguments (#{args.length}; must be 2 or 3)") + end + + msg = args[2] || "validate_cmd(): failed to validate content with command #{args[1].inspect}" + + content = args[0] + checkscript = args[1] + + # Test content in a temporary file + tmpfile = Tempfile.new("validate_cmd") + tmpfile.write(content) + tmpfile.close + output = `#{checkscript} #{tmpfile.path} 2>&1 1>/dev/null` + r = $? + File.delete(tmpfile.path) + if output + msg += "\nOutput is:\n#{output}" + end + raise Puppet::ParseError, (msg) unless r == 0 + end +end -- cgit v1.2.3 From bda25ac0872a101b59d8ab473218ff0f14bb7433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Fri, 18 Jan 2013 21:29:29 +0100 Subject: validate_cmd: Make sure tmpfile is always closed and unlinked --- lib/puppet/parser/functions/validate_cmd.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index e7793c3..00fe1ae 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -28,11 +28,14 @@ module Puppet::Parser::Functions # Test content in a temporary file tmpfile = Tempfile.new("validate_cmd") - tmpfile.write(content) - tmpfile.close - output = `#{checkscript} #{tmpfile.path} 2>&1 1>/dev/null` - r = $? - File.delete(tmpfile.path) + begin + tmpfile.write(content) + output = `#{checkscript} #{tmpfile.path} 2>&1 1>/dev/null` + r = $? + ensure + tmpfile.close + tmpfile.unlink + end if output msg += "\nOutput is:\n#{output}" end -- cgit v1.2.3 From 3a97c2314c67245be3190cc485cff63e40a833fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Thu, 6 Dec 2012 11:33:43 +0100 Subject: Add validate_augeas function --- lib/puppet/parser/functions/validate_augeas.rb | 70 ++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 lib/puppet/parser/functions/validate_augeas.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/validate_augeas.rb b/lib/puppet/parser/functions/validate_augeas.rb new file mode 100644 index 0000000..01a2e91 --- /dev/null +++ b/lib/puppet/parser/functions/validate_augeas.rb @@ -0,0 +1,70 @@ +module Puppet::Parser::Functions + newfunction(:validate_augeas, :doc => <<-'ENDHEREDOC') do |args| + Perform validation of a string using an Augeas lens + The first argument of this function should be a string to + test, and the second argument should be the name of the Augeas lens to use. + If Augeas fails to parse the string with the lens, the compilation will + abort with a parse error. + + A third argument can be specified, listing paths which should + not be found in the file. The `$file` variable points to the location + of the temporary file being tested in the Augeas tree. + + For example, if you want to make sure your passwd content never contains + a user `foo`, you could write: + + validate_augeas($passwdcontent, 'Passwd.lns', ['$file/foo']) + + Or if you wanted to ensure that no users used the '/bin/barsh' shell, + you could use: + + validate_augeas($passwdcontent, 'Passwd.lns', ['$file/*[shell="/bin/barsh"]'] + + If a fourth argument is specified, this will be the error message raised and + seen by the user. + + A helpful error message can be returned like this: + + validate_augeas($sudoerscontent, 'Sudoers.lns', [], 'Failed to validate sudoers content with Augeas') + + ENDHEREDOC + if (args.length < 2) or (args.length > 4) then + raise Puppet::ParseError, ("validate_augeas(): wrong number of arguments (#{args.length}; must be 2, 3, or 4)") + end + + msg = args[3] || "validate_augeas(): Failed to validate content against #{args[1].inspect}" + + require 'augeas' + aug = Augeas::open(nil, nil, Augeas::NO_MODL_AUTOLOAD) + + content = args[0] + + # Test content in a temporary file + tmpfile = Tempfile.new("validate_augeas") + tmpfile.write(content) + tmpfile.close + + # Check for syntax + lens = args[1] + aug.transform( + :lens => lens, + :name => 'Validate_augeas', + :incl => tmpfile.path + ) + aug.load! + + unless aug.match("/augeas/files#{tmpfile.path}//error").empty? + error = aug.get("/augeas/files#{tmpfile.path}//error/message") + msg += " with error: #{error}" + raise Puppet::ParseError, (msg) + end + + # Launch unit tests + tests = args[2] || [] + aug.defvar('file', "/files#{tmpfile.path}") + tests.each do |t| + msg += " testing path #{t}" + raise Puppet::ParseError, (msg) unless aug.match(t).empty? + end + end +end -- cgit v1.2.3 From 41bc722139028929b9ab1f9a14b318d1a23206f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Fri, 18 Jan 2013 21:42:54 +0100 Subject: validate_augeas: Ensure augeas handler gets closed --- lib/puppet/parser/functions/validate_augeas.rb | 55 ++++++++++++++------------ 1 file changed, 29 insertions(+), 26 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/validate_augeas.rb b/lib/puppet/parser/functions/validate_augeas.rb index 01a2e91..273b954 100644 --- a/lib/puppet/parser/functions/validate_augeas.rb +++ b/lib/puppet/parser/functions/validate_augeas.rb @@ -17,7 +17,7 @@ module Puppet::Parser::Functions Or if you wanted to ensure that no users used the '/bin/barsh' shell, you could use: - + validate_augeas($passwdcontent, 'Passwd.lns', ['$file/*[shell="/bin/barsh"]'] If a fourth argument is specified, this will be the error message raised and @@ -36,35 +36,38 @@ module Puppet::Parser::Functions require 'augeas' aug = Augeas::open(nil, nil, Augeas::NO_MODL_AUTOLOAD) + begin + content = args[0] - content = args[0] - - # Test content in a temporary file - tmpfile = Tempfile.new("validate_augeas") - tmpfile.write(content) - tmpfile.close + # Test content in a temporary file + tmpfile = Tempfile.new("validate_augeas") + tmpfile.write(content) + tmpfile.close - # Check for syntax - lens = args[1] - aug.transform( - :lens => lens, - :name => 'Validate_augeas', - :incl => tmpfile.path - ) - aug.load! + # Check for syntax + lens = args[1] + aug.transform( + :lens => lens, + :name => 'Validate_augeas', + :incl => tmpfile.path + ) + aug.load! - unless aug.match("/augeas/files#{tmpfile.path}//error").empty? - error = aug.get("/augeas/files#{tmpfile.path}//error/message") - msg += " with error: #{error}" - raise Puppet::ParseError, (msg) - end + unless aug.match("/augeas/files#{tmpfile.path}//error").empty? + error = aug.get("/augeas/files#{tmpfile.path}//error/message") + msg += " with error: #{error}" + raise Puppet::ParseError, (msg) + end - # Launch unit tests - tests = args[2] || [] - aug.defvar('file', "/files#{tmpfile.path}") - tests.each do |t| - msg += " testing path #{t}" - raise Puppet::ParseError, (msg) unless aug.match(t).empty? + # Launch unit tests + tests = args[2] || [] + aug.defvar('file', "/files#{tmpfile.path}") + tests.each do |t| + msg += " testing path #{t}" + raise Puppet::ParseError, (msg) unless aug.match(t).empty? + end + ensure + aug.close end end end -- cgit v1.2.3 From d568c4e0f7a6323cf462045159e8203b4715e196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Fri, 18 Jan 2013 21:54:35 +0100 Subject: validate_augeas: Ensure tmpfile is closed and unlinked --- lib/puppet/parser/functions/validate_augeas.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/validate_augeas.rb b/lib/puppet/parser/functions/validate_augeas.rb index 273b954..a407118 100644 --- a/lib/puppet/parser/functions/validate_augeas.rb +++ b/lib/puppet/parser/functions/validate_augeas.rb @@ -41,8 +41,11 @@ module Puppet::Parser::Functions # Test content in a temporary file tmpfile = Tempfile.new("validate_augeas") - tmpfile.write(content) - tmpfile.close + begin + tmpfile.write(content) + ensure + tmpfile.close + end # Check for syntax lens = args[1] @@ -68,6 +71,7 @@ module Puppet::Parser::Functions end ensure aug.close + tmpfile.unlink end end end -- cgit v1.2.3 From 35f9a01879858fcbd0c93c1fd90ff71365a27d9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Fri, 18 Jan 2013 21:59:47 +0100 Subject: validate_augeas: requires augeas --- lib/puppet/parser/functions/validate_augeas.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/validate_augeas.rb b/lib/puppet/parser/functions/validate_augeas.rb index a407118..d66e340 100644 --- a/lib/puppet/parser/functions/validate_augeas.rb +++ b/lib/puppet/parser/functions/validate_augeas.rb @@ -28,6 +28,10 @@ module Puppet::Parser::Functions validate_augeas($sudoerscontent, 'Sudoers.lns', [], 'Failed to validate sudoers content with Augeas') ENDHEREDOC + unless Puppet.features.augeas? + raise Puppet::ParseError, ("validate_augeas(): requires the ruby augeas bindings") + end + if (args.length < 2) or (args.length > 4) then raise Puppet::ParseError, ("validate_augeas(): wrong number of arguments (#{args.length}; must be 2, 3, or 4)") end -- cgit v1.2.3 From c5f0309b1d3806a6caab9160033653cd40634894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Sun, 20 Jan 2013 13:15:22 +0100 Subject: Add an URL to a doc on how to activate augeas in puppet --- lib/puppet/parser/functions/validate_augeas.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/validate_augeas.rb b/lib/puppet/parser/functions/validate_augeas.rb index d66e340..154d660 100644 --- a/lib/puppet/parser/functions/validate_augeas.rb +++ b/lib/puppet/parser/functions/validate_augeas.rb @@ -29,7 +29,7 @@ module Puppet::Parser::Functions ENDHEREDOC unless Puppet.features.augeas? - raise Puppet::ParseError, ("validate_augeas(): requires the ruby augeas bindings") + raise Puppet::ParseError, ("validate_augeas(): this function requires the augeas feature. See http://projects.puppetlabs.com/projects/puppet/wiki/Puppet_Augeas#Pre-requisites for how to activate it.") end if (args.length < 2) or (args.length > 4) then -- cgit v1.2.3 From 683ac8f8aa5f349ece98165d92a8d271b99b855e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Tue, 5 Feb 2013 09:01:48 +0100 Subject: validate_cmd: Use Puppet::Util.execute --- lib/puppet/parser/functions/validate_cmd.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index 00fe1ae..4f6a766 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -1,3 +1,5 @@ +require 'puppet/util/execution' + module Puppet::Parser::Functions newfunction(:validate_cmd, :doc => <<-'ENDHEREDOC') do |args| Perform validation of a string with an external command. @@ -30,15 +32,12 @@ module Puppet::Parser::Functions tmpfile = Tempfile.new("validate_cmd") begin tmpfile.write(content) - output = `#{checkscript} #{tmpfile.path} 2>&1 1>/dev/null` - r = $? + Puppet::Util.execute("#{checkscript} #{tmpfile.path}") + rescue Puppet::ExecutionFailure => detail + msg += "\n#{detail}" + raise Puppet::ParseError, msg ensure - tmpfile.close tmpfile.unlink end - if output - msg += "\nOutput is:\n#{output}" - end - raise Puppet::ParseError, (msg) unless r == 0 end end -- cgit v1.2.3 From 69248dfd8ab09f6d78054d10c7162bb18ec040e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Thu, 7 Feb 2013 08:56:52 +0100 Subject: validate_cmd(): Use Puppet::Util::Execution.execute when available --- lib/puppet/parser/functions/validate_cmd.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb index 4f6a766..344a80c 100644 --- a/lib/puppet/parser/functions/validate_cmd.rb +++ b/lib/puppet/parser/functions/validate_cmd.rb @@ -32,7 +32,11 @@ module Puppet::Parser::Functions tmpfile = Tempfile.new("validate_cmd") begin tmpfile.write(content) - Puppet::Util.execute("#{checkscript} #{tmpfile.path}") + if Puppet::Util::Execution.respond_to?('execute') + Puppet::Util::Execution.execute("#{checkscript} #{tmpfile.path}") + else + Puppet::Util.execute("#{checkscript} #{tmpfile.path}") + end rescue Puppet::ExecutionFailure => detail msg += "\n#{detail}" raise Puppet::ParseError, msg -- cgit v1.2.3 From 3cef5d9e3323115c896e76f9e217cac061ab156f Mon Sep 17 00:00:00 2001 From: fatmcgav Date: Tue, 12 Feb 2013 15:07:18 +0000 Subject: (#19201) Add concat function to join two arrays Without this patch applied there is no easy way to append one array to another. This is a problem because it is often desirable to join two arrays without flattening the contents into a single, one dimensional array. This patch addresses the problem by adding a `concat()` function which takes two arguments. The arguments will be concatenated together and a new array returned to the caller. Reviewed-by: Jeff McCune --- lib/puppet/parser/functions/concat.rb | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 lib/puppet/parser/functions/concat.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb new file mode 100644 index 0000000..c86aa00 --- /dev/null +++ b/lib/puppet/parser/functions/concat.rb @@ -0,0 +1,37 @@ +# +# concat.rb +# + +module Puppet::Parser::Functions + newfunction(:concat, :type => :rvalue, :doc => <<-EOS +Appends the contents of array 2 onto array 1. + +*Example:* + + concat(['1','2','3'],['4','5','6']) + +Would result in: + + ['1','2','3','4','5','6'] + EOS + ) do |arguments| + + # Check that 2 arguments have been given ... + raise(Puppet::ParseError, "concat(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size != 2 + + a = arguments[0] + b = arguments[1] + + # Check that both args are arrays. + unless a.is_a?(Array) and b.is_a?(Array) + raise(Puppet::ParseError, 'concat(): Requires array to work with') + end + + result = a.concat(b) + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 95cf3fed689a72c6a7c6641fdf462770f94112ca Mon Sep 17 00:00:00 2001 From: Joshua Hoblitt Date: Thu, 14 Feb 2013 11:57:35 -0700 Subject: (#19272) Add has_element() function It is exceptionally difficult to determine if an array contains an element matching a specific value without an iteration or loop construct. This function is the Puppet equivalent of Array.includes?(foo) in Ruby. The implementation is a verbatim copy of has_key() with the minor modifications needed to support arrays instead of hashes. --- lib/puppet/parser/functions/has_element.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 lib/puppet/parser/functions/has_element.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/has_element.rb b/lib/puppet/parser/functions/has_element.rb new file mode 100644 index 0000000..e29bbb9 --- /dev/null +++ b/lib/puppet/parser/functions/has_element.rb @@ -0,0 +1,28 @@ +module Puppet::Parser::Functions + + newfunction(:has_element, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| + Determine if an array has an element with a matching value. + + Example: + + $my_array = ['key_one'] + if has_element($my_array, 'key_two') { + notice('we will not reach here') + } + if has_element($my_array, 'key_one') { + notice('this will be printed') + } + + ENDHEREDOC + + unless args.length == 2 + raise Puppet::ParseError, ("has_element(): wrong number of arguments (#{args.length}; must be 2)") + end + unless args[0].is_a?(Array) + raise Puppet::ParseError, "has_element(): expects the first argument to be an array, got #{args[0].inspect} which is of type #{args[0].class}" + end + args[0].include?(args[1]) + + end + +end -- cgit v1.2.3 From a45d4f13070cd05c029b96c789f499133721d523 Mon Sep 17 00:00:00 2001 From: Jeff McCune Date: Fri, 15 Feb 2013 11:56:16 -0800 Subject: Revert "Merge pull request #130 from jhoblitt/has_element" This reverts commit f7a18189ec338b01b0fc89d75def832753af3868, reversing changes made to 36a7b29630a4d4de17af79b5dd4e9491ec20b123. I'm reverting this change because of concerns raised by Peter Meier that it duplicates the "in" operator in the DSL. The "in" operator is new information that I did not posses when I made the decision to merge. Because of this new information I'm un-merging and continuing the discussion in the comments of https://projects.puppetlabs.com/issues/19272 Reference: GH-130 --- lib/puppet/parser/functions/has_element.rb | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 lib/puppet/parser/functions/has_element.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/has_element.rb b/lib/puppet/parser/functions/has_element.rb deleted file mode 100644 index e29bbb9..0000000 --- a/lib/puppet/parser/functions/has_element.rb +++ /dev/null @@ -1,28 +0,0 @@ -module Puppet::Parser::Functions - - newfunction(:has_element, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| - Determine if an array has an element with a matching value. - - Example: - - $my_array = ['key_one'] - if has_element($my_array, 'key_two') { - notice('we will not reach here') - } - if has_element($my_array, 'key_one') { - notice('this will be printed') - } - - ENDHEREDOC - - unless args.length == 2 - raise Puppet::ParseError, ("has_element(): wrong number of arguments (#{args.length}; must be 2)") - end - unless args[0].is_a?(Array) - raise Puppet::ParseError, "has_element(): expects the first argument to be an array, got #{args[0].inspect} which is of type #{args[0].class}" - end - args[0].include?(args[1]) - - end - -end -- cgit v1.2.3 From 9fa70ae43f14a9b13343ae67ab8d1520f43ebb4d Mon Sep 17 00:00:00 2001 From: Martin Hellmich Date: Mon, 18 Feb 2013 16:02:15 +0100 Subject: changed .count to .size to support legacy ruby --- lib/puppet/provider/file_line/ruby.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet') diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index e21eaa8..a3219d3 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -34,7 +34,7 @@ Puppet::Type.type(:file_line).provide(:ruby) do def handle_create_with_match() regex = resource[:match] ? Regexp.new(resource[:match]) : nil - match_count = lines.select { |l| regex.match(l) }.count + match_count = lines.select { |l| regex.match(l) }.size if match_count > 1 raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'" end -- cgit v1.2.3 From e80207bede37e498c32bea6d56c46f1dd709721e Mon Sep 17 00:00:00 2001 From: Uwe Stuehler Date: Tue, 23 Oct 2012 16:43:03 +0200 Subject: Fix number of arguments check in flatten() The function only uses the first argument, so raise an error with too few arguments *and* with too many arguments. --- lib/puppet/parser/functions/flatten.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/flatten.rb b/lib/puppet/parser/functions/flatten.rb index 781da78..a1ed183 100644 --- a/lib/puppet/parser/functions/flatten.rb +++ b/lib/puppet/parser/functions/flatten.rb @@ -16,7 +16,7 @@ Would return: ['a','b','c'] ) do |arguments| raise(Puppet::ParseError, "flatten(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + "given (#{arguments.size} for 1)") if arguments.size != 1 array = arguments[0] -- cgit v1.2.3 From 5a11279cc54e7f9f5d168c75728f990d0bd0b694 Mon Sep 17 00:00:00 2001 From: Uwe Stuehler Date: Tue, 23 Oct 2012 16:43:03 +0200 Subject: Fix number of arguments check in flatten() The function only uses the first argument, so raise an error with too few arguments *and* with too many arguments. --- lib/puppet/parser/functions/flatten.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/flatten.rb b/lib/puppet/parser/functions/flatten.rb index 781da78..a1ed183 100644 --- a/lib/puppet/parser/functions/flatten.rb +++ b/lib/puppet/parser/functions/flatten.rb @@ -16,7 +16,7 @@ Would return: ['a','b','c'] ) do |arguments| raise(Puppet::ParseError, "flatten(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + "given (#{arguments.size} for 1)") if arguments.size != 1 array = arguments[0] -- cgit v1.2.3 From 5d5a4d466ed6530fa8b68162e96bdaaaf35066e1 Mon Sep 17 00:00:00 2001 From: Justin Lambert Date: Mon, 17 Dec 2012 06:22:36 -0700 Subject: str2bool should return a boolean if called with a boolean --- lib/puppet/parser/functions/str2bool.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/str2bool.rb b/lib/puppet/parser/functions/str2bool.rb index c320da6..9ea6dd5 100644 --- a/lib/puppet/parser/functions/str2bool.rb +++ b/lib/puppet/parser/functions/str2bool.rb @@ -14,6 +14,11 @@ like: 0, f, n, false, no to 'false'. "given (#{arguments.size} for 1)") if arguments.size < 1 string = arguments[0] + + # If string is already Boolean, return it + if !!string == string + return string + end unless string.is_a?(String) raise(Puppet::ParseError, 'str2bool(): Requires either ' + -- cgit v1.2.3 From 961dcab15d3cf00eabeb5a496efc9da051169241 Mon Sep 17 00:00:00 2001 From: Eric Shamow Date: Tue, 3 Apr 2012 22:30:46 -0400 Subject: (#13610) Add is_function_available to stdlib This function provides a simple wrapper around Puppet::Parser::Functions.function for access within Puppet manifests. This will allow users to check whether or not a plugin or functionality such as hiera is installed on the server. --- .../parser/functions/is_function_available.rb | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lib/puppet/parser/functions/is_function_available.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/is_function_available.rb b/lib/puppet/parser/functions/is_function_available.rb new file mode 100644 index 0000000..6cbd35c --- /dev/null +++ b/lib/puppet/parser/functions/is_function_available.rb @@ -0,0 +1,23 @@ +# +# is_function_available.rb +# + +module Puppet::Parser::Functions + newfunction(:is_function_available, :type => :rvalue, :doc => <<-EOS +This function accepts a string as an argument, determines whether the +Puppet runtime has access to a function by that name. It returns a +true if the function exists, false if not. + EOS + ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_function_available?(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + function = Puppet::Parser::Functions.function(arguments[0].to_sym) + function.is_a?(String) and not function.empty? + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 05273419e1c8b34115ede15b1d8a8739f6a0db00 Mon Sep 17 00:00:00 2001 From: Kristof Willaert Date: Tue, 19 Mar 2013 10:00:57 +0100 Subject: Add floor function implementation and unit tests --- lib/puppet/parser/functions/floor.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 lib/puppet/parser/functions/floor.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/floor.rb b/lib/puppet/parser/functions/floor.rb new file mode 100644 index 0000000..a401923 --- /dev/null +++ b/lib/puppet/parser/functions/floor.rb @@ -0,0 +1,20 @@ +module Puppet::Parser::Functions + newfunction(:floor, :type => :rvalue, :doc => <<-EOS + Returns the largest integer less or equal to the argument. + Takes a single numeric value as an argument. + EOS + ) do |arguments| + + raise(Puppet::ParseError, "floor(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size != 1 + + arg = arguments[0] + + raise(Puppet::ParseError, "floor(): Wrong argument type " + + "given (#{arg.class} for Numeric)") if arg.is_a?(Numeric) == false + + arg.floor + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 88a93ac6cdf38045e1cf29325a70e5e4143016b3 Mon Sep 17 00:00:00 2001 From: Richard Soderberg Date: Tue, 26 Mar 2013 15:45:40 -0700 Subject: add suffix function to accompany the prefix function --- lib/puppet/parser/functions/suffix.rb | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lib/puppet/parser/functions/suffix.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/suffix.rb b/lib/puppet/parser/functions/suffix.rb new file mode 100644 index 0000000..5018280 --- /dev/null +++ b/lib/puppet/parser/functions/suffix.rb @@ -0,0 +1,45 @@ +# +# suffix.rb +# + +module Puppet::Parser::Functions + newfunction(:suffix, :type => :rvalue, :doc => <<-EOS +This function applies a suffix to all elements in an array. + +*Examples:* + + suffix(['a','b','c'], 'p') + +Will return: ['ap','bp','cp'] + EOS + ) do |arguments| + + # Technically we support two arguments but only first is mandatory ... + raise(Puppet::ParseError, "suffix(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'suffix(): Requires array to work with') + end + + suffix = arguments[1] if arguments[1] + + if suffix + unless suffix.is_a?(String) + raise(Puppet::ParseError, 'suffix(): 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 + suffix ? i + suffix : i + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From a83318d3ee41683118a55a2b15769c97efbcf6d9 Mon Sep 17 00:00:00 2001 From: Richard Soderberg Date: Tue, 26 Mar 2013 15:49:09 -0700 Subject: prefix: fix doc typo Examles -> Examples --- lib/puppet/parser/functions/prefix.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb index 4593976..243cf18 100644 --- a/lib/puppet/parser/functions/prefix.rb +++ b/lib/puppet/parser/functions/prefix.rb @@ -6,7 +6,7 @@ module Puppet::Parser::Functions newfunction(:prefix, :type => :rvalue, :doc => <<-EOS This function applies a prefix to all elements in an array. -*Examles:* +*Examples:* prefix(['a','b','c'], 'p') -- cgit v1.2.3 From 29402f31e73a426dcc449216010834884d0251ec Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Wed, 27 Mar 2013 13:37:25 -0700 Subject: (maint) better error reporting for prefix and suffix When prefix and suffix did error checking with positional arguments, they would not report the position of the argument that failed to validate. This commit changes the messages to indicate which argument failed. --- lib/puppet/parser/functions/prefix.rb | 4 ++-- lib/puppet/parser/functions/suffix.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb index 243cf18..62211ae 100644 --- a/lib/puppet/parser/functions/prefix.rb +++ b/lib/puppet/parser/functions/prefix.rb @@ -21,14 +21,14 @@ Will return: ['pa','pb','pc'] array = arguments[0] unless array.is_a?(Array) - raise(Puppet::ParseError, 'prefix(): Requires array to work with') + raise Puppet::ParseError, "prefix(): expected first argument to be an Array, got #{array.inspect}" end prefix = arguments[1] if arguments[1] if prefix unless prefix.is_a?(String) - raise(Puppet::ParseError, 'prefix(): Requires string to work with') + raise Puppet::ParseError, "prefix(): expected second argument to be a String, got #{suffix.inspect}" end end diff --git a/lib/puppet/parser/functions/suffix.rb b/lib/puppet/parser/functions/suffix.rb index 5018280..f7792d6 100644 --- a/lib/puppet/parser/functions/suffix.rb +++ b/lib/puppet/parser/functions/suffix.rb @@ -21,14 +21,14 @@ Will return: ['ap','bp','cp'] array = arguments[0] unless array.is_a?(Array) - raise(Puppet::ParseError, 'suffix(): Requires array to work with') + raise Puppet::ParseError, "suffix(): expected first argument to be an Array, got #{array.inspect}" end suffix = arguments[1] if arguments[1] if suffix - unless suffix.is_a?(String) - raise(Puppet::ParseError, 'suffix(): Requires string to work with') + unless suffix.is_a? String + raise Puppet::ParseError, "suffix(): expected second argument to be a String, got #{suffix.inspect}" end end -- cgit v1.2.3 From ff5dd5d75adb3723e106ca20bac4e68466395a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Dal=C3=A9n?= Date: Wed, 20 Mar 2013 16:36:20 +0100 Subject: Allow comparisons of Numeric and number as String Puppet passes numbers as String to functions, but it makes more sense to compare them as Numeric. But sometimes Puppet passes them as the wrong type, see: https://projects.puppetlabs.com/issues/19812 --- lib/puppet/parser/functions/max.rb | 10 +++++++++- lib/puppet/parser/functions/min.rb | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/max.rb b/lib/puppet/parser/functions/max.rb index 10b6f74..60fb94a 100644 --- a/lib/puppet/parser/functions/max.rb +++ b/lib/puppet/parser/functions/max.rb @@ -8,6 +8,14 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "max(): Wrong number of arguments " + "need at least one") if args.size == 0 - return args.max + # Sometimes we get numbers as numerics and sometimes as strings. + # We try to compare them as numbers when possible + return args.max do |a,b| + if a.to_s =~ /\A-?\d+(.\d+)?\z/ and b.to_s =~ /\A-?\d+(.\d+)?\z/ then + a.to_f <=> b.to_f + else + a.to_s <=> b.to_s + end + end end end diff --git a/lib/puppet/parser/functions/min.rb b/lib/puppet/parser/functions/min.rb index abf1b62..6bd6ebf 100644 --- a/lib/puppet/parser/functions/min.rb +++ b/lib/puppet/parser/functions/min.rb @@ -8,6 +8,14 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "min(): Wrong number of arguments " + "need at least one") if args.size == 0 - return args.min + # Sometimes we get numbers as numerics and sometimes as strings. + # We try to compare them as numbers when possible + return args.min do |a,b| + if a.to_s =~ /\A^-?\d+(.\d+)?\z/ and b.to_s =~ /\A-?\d+(.\d+)?\z/ then + a.to_f <=> b.to_f + else + a.to_s <=> b.to_s + end + end end end -- cgit v1.2.3 From e6916f83fd35989db4b86dfb10716c9198994389 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Fri, 29 Mar 2013 10:04:05 -0400 Subject: Enable num2bool to accept numeric input Also ignore rspec fixtures directory --- lib/puppet/parser/functions/num2bool.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index 874db22..638f693 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -14,7 +14,8 @@ higher then 0 become true. raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size < 1 - number = arguments[0] + # Since we're matching against a regex, coerce to String + number = arguments[0].to_s # Only numbers allowed ... unless number.match(/^\-?\d+$/) -- cgit v1.2.3 From 4a5218a8af8c3ffaf9ea2348a3900b19d6a95416 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Fri, 29 Mar 2013 12:03:33 -0400 Subject: Reworked number-handling logic No more coercing to String and regex matching. Instead, we now coerce to Integer at the beginning or raise an error if we cannot coerce to Integer. A consequence of this change is that the function will now accept blatantly non-numeric strings as input, and return false. This seems a bit goofy to me, but it's how String#to_i works. If we really don't like this, then I'm open to suggestions. --- lib/puppet/parser/functions/num2bool.rb | 35 +++++++++++---------------------- 1 file changed, 11 insertions(+), 24 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index 638f693..15dd560 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -2,39 +2,26 @@ # num2bool.rb # -# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... - 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. +This function converts a number or a string representation of a number into a +true boolean. Zero or anything non-numeric becomes false. Numbers higher then 0 +become true. EOS ) do |arguments| raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - # Since we're matching against a regex, coerce to String - number = arguments[0].to_s - - # Only numbers allowed ... - unless number.match(/^\-?\d+$/) - raise(Puppet::ParseError, 'num2bool(): Requires integer to work with') - end + "given (#{arguments.size} for 1)") if arguments.size != 1 - 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') + # we need to get an Integer out of this + begin + number = arguments[0].to_i + rescue NoMethodError + raise(Puppet::ParseError, 'num2bool(): Unable to parse number: ' + $!) end - return result + # Return true for any positive number and false otherwise + return number > 0 ? true : false end end -- cgit v1.2.3 From 8d217f0012fef332642faf485ad187773a95bcc1 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Fri, 29 Mar 2013 15:06:36 -0400 Subject: (19864) num2bool match fix This is a bit more heavy-handed than I might like, but it does appear to do the right things: * accepts numeric input appropriately, truncating floats * matches string input against a regex, then coerces number-looking strings to int * makes a best effort to coerce anything else to a string, then subjects it to the same treatment * raises an error in the event of incorrect number of arguments or non-number-looking strings I've also included some additional unit tests. --- lib/puppet/parser/functions/num2bool.rb | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index 15dd560..cf98f80 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -13,13 +13,32 @@ become true. raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size != 1 - # we need to get an Integer out of this - begin - number = arguments[0].to_i - rescue NoMethodError - raise(Puppet::ParseError, 'num2bool(): Unable to parse number: ' + $!) + number = arguments[0] + + case number + when Numeric + # Yay, it's a number + when String + # Deal with strings later + else + begin + number = number.to_s + rescue NoMethodError + raise(Puppet::ParseError, 'num2bool(): Unable to parse argument: ' + $!) + end + end + + case number + when String + # Only accept strings that look somewhat like numbers + unless number =~ /^-?\d+/ + raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number") + end end + # Truncate floats + number = number.to_i + # Return true for any positive number and false otherwise return number > 0 ? true : false end -- cgit v1.2.3 From c372f177708df4c844337e9901646b7b84b86cd8 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Mon, 1 Apr 2013 11:44:09 -0400 Subject: Cleanup per adrianthebo suggestions * use Float() to process string arguments * get rid of doubly nested arrays * removing needless ternary operator * improving error message handling --- lib/puppet/parser/functions/num2bool.rb | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index cf98f80..af0e6ed 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -19,28 +19,24 @@ become true. when Numeric # Yay, it's a number when String - # Deal with strings later + begin + number = Float(number) + rescue ArgumentError => ex + raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number: #{ex.message}") + end else begin number = number.to_s - rescue NoMethodError - raise(Puppet::ParseError, 'num2bool(): Unable to parse argument: ' + $!) - end - end - - case number - when String - # Only accept strings that look somewhat like numbers - unless number =~ /^-?\d+/ - raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number") + rescue NoMethodError => ex + raise(Puppet::ParseError, "num2bool(): Unable to parse argument: #{ex.message}") end end - # Truncate floats + # Truncate Floats number = number.to_i # Return true for any positive number and false otherwise - return number > 0 ? true : false + return number > 0 end end -- cgit v1.2.3 From 7a2fb80834d3ec4d21efe58e5e8af188e5bdd07c Mon Sep 17 00:00:00 2001 From: Amos Shapira Date: Sun, 31 Mar 2013 23:37:30 +1100 Subject: (#19998) Implement any2array This change is to implement a new function "any2array", which will take any argument or arguments and create an array which contains it. If the argument is a single array then it will be returned as-is. If the argument is a single hash then it will be converted into an array. Otherwise (if there are more than one argument, or the only argument is not an array or a hash) the function will return an array containing all the arguments. --- lib/puppet/parser/functions/any2array.rb | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lib/puppet/parser/functions/any2array.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/any2array.rb b/lib/puppet/parser/functions/any2array.rb new file mode 100644 index 0000000..d150743 --- /dev/null +++ b/lib/puppet/parser/functions/any2array.rb @@ -0,0 +1,35 @@ +# +# str2bool.rb +# + +module Puppet::Parser::Functions + newfunction(:any2array, :type => :rvalue, :doc => <<-EOS +This converts any object to an array containing that object. Empty argument +lists are converted to an empty array. Arrays are left untouched. Hashes are +converted to arrays of alternating keys and values. + EOS + ) do |arguments| + + if arguments.empty? + return [] + end + + if arguments.length == 1 + if arguments[0].kind_of?(Array) + return arguments[0] + elsif arguments[0].kind_of?(Hash) + result = [] + arguments[0].each do |key, value| + result << key << value + end + return result + else + return [arguments[0]] + end + end + + return arguments + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 003cde074c86f034d2730eeb40d676d4e35c6022 Mon Sep 17 00:00:00 2001 From: Amos Shapira Date: Sun, 31 Mar 2013 23:47:11 +1100 Subject: (#19998) fix name of function in a comment --- lib/puppet/parser/functions/any2array.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/any2array.rb b/lib/puppet/parser/functions/any2array.rb index d150743..95944e1 100644 --- a/lib/puppet/parser/functions/any2array.rb +++ b/lib/puppet/parser/functions/any2array.rb @@ -1,5 +1,5 @@ # -# str2bool.rb +# any2array.rb # module Puppet::Parser::Functions -- cgit v1.2.3 From e7d394b6e9fc3ca07803d18fcf2f5d19b91e92e0 Mon Sep 17 00:00:00 2001 From: Amos Shapira Date: Mon, 1 Apr 2013 14:52:07 +1100 Subject: Remove resundant code --- lib/puppet/parser/functions/any2array.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/any2array.rb b/lib/puppet/parser/functions/any2array.rb index 95944e1..e71407e 100644 --- a/lib/puppet/parser/functions/any2array.rb +++ b/lib/puppet/parser/functions/any2array.rb @@ -23,8 +23,6 @@ converted to arrays of alternating keys and values. result << key << value end return result - else - return [arguments[0]] end end -- cgit v1.2.3 From f28550e78996f908b076cdc9aebcbe584c777cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Dal=C3=A9n?= Date: Wed, 10 Apr 2013 14:38:14 +0200 Subject: Add a count function Similar to the ruby count method on arrays. --- lib/puppet/parser/functions/count.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 lib/puppet/parser/functions/count.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/count.rb b/lib/puppet/parser/functions/count.rb new file mode 100644 index 0000000..52de1b8 --- /dev/null +++ b/lib/puppet/parser/functions/count.rb @@ -0,0 +1,22 @@ +module Puppet::Parser::Functions + newfunction(:count, :type => :rvalue, :arity => -2, :doc => <<-EOS +Takes an array as first argument and an optional second argument. +Count the number of elements in array that matches second argument. +If called with only an array it counts the number of elements that are not nil/undef. + EOS + ) do |args| + + if (args.size > 2) then + raise(ArgumentError, "count(): Wrong number of arguments "+ + "given #{args.size} for 1 or 2.") + end + + collection, item = args + + if item then + collection.count item + else + collection.count { |obj| obj != nil && obj != :undef && obj != '' } + end + end +end -- cgit v1.2.3 From ddad4455cc914862f16c2e8cae96812e244b0a1d Mon Sep 17 00:00:00 2001 From: Philip Potter Date: Thu, 12 Jul 2012 17:05:23 +0100 Subject: Make the anchor type propagate refresh events Without this patch the anchor resource does not propogate refresh events, making it difficult to subscribe to a class which has been notified by another resource. --- lib/puppet/type/anchor.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'lib/puppet') diff --git a/lib/puppet/type/anchor.rb b/lib/puppet/type/anchor.rb index 6b81732..fe1e5aa 100644 --- a/lib/puppet/type/anchor.rb +++ b/lib/puppet/type/anchor.rb @@ -38,4 +38,9 @@ Puppet::Type.newtype(:anchor) do desc "The name of the anchor resource." end + def refresh + # We don't do anything with them, but we need this to + # show that we are "refresh aware" and not break the + # chain of propagation. + end end -- cgit v1.2.3 From 3077d26b00b170ef2c5c808831577d421613b198 Mon Sep 17 00:00:00 2001 From: Martin Hellmich Date: Mon, 22 Apr 2013 23:26:22 +0200 Subject: check if an argument supposed to be merged is empty to pass over undefs without failing --- lib/puppet/parser/functions/merge.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/merge.rb b/lib/puppet/parser/functions/merge.rb index 6ec085e..54d1c1c 100644 --- a/lib/puppet/parser/functions/merge.rb +++ b/lib/puppet/parser/functions/merge.rb @@ -22,6 +22,7 @@ module Puppet::Parser::Functions accumulator = Hash.new # Merge into the accumulator hash args.each do |arg| + next if arg.empty? # empty string is synonym for puppet's undef unless arg.is_a?(Hash) raise Puppet::ParseError, "merge: unexpected argument type #{arg.class}, only expects hash arguments" end -- cgit v1.2.3 From 928036ac538dd32cd5af81bea90fac077a89d721 Mon Sep 17 00:00:00 2001 From: Mark Smith-Guerrero Date: Mon, 29 Apr 2013 09:19:07 -0400 Subject: (maint) Fix a small typo in hash() description --- lib/puppet/parser/functions/hash.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/hash.rb b/lib/puppet/parser/functions/hash.rb index 453ba1e..8cc4823 100644 --- a/lib/puppet/parser/functions/hash.rb +++ b/lib/puppet/parser/functions/hash.rb @@ -4,7 +4,7 @@ module Puppet::Parser::Functions newfunction(:hash, :type => :rvalue, :doc => <<-EOS -This function converts and array into a hash. +This function converts an array into a hash. *Examples:* -- cgit v1.2.3 From 2ba9e4721b6d96f0b9e66bbb2f8b09c9c413efe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Thu, 2 May 2013 15:10:59 +0200 Subject: Add a dirname function --- lib/puppet/parser/functions/dirname.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 lib/puppet/parser/functions/dirname.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/dirname.rb b/lib/puppet/parser/functions/dirname.rb new file mode 100644 index 0000000..ea8cc1e --- /dev/null +++ b/lib/puppet/parser/functions/dirname.rb @@ -0,0 +1,15 @@ +module Puppet::Parser::Functions + newfunction(:dirname, :type => :rvalue, :doc => <<-EOS + Returns the dirname of a path. + EOS + ) do |arguments| + + raise(Puppet::ParseError, "dirname(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + path = arguments[0] + return File.dirname(path) + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From e08734af5a38cb9ad4bc2323104a1a8a76019bb8 Mon Sep 17 00:00:00 2001 From: Alex Cline Date: Fri, 3 May 2013 15:47:27 -0400 Subject: (#20548) Allow an array of resource titles to be passed into the ensure_resource function This patch allows an array of resource titles to be passed into the ensure_resource function. Each item in the array will be checked for existence and will be created if it doesn't already exist. --- lib/puppet/parser/functions/ensure_resource.rb | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/ensure_resource.rb b/lib/puppet/parser/functions/ensure_resource.rb index fba2035..8ede967 100644 --- a/lib/puppet/parser/functions/ensure_resource.rb +++ b/lib/puppet/parser/functions/ensure_resource.rb @@ -19,17 +19,27 @@ If the resource already exists but does not match the specified parameters, this function will attempt to recreate the resource leading to a duplicate resource definition error. +An array of resources can also be passed in and each will be created with +the type and parameters specified if it doesn't already exist. + + ensure_resource('user', ['dan','alex'], {'ensure' => 'present'}) + ENDOFDOC ) do |vals| type, title, params = vals raise(ArgumentError, 'Must specify a type') unless type raise(ArgumentError, 'Must specify a title') unless title params ||= {} - Puppet::Parser::Functions.function(:defined_with_params) - if function_defined_with_params(["#{type}[#{title}]", params]) - Puppet.debug("Resource #{type}[#{title}] not created b/c it already exists") - else - Puppet::Parser::Functions.function(:create_resources) - function_create_resources([type.capitalize, { title => params }]) + + items = title.kind_of?(Array) ? title : [].push(title) + + items.each do |item| + Puppet::Parser::Functions.function(:defined_with_params) + if function_defined_with_params(["#{type}[#{item}]", params]) + Puppet.debug("Resource #{type}[#{item}] not created b/c it already exists") + else + Puppet::Parser::Functions.function(:create_resources) + function_create_resources([type.capitalize, { item => params }]) + end end end -- cgit v1.2.3 From de253db5648c0aa15e7fcead9755488e9d8642fc Mon Sep 17 00:00:00 2001 From: Alex Cline Date: Mon, 6 May 2013 13:52:57 -0400 Subject: Changed str-to-array conversion and removed abbreviation. --- lib/puppet/parser/functions/ensure_resource.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/ensure_resource.rb b/lib/puppet/parser/functions/ensure_resource.rb index 8ede967..36c423a 100644 --- a/lib/puppet/parser/functions/ensure_resource.rb +++ b/lib/puppet/parser/functions/ensure_resource.rb @@ -31,12 +31,12 @@ ENDOFDOC raise(ArgumentError, 'Must specify a title') unless title params ||= {} - items = title.kind_of?(Array) ? title : [].push(title) + items = title.kind_of?(Array) ? title : [title] items.each do |item| Puppet::Parser::Functions.function(:defined_with_params) if function_defined_with_params(["#{type}[#{item}]", params]) - Puppet.debug("Resource #{type}[#{item}] not created b/c it already exists") + Puppet.debug("Resource #{type}[#{item}] not created because it already exists") else Puppet::Parser::Functions.function(:create_resources) function_create_resources([type.capitalize, { item => params }]) -- cgit v1.2.3 From d38bce0488dd70a6f8a8a2c9b91a98a231f28f02 Mon Sep 17 00:00:00 2001 From: Alex Cline Date: Mon, 6 May 2013 16:16:38 -0400 Subject: Terser method of string to array conversion courtesy of ethooz. --- lib/puppet/parser/functions/ensure_resource.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/ensure_resource.rb b/lib/puppet/parser/functions/ensure_resource.rb index 36c423a..a9a1733 100644 --- a/lib/puppet/parser/functions/ensure_resource.rb +++ b/lib/puppet/parser/functions/ensure_resource.rb @@ -31,7 +31,7 @@ ENDOFDOC raise(ArgumentError, 'Must specify a title') unless title params ||= {} - items = title.kind_of?(Array) ? title : [title] + items = [title].flatten items.each do |item| Puppet::Parser::Functions.function(:defined_with_params) -- cgit v1.2.3 From f496005bf3db8a5202bf9c16daf9a524b178c67a Mon Sep 17 00:00:00 2001 From: Martin Hellmich Date: Wed, 8 May 2013 17:14:28 +0200 Subject: fix in merge.rb: refine the checking if an argument is an empty string --- lib/puppet/parser/functions/merge.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/merge.rb b/lib/puppet/parser/functions/merge.rb index 54d1c1c..1b39f20 100644 --- a/lib/puppet/parser/functions/merge.rb +++ b/lib/puppet/parser/functions/merge.rb @@ -22,7 +22,7 @@ module Puppet::Parser::Functions accumulator = Hash.new # Merge into the accumulator hash args.each do |arg| - next if arg.empty? # empty string is synonym for puppet's undef + next if arg.is_a? String and arg.empty? # empty string is synonym for puppet's undef unless arg.is_a?(Hash) raise Puppet::ParseError, "merge: unexpected argument type #{arg.class}, only expects hash arguments" end -- cgit v1.2.3 From 737aa31546e71e9febea2199582510ef88a2560c Mon Sep 17 00:00:00 2001 From: Alex Cline Date: Mon, 13 May 2013 12:09:33 -0400 Subject: (#20684) Add array comparison functions, difference, intersection and union. Included is code, tests and documentation for the difference, intersection and union functions for comparing arrays. --- lib/puppet/parser/functions/difference.rb | 36 +++++++++++++++++++++++++++++ lib/puppet/parser/functions/intersection.rb | 34 +++++++++++++++++++++++++++ lib/puppet/parser/functions/union.rb | 34 +++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 lib/puppet/parser/functions/difference.rb create mode 100644 lib/puppet/parser/functions/intersection.rb create mode 100644 lib/puppet/parser/functions/union.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/difference.rb b/lib/puppet/parser/functions/difference.rb new file mode 100644 index 0000000..cd258f7 --- /dev/null +++ b/lib/puppet/parser/functions/difference.rb @@ -0,0 +1,36 @@ +# +# difference.rb +# + +module Puppet::Parser::Functions + newfunction(:difference, :type => :rvalue, :doc => <<-EOS +This function returns the difference between two arrays. +The returned array is a copy of the original array, removing any items that +also appear in the second array. + +*Examples:* + + difference(["a","b","c"],["b","c","d"]) + +Would return: ["a"] + EOS + ) do |arguments| + + # Two arguments are required + raise(Puppet::ParseError, "difference(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size != 2 + + first = arguments[0] + second = arguments[1] + + unless first.is_a?(Array) && second.is_a?(Array) + raise(Puppet::ParseError, 'difference(): Requires 2 arrays') + end + + result = first - second + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/intersection.rb b/lib/puppet/parser/functions/intersection.rb new file mode 100644 index 0000000..48f02e9 --- /dev/null +++ b/lib/puppet/parser/functions/intersection.rb @@ -0,0 +1,34 @@ +# +# intersection.rb +# + +module Puppet::Parser::Functions + newfunction(:intersection, :type => :rvalue, :doc => <<-EOS +This function returns an array an intersection of two. + +*Examples:* + + intersection(["a","b","c"],["b","c","d"]) + +Would return: ["b","c"] + EOS + ) do |arguments| + + # Two arguments are required + raise(Puppet::ParseError, "intersection(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size != 2 + + first = arguments[0] + second = arguments[1] + + unless first.is_a?(Array) && second.is_a?(Array) + raise(Puppet::ParseError, 'intersection(): Requires 2 arrays') + end + + result = first & second + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/union.rb b/lib/puppet/parser/functions/union.rb new file mode 100644 index 0000000..c91bb80 --- /dev/null +++ b/lib/puppet/parser/functions/union.rb @@ -0,0 +1,34 @@ +# +# union.rb +# + +module Puppet::Parser::Functions + newfunction(:union, :type => :rvalue, :doc => <<-EOS +This function returns a union of two arrays. + +*Examples:* + + union(["a","b","c"],["b","c","d"]) + +Would return: ["a","b","c","d"] + EOS + ) do |arguments| + + # Two arguments are required + raise(Puppet::ParseError, "union(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size != 2 + + first = arguments[0] + second = arguments[1] + + unless first.is_a?(Array) && second.is_a?(Array) + raise(Puppet::ParseError, 'union(): Requires 2 arrays') + end + + result = first | second + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 3707c47dcdf154b2396d9240fc90f76450890b71 Mon Sep 17 00:00:00 2001 From: fiddyspence Date: Fri, 24 May 2013 16:33:24 +0100 Subject: Adding base64 function Adding base64 function and spec test. Included a bonus fix to validate_slength_spec.rb to put the expectation message in the right place. --- lib/puppet/parser/functions/base64.rb | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 lib/puppet/parser/functions/base64.rb (limited to 'lib/puppet') diff --git a/lib/puppet/parser/functions/base64.rb b/lib/puppet/parser/functions/base64.rb new file mode 100644 index 0000000..d9a590a --- /dev/null +++ b/lib/puppet/parser/functions/base64.rb @@ -0,0 +1,37 @@ +module Puppet::Parser::Functions + + newfunction(:base64, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| + + Base64 encode or decode a string based on the command and the string submitted + + Usage: + + $encodestring = base64('encode','thestring') + $decodestring = base64('decode','dGhlc3RyaW5n') + + ENDHEREDOC + + require 'base64' + + raise Puppet::ParseError, ("base64(): Wrong number of arguments (#{args.length}; must be = 2)") unless args.length == 2 + + actions = ['encode','decode'] + + unless actions.include?(args[0]) + raise Puppet::ParseError, ("base64(): the first argument must be one of 'encode' or 'decode'") + end + + unless args[1].is_a?(String) + raise Puppet::ParseError, ("base64(): the second argument must be a string to base64") + end + + case args[0] + when 'encode' + result = Base64.encode64(args[1]) + when 'decode' + result = Base64.decode64(args[1]) + end + + return result + end +end -- cgit v1.2.3