Use puppet4 functions-api

Depends-On: https://review.openstack.org/#/c/614782/
Change-Id: I7bb50c4e591585b80661fe0d41c7cc2dfec5fdf8
This commit is contained in:
Tobias Urdin 2018-10-30 15:21:01 +01:00
parent eb4aecb517
commit 79481df8ef
3 changed files with 89 additions and 59 deletions

View File

@ -0,0 +1,58 @@
Puppet::Functions.create_function(:validate_yum_hash) do
def validate_yum_hash(*args)
yumrepo_arguments = [
'name',
'ensure',
'baseurl',
'cost',
'descr',
'enabled',
'enablegroups',
'exclude',
'failovermethod',
'gpgcheck',
'gpgkey',
'http_caching',
'include',
'includepkgs',
'keepalive',
'metadata_expire',
'metalink',
'mirrorlist',
'priority',
'protect',
'provider',
'proxy',
'proxy_password',
'proxy_username',
'repo_gpgcheck',
's3_enabled',
'skip_if_unavailable',
'sslcacert',
'sslclientcert',
'sslclientkey',
'sslverify',
'target',
'timeout'
]
if args.size > 1
raise Puppet::Error, "validate_yum_hash takes only a single argument, #{args.size} provided"
end
arg = args[0]
if not arg.kind_of?(Hash)
raise Puppet::Error, "non-hash argument provided to validate_yum_hash"
end
if arg.size > 0
arg.each do |title, params|
params.each do |param, value|
if ! yumrepo_arguments.include?(param)
raise Puppet::Error, "Parameter #{param} is not valid for the yumrepo type"
end
end
end
end
end
end

View File

@ -1,59 +0,0 @@
module Puppet::Parser::Functions
yumrepo_arguments = [
'name',
'ensure',
'baseurl',
'cost',
'descr',
'enabled',
'enablegroups',
'exclude',
'failovermethod',
'gpgcheck',
'gpgkey',
'http_caching',
'include',
'includepkgs',
'keepalive',
'metadata_expire',
'metalink',
'mirrorlist',
'priority',
'protect',
'provider',
'proxy',
'proxy_password',
'proxy_username',
'repo_gpgcheck',
's3_enabled',
'skip_if_unavailable',
'sslcacert',
'sslclientcert',
'sslclientkey',
'sslverify',
'target',
'timeout'
]
newfunction(:validate_yum_hash) do |args|
if args.size > 1
raise Puppet::Error, "validate_yum_hash takes only a single argument, #{args.size} provided"
end
arg = args[0]
if not arg.kind_of?(Hash)
raise Puppet::Error, "non-hash argument provided to validate_yum_hash"
end
if arg.size > 0
arg.each do |title, params|
params.each do |param, value|
if ! yumrepo_arguments.include?(param)
raise Puppet::Error, "Parameter #{param} is not valid for the yumrepo type"
end
end
end
end
end
end

View File

@ -0,0 +1,31 @@
require 'spec_helper'
describe 'validate_yum_hash' do
it 'exists' do
is_expected.not_to eq(nil)
end
it 'throws error with more than one argument' do
is_expected.to run.with_params({'title' => {'key1' => 'value1'}, 'title2' => {'key2' => 'value2'}}).and_raise_error(Puppet::Error)
end
it 'fails with no arguments' do
is_expected.to run.with_params.and_raise_error(Puppet::Error)
end
it 'fails with invalid hash' do
is_expected.to run.with_params({'title' => {'invalid' => 'val'}}).and_raise_error(Puppet::Error)
end
it 'fails with invalid hash with multiple entries' do
is_expected.to run.with_params({'title' => {'baseurl' => 'placeholder', 'invalid' => 'val'}}).and_raise_error(Puppet::Error)
end
it 'works with valid entry' do
is_expected.to run.with_params({'title' => {'baseurl' => 'placeholder'}})
end
it 'works with multiple valid entries' do
is_expected.to run.with_params({'title' => {'baseurl' => 'placeholder', 'timeout' => 30}})
end
end