Skip to content

Commit 49f16a8

Browse files
authored
Merge pull request #1274 from binford2k/puppet_4_functions
Puppet 4 functions
2 parents cbef479 + e14be6b commit 49f16a8

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# This is an autogenerated function, ported from the original legacy version.
2+
# It /should work/ as is, but will not have all the benefits of the modern
3+
# function API. You should see the function docs to learn how to add function
4+
# signatures for type safety and to document this function using puppet-strings.
5+
#
6+
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
7+
#
8+
# ---- original file header ----
9+
require 'digest/sha1'
10+
# ---- original file header ----
11+
#
12+
# @summary
13+
# @summary
14+
# Hash a string as mysql's "PASSWORD()" function would do it
15+
#
16+
# @param [String] password Plain text password.
17+
#
18+
# @return [String] the mysql password hash from the clear text password.
19+
#
20+
#
21+
Puppet::Functions.create_function(:'mysql::mysql_password') do
22+
# @param args
23+
# The original array of arguments. Port this to individually managed params
24+
# to get the full benefit of the modern function API.
25+
#
26+
# @return [Data type]
27+
# Describe what the function returns here
28+
#
29+
dispatch :default_impl do
30+
# Call the method named 'default_impl' when this is matched
31+
# Port this to match individual params for better type safety
32+
repeated_param 'Any', :args
33+
end
34+
35+
def default_impl(*args)
36+
if args.size != 1
37+
raise Puppet::ParseError, _('mysql_password(): Wrong number of arguments given (%{args_length} for 1)') % { args_length: args.length }
38+
end
39+
40+
return '' if args[0].empty?
41+
return args[0] if args[0] =~ %r{\*[A-F0-9]{40}$}
42+
'*' + Digest::SHA1.hexdigest(Digest::SHA1.digest(args[0])).upcase
43+
end
44+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'spec_helper'
2+
3+
describe 'mysql::mysql_password' do
4+
# without knowing details about the implementation, this is the only test
5+
# case that we can autogenerate. You should add more examples below!
6+
it { is_expected.not_to eq(nil) }
7+
8+
#################################
9+
# Below are some example test cases. You may uncomment and modify them to match
10+
# your needs. Notice that they all expect the base error class of `StandardError`.
11+
# This is because the autogenerated function uses an untyped array for parameters
12+
# and relies on your implementation to do the validation. As you convert your
13+
# function to proper dispatches and typed signatures, you should change the
14+
# expected error of the argument validation examples to `ArgumentError`.
15+
#
16+
# Other error types you might encounter include
17+
#
18+
# * StandardError
19+
# * ArgumentError
20+
# * Puppet::ParseError
21+
#
22+
# Read more about writing function unit tests at https://rspec-puppet.com/documentation/functions/
23+
#
24+
# it 'raises an error if called with no argument' do
25+
# is_expected.to run.with_params.and_raise_error(StandardError)
26+
# end
27+
#
28+
# it 'raises an error if there is more than 1 arguments' do
29+
# is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(StandardError)
30+
# end
31+
#
32+
# it 'raises an error if argument is not the proper type' do
33+
# is_expected.to run.with_params('foo').and_raise_error(StandardError)
34+
# end
35+
#
36+
# it 'returns the proper output' do
37+
# is_expected.to run.with_params(123).and_return('the expected output')
38+
# end
39+
#################################
40+
end

0 commit comments

Comments
 (0)