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

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

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

File size: 1.3 KB
Line 
1# Test whether a given class or definition is defined
2require 'puppet/parser/functions'
3
4Puppet::Parser::Functions.newfunction(:defined_with_params,
5 :type => :rvalue,
6 :doc => <<-'ENDOFDOC'
7Takes a resource reference and an optional hash of attributes.
8
9Returns true if a resource with the specified attributes has already been added
10to the catalog, and false otherwise.
11
12 user { 'dan':
13 ensure => present,
14 }
15
16 if ! defined_with_params(User[dan], {'ensure' => 'present' }) {
17 user { 'dan': ensure => present, }
18 }
19ENDOFDOC
20) do |vals|
21 reference, params = vals
22 raise(ArgumentError, 'Must specify a reference') unless reference
23 if (! params) || params == ''
24 params = {}
25 end
26 ret = false
27 if resource = findresource(reference.to_s)
28 matches = params.collect do |key, value|
29 # eql? avoids bugs caused by monkeypatching in puppet
30 resource_is_undef = resource[key].eql?(:undef) || resource[key].nil?
31 value_is_undef = value.eql?(:undef) || value.nil?
32 (resource_is_undef && value_is_undef) || (resource[key] == value)
33 end
34 ret = params.empty? || !matches.include?(false)
35 end
36 Puppet.debug("Resource #{reference} was not determined to be defined")
37 ret
38end
Note: See TracBrowser for help on using the repository browser.