Skip to content

Porting functions to the modern Puppet 4.x API #1128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions lib/puppet/functions/postgresql/postgresql_acls_to_resources_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# This is an autogenerated function, ported from the original legacy version.
# It /should work/ as is, but will not have all the benefits of the modern
# function API. You should see the function docs to learn how to add function
# signatures for type safety and to document this function using puppet-strings.
#
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
#
# ---- original file header ----
# postgresql_acls_to_resources_hash.rb
# ---- original file header ----
#
# @summary
# This internal function translates the ipv(4|6)acls format into a resource
# suitable for create_resources. It is not intended to be used outside of the
# postgresql internal classes/defined resources.
#
# @return This function accepts an array of strings that are pg_hba.conf rules. It
# will return a hash that can be fed into create_resources to create multiple
# individual pg_hba_rule resources.
#
# The second parameter is an identifier that will be included in the namevar
# to provide uniqueness. It must be a string.
#
# The third parameter is an order offset, so you can start the order at an
# arbitrary starting point.
#
#
Puppet::Functions.create_function(:'postgresql::postgresql_acls_to_resources_hash') do
# @param args
# The original array of arguments. Port this to individually managed params
# to get the full benefit of the modern function API.
#
# @return [Data type]
# Describe what the function returns here
#
dispatch :default_impl do
# Call the method named 'default_impl' when this is matched
# Port this to match individual params for better type safety
repeated_param 'Any', :args
end


def default_impl(*args)

func_name = 'postgresql_acls_to_resources_hash()'

if args.size != 3
raise(Puppet::ParseError, "#{func_name}: Wrong number of arguments " \
"given (#{args.size} for 3)")
end

acls = args[0]
raise(Puppet::ParseError, "#{func_name}: first argument must be an array") \
unless acls.instance_of? Array

id = args[1]
raise(Puppet::ParseError, "#{func_name}: second argument must be a string") \
unless id.instance_of? String

offset = args[2].to_i
raise(Puppet::ParseError, "#{func_name}: third argument must be a number") \
unless offset.is_a? Integer

resources = {}
acls.each do |acl|
index = acls.index(acl)

parts = acl.split

unless parts.length >= 4
raise(Puppet::ParseError, "#{func_name}: acl line #{index} does not " \
'have enough parts')
end

resource = {
'type' => parts[0],
'database' => parts[1],
'user' => parts[2],
'order' => format('%03d', offset + index), # rubocop:disable Style/FormatString
}
if parts[0] == 'local'
resource['auth_method'] = parts[3]
if parts.length > 4
resource['auth_option'] = parts.last(parts.length - 4).join(' ')
end
elsif parts[4] =~ %r{^\d}
resource['address'] = parts[3] + ' ' + parts[4]
resource['auth_method'] = parts[5]

resource['auth_option'] = parts.last(parts.length - 6).join(' ') if parts.length > 6
else
resource['address'] = parts[3]
resource['auth_method'] = parts[4]

resource['auth_option'] = parts.last(parts.length - 5).join(' ') if parts.length > 5
end
resources["postgresql class generated rule #{id} #{index}"] = resource
end
resources

end
end
56 changes: 56 additions & 0 deletions lib/puppet/functions/postgresql/postgresql_escape.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# This is an autogenerated function, ported from the original legacy version.
# It /should work/ as is, but will not have all the benefits of the modern
# function API. You should see the function docs to learn how to add function
# signatures for type safety and to document this function using puppet-strings.
#
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
#
# ---- original file header ----
require 'digest/md5'

# postgresql_escape.rb
# ---- original file header ----
#
# @summary
# This function safely escapes a string using a consistent random tag
# @return Safely escapes a string using $$ using a random tag which should be consistent
#
#
Puppet::Functions.create_function(:'postgresql::postgresql_escape') do
# @param args
# The original array of arguments. Port this to individually managed params
# to get the full benefit of the modern function API.
#
# @return [Data type]
# Describe what the function returns here
#
dispatch :default_impl do
# Call the method named 'default_impl' when this is matched
# Port this to match individual params for better type safety
repeated_param 'Any', :args
end


def default_impl(*args)


if args.size != 1
raise(Puppet::ParseError, 'postgresql_escape(): Wrong number of arguments ' \
"given (#{args.size} for 1)")
end

password = args[0]

