# frozen_string_literal: true
# @summary
# This function accepts HOCON as a string and converts it into the correct
# Puppet structure
#
# @return
# Data
#
# @example How to parse hocon
# $data = parsehocon("{any valid hocon: string}")
#
Puppet::Functions.create_function(:parsehocon) do
# @param hocon_string A valid HOCON string
# @param default An optional default to return if parsing hocon_string fails
dispatch :parsehocon do
param 'String', :hocon_string
optional_param 'Any', :default
end
def parsehocon(hocon_string, default = :no_default_provided)
require 'hocon/config_factory'
begin
data = Hocon::ConfigFactory.parse_string(hocon_string)
data.resolve.root.unwrapped
rescue Hocon::ConfigError::ConfigParseError => err
Puppet.debug("Parsing hocon failed with error: #{err.message}")
raise err if default == :no_default_provided
default
end
end
end
# frozen_string_literal: true # Generates a consistent random string of specific length based on provided seed.
# @summary
# Generates a consistent random string of specific length based on provided seed.
# #
# @example Generate a consistently random string of length 8 with a seed: # @example Generate a consistently random string of length 8 with a seed:
# seeded_rand_string(8, "${module_name}::redis_password") # seeded_rand_string(8, "${module_name}::redis_password")
......
# frozen_string_literal: true # Uses sprintf with named references.
# @summary
# Uses sprintf with named references.
# #
# The first parameter is format string describing how the rest of the parameters in the hash # The first parameter is format string describing how the rest of the parameters in the hash
# should be formatted. See the documentation for the `Kernel::sprintf` function in Ruby for # should be formatted. See the documentation for the `Kernel::sprintf` function in Ruby for
......
# frozen_string_literal: true
# @summary
# Returns true if str ends with one of the prefixes given. Each of the prefixes should be a String.
#
Puppet::Functions.create_function(:'stdlib::end_with') do
# @param test_string The string to check
# @param suffixes The suffixes to check
# @example
# 'foobar'.stdlib::end_with('bar') => true
# 'foobar'.stdlib::end_with('foo') => false
# 'foobar'.stdlib::end_with(['foo', 'baz']) => false
# @return [Boolean] True or False
dispatch :end_with do
param 'String', :test_string
param 'Variant[String[1],Array[String[1], 1]]', :suffixes
return_type 'Boolean'
end
def end_with(test_string, suffixes)
test_string.end_with?(*suffixes)
end
end
# frozen_string_literal: true # Returns the Extension (the Portion of Filename in Path starting from the
# last Period).
# @summary
# Returns the Extension (the Portion of Filename in Path starting from the
# last Period).
# #
# If Path is a Dotfile, or starts with a Period, then the starting Dot is not # If Path is a Dotfile, or starts with a Period, then the starting Dot is not
# dealt with the Start of the Extension. # dealt with the Start of the Extension.
# #
# An empty String will also be returned, when the Period is the last Character # An empty String will also be returned, when the Period is the last Character
# in Path. # in Path.
Puppet::Functions.create_function(:'stdlib::extname') do Puppet::Functions.create_function(:'stdlib::extname') do
# @param filename The Filename # @param filename The Filename
# @return [String] The Extension starting from the last Period # @return [String] The Extension starting from the last Period
......
# frozen_string_literal: true
# @summary
# Returns true if the ipaddress is within the given CIDRs
#
# @example ip_in_range(<IPv4 Address>, <IPv4 CIDR>)
# stdlib::ip_in_range('10.10.10.53', '10.10.10.0/24') => true
Puppet::Functions.create_function(:'stdlib::ip_in_range') do
# @param ipaddress The IP address to check
# @param range One CIDR or an array of CIDRs
# defining the range(s) to check against
#
# @return [Boolean] True or False
dispatch :ip_in_range do
param 'String', :ipaddress
param 'Variant[String, Array]', :range
return_type 'Boolean'
end
require 'ipaddr'
def ip_in_range(ipaddress, range)
ip = IPAddr.new(ipaddress)
if range.is_a? Array
ranges = range.map { |r| IPAddr.new(r) }
ranges.any? { |rng| rng.include?(ip) }
elsif range.is_a? String
ranges = IPAddr.new(range)
ranges.include?(ip)
end
end
end
# frozen_string_literal: true
# @summary
# Returns true if str starts with one of the prefixes given. Each of the prefixes should be a String.
#
Puppet::Functions.create_function(:'stdlib::start_with') do
# @param test_string The string to check
# @param prefixes The prefixes to check.
# @example
# 'foobar'.stdlib::start_with('foo') => true
# 'foobar'.stdlib::start_with('bar') => false
# 'foObar'.stdlib::start_with(['bar', 'baz']) => false
# @return [Boolean] True or False
dispatch :start_with do
param 'String', :test_string
param 'Variant[String[1],Array[String[1], 1]]', :prefixes
return_type 'Boolean'
end
def start_with(test_string, prefixes)
test_string.start_with?(*prefixes)
end
end
# frozen_string_literal: true # Take a data structure and output it as JSON
require 'json'
# @summary
# Convert a data structure and output to JSON
# #
# @example how to output JSON # @example how to output JSON
# # output json to a file # # output json to a file
...@@ -11,10 +7,10 @@ require 'json' ...@@ -11,10 +7,10 @@ require 'json'
# content => to_json($myhash), # content => to_json($myhash),
# } # }
# #
#
require 'json'
Puppet::Functions.create_function(:to_json) do Puppet::Functions.create_function(:to_json) do
# @param data
# data structure which needs to be converted into JSON
# @return converted data to json
dispatch :to_json do dispatch :to_json do
param 'Any', :data param 'Any', :data
end end
......
# frozen_string_literal: true # Take a data structure and output it as pretty JSON
require 'json'
# @summary
# Convert data structure and output to pretty JSON
# #
# @example **Usage** # @example how to output pretty JSON
# * how to output pretty JSON to file # # output pretty json to a file
# file { '/tmp/my.json': # file { '/tmp/my.json':
# ensure => file, # ensure => file,
# content => to_json_pretty($myhash), # content => to_json_pretty($myhash),
# } # }
# #
# * how to output pretty JSON skipping over keys with undef values # @example how to output pretty JSON skipping over keys with undef values
# # output pretty JSON to a file skipping over undef values
# file { '/tmp/my.json': # file { '/tmp/my.json':
# ensure => file, # ensure => file,
# content => to_json_pretty({ # content => to_json_pretty({
# param_one => 'value', # param_one => 'value',
# param_two => undef, # param_two => undef,
# }, true), # }),
# } # }
# #
# * how to output pretty JSON using tabs for indentation require 'json'
# file { '/tmp/my.json':
# ensure => file,
# content => to_json_pretty({
# param_one => 'value',
# param_two => {
# param_more => 42,
# },
# }, nil, {indent => ' '}),
# }
Puppet::Functions.create_function(:to_json_pretty) do Puppet::Functions.create_function(:to_json_pretty) do
# @param data
# data structure which needs to be converted to pretty json
# @param skip_undef
# value `true` or `false`
# @param opts
# hash-map of settings passed to JSON.pretty_generate, see
# https://ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html#method-i-generate.
# Note that `max_nesting` doesn't take the value `false`; use `-1` instead.
# @return
# converted data to pretty json
dispatch :to_json_pretty do dispatch :to_json_pretty do
param 'Variant[Hash, Array]', :data param 'Variant[Hash, Array]', :data
optional_param 'Optional[Boolean]', :skip_undef optional_param 'Boolean', :skip_undef
optional_param 'Struct[{
indent => Optional[String],
space => Optional[String],
space_before => Optional[String],
object_nl => Optional[String],
array_nl => Optional[String],
allow_nan => Optional[Boolean],
max_nesting => Optional[Integer[-1,default]],
}]', :opts
end end
def to_json_pretty(data, skip_undef = false, opts = nil) def to_json_pretty(data, skip_undef = false)
# It's not possible to make an abstract type that can be either a boolean
# false or an integer, so we use -1 as the falsey value
if opts
opts = Hash[opts.map { |k, v| [k.to_sym, v] }]
if opts[:max_nesting] == -1
opts[:max_nesting] = false
end
end
if skip_undef if skip_undef
if data.is_a? Array if data.is_a? Array
data = data.reject { |value| value.nil? } data = data.reject { |value| value.nil? }
...@@ -75,8 +33,6 @@ max_nesting => Optional[Integer[-1,default]], ...@@ -75,8 +33,6 @@ max_nesting => Optional[Integer[-1,default]],
data = data.reject { |_, value| value.nil? } data = data.reject { |_, value| value.nil? }
end end
end end
# Call ::JSON to ensure it references the JSON library from Ruby's standard library JSON.pretty_generate(data) << "\n"
# instead of a random JSON namespace that might be in scope due to user code.
::JSON.pretty_generate(data, opts) << "\n"
end end
end end
# frozen_string_literal: true # Take a data structure and output it as YAML
require 'yaml'
# @summary
# Convert a data structure and output it as YAML
# #
# @example How to output YAML # @example how to output YAML
# # output yaml to a file # # output yaml to a file
# file { '/tmp/my.yaml': # file { '/tmp/my.yaml':
# ensure => file, # ensure => file,
# content => to_yaml($myhash), # content => to_yaml($myhash),
# } # }
# @example Use options control the output format #
# file { '/tmp/my.yaml': #
# ensure => file, require 'yaml'
# content => to_yaml($myhash, {indentation: 4})
# }
Puppet::Functions.create_function(:to_yaml) do Puppet::Functions.create_function(:to_yaml) do
# @param data
# @param options
#
# @return [String]
dispatch :to_yaml do dispatch :to_yaml do
param 'Any', :data param 'Any', :data
optional_param 'Hash', :options
end end
def to_yaml(data, options = {}) def to_yaml(data)
data.to_yaml(options) data.to_yaml
end end
end end
# frozen_string_literal: true # Returns the type when passed a value.
# @summary
# Returns the type of the passed value.
# #
# @example how to compare values' types # @example how to compare values' types
# # compare the types of two values # # compare the types of two values
...@@ -16,10 +13,6 @@ ...@@ -16,10 +13,6 @@
# The built-in type() function in puppet is generally preferred over this function # The built-in type() function in puppet is generally preferred over this function
# this function is provided for backwards compatibility. # this function is provided for backwards compatibility.
Puppet::Functions.create_function(:type_of) do Puppet::Functions.create_function(:type_of) do
# @return [String]
# the type of the passed value
#
# @param value
def type_of(value) def type_of(value)
Puppet::Pops::Types::TypeCalculator.infer_set(value) Puppet::Pops::Types::TypeCalculator.infer_set(value)
end end
......
# frozen_string_literal: true
# @summary
# Validate the string represents an absolute path in the filesystem.
Puppet::Functions.create_function(:validate_absolute_path) do Puppet::Functions.create_function(:validate_absolute_path) do
# @param scope
# The main value that will be passed to the method
#
# @param args
# Any additional values that are to be passed to the method
#
# @return [Boolean]
# A boolean value returned from the called function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Validate the passed value represents an array.
Puppet::Functions.create_function(:validate_array) do Puppet::Functions.create_function(:validate_array) do
# @param scope
# The main value that will be passed to the method
#
# @param args
# Any additional values that are to be passed to the method
#
# @return
# A boolean value (`true` or `false`) returned from the called function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Validate the passed value represents a boolean.
Puppet::Functions.create_function(:validate_bool) do Puppet::Functions.create_function(:validate_bool) do
# @param scope
# The main value that will be passed to the method
#
# @param args
# Any additional values that are to be passed to the method
#
# @return [Boolean] `true` or `false`
# A boolean value returned from the called function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Validate the passed value represents a hash.
Puppet::Functions.create_function(:validate_hash) do Puppet::Functions.create_function(:validate_hash) do
# @param scope
# The main value that will be passed to the method
#
# @param args
# Any additional values that are to be passed to the method
#
# @return
# A boolean value (`true` or `false`) returned from the called function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Validate the passed value represents an integer.
Puppet::Functions.create_function(:validate_integer) do Puppet::Functions.create_function(:validate_integer) do
# @param scope
# The main value that will be passed to the method
#
# @param args
# Any additional values that are to be passed to the method
#
# @return [Boolean] `true` or `false`
# A boolean value returned from the called function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Validate the passed value represents an ip_address.
Puppet::Functions.create_function(:validate_ip_address) do Puppet::Functions.create_function(:validate_ip_address) do
# @param scope
# The main value that will be passed to the method
#
# @param args
# Any additional values that are to be passed to the method
#
# @return [Boolean] `true` or `false`
# A boolean value returned from the called function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Validate the passed value represents an ipv4_address.
Puppet::Functions.create_function(:validate_ipv4_address) do Puppet::Functions.create_function(:validate_ipv4_address) do
# @param scope
# The main value that will be passed to the method
#
# @param args
# Any additional values that are to be passed to the method
#
# @return [Boolean] `true` or `false`
# A boolean value returned from the called function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Validate the passed value represents an ipv6_address.
Puppet::Functions.create_function(:validate_ipv6_address) do Puppet::Functions.create_function(:validate_ipv6_address) do
# @param scope
# The main value that will be passed to the method
#
# @param args
# Any additional values that are to be passed to the method
#
# @return [Boolean] `true` or `false`
# A boolean value returned from the called function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Validate a value against both the target_type (new) and the previous_validation function (old).
Puppet::Functions.create_function(:validate_legacy) do Puppet::Functions.create_function(:validate_legacy) do
# The function checks a value against both the target_type (new) and the previous_validation function (old). # The function checks a value against both the target_type (new) and the previous_validation function (old).
# @param scope
# The main value that will be passed to the method
# @param target_type
# @param function_name
# @param value
# @param args
# Any additional values that are to be passed to the method
# @return
# A boolean value (`true` or `false`) returned from the called function.
dispatch :validate_legacy do dispatch :validate_legacy do
param 'Any', :scope param 'Any', :scope
param 'Type', :target_type param 'Type', :target_type
...@@ -21,14 +9,6 @@ Puppet::Functions.create_function(:validate_legacy) do ...@@ -21,14 +9,6 @@ Puppet::Functions.create_function(:validate_legacy) do
repeated_param 'Any', :args repeated_param 'Any', :args
end end
# @param scope
# The main value that will be passed to the method
# @param type_string
# @param function_name
# @param value
# @param args Any additional values that are to be passed to the method
# @return Legacy validation method
#
dispatch :validate_legacy_s do dispatch :validate_legacy_s do
param 'Any', :scope param 'Any', :scope
param 'String', :type_string param 'String', :type_string
......