1 # Test whether a given class or definition is defined
2 require 'puppet/parser/functions'
4 Puppet::Parser::Functions.newfunction(:ensure_resource,
8 Takes a resource type, title, and a list of attributes that describe a
16 created or recreated the passed resource with the passed type and attributes
18 @example Example usage
20 Creates the resource if it does not already exist:
22 ensure_resource('user', 'dan', {'ensure' => 'present' })
24 If the resource already exists but does not match the specified parameters,
25 this function will attempt to recreate the resource leading to a duplicate
26 resource definition error.
28 An array of resources can also be passed in and each will be created with
29 the type and parameters specified if it doesn't already exist.
31 ensure_resource('user', ['dan','alex'], {'ensure' => 'present'})
35 type, title, params = vals
36 raise(ArgumentError, 'Must specify a type') unless type
37 raise(ArgumentError, 'Must specify a title') unless title
40 items = [title].flatten
43 Puppet::Parser::Functions.function(:defined_with_params)
44 if function_defined_with_params(["#{type}[#{item}]", params])
45 Puppet.debug("Resource #{type}[#{item}] with params #{params} not created because it already exists")
47 Puppet.debug("Create new resource #{type}[#{item}] with params #{params}")
48 Puppet::Parser::Functions.function(:create_resources)
49 function_create_resources([type.capitalize, { item => params }])