Browse Source

Use puppet4 functions-api

Depends-On: https://review.openstack.org/#/c/614782/
Change-Id: I7bb50c4e591585b80661fe0d41c7cc2dfec5fdf8
changes/26/614226/7
Tobias Urdin 3 years ago
parent
commit
79481df8ef
3 changed files with 89 additions and 59 deletions
  1. +58
    -0
      lib/puppet/functions/validate_yum_hash.rb
  2. +0
    -59
      lib/puppet/parser/functions/validate_yum_hash.rb
  3. +31
    -0
      spec/functions/validate_yum_hash_spec.rb

+ 58
- 0
lib/puppet/functions/validate_yum_hash.rb 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

+ 0
- 59
lib/puppet/parser/functions/validate_yum_hash.rb 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

+ 31
- 0
spec/functions/validate_yum_hash_spec.rb 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

Loading…
Cancel
Save