diff options
author | Varac <varac@leap.se> | 2017-10-13 14:22:08 +0200 |
---|---|---|
committer | Varac <varac@leap.se> | 2017-10-13 14:22:08 +0200 |
commit | f7779ce2eacaca51790d4e8d7756442b02612d30 (patch) | |
tree | 56871c123e1c964fc26e36bb86831a4a3511bd4f /lib/puppet | |
parent | 066c08f8362d53f0f30897cb8710d11260c726ea (diff) | |
parent | 2339ea8db67ac7ef02d707c2a6011ae50f5d82b5 (diff) |
Diffstat (limited to 'lib/puppet')
97 files changed, 545 insertions, 321 deletions
diff --git a/lib/puppet/functions/fact.rb b/lib/puppet/functions/fact.rb new file mode 100644 index 0000000..dfb048b --- /dev/null +++ b/lib/puppet/functions/fact.rb @@ -0,0 +1,58 @@ +# Digs into the facts hash using dot-notation +# +# Example usage: +# +# fact('osfamily') +# fact('os.architecture') +# +# Array indexing: +# +# fact('mountpoints."/dev".options.1') +# +# Fact containing a "." in the name: +# +# fact('vmware."VRA.version"') +# +Puppet::Functions.create_function(:fact) do + dispatch :fact do + param 'String', :fact_name + end + + def to_dot_syntax(array_path) + array_path.map do |string| + string.include?('.') ? %Q{"#{string}"} : string + end.join('.') + end + + def fact(fact_name) + facts = closure_scope['facts'] + + # Transform the dot-notation string into an array of paths to walk. Make + # sure to correctly extract double-quoted values containing dots as single + # elements in the path. + path = fact_name.scan(/([^."]+)|(?:")([^"]+)(?:")/).map {|x| x.compact.first } + + walked_path = [] + path.reduce(facts) do |d, k| + return nil if d.nil? || k.nil? + + case + when d.is_a?(Array) + begin + result = d[Integer(k)] + rescue ArgumentError => e + Puppet.warning("fact request for #{fact_name} returning nil: '#{to_dot_syntax(walked_path)}' is an array; cannot index to '#{k}'") + result = nil + end + when d.is_a?(Hash) + result = d[k] + else + Puppet.warning("fact request for #{fact_name} returning nil: '#{to_dot_syntax(walked_path)}' is not a collection; cannot walk to '#{k}'") + result = nil + end + + walked_path << k + result + end + end +end diff --git a/lib/puppet/functions/length.rb b/lib/puppet/functions/length.rb new file mode 100644 index 0000000..5ebd455 --- /dev/null +++ b/lib/puppet/functions/length.rb @@ -0,0 +1,14 @@ +#A function to eventually replace the old size() function for stdlib - The original size function did not handle Puppets new type capabilities, so this function is a Puppet 4 compatible solution. +Puppet::Functions.create_function(:length) do + dispatch :length do + param 'Variant[String,Array,Hash]', :value + end + def length(value) + if value.is_a?(String) + result = value.length + elsif value.is_a?(Array) || value.is_a?(Hash) + result = value.size + end + return result + end +end diff --git a/lib/puppet/functions/to_json.rb b/lib/puppet/functions/to_json.rb new file mode 100644 index 0000000..782d695 --- /dev/null +++ b/lib/puppet/functions/to_json.rb @@ -0,0 +1,21 @@ +# Take a data structure and output it as JSON +# +# @example how to output JSON +# # output json to a file +# file { '/tmp/my.json': +# ensure => file, +# content => to_json($myhash), +# } +# +# +require 'json' + +Puppet::Functions.create_function(:to_json) do + dispatch :to_json do + param 'Any', :data + end + + def to_json(data) + data.to_json + end +end diff --git a/lib/puppet/functions/to_json_pretty.rb b/lib/puppet/functions/to_json_pretty.rb new file mode 100644 index 0000000..4c28539 --- /dev/null +++ b/lib/puppet/functions/to_json_pretty.rb @@ -0,0 +1,21 @@ +# Take a data structure and output it as pretty JSON +# +# @example how to output pretty JSON +# # output pretty json to a file +# file { '/tmp/my.json': +# ensure => file, +# content => to_json_pretty($myhash), +# } +# +# +require 'json' + +Puppet::Functions.create_function(:to_json_pretty) do + dispatch :to_json_pretty do + param 'Variant[Hash, Array]', :data + end + + def to_json_pretty(data) + JSON.pretty_generate(data) + end +end diff --git a/lib/puppet/functions/to_yaml.rb b/lib/puppet/functions/to_yaml.rb new file mode 100644 index 0000000..fdd7370 --- /dev/null +++ b/lib/puppet/functions/to_yaml.rb @@ -0,0 +1,21 @@ +# Take a data structure and output it as YAML +# +# @example how to output YAML +# # output yaml to a file +# file { '/tmp/my.yaml': +# ensure => file, +# content => to_yaml($myhash), +# } +# +# +require 'yaml' + +Puppet::Functions.create_function(:to_yaml) do + dispatch :to_yaml do + param 'Any', :data + end + + def to_yaml(data) + data.to_yaml + end +end diff --git a/lib/puppet/parser/functions/abs.rb b/lib/puppet/parser/functions/abs.rb index 11d2d7f..222a902 100644 --- a/lib/puppet/parser/functions/abs.rb +++ b/lib/puppet/parser/functions/abs.rb @@ -9,8 +9,7 @@ module Puppet::Parser::Functions EOS ) do |arguments| - raise(Puppet::ParseError, "abs(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "abs(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] @@ -21,8 +20,7 @@ module Puppet::Parser::Functions elsif value.match(/^-?\d+$/) value = value.to_i else - raise(Puppet::ParseError, 'abs(): Requires float or ' + - 'integer to work with') + raise(Puppet::ParseError, 'abs(): Requires float or integer to work with') end end diff --git a/lib/puppet/parser/functions/any2bool.rb b/lib/puppet/parser/functions/any2bool.rb index f0f8f83..17612bf 100644 --- a/lib/puppet/parser/functions/any2bool.rb +++ b/lib/puppet/parser/functions/any2bool.rb @@ -15,8 +15,7 @@ This converts 'anything' to a boolean. In practise it does the following: EOS ) do |arguments| - raise(Puppet::ParseError, "any2bool(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "any2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 # If argument is already Boolean, return it if !!arguments[0] == arguments[0] diff --git a/lib/puppet/parser/functions/assert_private.rb b/lib/puppet/parser/functions/assert_private.rb index 66c79cc..62e2c6b 100644 --- a/lib/puppet/parser/functions/assert_private.rb +++ b/lib/puppet/parser/functions/assert_private.rb @@ -9,8 +9,7 @@ module Puppet::Parser::Functions EOS ) do |args| - raise(Puppet::ParseError, "assert_private(): Wrong number of arguments "+ - "given (#{args.size}}) for 0 or 1)") if args.size > 1 + raise(Puppet::ParseError, "assert_private(): Wrong number of arguments given (#{args.size}}) for 0 or 1)") if args.size > 1 scope = self if scope.lookupvar('module_name') != scope.lookupvar('caller_module_name') diff --git a/lib/puppet/parser/functions/base64.rb b/lib/puppet/parser/functions/base64.rb index a8998f2..d610146 100644 --- a/lib/puppet/parser/functions/base64.rb +++ b/lib/puppet/parser/functions/base64.rb @@ -1,3 +1,6 @@ + +# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. + module Puppet::Parser::Functions newfunction(:base64, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args| diff --git a/lib/puppet/parser/functions/bool2num.rb b/lib/puppet/parser/functions/bool2num.rb index 6ad6cf4..92e4dde 100644 --- a/lib/puppet/parser/functions/bool2num.rb +++ b/lib/puppet/parser/functions/bool2num.rb @@ -11,8 +11,7 @@ module Puppet::Parser::Functions EOS ) do |arguments| - raise(Puppet::ParseError, "bool2num(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "bool2num(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = function_str2bool([arguments[0]]) diff --git a/lib/puppet/parser/functions/bool2str.rb b/lib/puppet/parser/functions/bool2str.rb index 7e36474..37d4a4e 100644 --- a/lib/puppet/parser/functions/bool2str.rb +++ b/lib/puppet/parser/functions/bool2str.rb @@ -20,8 +20,7 @@ module Puppet::Parser::Functions ) do |arguments| unless arguments.size == 1 or arguments.size == 3 - raise(Puppet::ParseError, "bool2str(): Wrong number of arguments " + - "given (#{arguments.size} for 3)") + raise(Puppet::ParseError, "bool2str(): Wrong number of arguments given (#{arguments.size} for 3)") end value = arguments[0] diff --git a/lib/puppet/parser/functions/camelcase.rb b/lib/puppet/parser/functions/camelcase.rb index d7f43f7..085b3c6 100644 --- a/lib/puppet/parser/functions/camelcase.rb +++ b/lib/puppet/parser/functions/camelcase.rb @@ -1,5 +1,6 @@ # # camelcase.rb +# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. # module Puppet::Parser::Functions @@ -8,15 +9,13 @@ Converts the case of a string or all strings in an array to camel case. EOS ) do |arguments| - raise(Puppet::ParseError, "camelcase(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "camelcase(): 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, 'camelcase(): Requires either ' + - 'array or string to work with') + raise(Puppet::ParseError, 'camelcase(): Requires either array or string to work with') end if value.is_a?(Array) diff --git a/lib/puppet/parser/functions/capitalize.rb b/lib/puppet/parser/functions/capitalize.rb index 98b2d16..096bc5d 100644 --- a/lib/puppet/parser/functions/capitalize.rb +++ b/lib/puppet/parser/functions/capitalize.rb @@ -1,5 +1,6 @@ # # capitalize.rb +# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. # module Puppet::Parser::Functions @@ -9,14 +10,12 @@ module Puppet::Parser::Functions EOS ) do |arguments| - raise(Puppet::ParseError, "capitalize(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "capitalize(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'capitalize(): Requires either ' + - 'array or string to work with') + raise(Puppet::ParseError, 'capitalize(): Requires either array or string to work with') end if value.is_a?(Array) @@ -29,5 +28,3 @@ module Puppet::Parser::Functions return result end end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/ceiling.rb b/lib/puppet/parser/functions/ceiling.rb index 5f3b10b..bec4266 100644 --- a/lib/puppet/parser/functions/ceiling.rb +++ b/lib/puppet/parser/functions/ceiling.rb @@ -5,18 +5,15 @@ module Puppet::Parser::Functions EOS ) do |arguments| - raise(Puppet::ParseError, "ceiling(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size != 1 + raise(Puppet::ParseError, "ceiling(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 begin arg = Float(arguments[0]) rescue TypeError, ArgumentError => e - raise(Puppet::ParseError, "ceiling(): Wrong argument type " + - "given (#{arguments[0]} for Numeric)") + raise(Puppet::ParseError, "ceiling(): Wrong argument type given (#{arguments[0]} for Numeric)") end - raise(Puppet::ParseError, "ceiling(): Wrong argument type " + - "given (#{arg.class} for Numeric)") if arg.is_a?(Numeric) == false + raise(Puppet::ParseError, "ceiling(): Wrong argument type given (#{arg.class} for Numeric)") if arg.is_a?(Numeric) == false arg.ceil end diff --git a/lib/puppet/parser/functions/chomp.rb b/lib/puppet/parser/functions/chomp.rb index c55841e..f9da50f 100644 --- a/lib/puppet/parser/functions/chomp.rb +++ b/lib/puppet/parser/functions/chomp.rb @@ -10,14 +10,12 @@ module Puppet::Parser::Functions EOS ) do |arguments| - raise(Puppet::ParseError, "chomp(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "chomp(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'chomp(): Requires either ' + - 'array or string to work with') + raise(Puppet::ParseError, 'chomp(): Requires either array or string to work with') end if value.is_a?(Array) diff --git a/lib/puppet/parser/functions/chop.rb b/lib/puppet/parser/functions/chop.rb index b24ab78..809349d 100644 --- a/lib/puppet/parser/functions/chop.rb +++ b/lib/puppet/parser/functions/chop.rb @@ -12,14 +12,12 @@ module Puppet::Parser::Functions EOS ) do |arguments| - raise(Puppet::ParseError, "chop(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "chop(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'chop(): Requires either an ' + - 'array or string to work with') + raise(Puppet::ParseError, 'chop(): Requires either an array or string to work with') end if value.is_a?(Array) diff --git a/lib/puppet/parser/functions/clamp.rb b/lib/puppet/parser/functions/clamp.rb index 432c7c1..c4503fe 100644 --- a/lib/puppet/parser/functions/clamp.rb +++ b/lib/puppet/parser/functions/clamp.rb @@ -10,8 +10,7 @@ module Puppet::Parser::Functions args.flatten! - raise(Puppet::ParseError, 'clamp(): Wrong number of arguments, ' + - 'need three to clamp') if args.size != 3 + raise(Puppet::ParseError, 'clamp(): Wrong number of arguments, need three to clamp') if args.size != 3 # check values out args.each do |value| diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb index 91edb4e..0a49cfe 100644 --- a/lib/puppet/parser/functions/concat.rb +++ b/lib/puppet/parser/functions/concat.rb @@ -17,8 +17,7 @@ Would result in: ) do |arguments| # Check that more than 2 arguments have been given ... - raise(Puppet::ParseError, "concat(): Wrong number of arguments " + - "given (#{arguments.size} for < 2)") if arguments.size < 2 + raise(Puppet::ParseError, "concat(): Wrong number of arguments given (#{arguments.size} for < 2)") if arguments.size < 2 a = arguments[0] diff --git a/lib/puppet/parser/functions/count.rb b/lib/puppet/parser/functions/count.rb index 52de1b8..cef2637 100644 --- a/lib/puppet/parser/functions/count.rb +++ b/lib/puppet/parser/functions/count.rb @@ -7,8 +7,7 @@ If called with only an array it counts the number of elements that are not nil/u ) do |args| if (args.size > 2) then - raise(ArgumentError, "count(): Wrong number of arguments "+ - "given #{args.size} for 1 or 2.") + raise(ArgumentError, "count(): Wrong number of arguments given #{args.size} for 1 or 2.") end collection, item = args diff --git a/lib/puppet/parser/functions/defined_with_params.rb b/lib/puppet/parser/functions/defined_with_params.rb index 99687ae..c45a9df 100644 --- a/lib/puppet/parser/functions/defined_with_params.rb +++ b/lib/puppet/parser/functions/defined_with_params.rb @@ -24,7 +24,25 @@ ENDOFDOC params = {} end ret = false - if resource = findresource(reference.to_s) + + if Puppet::Util::Package.versioncmp(Puppet.version, '4.6.0') >= 0 + # Workaround for PE-20308 + if reference.is_a?(String) + type_name, title = Puppet::Resource.type_and_title(reference, nil) + type = Puppet::Pops::Evaluator::Runtime3ResourceSupport.find_resource_type_or_class(find_global_scope, type_name.downcase) + elsif reference.is_a?(Puppet::Resource) + type = reference.resource_type + title = reference.title + else + raise(ArgumentError, "Reference is not understood: '#{reference.class}'") + end + #end workaround + else + type = reference.to_s + title = nil + end + + if resource = findresource(type, title) matches = params.collect do |key, value| # eql? avoids bugs caused by monkeypatching in puppet resource_is_undef = resource[key].eql?(:undef) || resource[key].nil? diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb index 466c55c..9dd5164 100644 --- a/lib/puppet/parser/functions/delete.rb +++ b/lib/puppet/parser/functions/delete.rb @@ -23,8 +23,7 @@ string, or key from a hash. EOS ) do |arguments| - raise(Puppet::ParseError, "delete(): Wrong number of arguments "+ - "given #{arguments.size} for 2") unless arguments.size == 2 + raise(Puppet::ParseError, "delete(): Wrong number of arguments given #{arguments.size} for 2") unless arguments.size == 2 collection = arguments[0].dup Array(arguments[1]).each do |item| @@ -34,8 +33,7 @@ string, or key from a hash. when String collection.gsub! item, '' else - raise(TypeError, "delete(): First argument must be an Array, " + - "String, or Hash. Given an argument of class #{collection.class}.") + raise(TypeError, "delete(): First argument must be an Array, String, or Hash. Given an argument of class #{collection.class}.") end end collection diff --git a/lib/puppet/parser/functions/delete_at.rb b/lib/puppet/parser/functions/delete_at.rb index 3eb4b53..daf3721 100644 --- a/lib/puppet/parser/functions/delete_at.rb +++ b/lib/puppet/parser/functions/delete_at.rb @@ -14,8 +14,7 @@ Would return: ['a','c'] EOS ) do |arguments| - raise(Puppet::ParseError, "delete_at(): Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 + raise(Puppet::ParseError, "delete_at(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size < 2 array = arguments[0] @@ -26,8 +25,7 @@ Would return: ['a','c'] 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') + raise(Puppet::ParseError, 'delete_at(): You must provide non-negative numeric index') end result = array.clone @@ -36,8 +34,7 @@ Would return: ['a','c'] 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') + raise(Puppet::ParseError, 'delete_at(): Given index exceeds size of array given') end result.delete_at(index) # We ignore the element that got deleted ... diff --git a/lib/puppet/parser/functions/delete_regex.rb b/lib/puppet/parser/functions/delete_regex.rb index d72b3e9..e5dc1fd 100644 --- a/lib/puppet/parser/functions/delete_regex.rb +++ b/lib/puppet/parser/functions/delete_regex.rb @@ -1,5 +1,6 @@ # -# delete_regex.rb +# delete_regex.rb +# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. # module Puppet::Parser::Functions @@ -12,7 +13,7 @@ to be matched as an OR. delete_regex(['a','b','c','b'], 'b') Would return: ['a','c'] - + delete_regex(['a','b','c','b'], ['b', 'c']) Would return: ['a'] @@ -25,8 +26,7 @@ to be matched as an OR. EOS ) do |arguments| - raise(Puppet::ParseError, "delete_regex(): Wrong number of arguments "+ - "given #{arguments.size} for 2") unless arguments.size == 2 + raise(Puppet::ParseError, "delete_regex(): Wrong number of arguments given #{arguments.size} for 2") unless arguments.size == 2 collection = arguments[0].dup Array(arguments[1]).each do |item| @@ -34,8 +34,7 @@ to be matched as an OR. when Array, Hash, String collection.reject! { |coll_item| (coll_item =~ %r{\b#{item}\b}) } else - raise(TypeError, "delete_regex(): First argument must be an Array, " + - "Hash, or String. Given an argument of class #{collection.class}.") + raise(TypeError, "delete_regex(): First argument must be an Array, Hash, or String. Given an argument of class #{collection.class}.") end end collection diff --git a/lib/puppet/parser/functions/delete_undef_values.rb b/lib/puppet/parser/functions/delete_undef_values.rb index f94d4da..00bd252 100644 --- a/lib/puppet/parser/functions/delete_undef_values.rb +++ b/lib/puppet/parser/functions/delete_undef_values.rb @@ -15,13 +15,10 @@ Would return: ['A','',false] EOS ) do |args| - raise(Puppet::ParseError, - "delete_undef_values(): Wrong number of arguments given " + - "(#{args.size})") if args.size < 1 + raise(Puppet::ParseError, "delete_undef_values(): Wrong number of arguments given (#{args.size})") if args.size < 1 unless args[0].is_a? Array or args[0].is_a? Hash - raise(Puppet::ParseError, - "delete_undef_values(): expected an array or hash, got #{args[0]} type #{args[0].class} ") + raise(Puppet::ParseError, "delete_undef_values(): expected an array or hash, got #{args[0]} type #{args[0].class} ") end result = args[0].dup if result.is_a?(Hash) diff --git a/lib/puppet/parser/functions/delete_values.rb b/lib/puppet/parser/functions/delete_values.rb index f6c8c0e..e799aef 100644 --- a/lib/puppet/parser/functions/delete_values.rb +++ b/lib/puppet/parser/functions/delete_values.rb @@ -11,15 +11,12 @@ Would return: {'a'=>'A','c'=>'C','B'=>'D'} EOS ) do |arguments| - raise(Puppet::ParseError, - "delete_values(): Wrong number of arguments given " + - "(#{arguments.size} of 2)") if arguments.size != 2 + raise(Puppet::ParseError, "delete_values(): Wrong number of arguments given (#{arguments.size} of 2)") if arguments.size != 2 hash, item = arguments if not hash.is_a?(Hash) - raise(TypeError, "delete_values(): First argument must be a Hash. " + \ - "Given an argument of class #{hash.class}.") + raise(TypeError, "delete_values(): First argument must be a Hash. Given an argument of class #{hash.class}.") end hash.dup.delete_if { |key, val| item == val } end diff --git a/lib/puppet/parser/functions/deprecation.rb b/lib/puppet/parser/functions/deprecation.rb index cd64fe2..39d306a 100644 --- a/lib/puppet/parser/functions/deprecation.rb +++ b/lib/puppet/parser/functions/deprecation.rb @@ -4,8 +4,7 @@ module Puppet::Parser::Functions EOS ) do |arguments| - raise(Puppet::ParseError, "deprecation: Wrong number of arguments " + - "given (#{arguments.size} for 2)") unless arguments.size == 2 + raise(Puppet::ParseError, "deprecation: Wrong number of arguments given (#{arguments.size} for 2)") unless arguments.size == 2 key = arguments[0] message = arguments[1] diff --git a/lib/puppet/parser/functions/difference.rb b/lib/puppet/parser/functions/difference.rb index cd258f7..c9ac478 100644 --- a/lib/puppet/parser/functions/difference.rb +++ b/lib/puppet/parser/functions/difference.rb @@ -17,8 +17,7 @@ Would return: ["a"] ) do |arguments| # Two arguments are required - raise(Puppet::ParseError, "difference(): Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size != 2 + raise(Puppet::ParseError, "difference(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size != 2 first = arguments[0] second = arguments[1] diff --git a/lib/puppet/parser/functions/dig44.rb b/lib/puppet/parser/functions/dig44.rb index 1e7c318..21c0a8c 100644 --- a/lib/puppet/parser/functions/dig44.rb +++ b/lib/puppet/parser/functions/dig44.rb @@ -42,19 +42,16 @@ missing. And the fourth argument can set a variable path separator. eos ) do |arguments| # Two arguments are required - raise(Puppet::ParseError, "dig44(): Wrong number of arguments " + - "given (#{arguments.size} for at least 2)") if arguments.size < 2 + raise(Puppet::ParseError, "dig44(): Wrong number of arguments given (#{arguments.size} for at least 2)") if arguments.size < 2 data, path, default = *arguments unless data.is_a?(Hash) or data.is_a?(Array) - raise(Puppet::ParseError, "dig44(): first argument must be a hash or an array, " << - "given #{data.class.name}") + raise(Puppet::ParseError, "dig44(): first argument must be a hash or an array, given #{data.class.name}") end unless path.is_a? Array - raise(Puppet::ParseError, "dig44(): second argument must be an array, " << - "given #{path.class.name}") + raise(Puppet::ParseError, "dig44(): second argument must be an array, given #{path.class.name}") end value = path.reduce(data) do |structure, key| diff --git a/lib/puppet/parser/functions/downcase.rb b/lib/puppet/parser/functions/downcase.rb index 040b84f..ab04f8a 100644 --- a/lib/puppet/parser/functions/downcase.rb +++ b/lib/puppet/parser/functions/downcase.rb @@ -1,5 +1,6 @@ # # downcase.rb +# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. # module Puppet::Parser::Functions @@ -8,14 +9,12 @@ Converts the case of a string or all strings in an array to lower case. EOS ) do |arguments| - raise(Puppet::ParseError, "downcase(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "downcase(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'downcase(): Requires either ' + - 'array or string to work with') + raise(Puppet::ParseError, 'downcase(): Requires either array or string to work with') end if value.is_a?(Array) diff --git a/lib/puppet/parser/functions/empty.rb b/lib/puppet/parser/functions/empty.rb index b5a3cde..4f77ad3 100644 --- a/lib/puppet/parser/functions/empty.rb +++ b/lib/puppet/parser/functions/empty.rb @@ -8,14 +8,12 @@ Returns true if the variable is empty. EOS ) do |arguments| - raise(Puppet::ParseError, "empty(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "empty(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] unless value.is_a?(Array) || value.is_a?(Hash) || value.is_a?(String) || value.is_a?(Numeric) - raise(Puppet::ParseError, 'empty(): Requires either ' + - 'array, hash, string or integer to work with') + raise(Puppet::ParseError, 'empty(): Requires either array, hash, string or integer to work with') end if value.is_a?(Numeric) diff --git a/lib/puppet/parser/functions/enclose_ipv6.rb b/lib/puppet/parser/functions/enclose_ipv6.rb index 80ffc3a..1f8a454 100644 --- a/lib/puppet/parser/functions/enclose_ipv6.rb +++ b/lib/puppet/parser/functions/enclose_ipv6.rb @@ -16,12 +16,10 @@ Takes an array of ip addresses and encloses the ipv6 addresses with square brack end if (arguments.size != 1) then - raise(Puppet::ParseError, "enclose_ipv6(): Wrong number of arguments "+ - "given #{arguments.size} for 1") + raise(Puppet::ParseError, "enclose_ipv6(): Wrong number of arguments given #{arguments.size} for 1") end unless arguments[0].is_a?(String) or arguments[0].is_a?(Array) then - raise(Puppet::ParseError, "enclose_ipv6(): Wrong argument type "+ - "given #{arguments[0].class} expected String or Array") + raise(Puppet::ParseError, "enclose_ipv6(): Wrong argument type given #{arguments[0].class} expected String or Array") end input = [arguments[0]].flatten.compact @@ -32,8 +30,7 @@ Takes an array of ip addresses and encloses the ipv6 addresses with square brack begin ip = IPAddr.new(val) rescue *rescuable_exceptions - raise(Puppet::ParseError, "enclose_ipv6(): Wrong argument "+ - "given #{val} is not an ip address.") + raise(Puppet::ParseError, "enclose_ipv6(): Wrong argument given #{val} is not an ip address.") end val = "[#{ip.to_s}]" if ip.ipv6? end diff --git a/lib/puppet/parser/functions/ensure_packages.rb b/lib/puppet/parser/functions/ensure_packages.rb index 439af1e..1bf8bf1 100644 --- a/lib/puppet/parser/functions/ensure_packages.rb +++ b/lib/puppet/parser/functions/ensure_packages.rb @@ -11,15 +11,17 @@ third argument to the ensure_resource() function. ) do |arguments| if arguments.size > 2 or arguments.size == 0 - raise(Puppet::ParseError, "ensure_packages(): Wrong number of arguments " + - "given (#{arguments.size} for 1 or 2)") - elsif arguments.size == 2 and !arguments[1].is_a?(Hash) + raise(Puppet::ParseError, "ensure_packages(): Wrong number of arguments given (#{arguments.size} for 1 or 2)") + elsif arguments.size == 2 and !arguments[1].is_a?(Hash) raise(Puppet::ParseError, 'ensure_packages(): Requires second argument to be a Hash') end if arguments[0].is_a?(Hash) if arguments[1] defaults = { 'ensure' => 'present' }.merge(arguments[1]) + if defaults['ensure'] == 'installed' + defaults['ensure'] = 'present' + end else defaults = { 'ensure' => 'present' } end @@ -31,13 +33,19 @@ third argument to the ensure_resource() function. if arguments[1] defaults = { 'ensure' => 'present' }.merge(arguments[1]) + if defaults['ensure'] == 'installed' + defaults['ensure'] = 'present' + end else defaults = { 'ensure' => 'present' } end Puppet::Parser::Functions.function(:ensure_resource) packages.each { |package_name| + raise(Puppet::ParseError, 'ensure_packages(): Empty String provided for package name') if package_name.length == 0 + if !findresource("Package[#{package_name}]") function_ensure_resource(['package', package_name, defaults ]) + end } end end diff --git a/lib/puppet/parser/functions/flatten.rb b/lib/puppet/parser/functions/flatten.rb index a1ed183..4401bdb 100644 --- a/lib/puppet/parser/functions/flatten.rb +++ b/lib/puppet/parser/functions/flatten.rb @@ -15,8 +15,7 @@ Would return: ['a','b','c'] EOS ) do |arguments| - raise(Puppet::ParseError, "flatten(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size != 1 + raise(Puppet::ParseError, "flatten(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 array = arguments[0] diff --git a/lib/puppet/parser/functions/floor.rb b/lib/puppet/parser/functions/floor.rb index 9a6f014..9e4b504 100644 --- a/lib/puppet/parser/functions/floor.rb +++ b/lib/puppet/parser/functions/floor.rb @@ -5,18 +5,15 @@ module Puppet::Parser::Functions EOS ) do |arguments| - raise(Puppet::ParseError, "floor(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size != 1 + raise(Puppet::ParseError, "floor(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 begin arg = Float(arguments[0]) rescue TypeError, ArgumentError => e - raise(Puppet::ParseError, "floor(): Wrong argument type " + - "given (#{arguments[0]} for Numeric)") + raise(Puppet::ParseError, "floor(): Wrong argument type given (#{arguments[0]} for Numeric)") end - raise(Puppet::ParseError, "floor(): Wrong argument type " + - "given (#{arg.class} for Numeric)") if arg.is_a?(Numeric) == false + raise(Puppet::ParseError, "floor(): Wrong argument type given (#{arg.class} for Numeric)") if arg.is_a?(Numeric) == false arg.floor end diff --git a/lib/puppet/parser/functions/fqdn_rotate.rb b/lib/puppet/parser/functions/fqdn_rotate.rb index b66431d..05bdcc7 100644 --- a/lib/puppet/parser/functions/fqdn_rotate.rb +++ b/lib/puppet/parser/functions/fqdn_rotate.rb @@ -15,15 +15,13 @@ Puppet::Parser::Functions.newfunction( result will be the same every time unless its hostname changes.) Adding a SEED can be useful if you need more than one unrelated rotation.") do |args| - raise(Puppet::ParseError, "fqdn_rotate(): Wrong number of arguments " + - "given (#{args.size} for 1)") if args.size < 1 + raise(Puppet::ParseError, "fqdn_rotate(): Wrong number of arguments given (#{args.size} for 1)") if args.size < 1 value = args.shift require 'digest/md5' unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'fqdn_rotate(): Requires either ' + - 'array or string to work with') + raise(Puppet::ParseError, 'fqdn_rotate(): Requires either array or string to work with') end result = value.clone diff --git a/lib/puppet/parser/functions/glob.rb b/lib/puppet/parser/functions/glob.rb new file mode 100644 index 0000000..54cdda6 --- /dev/null +++ b/lib/puppet/parser/functions/glob.rb @@ -0,0 +1,22 @@ +# +# glob.rb +# + +module Puppet::Parser::Functions + newfunction(:glob, :type => :rvalue, :doc => <<-'EOS' + Returns an Array of file entries of a directory or an Array of directories. + Uses same patterns as Dir#glob + EOS + ) do |arguments| + + raise(Puppet::ParseError, "glob(): Wrong number of arguments given " + + "(#{arguments.size} for 1)") unless arguments.size == 1 + + pattern = arguments[0] + + raise(Puppet::ParseError, 'glob(): Requires either array or string ' + + 'to work') unless pattern.is_a?(String) || pattern.is_a?(Array) + + Dir.glob(pattern) + end +end diff --git a/lib/puppet/parser/functions/grep.rb b/lib/puppet/parser/functions/grep.rb index ceba9ec..c611a7e 100644 --- a/lib/puppet/parser/functions/grep.rb +++ b/lib/puppet/parser/functions/grep.rb @@ -18,8 +18,7 @@ Would return: ) do |arguments| if (arguments.size != 2) then - raise(Puppet::ParseError, "grep(): Wrong number of arguments "+ - "given #{arguments.size} for 2") + raise(Puppet::ParseError, "grep(): Wrong number of arguments given #{arguments.size} for 2") end a = arguments[0] diff --git a/lib/puppet/parser/functions/has_interface_with.rb b/lib/puppet/parser/functions/has_interface_with.rb index e762798..f6cb74b 100644 --- a/lib/puppet/parser/functions/has_interface_with.rb +++ b/lib/puppet/parser/functions/has_interface_with.rb @@ -19,8 +19,7 @@ has_interface_with("lo") => true EOS ) do |args| - raise(Puppet::ParseError, "has_interface_with(): Wrong number of arguments " + - "given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2 + raise(Puppet::ParseError, "has_interface_with(): Wrong number of arguments given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2 interfaces = lookupvar('interfaces') diff --git a/lib/puppet/parser/functions/has_ip_address.rb b/lib/puppet/parser/functions/has_ip_address.rb index 842c8ec..a0071c8 100644 --- a/lib/puppet/parser/functions/has_ip_address.rb +++ b/lib/puppet/parser/functions/has_ip_address.rb @@ -11,8 +11,7 @@ This function iterates through the 'interfaces' fact and checks the EOS ) do |args| - raise(Puppet::ParseError, "has_ip_address(): Wrong number of arguments " + - "given (#{args.size} for 1)") if args.size != 1 + raise(Puppet::ParseError, "has_ip_address(): Wrong number of arguments given (#{args.size} for 1)") if args.size != 1 Puppet::Parser::Functions.autoloader.load(:has_interface_with) \ unless Puppet::Parser::Functions.autoloader.loaded?(:has_interface_with) diff --git a/lib/puppet/parser/functions/has_ip_network.rb b/lib/puppet/parser/functions/has_ip_network.rb index 9ccf902..b02c0c0 100644 --- a/lib/puppet/parser/functions/has_ip_network.rb +++ b/lib/puppet/parser/functions/has_ip_network.rb @@ -11,8 +11,7 @@ This function iterates through the 'interfaces' fact and checks the EOS ) do |args| - raise(Puppet::ParseError, "has_ip_network(): Wrong number of arguments " + - "given (#{args.size} for 1)") if args.size != 1 + raise(Puppet::ParseError, "has_ip_network(): Wrong number of arguments given (#{args.size} for 1)") if args.size != 1 Puppet::Parser::Functions.autoloader.load(:has_interface_with) \ unless Puppet::Parser::Functions.autoloader.loaded?(:has_interface_with) diff --git a/lib/puppet/parser/functions/hash.rb b/lib/puppet/parser/functions/hash.rb index 89d0e07..22763f3 100644 --- a/lib/puppet/parser/functions/hash.rb +++ b/lib/puppet/parser/functions/hash.rb @@ -14,8 +14,7 @@ Would return: {'a'=>1,'b'=>2,'c'=>3} EOS ) do |arguments| - raise(Puppet::ParseError, "hash(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "hash(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 array = arguments[0] @@ -30,8 +29,7 @@ Would return: {'a'=>1,'b'=>2,'c'=>3} array = array.flatten result = Hash[*array] rescue StandardError - raise(Puppet::ParseError, 'hash(): Unable to compute ' + - 'hash from array given') + raise(Puppet::ParseError, 'hash(): Unable to compute hash from array given') end return result diff --git a/lib/puppet/parser/functions/intersection.rb b/lib/puppet/parser/functions/intersection.rb index bfbb4ba..8a438f4 100644 --- a/lib/puppet/parser/functions/intersection.rb +++ b/lib/puppet/parser/functions/intersection.rb @@ -15,8 +15,7 @@ This function returns an array of the intersection of two. ) do |arguments| # Two arguments are required - raise(Puppet::ParseError, "intersection(): Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size != 2 + raise(Puppet::ParseError, "intersection(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size != 2 first = arguments[0] second = arguments[1] diff --git a/lib/puppet/parser/functions/is_array.rb b/lib/puppet/parser/functions/is_array.rb index 1d2c0fa..a33afe4 100644 --- a/lib/puppet/parser/functions/is_array.rb +++ b/lib/puppet/parser/functions/is_array.rb @@ -10,8 +10,7 @@ Returns true if the variable passed to this function is an array. function_deprecation([:is_array, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Array. There is further documentation for validate_legacy function in the README.']) - raise(Puppet::ParseError, "is_array(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "is_array(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 type = arguments[0] diff --git a/lib/puppet/parser/functions/is_bool.rb b/lib/puppet/parser/functions/is_bool.rb index 83d2ebe..2ebe430 100644 --- a/lib/puppet/parser/functions/is_bool.rb +++ b/lib/puppet/parser/functions/is_bool.rb @@ -10,8 +10,7 @@ Returns true if the variable passed to this function is a boolean. function_deprecation([:is_bool, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Bool. There is further documentation for validate_legacy function in the README.']) - raise(Puppet::ParseError, "is_bool(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size != 1 + raise(Puppet::ParseError, "is_bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 type = arguments[0] diff --git a/lib/puppet/parser/functions/is_domain_name.rb b/lib/puppet/parser/functions/is_domain_name.rb index 90ede32..247db3b 100644 --- a/lib/puppet/parser/functions/is_domain_name.rb +++ b/lib/puppet/parser/functions/is_domain_name.rb @@ -9,8 +9,7 @@ Returns true if the string passed to this function is a syntactically correct do ) do |arguments| if (arguments.size != 1) then - raise(Puppet::ParseError, "is_domain_name(): Wrong number of arguments "+ - "given #{arguments.size} for 1") + raise(Puppet::ParseError, "is_domain_name(): Wrong number of arguments given #{arguments.size} for 1") end # Only allow string types diff --git a/lib/puppet/parser/functions/is_email_address.rb b/lib/puppet/parser/functions/is_email_address.rb index 4fb0229..bcd7921 100644 --- a/lib/puppet/parser/functions/is_email_address.rb +++ b/lib/puppet/parser/functions/is_email_address.rb @@ -8,8 +8,7 @@ Returns true if the string passed to this function is a valid email address. EOS ) do |arguments| if arguments.size != 1 - raise(Puppet::ParseError, 'is_email_address(): Wrong number of arguments '\ - "given #{arguments.size} for 1") + raise(Puppet::ParseError, "is_email_address(): Wrong number of arguments given #{arguments.size} for 1") end # Taken from http://emailregex.com/ (simpler regex) diff --git a/lib/puppet/parser/functions/is_float.rb b/lib/puppet/parser/functions/is_float.rb index 1186458..aa84a2b 100644 --- a/lib/puppet/parser/functions/is_float.rb +++ b/lib/puppet/parser/functions/is_float.rb @@ -11,8 +11,7 @@ Returns true if the variable passed to this function is a float. function_deprecation([:is_float, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Float. There is further documentation for validate_legacy function in the README.']) if (arguments.size != 1) then - raise(Puppet::ParseError, "is_float(): Wrong number of arguments "+ - "given #{arguments.size} for 1") + raise(Puppet::ParseError, "is_float(): Wrong number of arguments given #{arguments.size} for 1") end value = arguments[0] diff --git a/lib/puppet/parser/functions/is_function_available.rb b/lib/puppet/parser/functions/is_function_available.rb index 6da82c8..50cd5e1 100644 --- a/lib/puppet/parser/functions/is_function_available.rb +++ b/lib/puppet/parser/functions/is_function_available.rb @@ -11,8 +11,7 @@ true if the function exists, false if not. ) do |arguments| if (arguments.size != 1) then - raise(Puppet::ParseError, "is_function_available?(): Wrong number of arguments "+ - "given #{arguments.size} for 1") + raise(Puppet::ParseError, "is_function_available?(): Wrong number of arguments given #{arguments.size} for 1") end # Only allow String types diff --git a/lib/puppet/parser/functions/is_hash.rb b/lib/puppet/parser/functions/is_hash.rb index ad907f0..3162f7d 100644 --- a/lib/puppet/parser/functions/is_hash.rb +++ b/lib/puppet/parser/functions/is_hash.rb @@ -8,8 +8,7 @@ Returns true if the variable passed to this function is a hash. EOS ) do |arguments| - raise(Puppet::ParseError, "is_hash(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size != 1 + raise(Puppet::ParseError, "is_hash(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 type = arguments[0] diff --git a/lib/puppet/parser/functions/is_integer.rb b/lib/puppet/parser/functions/is_integer.rb index e04fd1f..8965b15 100644 --- a/lib/puppet/parser/functions/is_integer.rb +++ b/lib/puppet/parser/functions/is_integer.rb @@ -16,8 +16,7 @@ If given any other argument `false` is returned. function_deprecation([:is_integer, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Integer. There is further documentation for validate_legacy function in the README.']) if (arguments.size != 1) then - raise(Puppet::ParseError, "is_integer(): Wrong number of arguments "+ - "given #{arguments.size} for 1") + raise(Puppet::ParseError, "is_integer(): Wrong number of arguments given #{arguments.size} for 1") end value = arguments[0] diff --git a/lib/puppet/parser/functions/is_ip_address.rb b/lib/puppet/parser/functions/is_ip_address.rb index 5f1d765..79ddb98 100644 --- a/lib/puppet/parser/functions/is_ip_address.rb +++ b/lib/puppet/parser/functions/is_ip_address.rb @@ -13,8 +13,7 @@ Returns true if the string passed to this function is a valid IP address. function_deprecation([:is_ip_address, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Ip_address. There is further documentation for validate_legacy function in the README.']) if (arguments.size != 1) then - raise(Puppet::ParseError, "is_ip_address(): Wrong number of arguments "+ - "given #{arguments.size} for 1") + raise(Puppet::ParseError, "is_ip_address(): Wrong number of arguments given #{arguments.size} for 1") end begin diff --git a/lib/puppet/parser/functions/is_ipv4_address.rb b/lib/puppet/parser/functions/is_ipv4_address.rb index 1764e61..91869b6 100644 --- a/lib/puppet/parser/functions/is_ipv4_address.rb +++ b/lib/puppet/parser/functions/is_ipv4_address.rb @@ -13,8 +13,7 @@ Returns true if the string passed to this function is a valid IPv4 address. function_deprecation([:is_ipv4_address, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Ipv4. There is further documentation for validate_legacy function in the README.']) if (arguments.size != 1) then - raise(Puppet::ParseError, "is_ipv4_address(): Wrong number of arguments "+ - "given #{arguments.size} for 1") + raise(Puppet::ParseError, "is_ipv4_address(): Wrong number of arguments given #{arguments.size} for 1") end begin diff --git a/lib/puppet/parser/functions/is_ipv6_address.rb b/lib/puppet/parser/functions/is_ipv6_address.rb index 7ca4997..4d96202 100644 --- a/lib/puppet/parser/functions/is_ipv6_address.rb +++ b/lib/puppet/parser/functions/is_ipv6_address.rb @@ -13,8 +13,7 @@ Returns true if the string passed to this function is a valid IPv6 address. require 'ipaddr' if (arguments.size != 1) then - raise(Puppet::ParseError, "is_ipv6_address(): Wrong number of arguments "+ - "given #{arguments.size} for 1") + raise(Puppet::ParseError, "is_ipv6_address(): Wrong number of arguments given #{arguments.size} for 1") end begin diff --git a/lib/puppet/parser/functions/is_mac_address.rb b/lib/puppet/parser/functions/is_mac_address.rb index 2619d44..5993ed2 100644 --- a/lib/puppet/parser/functions/is_mac_address.rb +++ b/lib/puppet/parser/functions/is_mac_address.rb @@ -9,8 +9,7 @@ Returns true if the string passed to this function is a valid mac address. ) do |arguments| if (arguments.size != 1) then - raise(Puppet::ParseError, "is_mac_address(): Wrong number of arguments "+ - "given #{arguments.size} for 1") + raise(Puppet::ParseError, "is_mac_address(): Wrong number of arguments given #{arguments.size} for 1") end mac = arguments[0] diff --git a/lib/puppet/parser/functions/is_numeric.rb b/lib/puppet/parser/functions/is_numeric.rb index 4a55225..7800edd 100644 --- a/lib/puppet/parser/functions/is_numeric.rb +++ b/lib/puppet/parser/functions/is_numeric.rb @@ -27,8 +27,7 @@ Valid examples: function_deprecation([:is_numeric, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Numeric. There is further documentation for validate_legacy function in the README.']) if (arguments.size != 1) then - raise(Puppet::ParseError, "is_numeric(): Wrong number of arguments "+ - "given #{arguments.size} for 1") + raise(Puppet::ParseError, "is_numeric(): Wrong number of arguments given #{arguments.size} for 1") end value = arguments[0] diff --git a/lib/puppet/parser/functions/is_string.rb b/lib/puppet/parser/functions/is_string.rb index 31ee91e..0ed6aec 100644 --- a/lib/puppet/parser/functions/is_string.rb +++ b/lib/puppet/parser/functions/is_string.rb @@ -10,8 +10,7 @@ Returns true if the variable passed to this function is a string. function_deprecation([:is_string, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::String. There is further documentation for validate_legacy function in the README.']) - raise(Puppet::ParseError, "is_string(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "is_string(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 type = arguments[0] diff --git a/lib/puppet/parser/functions/join.rb b/lib/puppet/parser/functions/join.rb index 6c0a6ba..1c17997 100644 --- a/lib/puppet/parser/functions/join.rb +++ b/lib/puppet/parser/functions/join.rb @@ -15,8 +15,7 @@ Would result in: "a,b,c" ) 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 + raise(Puppet::ParseError, "join(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 array = arguments[0] diff --git a/lib/puppet/parser/functions/join_keys_to_values.rb b/lib/puppet/parser/functions/join_keys_to_values.rb index e3baf9f..d8966ba 100644 --- a/lib/puppet/parser/functions/join_keys_to_values.rb +++ b/lib/puppet/parser/functions/join_keys_to_values.rb @@ -23,22 +23,19 @@ Would result in: ["a is 1","b is 2","b is 3"] # Validate the number of arguments. if arguments.size != 2 - raise(Puppet::ParseError, "join_keys_to_values(): Takes exactly two " + - "arguments, but #{arguments.size} given.") + raise(Puppet::ParseError, "join_keys_to_values(): Takes exactly two arguments, but #{arguments.size} given.") end # Validate the first argument. hash = arguments[0] if not hash.is_a?(Hash) - raise(TypeError, "join_keys_to_values(): The first argument must be a " + - "hash, but a #{hash.class} was given.") + raise(TypeError, "join_keys_to_values(): The first argument must be a hash, but a #{hash.class} was given.") end # Validate the second argument. separator = arguments[1] if not separator.is_a?(String) - raise(TypeError, "join_keys_to_values(): The second argument must be a " + - "string, but a #{separator.class} was given.") + raise(TypeError, "join_keys_to_values(): The second argument must be a string, but a #{separator.class} was given.") end # Join the keys to their values. diff --git a/lib/puppet/parser/functions/keys.rb b/lib/puppet/parser/functions/keys.rb index f0d13b6..199e319 100644 --- a/lib/puppet/parser/functions/keys.rb +++ b/lib/puppet/parser/functions/keys.rb @@ -8,8 +8,7 @@ Returns the keys of a hash as an array. EOS ) do |arguments| - raise(Puppet::ParseError, "keys(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "keys(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 hash = arguments[0] diff --git a/lib/puppet/parser/functions/lstrip.rb b/lib/puppet/parser/functions/lstrip.rb index 624e4c8..9a9ee29 100644 --- a/lib/puppet/parser/functions/lstrip.rb +++ b/lib/puppet/parser/functions/lstrip.rb @@ -8,14 +8,12 @@ Strips leading spaces to the left of a string. EOS ) do |arguments| - raise(Puppet::ParseError, "lstrip(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "lstrip(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'lstrip(): Requires either ' + - 'array or string to work with') + raise(Puppet::ParseError, 'lstrip(): Requires either array or string to work with') end if value.is_a?(Array) diff --git a/lib/puppet/parser/functions/max.rb b/lib/puppet/parser/functions/max.rb index 60fb94a..758c42c 100644 --- a/lib/puppet/parser/functions/max.rb +++ b/lib/puppet/parser/functions/max.rb @@ -5,8 +5,7 @@ module Puppet::Parser::Functions EOS ) do |args| - raise(Puppet::ParseError, "max(): Wrong number of arguments " + - "need at least one") if args.size == 0 + raise(Puppet::ParseError, "max(): Wrong number of arguments need at least one") if args.size == 0 # Sometimes we get numbers as numerics and sometimes as strings. # We try to compare them as numbers when possible diff --git a/lib/puppet/parser/functions/member.rb b/lib/puppet/parser/functions/member.rb index 1e5b3de..fb93452 100644 --- a/lib/puppet/parser/functions/member.rb +++ b/lib/puppet/parser/functions/member.rb @@ -30,8 +30,7 @@ would return: false EOS ) do |arguments| - raise(Puppet::ParseError, "member(): Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 + raise(Puppet::ParseError, "member(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size < 2 array = arguments[0] @@ -50,8 +49,7 @@ would return: false end - raise(Puppet::ParseError, 'member(): You must provide item ' + - 'to search for within array given') if item.respond_to?('empty?') && item.empty? + raise(Puppet::ParseError, 'member(): You must provide item to search for within array given') if item.respond_to?('empty?') && item.empty? result = (item - array).empty? diff --git a/lib/puppet/parser/functions/min.rb b/lib/puppet/parser/functions/min.rb index 6bd6ebf..f10a2b2 100644 --- a/lib/puppet/parser/functions/min.rb +++ b/lib/puppet/parser/functions/min.rb @@ -5,8 +5,7 @@ module Puppet::Parser::Functions EOS ) do |args| - raise(Puppet::ParseError, "min(): Wrong number of arguments " + - "need at least one") if args.size == 0 + raise(Puppet::ParseError, "min(): Wrong number of arguments need at least one") if args.size == 0 # Sometimes we get numbers as numerics and sometimes as strings. # We try to compare them as numbers when possible diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index af0e6ed..9ad59b2 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -10,8 +10,7 @@ become true. EOS ) do |arguments| - raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size != 1 + raise(Puppet::ParseError, "num2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 number = arguments[0] diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb index ac1c58a..b1c1e35 100644 --- a/lib/puppet/parser/functions/prefix.rb +++ b/lib/puppet/parser/functions/prefix.rb @@ -15,8 +15,7 @@ Will return: ['pa','pb','pc'] ) 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 + raise(Puppet::ParseError, "prefix(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 enumerable = arguments[0] diff --git a/lib/puppet/parser/functions/pw_hash.rb b/lib/puppet/parser/functions/pw_hash.rb index 41d4223..adcc719 100644 --- a/lib/puppet/parser/functions/pw_hash.rb +++ b/lib/puppet/parser/functions/pw_hash.rb @@ -1,3 +1,6 @@ + +# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. + Puppet::Parser::Functions::newfunction( :pw_hash, :type => :rvalue, @@ -24,6 +27,13 @@ Puppet::Parser::Functions::newfunction( environment contains several different operating systems, ensure that they are compatible before using this function.") do |args| raise ArgumentError, "pw_hash(): wrong number of arguments (#{args.size} for 3)" if args.size != 3 + args.map! do |arg| + if (defined? Puppet::Pops::Types::PSensitiveType::Sensitive) && (arg.is_a? Puppet::Pops::Types::PSensitiveType::Sensitive) + arg.unwrap + else + arg + end + end raise ArgumentError, "pw_hash(): first argument must be a string" unless args[0].is_a? String or args[0].nil? raise ArgumentError, "pw_hash(): second argument must be a string" unless args[1].is_a? String hashes = { 'md5' => '1', diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb index d690df7..72c373a 100644 --- a/lib/puppet/parser/functions/range.rb +++ b/lib/puppet/parser/functions/range.rb @@ -37,8 +37,7 @@ Will return: [0,2,4,6,8] EOS ) do |arguments| - raise(Puppet::ParseError, 'range(): Wrong number of ' + - 'arguments given (0 for 1)') if arguments.size == 0 + raise(Puppet::ParseError, 'range(): Wrong number of arguments given (0 for 1)') if arguments.size == 0 if arguments.size > 1 start = arguments[0] @@ -57,8 +56,7 @@ Will return: [0,2,4,6,8] type = m[2] step = 1 elsif value.match(/^.+$/) - raise(Puppet::ParseError, "range(): Unable to compute range " + - "from the value: #{value}") + raise(Puppet::ParseError, "range(): Unable to compute range from the value: #{value}") else raise(Puppet::ParseError, "range(): Unknown range format: #{value}") end diff --git a/lib/puppet/parser/functions/regexpescape.rb b/lib/puppet/parser/functions/regexpescape.rb index 477ee87..2cfa3bb 100644 --- a/lib/puppet/parser/functions/regexpescape.rb +++ b/lib/puppet/parser/functions/regexpescape.rb @@ -7,14 +7,12 @@ module Puppet::Parser::Functions Requires either a single string or an array as an input. EOS ) do |arguments| # rubocop:disable Style/ClosingParenthesisIndentation - raise(Puppet::ParseError, 'regexpescape(): Wrong number of arguments ' \ - "given (#{arguments.size} for 1)") if arguments.empty? + raise(Puppet::ParseError, "regexpescape(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty? value = arguments[0] unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'regexpescape(): Requires either ' \ - 'array or string to work with') + raise(Puppet::ParseError, 'regexpescape(): Requires either array or string to work with') end result = if value.is_a?(Array) diff --git a/lib/puppet/parser/functions/reverse.rb b/lib/puppet/parser/functions/reverse.rb index 7f1018f..aca98ce 100644 --- a/lib/puppet/parser/functions/reverse.rb +++ b/lib/puppet/parser/functions/reverse.rb @@ -8,14 +8,12 @@ Reverses the order of a string or array. EOS ) do |arguments| - raise(Puppet::ParseError, "reverse(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "reverse(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'reverse(): Requires either ' + - 'array or string to work with') + raise(Puppet::ParseError, 'reverse(): Requires either array or string to work with') end result = value.reverse diff --git a/lib/puppet/parser/functions/round.rb b/lib/puppet/parser/functions/round.rb new file mode 100644 index 0000000..489c301 --- /dev/null +++ b/lib/puppet/parser/functions/round.rb @@ -0,0 +1,33 @@ +# +# round.rb +# + +module Puppet::Parser::Functions + newfunction(:round, :type => :rvalue, :doc => <<-EOS + Rounds a number to the nearest integer + + *Examples:* + + round(2.9) + + returns: 3 + + round(2.4) + + returns: 2 + + EOS + ) do |args| + + raise Puppet::ParseError, "round(): Wrong number of arguments given #{args.size} for 1" if args.size != 1 + raise Puppet::ParseError, "round(): Expected a Numeric, got #{args[0].class}" unless args[0].is_a? Numeric + + value = args[0] + + if value >= 0 + Integer(value + 0.5) + else + Integer(value - 0.5) + end + end +end diff --git a/lib/puppet/parser/functions/rstrip.rb b/lib/puppet/parser/functions/rstrip.rb index 0cf8d22..e24abd5 100644 --- a/lib/puppet/parser/functions/rstrip.rb +++ b/lib/puppet/parser/functions/rstrip.rb @@ -8,14 +8,12 @@ Strips leading spaces to the right of the string. EOS ) do |arguments| - raise(Puppet::ParseError, "rstrip(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "rstrip(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'rstrip(): Requires either ' + - 'array or string to work with') + raise(Puppet::ParseError, 'rstrip(): Requires either array or string to work with') end if value.is_a?(Array) diff --git a/lib/puppet/parser/functions/shell_escape.rb b/lib/puppet/parser/functions/shell_escape.rb index 447fe35..7306b7c 100644 --- a/lib/puppet/parser/functions/shell_escape.rb +++ b/lib/puppet/parser/functions/shell_escape.rb @@ -15,8 +15,7 @@ This function behaves the same as ruby's Shellwords.shellescape() function. EOS ) do |arguments| - raise(Puppet::ParseError, "shell_escape(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size != 1 + raise(Puppet::ParseError, "shell_escape(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 # explicit conversion to string is required for ruby 1.9 string = arguments[0].to_s diff --git a/lib/puppet/parser/functions/shell_join.rb b/lib/puppet/parser/functions/shell_join.rb index 05aeb95..682ed8d 100644 --- a/lib/puppet/parser/functions/shell_join.rb +++ b/lib/puppet/parser/functions/shell_join.rb @@ -13,8 +13,7 @@ This function behaves the same as ruby's Shellwords.shelljoin() function EOS ) do |arguments| - raise(Puppet::ParseError, "shell_join(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size != 1 + raise(Puppet::ParseError, "shell_join(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 array = arguments[0] diff --git a/lib/puppet/parser/functions/shell_split.rb b/lib/puppet/parser/functions/shell_split.rb index 0446448..09e6e78 100644 --- a/lib/puppet/parser/functions/shell_split.rb +++ b/lib/puppet/parser/functions/shell_split.rb @@ -12,8 +12,7 @@ This function behaves the same as ruby's Shellwords.shellsplit() function EOS ) do |arguments| - raise(Puppet::ParseError, "shell_split(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size != 1 + raise(Puppet::ParseError, "shell_split(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 string = arguments[0].to_s diff --git a/lib/puppet/parser/functions/shuffle.rb b/lib/puppet/parser/functions/shuffle.rb index 30c663d..942cbce 100644 --- a/lib/puppet/parser/functions/shuffle.rb +++ b/lib/puppet/parser/functions/shuffle.rb @@ -8,14 +8,12 @@ Randomizes the order of a string or array elements. EOS ) do |arguments| - raise(Puppet::ParseError, "shuffle(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "shuffle(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'shuffle(): Requires either ' + - 'array or string to work with') + raise(Puppet::ParseError, 'shuffle(): Requires either array or string to work with') end result = value.clone diff --git a/lib/puppet/parser/functions/size.rb b/lib/puppet/parser/functions/size.rb index 0d6cc96..b503aa0 100644 --- a/lib/puppet/parser/functions/size.rb +++ b/lib/puppet/parser/functions/size.rb @@ -8,11 +8,12 @@ Returns the number of elements in a string, an array or a hash EOS ) do |arguments| - raise(Puppet::ParseError, "size(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "size(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 item = arguments[0] + function_deprecation([:size, 'This method is going to be deprecated, please use the stdlib length function.']) + if item.is_a?(String) begin @@ -26,8 +27,7 @@ Returns the number of elements in a string, an array or a hash # Float(item) - raise(Puppet::ParseError, 'size(): Requires either ' + - 'string, array or hash to work with') + raise(Puppet::ParseError, 'size(): Requires either string, array or hash to work with') rescue ArgumentError result = item.size diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb index cefbe54..d7792f5 100644 --- a/lib/puppet/parser/functions/sort.rb +++ b/lib/puppet/parser/functions/sort.rb @@ -1,5 +1,6 @@ # -# sort.rb +# sort.rb +# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. # module Puppet::Parser::Functions @@ -9,8 +10,7 @@ Sorts strings and arrays lexically. ) do |arguments| if (arguments.size != 1) then - raise(Puppet::ParseError, "sort(): Wrong number of arguments "+ - "given #{arguments.size} for 1") + raise(Puppet::ParseError, "sort(): Wrong number of arguments given #{arguments.size} for 1") end value = arguments[0] diff --git a/lib/puppet/parser/functions/squeeze.rb b/lib/puppet/parser/functions/squeeze.rb index 81fadfd..f5757d1 100644 --- a/lib/puppet/parser/functions/squeeze.rb +++ b/lib/puppet/parser/functions/squeeze.rb @@ -9,8 +9,7 @@ Returns a new string where runs of the same character that occur in this set are ) do |arguments| if ((arguments.size != 2) and (arguments.size != 1)) then - raise(Puppet::ParseError, "squeeze(): Wrong number of arguments "+ - "given #{arguments.size} for 2 or 1") + raise(Puppet::ParseError, "squeeze(): Wrong number of arguments given #{arguments.size} for 2 or 1") end item = arguments[0] diff --git a/lib/puppet/parser/functions/str2bool.rb b/lib/puppet/parser/functions/str2bool.rb index 472506d..38ad1ce 100644 --- a/lib/puppet/parser/functions/str2bool.rb +++ b/lib/puppet/parser/functions/str2bool.rb @@ -10,8 +10,7 @@ like: 0, F,f, N,n, false, FALSE, no to 'false'. EOS ) do |arguments| - raise(Puppet::ParseError, "str2bool(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "str2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 string = arguments[0] @@ -21,8 +20,7 @@ like: 0, F,f, N,n, false, FALSE, no to 'false'. end unless string.is_a?(String) - raise(Puppet::ParseError, 'str2bool(): Requires ' + - 'string to work with') + raise(Puppet::ParseError, 'str2bool(): Requires string to work with') end # We consider all the yes, no, y, n and so on too ... diff --git a/lib/puppet/parser/functions/str2saltedsha512.rb b/lib/puppet/parser/functions/str2saltedsha512.rb index 7fe7b01..0410c62 100644 --- a/lib/puppet/parser/functions/str2saltedsha512.rb +++ b/lib/puppet/parser/functions/str2saltedsha512.rb @@ -1,5 +1,6 @@ # -# str2saltedsha512.rb +# str2saltedsha512.rb +# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. # module Puppet::Parser::Functions @@ -12,14 +13,12 @@ manifests as a valid password attribute. ) do |arguments| require 'digest/sha2' - raise(Puppet::ParseError, "str2saltedsha512(): Wrong number of arguments " + - "passed (#{arguments.size} but we require 1)") if arguments.size != 1 + raise(Puppet::ParseError, "str2saltedsha512(): Wrong number of arguments passed (#{arguments.size} but we require 1)") if arguments.size != 1 password = arguments[0] unless password.is_a?(String) - raise(Puppet::ParseError, 'str2saltedsha512(): Requires a ' + - "String argument, you passed: #{password.class}") + raise(Puppet::ParseError, "str2saltedsha512(): Requires a String argument, you passed: #{password.class}") end seedint = rand(2**31 - 1) diff --git a/lib/puppet/parser/functions/strftime.rb b/lib/puppet/parser/functions/strftime.rb index 0b52ade..deae329 100644 --- a/lib/puppet/parser/functions/strftime.rb +++ b/lib/puppet/parser/functions/strftime.rb @@ -1,5 +1,6 @@ # -# strftime.rb +# strftime.rb +# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. # module Puppet::Parser::Functions @@ -71,13 +72,11 @@ To return the date: ) 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 + 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? + 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] diff --git a/lib/puppet/parser/functions/strip.rb b/lib/puppet/parser/functions/strip.rb index 3fac47d..9e8366f 100644 --- a/lib/puppet/parser/functions/strip.rb +++ b/lib/puppet/parser/functions/strip.rb @@ -15,14 +15,12 @@ Would result in: "aaa" EOS ) do |arguments| - raise(Puppet::ParseError, "strip(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "strip(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'strip(): Requires either ' + - 'array or string to work with') + raise(Puppet::ParseError, 'strip(): Requires either array or string to work with') end if value.is_a?(Array) diff --git a/lib/puppet/parser/functions/suffix.rb b/lib/puppet/parser/functions/suffix.rb index 2908434..7c5057d 100644 --- a/lib/puppet/parser/functions/suffix.rb +++ b/lib/puppet/parser/functions/suffix.rb @@ -16,8 +16,7 @@ Will return: ['ap','bp','cp'] ) 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 + raise(Puppet::ParseError, "suffix(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 enumerable = arguments[0] diff --git a/lib/puppet/parser/functions/swapcase.rb b/lib/puppet/parser/functions/swapcase.rb index eb7fe13..f3276fb 100644 --- a/lib/puppet/parser/functions/swapcase.rb +++ b/lib/puppet/parser/functions/swapcase.rb @@ -1,5 +1,6 @@ # # swapcase.rb +# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. # module Puppet::Parser::Functions @@ -14,14 +15,12 @@ Would result in: "AbCd" EOS ) do |arguments| - raise(Puppet::ParseError, "swapcase(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "swapcase(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'swapcase(): Requires either ' + - 'array or string to work with') + raise(Puppet::ParseError, 'swapcase(): Requires either array or string to work with') end if value.is_a?(Array) diff --git a/lib/puppet/parser/functions/time.rb b/lib/puppet/parser/functions/time.rb index c574747..d7780c8 100644 --- a/lib/puppet/parser/functions/time.rb +++ b/lib/puppet/parser/functions/time.rb @@ -18,8 +18,7 @@ Will return something like: 1311972653 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") + raise(Puppet::ParseError, "time(): Wrong number of arguments given #{arguments.size} for 0 or 1") end time = Time.new diff --git a/lib/puppet/parser/functions/to_bytes.rb b/lib/puppet/parser/functions/to_bytes.rb index df490ea..bf72503 100644 --- a/lib/puppet/parser/functions/to_bytes.rb +++ b/lib/puppet/parser/functions/to_bytes.rb @@ -7,8 +7,7 @@ module Puppet::Parser::Functions EOS ) do |arguments| - raise(Puppet::ParseError, "to_bytes(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size != 1 + raise(Puppet::ParseError, "to_bytes(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 arg = arguments[0] diff --git a/lib/puppet/parser/functions/type3x.rb b/lib/puppet/parser/functions/type3x.rb index 0800b4a..b17380e 100644 --- a/lib/puppet/parser/functions/type3x.rb +++ b/lib/puppet/parser/functions/type3x.rb @@ -16,8 +16,7 @@ Returns the type when passed a value. Type can be one of: * boolean EOS ) do |args| - raise(Puppet::ParseError, "type3x(): Wrong number of arguments " + - "given (#{args.size} for 1)") if args.size < 1 + raise(Puppet::ParseError, "type3x(): Wrong number of arguments given (#{args.size} for 1)") if args.size < 1 value = args[0] diff --git a/lib/puppet/parser/functions/union.rb b/lib/puppet/parser/functions/union.rb index 6c5bb83..abe2dc8 100644 --- a/lib/puppet/parser/functions/union.rb +++ b/lib/puppet/parser/functions/union.rb @@ -15,8 +15,7 @@ Would return: ["a","b","c","d"] ) do |arguments| # Check that 2 or more arguments have been given ... - raise(Puppet::ParseError, "union(): Wrong number of arguments " + - "given (#{arguments.size} for < 2)") if arguments.size < 2 + raise(Puppet::ParseError, "union(): Wrong number of arguments given (#{arguments.size} for < 2)") if arguments.size < 2 arguments.each do |argument| raise(Puppet::ParseError, 'union(): Every parameter must be an array') unless argument.is_a?(Array) diff --git a/lib/puppet/parser/functions/unique.rb b/lib/puppet/parser/functions/unique.rb index cf770f3..1e2a895 100644 --- a/lib/puppet/parser/functions/unique.rb +++ b/lib/puppet/parser/functions/unique.rb @@ -24,14 +24,16 @@ This returns: EOS ) do |arguments| - raise(Puppet::ParseError, "unique(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + if Puppet::Util::Package.versioncmp(Puppet.version, '5.0.0') >= 0 + function_deprecation([:unique, 'This method is deprecated, please use the core puppet unique function. There is further documentation for the function in the release notes of Puppet 5.0.']) + end + + raise(Puppet::ParseError, "unique(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'unique(): Requires either ' + - 'array or string to work with') + raise(Puppet::ParseError, 'unique(): Requires either array or string to work with') end result = value.clone diff --git a/lib/puppet/parser/functions/upcase.rb b/lib/puppet/parser/functions/upcase.rb index 44b3bcd..e847e51 100644 --- a/lib/puppet/parser/functions/upcase.rb +++ b/lib/puppet/parser/functions/upcase.rb @@ -1,5 +1,6 @@ # -# upcase.rb +# upcase.rb +# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. # module Puppet::Parser::Functions @@ -16,14 +17,12 @@ Will return: EOS ) do |arguments| - raise(Puppet::ParseError, "upcase(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size != 1 + raise(Puppet::ParseError, "upcase(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1 value = arguments[0] unless value.is_a?(Array) || value.is_a?(Hash) || value.respond_to?(:upcase) - raise(Puppet::ParseError, 'upcase(): Requires an ' + - 'array, hash or object that responds to upcase in order to work') + raise(Puppet::ParseError, 'upcase(): Requires an array, hash or object that responds to upcase in order to work') end if value.is_a?(Array) diff --git a/lib/puppet/parser/functions/uriescape.rb b/lib/puppet/parser/functions/uriescape.rb index 45bbed2..4c5c400 100644 --- a/lib/puppet/parser/functions/uriescape.rb +++ b/lib/puppet/parser/functions/uriescape.rb @@ -1,5 +1,6 @@ # # uriescape.rb +# Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085. # require 'uri' @@ -10,14 +11,12 @@ module Puppet::Parser::Functions EOS ) do |arguments| - raise(Puppet::ParseError, "uriescape(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "uriescape(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] unless value.is_a?(Array) || value.is_a?(String) - raise(Puppet::ParseError, 'uriescape(): Requires either ' + - 'array or string to work with') + raise(Puppet::ParseError, 'uriescape(): Requires either array or string to work with') end if value.is_a?(Array) diff --git a/lib/puppet/parser/functions/validate_domain_name.rb b/lib/puppet/parser/functions/validate_domain_name.rb new file mode 100644 index 0000000..c3fad78 --- /dev/null +++ b/lib/puppet/parser/functions/validate_domain_name.rb @@ -0,0 +1,39 @@ +module Puppet::Parser::Functions + newfunction(:validate_domain_name, :doc => <<-ENDHEREDOC + Validate that all values passed are syntactically correct domain names. + Fail compilation if any value fails this check. + + The following values will pass: + + $my_domain_name = 'server.domain.tld' + validate_domain_name($my_domain_name) + validate_domain_name('domain.tld', 'puppet.com', $my_domain_name) + + The following values will fail, causing compilation to abort: + + validate_domain_name(1) + validate_domain_name(true) + validate_domain_name('invalid domain') + validate_domain_name('-foo.example.com') + validate_domain_name('www.example.2com') + + ENDHEREDOC + ) do |args| + + rescuable_exceptions = [ArgumentError] + + if args.empty? + raise Puppet::ParseError, "validate_domain_name(): wrong number of arguments (#{args.length}; must be > 0)" + end + + args.each do |arg| + raise Puppet::ParseError, "#{arg.inspect} is not a string." unless arg.is_a?(String) + + begin + raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name" unless function_is_domain_name([arg]) + rescue *rescuable_exceptions + raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name" + end + end + end +end diff --git a/lib/puppet/parser/functions/values.rb b/lib/puppet/parser/functions/values.rb index 1606756..0ca236c 100644 --- a/lib/puppet/parser/functions/values.rb +++ b/lib/puppet/parser/functions/values.rb @@ -21,8 +21,7 @@ This example would return: EOS ) do |arguments| - raise(Puppet::ParseError, "values(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + raise(Puppet::ParseError, "values(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 hash = arguments[0] diff --git a/lib/puppet/parser/functions/values_at.rb b/lib/puppet/parser/functions/values_at.rb index f350f53..04a3d1a 100644 --- a/lib/puppet/parser/functions/values_at.rb +++ b/lib/puppet/parser/functions/values_at.rb @@ -29,8 +29,7 @@ Would return ['a','c','d']. EOS ) do |arguments| - raise(Puppet::ParseError, "values_at(): Wrong number of " + - "arguments given (#{arguments.size} for 2)") if arguments.size < 2 + raise(Puppet::ParseError, "values_at(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size < 2 array = arguments.shift @@ -41,8 +40,7 @@ Would return ['a','c','d']. indices = [arguments.shift].flatten() # 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') + raise(Puppet::ParseError, 'values_at(): You must provide at least one positive index to collect') end result = [] @@ -57,11 +55,9 @@ Would return ['a','c','d']. type = m[2] if start > stop - raise(Puppet::ParseError, 'values_at(): Stop index in ' + - 'given indices range is smaller than the start index') + 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') + raise(Puppet::ParseError, 'values_at(): Stop index in given indices range exceeds array size') end range = case type @@ -73,16 +69,14 @@ Would return ['a','c','d']. else # Only positive numbers allowed in this case ... if not i.match(/^\d+$/) - raise(Puppet::ParseError, 'values_at(): Unknown format ' + - 'of given index') + 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') + raise(Puppet::ParseError, 'values_at(): Given index exceeds array size') end indices_list << i diff --git a/lib/puppet/parser/functions/zip.rb b/lib/puppet/parser/functions/zip.rb index 3074f28..13e24b6 100644 --- a/lib/puppet/parser/functions/zip.rb +++ b/lib/puppet/parser/functions/zip.rb @@ -17,8 +17,7 @@ Would result in: ) 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 + raise(Puppet::ParseError, "zip(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size < 2 a = arguments[0] b = arguments[1] diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index beeb430..16f2709 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -1,32 +1,42 @@ Puppet::Type.type(:file_line).provide(:ruby) do def exists? - if resource[:replace].to_s != 'true' and count_matches(match_regex) > 0 - true + found = false + lines_count = 0 + lines.each do |line| + found = line.chomp == resource[:line] + if found + lines_count += 1 + end + end + if resource[:match] == nil + found = lines_count > 0 else - lines.find do |line| - if resource[:ensure].to_s == 'absent' and resource[:match_for_absence].to_s == 'true' - line.chomp =~ Regexp.new(resource[:match]) - else - line.chomp == resource[:line].chomp - end + match_count = count_matches(new_match_regex) + if resource[:append_on_no_match].to_s == 'false' + found = true + elsif resource[:replace].to_s == 'true' + found = lines_count > 0 && lines_count == match_count + else + found = match_count > 0 end end + found end def create - unless resource[:replace].to_s != 'true' and count_matches(match_regex) > 0 + unless resource[:replace].to_s != 'true' && count_matches(new_match_regex) > 0 if resource[:match] handle_create_with_match elsif resource[:after] handle_create_with_after else - append_line + handle_append_line end end end def destroy - if resource[:match_for_absence].to_s == 'true' and resource[:match] + if resource[:match_for_absence].to_s == 'true' && resource[:match] handle_destroy_with_match else handle_destroy_line @@ -34,34 +44,49 @@ Puppet::Type.type(:file_line).provide(:ruby) do end private + def lines # If this type is ever used with very large files, we should # write this in a different way, using a temp # file; for now assuming that this type is only used on # small-ish config files that can fit into memory without # too much trouble. - @lines ||= File.readlines(resource[:path]) + begin + @lines ||= File.readlines(resource[:path], :encoding => resource[:encoding]) + rescue TypeError => e + # Ruby 1.8 doesn't support open_args + @lines ||= File.readlines(resource[:path]) + end end - def match_regex + def new_after_regex + resource[:after] ? Regexp.new(resource[:after]) : nil + end + + def new_match_regex resource[:match] ? Regexp.new(resource[:match]) : nil end + def count_matches(regex) + lines.select{ |line| line.match(regex) }.size + end + def handle_create_with_match() - regex_after = resource[:after] ? Regexp.new(resource[:after]) : nil - match_count = count_matches(match_regex) + after_regex = new_after_regex + match_regex = new_match_regex + match_count = count_matches(new_match_regex) if match_count > 1 && resource[:multiple].to_s != 'true' raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'" end File.open(resource[:path], 'w') do |fh| - lines.each do |l| - fh.puts(match_regex.match(l) ? resource[:line] : l) - if (match_count == 0 and regex_after) - if regex_after.match(l) + lines.each do |line| + fh.puts(match_regex.match(line) ? resource[:line] : line) + if match_count == 0 && after_regex + if after_regex.match(line) fh.puts(resource[:line]) - match_count += 1 #Increment match_count to indicate that the new line has been inserted. + match_count += 1 # Increment match_count to indicate that the new line has been inserted. end end end @@ -73,32 +98,29 @@ Puppet::Type.type(:file_line).provide(:ruby) do end def handle_create_with_after - regex = Regexp.new(resource[:after]) - count = count_matches(regex) + after_regex = new_after_regex + after_count = count_matches(after_regex) - if count > 1 && resource[:multiple].to_s != 'true' - raise Puppet::Error, "#{count} lines match pattern '#{resource[:after]}' in file '#{resource[:path]}'. One or no line must match the pattern." + if after_count > 1 && resource[:multiple].to_s != 'true' + raise Puppet::Error, "#{after_count} lines match pattern '#{resource[:after]}' in file '#{resource[:path]}'. One or no line must match the pattern." end - File.open(resource[:path], 'w') do |fh| - lines.each do |l| - fh.puts(l) - if regex.match(l) then + File.open(resource[:path],'w') do |fh| + lines.each do |line| + fh.puts(line) + if after_regex.match(line) fh.puts(resource[:line]) end end - end - if (count == 0) # append the line to the end of the file - append_line + if (after_count == 0) + fh.puts(resource[:line]) + end end end - def count_matches(regex) - lines.select{|l| l.match(regex)}.size - end - def handle_destroy_with_match + match_regex = new_match_regex match_count = count_matches(match_regex) if match_count > 1 && resource[:multiple].to_s != 'true' raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'" @@ -106,27 +128,23 @@ Puppet::Type.type(:file_line).provide(:ruby) do local_lines = lines File.open(resource[:path],'w') do |fh| - fh.write(local_lines.reject{|l| match_regex.match(l) }.join('')) + fh.write(local_lines.reject{ |line| match_regex.match(line) }.join('')) end end def handle_destroy_line local_lines = lines File.open(resource[:path],'w') do |fh| - fh.write(local_lines.reject{|l| l.chomp == resource[:line] }.join('')) + fh.write(local_lines.reject{ |line| line.chomp == resource[:line] }.join('')) end end - ## - # append the line to the file. - # - # @api private - def append_line - File.open(resource[:path], 'w') do |fh| - lines.each do |l| - fh.puts(l) + def handle_append_line + File.open(resource[:path],'w') do |fh| + lines.each do |line| + fh.puts(line) end - fh.puts resource[:line] + fh.puts(resource[:line]) end end end diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 7b7d44e..06be552 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -34,12 +34,16 @@ Puppet::Type.newtype(:file_line) do In this code example match will look for a line beginning with export followed by HTTP_PROXY and replace it with the value in line. - Match Example With `ensure => absent`: + Examples With `ensure => absent`: + + This type has two behaviors when `ensure => absent` is set. + + One possibility is to set `match => ...` and `match_for_absence => true`, + as in the following example: file_line { 'bashrc_proxy': ensure => absent, path => '/etc/bashrc', - line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128', match => '^export\ HTTP_PROXY\=', match_for_absence => true, } @@ -48,6 +52,38 @@ Puppet::Type.newtype(:file_line) do followed by HTTP_PROXY and delete it. If multiple lines match, an error will be raised unless the `multiple => true` parameter is set. + Note that the `line => ...` parameter would be accepted BUT IGNORED in + the above example. + + The second way of using `ensure => absent` is to specify a `line => ...`, + and no match: + + file_line { 'bashrc_proxy': + ensure => absent, + path => '/etc/bashrc', + line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128', + } + + Note that when ensuring lines are absent this way, the default behavior + this time is to always remove all lines matching, and this behavior + can't be disabled. + + Encoding example: + + file_line { "XScreenSaver": + ensure => present, + path => '/root/XScreenSaver' + line => "*lock: 10:00:00", + match => '^*lock:', + encoding => "iso-8859-1", + } + + Files with special characters that are not valid UTF-8 will give the + error message "invalid byte sequence in UTF-8". In this case, determine + the correct file encoding and specify the correct encoding using the + encoding attribute, the value of which needs to be a valid Ruby character + encoding. + **Autorequires:** If Puppet is managing the file that will contain the line being managed, the file_line resource will autorequire that file. EOT @@ -88,8 +124,16 @@ Puppet::Type.newtype(:file_line) do ' This is also takes a regex.' end - newparam(:line) do + # The line property never changes; the type only ever performs a create() or + # destroy(). line is a property in order to allow it to correctly handle + # Sensitive type values. Because it is a property which will never change, + # it should never be considered out of sync. + newproperty(:line) do desc 'The line to be appended to the file or used to replace matches found by the match attribute.' + + def retrieve + @resource[:line] + end end newparam(:path) do @@ -107,6 +151,17 @@ Puppet::Type.newtype(:file_line) do defaultto true end + newparam(:encoding) do + desc 'For files that are not UTF-8 encoded, specify encoding such as iso-8859-1' + defaultto 'UTF-8' + end + + newparam(:append_on_no_match) do + desc 'If true, append line if match is not found. If false, do not append line if a match is not found' + newvalues(true, false) + defaultto true + end + # Autorequire the file resource if it's being managed autorequire(:file) do self[:path] |