Skip to content

Commit f42fc4b

Browse files
author
Ashley Penney
committed
Merge pull request #238 from Spredzy/add_default_ensure_packages
(MODULES-603) Add defaults arguments to ensure_packages()
2 parents 1bdb213 + d9b5e91 commit f42fc4b

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

README.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ Returns true if the variable is empty.
275275
ensure_packages
276276
---------------
277277
Takes a list of packages and only installs them if they don't already exist.
278+
It optionally takes a hash as a second parameter that will be passed as the
279+
third argument to the ensure_resource() function.
278280

279281

280282
- *Type*: statement

lib/puppet/parser/functions/ensure_packages.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,29 @@
55
module Puppet::Parser::Functions
66
newfunction(:ensure_packages, :type => :statement, :doc => <<-EOS
77
Takes a list of packages and only installs them if they don't already exist.
8+
It optionally takes a hash as a second parameter that will be passed as the
9+
third argument to the ensure_resource() function.
810
EOS
911
) do |arguments|
1012

11-
if arguments.size != 1
13+
if arguments.size > 2 or arguments.size == 0
1214
raise(Puppet::ParseError, "ensure_packages(): Wrong number of arguments " +
13-
"given (#{arguments.size} for 1)")
15+
"given (#{arguments.size} for 1 or 2)")
16+
elsif arguments.size == 2 and !arguments[1].is_a?(Hash)
17+
raise(Puppet::ParseError, 'ensure_packages(): Requires second argument to be a Hash')
1418
end
1519

1620
packages = Array(arguments[0])
1721

22+
if arguments[1]
23+
defaults = { 'ensure' => 'present' }.merge(arguments[1])
24+
else
25+
defaults = { 'ensure' => 'present' }
26+
end
27+
1828
Puppet::Parser::Functions.function(:ensure_resource)
1929
packages.each { |package_name|
20-
function_ensure_resource(['package', package_name, {'ensure' => 'present' } ])
30+
function_ensure_resource(['package', package_name, defaults ])
2131
}
2232
end
2333
end

spec/functions/ensure_packages_spec.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
it 'fails with no arguments' do
3333
expect {
3434
scope.function_ensure_packages([])
35-
}.to raise_error(Puppet::ParseError, /0 for 1/)
35+
}.to raise_error(Puppet::ParseError, /0 for 1 or 2/)
3636
end
3737

3838
it 'accepts an array of values' do
@@ -67,4 +67,15 @@
6767
expect(catalog.resource(:package, 'facter')['ensure']).to eq('present')
6868
end
6969
end
70+
71+
context 'given a clean catalog and specified defaults' do
72+
let :catalog do
73+
compile_to_catalog('ensure_packages(["facter"], {"provider" => "gem"})')
74+
end
75+
76+
it 'declares package resources with ensure => present' do
77+
expect(catalog.resource(:package, 'facter')['ensure']).to eq('present')
78+
expect(catalog.resource(:package, 'facter')['provider']).to eq('gem')
79+
end
80+
end
7081
end

0 commit comments

Comments
 (0)