Skip to content
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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
63 changes: 63 additions & 0 deletions lib/puppet/functions/mesos/mesos_hash_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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 ----
#
# mesos_hash_parser.rb
#

# ---- original file header ----
#
# @summary
# This function converts simple key-value structure to a Hash
#that is required by create_resources function
#
#
#
Puppet::Functions.create_function(:'mesos::mesos_hash_parser') 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)


# Arguments: hash key_prefix [file_prefix]

if args.empty? || args.size > 3
raise(Puppet::ParseError, "mesos_hash_parser(): Wrong number of args, given #{args.size}, accepts either 1, 2 or 3")
end

if args[0].class != Hash
raise(Puppet::ParseError, 'mesos_hash_parser(): first argument must be a Hash, you passed a ' + args[0].class.to_s)
end

res = {}
key_prefix = args[1] if args.size >= 2
file_prefix = args[2] if args.size == 3
args[0].each do |key, val|
file = file_prefix ? "#{file_prefix}_#{key}" : key
key = "#{key_prefix}_#{key}" if key_prefix
res[key] = {
'value' => val,
'file' => file
}
end
res

end
end
71 changes: 71 additions & 0 deletions lib/puppet/functions/mesos/zookeeper_servers_url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# 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 ----

# ---- original file header ----
#
# @summary
# This function converts an array of ZooKeeper hostnames into a combined URL for
#ZooKeeper HA. Optionally you can pass custom path in ZooKeeper and default
#ZooKeeper port (applies only for servers without specified port)
#
#Usage: zookeeper_servers_url([10.0.0.1,10.0.0.2],'mesos', 2181)
#
#
Puppet::Functions.create_function(:'mesos::zookeeper_servers_url') 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)


# Only 1 argument should be passed
if args.size > 3
raise(Puppet::ParseError, 'zookeeper_servers_url(): Wrong number of args ' + "given (#{args.size} for 1)")
end

zk_path = args[1] if args.size > 1
zk_path ||= 'mesos'
zk_port = args[2] if args.size > 2
zk_port ||= 2181

# The argument should be an Array
case args[0].class.name
when 'Array'
zookeeper_servers = args[0].clone
when 'String'
# backward compatibility, will be removed in 1.x
return args[0]
else
raise(Puppet::ParseError, 'zookeeper_servers_url() accepts an Array, you passed a ' + args[0].class.name)
end

uri = 'zk://'
zookeeper_servers.each_with_index do |server, i|
uri << ',' if i > 0
uri << if server.index(':')
server
else
"#{server}:#{zk_port}"
end
end
return "#{uri}/#{zk_path}"

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

describe 'mesos::mesos_hash_parser' 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/mesos_zookeeper_servers_url_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'

describe 'mesos::zookeeper_servers_url' 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