Skip to content

Commit cfde131

Browse files
author
Nikhil Yadav
committed
Add ensure_resources() function
New function "ensure_resources()" to support passing hash as parameter OR from hiera backend This new function is extension of ensure_resource() which will now support to pass multiple values as hash/array OR from hiera backend variables in title argument with additional parameters needed. It will process multiple values for a resource type from the passed argument & pass each entry (type, title, params) to ensure_resource() in required format for further processing. Now user can have duplicate resource check functionality extended to multiple entries with this new function. Use: For multiple resources using hash: ensure_resources('user', {'dan' => { gid => 'mygroup', uid =>'600' } , 'alex' => { gid => 'mygroup' }}, {'ensure' =>'present'}) From Hiera Backend: userlist: dan: gid: 'mygroup' uid: '600' alex: gid: 'mygroup' Call: ensure_resources('user',hiera_hash('userlist'), {'ensure' => 'present'}) ensure_packages() Modified to also support Hash type argument for packages This modification will call newly added ensure_resources() for processing Hash as second argument. The original functionality remains same for Array type arguments. Use: hiera: packagelist: ksh: ensure: latest mlocate: {} myrpm: provider: rpm source: "/tmp/myrpm-1.0.0.x86_64.rpm" install_options: --prefix: /users/home openssl: provider: rpm source: "/tmp/openssl-1.0.1e-42.el7.x86_64.rpm" Call: ensure_packages($packagelist)
1 parent 69ca8d0 commit cfde131

3 files changed

Lines changed: 114 additions & 10 deletions

File tree

README.markdown

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,15 @@ Returns true if the argument is an array or hash that contains no elements, or a
346346

347347
#### `ensure_packages`
348348

349-
Takes a list of packages and only installs them if they don't already exist. It optionally takes a hash as a second parameter to be passed as the third argument to the `ensure_resource()` function. *Type*: statement.
349+
Takes a list of packages array/hash and only installs them if they don't already exist. It optionally takes a hash as a second parameter to be passed as the third argument to the `ensure_resource()` or `ensure_resources()` function. *Type*: statement.
350+
351+
For Array:
352+
353+
`ensure_packages(['ksh','openssl'], {'ensure' => 'present'})`
354+
355+
For Hash:
356+
357+
`ensure_packages({'ksh' => { enure => '20120801-1' } , 'mypackage' => { source => '/tmp/myrpm-1.0.0.x86_64.rpm', provider => "rpm" }}, {'ensure' => 'present'})`
350358

351359
#### `ensure_resource`
352360

@@ -370,7 +378,38 @@ An array of resources can also be passed in, and each will be created with the t
370378

371379
*Type*: statement.
372380

373-
#### `flatten`
381+
#### `ensure_resources`
382+
383+
Takes a resource type, title (only hash), and a hash of attributes that describe the resource(s).
384+
385+
~~~
386+
user { 'dan':
387+
gid => 'mygroup',
388+
ensure => present,
389+
}
390+
~~~
391+
392+
An hash of resources should be passed in and each will be created with the type and parameters specified if it doesn't already exist:
393+
394+
`ensure_resources('user', {'dan' => { gid => 'mygroup', uid => '600' } , 'alex' => { gid => 'mygroup' }}, {'ensure' => 'present'})`
395+
396+
From Hiera Backend:
397+
398+
~~~
399+
userlist:
400+
dan:
401+
gid: 'mygroup'
402+
uid: '600'
403+
alex:
404+
gid: 'mygroup'
405+
~~~
406+
407+
Call:
408+
409+
`ensure_resources('user', hiera_hash('userlist'), {'ensure' => 'present'})`
410+
411+
412+
### `flatten`
374413

375414
Flattens deeply nested arrays and returns a single flat array as a result. For example, `flatten(['a', ['b', ['c']]])` returns ['a','b','c']. *Type*: rvalue.
376415

lib/puppet/parser/functions/ensure_packages.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,29 @@ module Puppet::Parser::Functions
1717
raise(Puppet::ParseError, 'ensure_packages(): Requires second argument to be a Hash')
1818
end
1919

20-
packages = Array(arguments[0])
20+
if arguments[0].is_a?(Hash)
21+
if arguments[1]
22+
defaults = { 'ensure' => 'present' }.merge(arguments[1])
23+
else
24+
defaults = { 'ensure' => 'present' }
25+
end
2126

22-
if arguments[1]
23-
defaults = { 'ensure' => 'present' }.merge(arguments[1])
27+
Puppet::Parser::Functions.function(:ensure_resources)
28+
function_ensure_resources(['package', Hash(arguments[0]), defaults ])
2429
else
25-
defaults = { 'ensure' => 'present' }
26-
end
30+
packages = Array(arguments[0])
31+
32+
if arguments[1]
33+
defaults = { 'ensure' => 'present' }.merge(arguments[1])
34+
else
35+
defaults = { 'ensure' => 'present' }
36+
end
2737

28-
Puppet::Parser::Functions.function(:ensure_resource)
29-
packages.each { |package_name|
30-
function_ensure_resource(['package', package_name, defaults ])
38+
Puppet::Parser::Functions.function(:ensure_resource)
39+
packages.each { |package_name|
40+
function_ensure_resource(['package', package_name, defaults ])
3141
}
42+
end
3243
end
3344
end
3445

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require 'puppet/parser/functions'
2+
3+
Puppet::Parser::Functions.newfunction(:ensure_resources,
4+
:type => :statement,
5+
:doc => <<-'ENDOFDOC'
6+
Takes a resource type, title (only hash), and a list of attributes that describe a
7+
resource.
8+
9+
user { 'dan':
10+
gid => 'mygroup',
11+
ensure => present,
12+
}
13+
14+
An hash of resources should be passed in and each will be created with
15+
the type and parameters specified if it doesn't already exist.
16+
17+
ensure_resources('user', {'dan' => { gid => 'mygroup', uid => '600' } , 'alex' => { gid => 'mygroup' }}, {'ensure' => 'present'})
18+
19+
From Hiera Backend:
20+
21+
userlist:
22+
dan:
23+
gid: 'mygroup'
24+
uid: '600'
25+
alex:
26+
gid: 'mygroup'
27+
28+
Call:
29+
ensure_resources('user', hiera_hash('userlist'), {'ensure' => 'present'})
30+
31+
ENDOFDOC
32+
) do |vals|
33+
type, title, params = vals
34+
raise(ArgumentError, 'Must specify a type') unless type
35+
raise(ArgumentError, 'Must specify a title') unless title
36+
params ||= {}
37+
38+
if title.is_a?(Hash)
39+
resource_hash = Hash(title)
40+
resources = resource_hash.keys
41+
42+
Puppet::Parser::Functions.function(:ensure_resource)
43+
resources.each { |resource_name|
44+
if resource_hash[resource_name]
45+
params_merged = params.merge(resource_hash[resource_name])
46+
else
47+
params_merged = params
48+
end
49+
function_ensure_resource([ type, resource_name, params_merged ])
50+
}
51+
else
52+
raise(Puppet::ParseError, 'ensure_resources(): Requires second argument to be a Hash')
53+
end
54+
end

0 commit comments

Comments
 (0)