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.
275
275
ensure_packages
276
276
---------------
277
277
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.
278
280
279
281
280
282
- * Type* : statement
Original file line number Diff line number Diff line change 5
5
module Puppet ::Parser ::Functions
6
6
newfunction ( :ensure_packages , :type => :statement , :doc => <<-EOS
7
7
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.
8
10
EOS
9
11
) do |arguments |
10
12
11
- if arguments . size != 1
13
+ if arguments . size > 2 or arguments . size == 0
12
14
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' )
14
18
end
15
19
16
20
packages = Array ( arguments [ 0 ] )
17
21
22
+ if arguments [ 1 ]
23
+ defaults = { 'ensure' => 'present' } . merge ( arguments [ 1 ] )
24
+ else
25
+ defaults = { 'ensure' => 'present' }
26
+ end
27
+
18
28
Puppet ::Parser ::Functions . function ( :ensure_resource )
19
29
packages . each { |package_name |
20
- function_ensure_resource ( [ 'package' , package_name , { 'ensure' => 'present' } ] )
30
+ function_ensure_resource ( [ 'package' , package_name , defaults ] )
21
31
}
22
32
end
23
33
end
Original file line number Diff line number Diff line change 32
32
it 'fails with no arguments' do
33
33
expect {
34
34
scope . function_ensure_packages ( [ ] )
35
- } . to raise_error ( Puppet ::ParseError , /0 for 1/ )
35
+ } . to raise_error ( Puppet ::ParseError , /0 for 1 or 2 / )
36
36
end
37
37
38
38
it 'accepts an array of values' do
67
67
expect ( catalog . resource ( :package , 'facter' ) [ 'ensure' ] ) . to eq ( 'present' )
68
68
end
69
69
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
70
81
end
You can’t perform that action at this time.
0 commit comments