Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Porting functions to the modern Puppet 4.x API #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions lib/puppet/functions/access.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 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 ----
require "vine"

#
# access.rb
#

# ---- original file header ----
#
# @summary
#
# Uses Vine gem to deep access hash/array keys
#
#
Puppet::Functions.create_function(:'access') 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)


unless (args.length == 2) || (args.length == 3)
raise Puppet::ParseError, ("access(): wrong number of arguments (#{args.length}; must be 2 or 3)")
end

container = args[0]

if (!container.is_a?(Hash)) && (!container.is_a?(Array))
raise Puppet::ParseError, ("access(): expecting first parameter as Array or Hash, #{container.class} given")
end

keys = args[1]

default = (args[2].nil?) ? nil : args[2]

if container.access(keys).class == NilClass
return default
end

return container.access(keys)


end
end
60 changes: 60 additions & 0 deletions lib/puppet/functions/apache_directories_custom_fragment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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 ----
#
# apache_directories_custom_fragment.rb
#

# ---- original file header ----
#
# @summary
#
# Takes 'directories' hash match puppet-apache vhosts requirement. Explodes custom_fragment list into
# \n separated string.
#
#
Puppet::Functions.create_function(:'apache_directories_custom_fragment') 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)


unless args.length == 1
raise Puppet::ParseError, ("apache_directories_custom_fragment(): wrong number of arguments (#{args.length}; must be 1)")
end

directories = args[0]

unless directories.is_a?(Hash)
raise Puppet::ParseError, ("apache_directories_custom_fragment(): expects a hash)")
end

directories.each do |index, directory|
next if !directory.key?('custom_fragment')
next if !directory['custom_fragment'].is_a?(Array)

directories[index]['custom_fragment'] = directory['custom_fragment'].join("\n ")
end

return directories


end
end
77 changes: 77 additions & 0 deletions lib/puppet/functions/array_true.rb
Original file line number Diff line number Diff line change
@@ -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 ----
dir = File.dirname(File.expand_path(__FILE__))
require "#{dir}/to_bool.rb"

#
# array_true.rb
#

# ---- original file header ----
#
# @summary
#
# Returns true if value in array exists and is truthy
#
#
Puppet::Functions.create_function(:'array_true') 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)


unless args.length == 2
raise Puppet::ParseError, ("array_true(): wrong number of arguments (#{args.length}; must be 2)")
end

container = args[0]

if (!container.is_a?(Hash)) && (!container.is_a?(Array))
return false
end

keys = (args[1].is_a?(Array)) ? args[1] : [args[1]]

# If multiple values passed to check,
# all must pass truthyness check to return true
keys.each do |key|
if !container.has_key?(key)
return false
end

if (container[key].is_a?(Hash)) || (container[key].is_a?(Array))
if !(container[key].count > 0)
return false
end

next
end

if !(container[key].to_bool)
return false
end
end

return true


end
end
45 changes: 45 additions & 0 deletions lib/puppet/functions/deep_merge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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 ----
require 'deep_merge'

# ---- original file header ----
#
# @summary
# Deep merges two hashes using Hash#deep_merge
#
#
Puppet::Functions.create_function(:'deep_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)


if args.length < 2
raise Puppet::ParseError, ("deep_merge(): wrong number of arguments (#{args.length}; must be at least 2)")
end

hashA = args[0]
hashB = args[1]

return hashA.deep_merge!(hashB)

end
end
52 changes: 52 additions & 0 deletions lib/puppet/functions/hash_eval.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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_eval.rb
#

# ---- original file header ----
#
# @summary
#
# Turns Hash#inspect string back to hash
#
#
Puppet::Functions.create_function(:'hash_eval') 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)


unless args.length == 1
raise Puppet::ParseError, ("hash_eval(): wrong number of arguments (#{args.length}; must be 1)")
end

hash_string = args[0]

if (!hash_string.is_a?(String))
raise Puppet::ParseError, ("hash_eval(): expects string representation of a hash)")
end

return eval(hash_string)


end
end
63 changes: 63 additions & 0 deletions lib/puppet/functions/hash_key_equals.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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_key_equals.rb
#

# ---- original file header ----
#
# @summary
#
# Returns true if the variable passed to this function is a hash,
# it contains the key requested, and the key matches value.
#
#
Puppet::Functions.create_function(:'hash_key_equals') 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)


unless args.length == 3
raise Puppet::ParseError, ("hash_key_equals(): wrong number of arguments (#{args.length}; must be 3)")
end

hashVar = args[0]
keyVar = args[1]
expectedValue = args[2]

unless hashVar.is_a?(Hash)
return false
end

unless hashVar.has_key?(keyVar)
return false
end

unless hashVar[keyVar].to_i == expectedValue.to_i
return false
end

return true


end
end
Loading