Skip to content

Commit c3b06d6

Browse files
committed
Porting functions to the modern Puppet 4.x API
1 parent 60f69d5 commit c3b06d6

File tree

6 files changed

+330
-0
lines changed

6 files changed

+330
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
43+
def default_impl(*args)
44+
45+
func_name = 'postgresql_acls_to_resources_hash()'
46+
47+
if args.size != 3
48+
raise(Puppet::ParseError, "#{func_name}: Wrong number of arguments " \
49+
"given (#{args.size} for 3)")
50+
end
51+
52+
acls = args[0]
53+
raise(Puppet::ParseError, "#{func_name}: first argument must be an array") \
54+
unless acls.instance_of? Array
55+
56+
id = args[1]
57+
raise(Puppet::ParseError, "#{func_name}: second argument must be a string") \
58+
unless id.instance_of? String
59+
60+
offset = args[2].to_i
61+
raise(Puppet::ParseError, "#{func_name}: third argument must be a number") \
62+
unless offset.is_a? Integer
63+
64+
resources = {}
65+
acls.each do |acl|
66+
index = acls.index(acl)
67+
68+
parts = acl.split
69+
70+
unless parts.length >= 4
71+
raise(Puppet::ParseError, "#{func_name}: acl line #{index} does not " \
72+
'have enough parts')
73+
end
74+
75+
resource = {
76+
'type' => parts[0],
77+
'database' => parts[1],
78+
'user' => parts[2],
79+
'order' => format('%03d', offset + index), # rubocop:disable Style/FormatString
80+
}
81+
if parts[0] == 'local'
82+
resource['auth_method'] = parts[3]
83+
if parts.length > 4
84+
resource['auth_option'] = parts.last(parts.length - 4).join(' ')
85+
end
86+
elsif parts[4] =~ %r{^\d}
87+
resource['address'] = parts[3] + ' ' + parts[4]
88+
resource['auth_method'] = parts[5]
89+
90+
resource['auth_option'] = parts.last(parts.length - 6).join(' ') if parts.length > 6
91+
else
92+
resource['address'] = parts[3]
93+
resource['auth_method'] = parts[4]
94+
95+
resource['auth_option'] = parts.last(parts.length - 5).join(' ') if parts.length > 5
96+
end
97+
resources["postgresql class generated rule #{id} #{index}"] = resource
98+
end
99+
resources
100+
101+
end
102+
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
34+
def default_impl(*args)
35+
36+
37+
if args.size != 1
38+
raise(Puppet::ParseError, 'postgresql_escape(): Wrong number of arguments ' \
39+
"given (#{args.size} for 1)")
40+
end
41+
42+
password = args[0]
43+
44+
if password !~ %r{\$\$} && password[-1] != '$'
45+
retval = "$$#{password}$$"
46+
else
47+
escape = Digest::MD5.hexdigest(password)[0..5].gsub(%r{\d}, '')
48+
until password !~ %r{#{escape}}
49+
escape = Digest::MD5.hexdigest(escape)[0..5].gsub(%r{\d}, '')
50+
end
51+
retval = "$#{escape}$#{password}$#{escape}$"
52+
end
53+
retval
54+
55+
end
56+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
35+
def default_impl(*args)
36+
37+
38+
if args.size != 2
39+
raise(Puppet::ParseError, 'postgresql_password(): Wrong number of arguments ' \
40+
"given (#{args.size} for 2)")
41+
end
42+
43+
username = args[0]
44+
password = args[1]
45+
46+
'md5' + Digest::MD5.hexdigest(password.to_s + username.to_s)
47+
48+
end
49+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
41+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
41+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
41+
end

0 commit comments

Comments
 (0)