1 # Test whether a given class or definition is defined
2 require 'puppet/parser/functions'
4 Puppet::Parser::Functions.newfunction(:ensure_resource,
7 Takes a resource type, title, and a list of attributes that describe a
14 This example only creates the resource if it does not already exist:
16 ensure_resource('user', 'dan', {'ensure' => 'present' })
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.
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.
25 ensure_resource('user', ['dan','alex'], {'ensure' => 'present'})
29 type, title, params = vals
30 raise(ArgumentError, 'Must specify a type') unless type
31 raise(ArgumentError, 'Must specify a title') unless title
34 items = [title].flatten
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")
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 }])