]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/parser/functions/ensure_resource.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / ensure_resource.rb
1 # Test whether a given class or definition is defined
2 require 'puppet/parser/functions'
3
4 Puppet::Parser::Functions.newfunction(:ensure_resource,
5                                       :type => :statement,
6                                       :doc => <<-'ENDOFDOC'
7 Takes a resource type, title, and a list of attributes that describe a
8 resource.
9
10     user { 'dan':
11       ensure => present,
12     }
13
14 This example only creates the resource if it does not already exist:
15
16     ensure_resource('user', 'dan', {'ensure' => 'present' })
17
18 If the resource already exists but does not match the specified parameters,
19 this function will attempt to recreate the resource leading to a duplicate
20 resource definition error.
21
22 An array of resources can also be passed in and each will be created with
23 the type and parameters specified if it doesn't already exist.
24
25     ensure_resource('user', ['dan','alex'], {'ensure' => 'present'})
26
27 ENDOFDOC
28 ) do |vals|
29   type, title, params = vals
30   raise(ArgumentError, 'Must specify a type') unless type
31   raise(ArgumentError, 'Must specify a title') unless title
32   params ||= {}
33
34   items = [title].flatten
35
36   items.each do |item|
37     Puppet::Parser::Functions.function(:defined_with_params)
38     if function_defined_with_params(["#{type}[#{item}]", params])
39       Puppet.debug("Resource #{type}[#{item}] with params #{params} not created because it already exists")
40     else
41       Puppet.debug("Create new resource #{type}[#{item}] with params #{params}")
42       Puppet::Parser::Functions.function(:create_resources)
43       function_create_resources([type.capitalize, { item => params }])
44     end
45   end
46 end