diff --git a/lib/puppet/functions/sfu_stdlib/default_service_provider_merge.rb b/lib/puppet/functions/sfu_stdlib/default_service_provider_merge.rb new file mode 100644 index 0000000..a4b0721 --- /dev/null +++ b/lib/puppet/functions/sfu_stdlib/default_service_provider_merge.rb @@ -0,0 +1,77 @@ +# This is an autogenerated function, ported from the original legacy version. +# It /should work/ as is, but will not have all the benefits of the modern +# function API. You should see the function docs to learn how to add function +# signatures for type safety and to document this function using puppet-strings. +# +# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html +# +# ---- original file header ---- +# +# default_service_provider_merge.rb +# + +# ---- original file header ---- +# +# @summary +# Returns the default service provider for the system overridden by any parent class +#that has Service { provider => $my_service_provider } specified to cause an override. +#This is useful when we actually need to know the clients service provider and allow them +#to also override it in case they decide they need a different one due to puppet not picking +#the right one for their new or old Operating System. +# +#Also useful if we need to be able to send a service stop in order to do something +#life replacing a config or binary that cannot be replace while the service is running +#This can be done via another function, inline_template or exec, prior to letting puppet DSL +#complete it's usual state management of +# +#service { 'service' ensure => running } +# +#Usage possibilities below: +# +# $default_provider = default_service_provider_merge +# inline_template("<% Puppet::Type.type(:service).newservice(:name => 'service', :provider => #{default_provider}).provider.send('stop') %>") +# +# or: +# +# $default_provider = default_service_provider_merge +# exec { 'stop_this_service': +# command => "puppet resource service ${service_name} provider=${default_provider} ensure=stopped", +# path => $::path +# } +# +# or: +# +# # This method is 0.4 seconds faster than the Puppet Face (puppet resource service) method above but may be less supported? +# $default_provider = default_service_provider_merge +# exec { 'stop_this_service': +# command => "ruby -r 'puppet' -e "Puppet::Type.type(:service).newservice(:name => 'service', :provider => ${default_provider}).provider.send('stop')"", +# path => $::path +# } +# +# +# +Puppet::Functions.create_function(:'sfu_stdlib::default_service_provider_merge') do + # @param args + # The original array of arguments. Port this to individually managed params + # to get the full benefit of the modern function API. + # + # @return [Data type] + # Describe what the function returns here + # + dispatch :default_impl do + # Call the method named 'default_impl' when this is matched + # Port this to match individual params for better type safety + repeated_param 'Any', :args + end + + + def default_impl(*args) + + #override = scope.lookupdefaults('Service')[:provider].instance_variable_get(:@value) + override = self.lookupdefaults('Service')[:provider].instance_variable_get(:@value) + override = lookupvar('service_provider') if override.nil? || override.empty? + + return override + + end +end diff --git a/lib/puppet/functions/sfu_stdlib/hash_deep_sort.rb b/lib/puppet/functions/sfu_stdlib/hash_deep_sort.rb new file mode 100644 index 0000000..ff83097 --- /dev/null +++ b/lib/puppet/functions/sfu_stdlib/hash_deep_sort.rb @@ -0,0 +1,55 @@ +# This is an autogenerated function, ported from the original legacy version. +# It /should work/ as is, but will not have all the benefits of the modern +# function API. You should see the function docs to learn how to add function +# signatures for type safety and to document this function using puppet-strings. +# +# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html +# +# ---- original file header ---- +# +# hash_deep_sort.rb +# +class Hash + def deep_sort + Hash[sort.map {|k, v| [k, v.is_a?(Hash) ? v.deep_sort : v]}] + end +end + +# ---- original file header ---- +# +# @summary +# This will sort your supplied hash all levels deep based on key name +# +# +Puppet::Functions.create_function(:'sfu_stdlib::hash_deep_sort') do + # @param arguments + # The original array of arguments. Port this to individually managed params + # to get the full benefit of the modern function API. + # + # @return [Data type] + # Describe what the function returns here + # + dispatch :default_impl do + # Call the method named 'default_impl' when this is matched + # Port this to match individual params for better type safety + repeated_param 'Any', :arguments + end + + + def default_impl(*arguments) + + + raise(Puppet::ParseError, "hash_deep_sort(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size != 1 + + hash = arguments[0] + + unless hash.is_a?(Hash) + raise(Puppet::ParseError, 'hash_deep_sort(): Requires a ' + + 'hash to work with') + end + + hash.deep_sort + + end +end diff --git a/lib/puppet/functions/sfu_stdlib/merge_array_of_hash.rb b/lib/puppet/functions/sfu_stdlib/merge_array_of_hash.rb new file mode 100644 index 0000000..88730ac --- /dev/null +++ b/lib/puppet/functions/sfu_stdlib/merge_array_of_hash.rb @@ -0,0 +1,79 @@ +# This is an autogenerated function, ported from the original legacy version. +# It /should work/ as is, but will not have all the benefits of the modern +# function API. You should see the function docs to learn how to add function +# signatures for type safety and to document this function using puppet-strings. +# +# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html +# +# ---- original file header ---- + +# ---- original file header ---- +# +# @summary +# Merges two or more hashes together into an array of hashes and returns the resulting hash merges inside an array. +# +# For example: +# +# $hash1 = [{'name' => 'testrule', 'value', => 'testvalue'}, {'name' => 'secondtestrule', 'value' => 'secondtestvalue', 'three' => 'losingthree'}] +# $hash2 = {'two' => 'dos', 'three', => 'tres'} +# $merged_hash = merge($hash1, $hash2) +# # The resulting hash is equivalent to: +# # $merged_hash = [{'name' => 'testrule', 'value' => 'testvalue', 'two' => 'dos', 'three' => 'tres'}, {'name' => 'secondtestrule', 'value' => 'secondtestvalue', 'two' => 'dos', 'three' => 'tres'}] +# +# When there is a duplicate key, the key in the rightmost hash will "win." +# +# +# +Puppet::Functions.create_function(:'sfu_stdlib::merge_array_of_hash') do + # @param args + # The original array of arguments. Port this to individually managed params + # to get the full benefit of the modern function API. + # + # @return [Data type] + # Describe what the function returns here + # + dispatch :default_impl do + # Call the method named 'default_impl' when this is matched + # Port this to match individual params for better type safety + repeated_param 'Any', :args + end + + + def default_impl(*args) + + + if args.length < 2 + raise Puppet::ParseError, ("merge(): wrong number of arguments (#{args.length}; must be at least 2)") + end + + hash_array = args[0] + + # This is just incase they happen to pass us a hash that's not in an array as their source hash + hash_array = [hash_array] unless hash_array.is_a?(Array) + + result = Array.new + + hash_array.each do |arr| + next if arr.is_a? String and arr.empty? # empty string is synonym for puppet's undef + unless arr.is_a?(Hash) + raise Puppet::ParseError, "merge: unexpected argument type #{arr.class}, only expects hash arguments" + end + # Swap args[0] so that we place each hash, one at a time, into the merge loop + args[0] = arr + # The hash we accumulate into + accumulator = Hash.new + # Merge into the accumulator hash + args.each do |arg| + 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 + accumulator.merge!(arg) + end + # Place the fully merged hash into the array + result << accumulator + end + return result + + end +end diff --git a/lib/puppet/functions/sfu_stdlib/str2hash.rb b/lib/puppet/functions/sfu_stdlib/str2hash.rb new file mode 100644 index 0000000..17a3477 --- /dev/null +++ b/lib/puppet/functions/sfu_stdlib/str2hash.rb @@ -0,0 +1,81 @@ +# This is an autogenerated function, ported from the original legacy version. +# It /should work/ as is, but will not have all the benefits of the modern +# function API. You should see the function docs to learn how to add function +# signatures for type safety and to document this function using puppet-strings. +# +# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html +# +# ---- original file header ---- +# +# str2hash.rb +# +require 'json' + +# ---- original file header ---- +# +# @summary +# This converts a string to a hash. This attempt to convert strings that +#represent hashes, in any form, back to hash. +# +# +Puppet::Functions.create_function(:'sfu_stdlib::str2hash') do + # @param arguments + # The original array of arguments. Port this to individually managed params + # to get the full benefit of the modern function API. + # + # @return [Data type] + # Describe what the function returns here + # + dispatch :default_impl do + # Call the method named 'default_impl' when this is matched + # Port this to match individual params for better type safety + repeated_param 'Any', :arguments + end + + + def default_impl(*arguments) + + + raise(Puppet::ParseError, "str2hash(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + string = arguments[0] + + # If string is already Hash, return it + if string.is_a?(Hash) + return string + end + + unless string.is_a?(String) + raise(Puppet::ParseError, 'str2hash(): Requires either ' + + 'string to work with') + end + + # If the string is a hash inside an array representation, we clear the array part + string.sub!(/^\[(\{.*\})\]$/,'\1') + + # Transform object string symbols to quoted strings + string.gsub!(/([{,]\s*):([^>\s]+)\s*=>/, '\1"\2"=>') + + # Transform object string numbers to quoted strings + string.gsub!(/([{,]\s*)([0-9]+\.?[0-9]*)\s*=>/, '\1"\2"=>') + + # Transform object value symbols to quotes strings + string.gsub!(/([{,]\s*)(".+?"|[0-9]+\.?[0-9]*)\s*=>\s*:([^,}\s]+\s*)/, '\1\2=>"\3"') + + # Transform array value symbols to quotes strings + string.gsub!(/([\[,]\s*):([^,\]\s]+)/, '\1"\2"') + + # Transform object string object value delimiter to colon delimiter + string.gsub!(/([{,]\s*)(".+?"|[0-9]+\.?[0-9]*)\s*=>/, '\1\2:') + + begin + result = JSON.parse(string) + rescue + raise(Puppet::ParseError, 'str2hash(): Unknown type of hash given') + end + + return result + + end +end diff --git a/spec/functions/sfu_stdlib_default_service_provider_merge_spec.rb b/spec/functions/sfu_stdlib_default_service_provider_merge_spec.rb new file mode 100644 index 0000000..1c2015e --- /dev/null +++ b/spec/functions/sfu_stdlib_default_service_provider_merge_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'sfu_stdlib::default_service_provider_merge' do + # without knowing details about the implementation, this is the only test + # case that we can autogenerate. You should add more examples below! + it { is_expected.not_to eq(nil) } + +################################# +# Below are some example test cases. You may uncomment and modify them to match +# your needs. Notice that they all expect the base error class of `StandardError`. +# This is because the autogenerated function uses an untyped array for parameters +# and relies on your implementation to do the validation. As you convert your +# function to proper dispatches and typed signatures, you should change the +# expected error of the argument validation examples to `ArgumentError`. +# +# Other error types you might encounter include +# +# * StandardError +# * ArgumentError +# * Puppet::ParseError +# +# Read more about writing function unit tests at https://rspec-puppet.com/documentation/functions/ +# +# it 'raises an error if called with no argument' do +# is_expected.to run.with_params.and_raise_error(StandardError) +# end +# +# it 'raises an error if there is more than 1 arguments' do +# is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(StandardError) +# end +# +# it 'raises an error if argument is not the proper type' do +# is_expected.to run.with_params('foo').and_raise_error(StandardError) +# end +# +# it 'returns the proper output' do +# is_expected.to run.with_params(123).and_return('the expected output') +# end +################################# + +end diff --git a/spec/functions/sfu_stdlib_hash_deep_sort_spec.rb b/spec/functions/sfu_stdlib_hash_deep_sort_spec.rb new file mode 100644 index 0000000..b2d4cc5 --- /dev/null +++ b/spec/functions/sfu_stdlib_hash_deep_sort_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'sfu_stdlib::hash_deep_sort' do + # without knowing details about the implementation, this is the only test + # case that we can autogenerate. You should add more examples below! + it { is_expected.not_to eq(nil) } + +################################# +# Below are some example test cases. You may uncomment and modify them to match +# your needs. Notice that they all expect the base error class of `StandardError`. +# This is because the autogenerated function uses an untyped array for parameters +# and relies on your implementation to do the validation. As you convert your +# function to proper dispatches and typed signatures, you should change the +# expected error of the argument validation examples to `ArgumentError`. +# +# Other error types you might encounter include +# +# * StandardError +# * ArgumentError +# * Puppet::ParseError +# +# Read more about writing function unit tests at https://rspec-puppet.com/documentation/functions/ +# +# it 'raises an error if called with no argument' do +# is_expected.to run.with_params.and_raise_error(StandardError) +# end +# +# it 'raises an error if there is more than 1 arguments' do +# is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(StandardError) +# end +# +# it 'raises an error if argument is not the proper type' do +# is_expected.to run.with_params('foo').and_raise_error(StandardError) +# end +# +# it 'returns the proper output' do +# is_expected.to run.with_params(123).and_return('the expected output') +# end +################################# + +end diff --git a/spec/functions/sfu_stdlib_merge_array_of_hash_spec.rb b/spec/functions/sfu_stdlib_merge_array_of_hash_spec.rb new file mode 100644 index 0000000..3c2817c --- /dev/null +++ b/spec/functions/sfu_stdlib_merge_array_of_hash_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'sfu_stdlib::merge_array_of_hash' do + # without knowing details about the implementation, this is the only test + # case that we can autogenerate. You should add more examples below! + it { is_expected.not_to eq(nil) } + +################################# +# Below are some example test cases. You may uncomment and modify them to match +# your needs. Notice that they all expect the base error class of `StandardError`. +# This is because the autogenerated function uses an untyped array for parameters +# and relies on your implementation to do the validation. As you convert your +# function to proper dispatches and typed signatures, you should change the +# expected error of the argument validation examples to `ArgumentError`. +# +# Other error types you might encounter include +# +# * StandardError +# * ArgumentError +# * Puppet::ParseError +# +# Read more about writing function unit tests at https://rspec-puppet.com/documentation/functions/ +# +# it 'raises an error if called with no argument' do +# is_expected.to run.with_params.and_raise_error(StandardError) +# end +# +# it 'raises an error if there is more than 1 arguments' do +# is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(StandardError) +# end +# +# it 'raises an error if argument is not the proper type' do +# is_expected.to run.with_params('foo').and_raise_error(StandardError) +# end +# +# it 'returns the proper output' do +# is_expected.to run.with_params(123).and_return('the expected output') +# end +################################# + +end diff --git a/spec/functions/sfu_stdlib_str2hash_spec.rb b/spec/functions/sfu_stdlib_str2hash_spec.rb new file mode 100644 index 0000000..3f090be --- /dev/null +++ b/spec/functions/sfu_stdlib_str2hash_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'sfu_stdlib::str2hash' do + # without knowing details about the implementation, this is the only test + # case that we can autogenerate. You should add more examples below! + it { is_expected.not_to eq(nil) } + +################################# +# Below are some example test cases. You may uncomment and modify them to match +# your needs. Notice that they all expect the base error class of `StandardError`. +# This is because the autogenerated function uses an untyped array for parameters +# and relies on your implementation to do the validation. As you convert your +# function to proper dispatches and typed signatures, you should change the +# expected error of the argument validation examples to `ArgumentError`. +# +# Other error types you might encounter include +# +# * StandardError +# * ArgumentError +# * Puppet::ParseError +# +# Read more about writing function unit tests at https://rspec-puppet.com/documentation/functions/ +# +# it 'raises an error if called with no argument' do +# is_expected.to run.with_params.and_raise_error(StandardError) +# end +# +# it 'raises an error if there is more than 1 arguments' do +# is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(StandardError) +# end +# +# it 'raises an error if argument is not the proper type' do +# is_expected.to run.with_params('foo').and_raise_error(StandardError) +# end +# +# it 'returns the proper output' do +# is_expected.to run.with_params(123).and_return('the expected output') +# end +################################# + +end