if password !~ %r{\$\$} && password[-1] != '$'
retval = "$$#{password}$$"
else
escape = Digest::MD5.hexdigest(password)[0..5].gsub(%r{\d}, '')
until password !~ %r{#{escape}}
escape = Digest::MD5.hexdigest(escape)[0..5].gsub(%r{\d}, '')
end
retval = "$#{escape}$#{password}$#{escape}$"
end
retval

end
end
49 changes: 49 additions & 0 deletions lib/puppet/functions/postgresql/postgresql_password.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This is an autogenerated function, ported from the original legacy version.
# It /should work/ as is, but will not have all the benefits of the modern
# function API. You should see the function docs to learn how to add function
# signatures for type safety and to document this function using puppet-strings.
#
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
#
# ---- original file header ----
# hash a string as mysql's "PASSWORD()" function would do it
require 'digest/md5'

# postgresql_password.rb
# ---- original file header ----
#
# @summary
# This function returns the postgresql password hash from the clear text username / password
# @return Returns the postgresql password hash from the clear text username / password.
#
#
Puppet::Functions.create_function(:'postgresql::postgresql_password') do
# @param args
# The original array of arguments. Port this to individually managed params
# to get the full benefit of the modern function API.
#
# @return [Data type]
# Describe what the function returns here
#
dispatch :default_impl do
# Call the method named 'default_impl' when this is matched
# Port this to match individual params for better type safety
repeated_param 'Any', :args
end


def default_impl(*args)


if args.size != 2
raise(Puppet::ParseError, 'postgresql_password(): Wrong number of arguments ' \
"given (#{args.size} for 2)")
end

username = args[0]
password = args[1]

'md5' + Digest::MD5.hexdigest(password.to_s + username.to_s)

end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'

describe 'postgresql::postgresql_acls_to_resources_hash' do
# without knowing details about the implementation, this is the only test
# case that we can autogenerate. You should add more examples below!
it { is_expected.not_to eq(nil) }

#################################
# Below are some example test cases. You may uncomment and modify them to match
# your needs. Notice that they all expect the base error class of `StandardError`.
# This is because the autogenerated function uses an untyped array for parameters
# and relies on your implementation to do the validation. As you convert your
# function to proper dispatches and typed signatures, you should change the
# expected error of the argument validation examples to `ArgumentError`.
#
# Other error types you might encounter include
#
# * StandardError
# * ArgumentError
# * Puppet::ParseError
#
# Read more about writing function unit tests at https://rspec-puppet.com/documentation/functions/
#
# it 'raises an error if called with no argument' do
# is_expected.to run.with_params.and_raise_error(StandardError)
# end
#
# it 'raises an error if there is more than 1 arguments' do
# is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(StandardError)
# end
#
# it 'raises an error if argument is not the proper type' do
# is_expected.to run.with_params('foo').and_raise_error(StandardError)
# end
#
# it 'returns the proper output' do
# is_expected.to run.with_params(123).and_return('the expected output')
# end
#################################

end
41 changes: 41 additions & 0 deletions spec/functions/postgresql_postgresql_escape_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'

describe 'postgresql::postgresql_escape' do
# without knowing details about the implementation, this is the only test
# case that we can autogenerate. You should add more examples below!
it { is_expected.not_to eq(nil) }

#################################
# Below are some example test cases. You may uncomment and modify them to match
# your needs. Notice that they all expect the base error class of `StandardError`.
# This is because the autogenerated function uses an untyped array for parameters
# and relies on your implementation to do the validation. As you convert your
# function to proper dispatches and typed signatures, you should change the
# expected error of the argument validation examples to `ArgumentError`.
#
# Other error types you might encounter include
#
# * StandardError
# * ArgumentError
# * Puppet::ParseError
#
# Read more about writing function unit tests at https://rspec-puppet.com/documentation/functions/
#
# it 'raises an error if called with no argument' do
# is_expected.to run.with_params.and_raise_error(StandardError)
# end
#
# it 'raises an error if there is more than 1 arguments' do
# is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(StandardError)
# end
#
# it 'raises an error if argument is not the proper type' do
# is_expected.to run.with_params('foo').and_raise_error(StandardError)
# end
#
# it 'returns the proper output' do
# is_expected.to run.with_params(123).and_return('the expected output')
# end
#################################

end
41 changes: 41 additions & 0 deletions spec/functions/postgresql_postgresql_password_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'

describe 'postgresql::postgresql_password' do
# without knowing details about the implementation, this is the only test
# case that we can autogenerate. You should add more examples below!
it { is_expected.not_to eq(nil) }

#################################
# Below are some example test cases. You may uncomment and modify them to match
# your needs. Notice that they all expect the base error class of `StandardError`.
# This is because the autogenerated function uses an untyped array for parameters
# and relies on your implementation to do the validation. As you convert your
# function to proper dispatches and typed signatures, you should change the
# expected error of the argument validation examples to `ArgumentError`.
#
# Other error types you might encounter include
#
# * StandardError
# * ArgumentError
# * Puppet::ParseError
#
# Read more about writing function unit tests at https://rspec-puppet.com/documentation/functions/
#
# it 'raises an error if called with no argument' do
# is_expected.to run.with_params.and_raise_error(StandardError)
# end
#
# it 'raises an error if there is more than 1 arguments' do
# is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(StandardError)
# end
#
# it 'raises an error if argument is not the proper type' do
# is_expected.to run.with_params('foo').and_raise_error(StandardError)
# end
#
# it 'returns the proper output' do
# is_expected.to run.with_params(123).and_return('the expected output')
# end
#################################

end