source: other-projects/hathitrust/vagrant-hadoop-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/ensure_resource.rb@ 30903

Last change on this file since 30903 was 30903, checked in by davidb, 8 years ago

Vagrant provisioning files for a 4-node Hadoop cluster. See README.txt for more details

File size: 1.6 KB
Line 
1# Test whether a given class or definition is defined
2require 'puppet/parser/functions'
3
4Puppet::Parser::Functions.newfunction(:ensure_resource,
5 :type => :statement,
6 :doc => <<-'ENDOFDOC'
7Takes a resource type, title, and a list of attributes that describe a
8resource.
9
10 user { 'dan':
11 ensure => present,
12 }
13
14This example only creates the resource if it does not already exist:
15
16 ensure_resource('user', 'dan', {'ensure' => 'present' })
17
18If the resource already exists but does not match the specified parameters,
19this function will attempt to recreate the resource leading to a duplicate
20resource definition error.
21
22An array of resources can also be passed in and each will be created with
23the type and parameters specified if it doesn't already exist.
24
25 ensure_resource('user', ['dan','alex'], {'ensure' => 'present'})
26
27ENDOFDOC
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
46end
Note: See TracBrowser for help on using the repository browser.