summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions
diff options
context:
space:
mode:
authorTP Honey <tphoney@users.noreply.github.com>2017-08-11 09:56:53 +0100
committerGitHub <noreply@github.com>2017-08-11 09:56:53 +0100
commitc7ac34da3a8b70500143e326f8c4fa27ae7da311 (patch)
tree26721306dc6501ada77e48111b8e94f9856cf0b8 /lib/puppet/parser/functions
parentb89d5f388ca701e38a0e0337408f5ccb7e68565f (diff)
parent016f4dfd7f0c8a6b5b98fa38cf70434603c75883 (diff)
Merge branch 'master' into release
Diffstat (limited to 'lib/puppet/parser/functions')
-rw-r--r--lib/puppet/parser/functions/ensure_packages.rb6
-rw-r--r--lib/puppet/parser/functions/pw_hash.rb7
-rw-r--r--lib/puppet/parser/functions/round.rb33
-rw-r--r--lib/puppet/parser/functions/unique.rb4
-rw-r--r--lib/puppet/parser/functions/validate_domain_name.rb39
5 files changed, 89 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/ensure_packages.rb b/lib/puppet/parser/functions/ensure_packages.rb
index 034f997..1bf8bf1 100644
--- a/lib/puppet/parser/functions/ensure_packages.rb
+++ b/lib/puppet/parser/functions/ensure_packages.rb
@@ -19,6 +19,9 @@ third argument to the ensure_resource() function.
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
@@ -30,6 +33,9 @@ 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
diff --git a/lib/puppet/parser/functions/pw_hash.rb b/lib/puppet/parser/functions/pw_hash.rb
index d99ee5b..0deeb3a 100644
--- a/lib/puppet/parser/functions/pw_hash.rb
+++ b/lib/puppet/parser/functions/pw_hash.rb
@@ -27,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 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/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/unique.rb b/lib/puppet/parser/functions/unique.rb
index b57431d..1e2a895 100644
--- a/lib/puppet/parser/functions/unique.rb
+++ b/lib/puppet/parser/functions/unique.rb
@@ -24,6 +24,10 @@ This returns:
EOS
) do |arguments|
+ 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]
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