File tree Expand file tree Collapse file tree 3 files changed +27
-4
lines changed
lib/puppet/parser/functions Expand file tree Collapse file tree 3 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -275,6 +275,8 @@ Returns true if the variable is empty.
275275ensure_packages
276276---------------
277277Takes 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
Original file line number Diff line number Diff line change 55module Puppet ::Parser ::Functions
66 newfunction ( :ensure_packages , :type => :statement , :doc => <<-EOS
77Takes 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
2333end
Original file line number Diff line number Diff line change 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
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
7081end
You can’t perform that action at this time.
0 commit comments