Skip to content

Commit 3375956

Browse files
authored
Merge pull request #1129 from binford2k/puppet_4_functions
Puppet 4 functions
2 parents 60f69d5 + 2d9978f commit 3375956

File tree

6 files changed

+316
-0
lines changed

6 files changed

+316
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
# postgresql_acls_to_resources_hash.rb
10+
# ---- original file header ----
11+
#
12+
# @summary
13+
# This internal function translates the ipv(4|6)acls format into a resource
14+
# suitable for create_resources. It is not intended to be used outside of the
15+
# postgresql internal classes/defined resources.
16+
#
17+
# @return This function accepts an array of strings that are pg_hba.conf rules. It
18+
# will return a hash that can be fed into create_resources to create multiple
19+
# individual pg_hba_rule resources.
20+
#
21+
# The second parameter is an identifier that will be included in the namevar
22+
# to provide uniqueness. It must be a string.
23+
#
24+
# The third parameter is an order offset, so you can start the order at an
25+
# arbitrary starting point.
26+
#
27+
#
28+
Puppet::Functions.create_function(:'postgresql::postgresql_acls_to_resources_hash') do
29+
# @param args
30+
# The original array of arguments. Port this to individually managed params
31+
# to get the full benefit of the modern function API.
32+
#
33+
# @return [Data type]
34+
# Describe what the function returns here
35+
#
36+
dispatch :default_impl do
37+
# Call the method named 'default_impl' when this is matched
38+
# Port this to match individual params for better type safety
39+
repeated_param 'Any', :args
40+
end
41+
42+
def default_impl(*args)
43+
func_name = 'postgresql_acls_to_resources_hash()'
44+
45+
if args.size != 3
46+
raise(Puppet::ParseError, "#{func_name}: Wrong number of arguments " \
47+
"given (#{args.size} for 3)")
48+
end
49+
50+
acls = args[0]
51+
raise(Puppet::ParseError, "#{func_name}: first argument must be an array") \
52+
unless acls.instance_of? Array
53+
54+
id = args[1]
55+
raise(Puppet::ParseError, "#{func_name}: second argument must be a string") \
56+
unless id.instance_of? String
57+
58+
offset = args[2].to_i
59+
raise(Puppet::ParseError, "#{func_name}: third argument must be a number") \
60+
unless offset.is_a? Integer
61+
62+
resources = {}
63+
acls.each do |acl|
64+
index = acls.index(acl)
65+
66+
parts = acl.split
67+
68+
unless parts.length >= 4
69+
raise(Puppet::ParseError, "#{func_name}: acl line #{index} does not " \
70+
'have enough parts')
71+
end
72+
73+
resource = {
74+
'type' => parts[0],
75+
'database' => parts[1],
76+
'user' => parts[2],
77+
'order' => format('%03d', offset + index), # rubocop:disable Style/FormatString
78+
}
79+
if parts[0] == 'local'
80+
resource['auth_method'] = parts[3]
81+
if parts.length > 4
82+
resource['auth_option'] = parts.last(parts.length - 4).join(' ')
83+
end
84+
elsif parts[4] =~ %r{^\d}
85+
resource['address'] = parts[3] + ' ' + parts[4]
86+
resource['auth_method'] = parts[5]
87+
88+
resource['auth_option'] = parts.last(parts.length - 6).join(' ') if parts.length > 6
89+
else
90+
resource['address'] = parts[3]
91+
resource['auth_method'] = parts[4]
92+
93+
resource['auth_option'] = parts.last(parts.length - 5).join(' ') if parts.length > 5
94+
end
95+
resources["postgresql class generated rule #{id} #{index}"] = resource
96+
end
97+
resources
98+
end
99+
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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/md5'
10+
11+
# postgresql_escape.rb
12+
# ---- original file header ----
13+
#
14+
# @summary
15+
# This function safely escapes a string using a consistent random tag
16+
# @return Safely escapes a string using $$ using a random tag which should be consistent
17+
#
18+
#
19+
Puppet::Functions.create_function(:'postgresql::postgresql_escape') do
20+
# @param args
21+
# The original array of arguments. Port this to individually managed params
22+
# to get the full benefit of the modern function API.
23+
#
24+
# @return [Data type]
25+
# Describe what the function returns here
26+
#
27+
dispatch :default_impl do
28+
# Call the method named 'default_impl' when this is matched
29+
# Port this to match individual params for better type safety
30+
repeated_param 'Any', :args
31+
end
32+
33+
def default_impl(*args)
34+
if args.size != 1
35+
raise(Puppet::ParseError, 'postgresql_escape(): Wrong number of arguments ' \
36+
"given (#{args.size} for 1)")
37+
end
38+
39+
password = args[0]
40+
41+
if password !~ %r{\$\$} && password[-1] != '$'
42+
retval = "$$#{password}$$"
43+
else
44+
escape = Digest::MD5.hexdigest(password)[0..5].gsub(%r{\d}, '')
45+
until password !~ %r{#{escape}}
46+
escape = Digest::MD5.hexdigest(escape)[0..5].gsub(%r{\d}, '')
47+
end
48+
retval = "$#{escape}$#{password}$#{escape}$"
49+
end
50+
retval
51+
end
52+
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
# hash a string as mysql's "PASSWORD()" function would do it
10+
require 'digest/md5'
11+
12+
# postgresql_password.rb
13+
# ---- original file header ----
14+
#
15+
# @summary
16+
# This function returns the postgresql password hash from the clear text username / password
17+
# @return Returns the postgresql password hash from the clear text username / password.
18+
#
19+
#
20+
Puppet::Functions.create_function(:'postgresql::postgresql_password') do
21+
# @param args
22+
# The original array of arguments. Port this to individually managed params
23+
# to get the full benefit of the modern function API.
24+
#
25+
# @return [Data type]
26+
# Describe what the function returns here
27+
#
28+
dispatch :default_impl do
29+
# Call the method named 'default_impl' when this is matched
30+
# Port this to match individual params for better type safety
31+
repeated_param 'Any', :args
32+
end
33+
34+
def default_impl(*args)
35+
if args.size != 2
36+
raise(Puppet::ParseError, 'postgresql_password(): Wrong number of arguments ' \
37+
"given (#{args.size} for 2)")
38+
end
39+
40+
username = args[0]
41+
password = args[1]
42+
43+
'md5' + Digest::MD5.hexdigest(password.to_s + username.to_s)
44+
end
45+
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 'postgresql::postgresql_acls_to_resources_hash' 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
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 'postgresql::postgresql_escape' 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
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 'postgresql::postgresql_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)