1 # Test whether a given class or definition is defined
2 require 'puppet/parser/functions'
4 Puppet::Parser::Functions.newfunction(:defined_with_params,
8 Takes a resource reference and an optional hash of attributes.
10 Returns `true` if a resource with the specified attributes has already been added
11 to the catalog, and `false` otherwise.
18 if ! defined_with_params(User[dan], {'ensure' => 'present' }) {
19 user { 'dan': ensure => present, }
24 returns `true` or `false`
27 reference, params = vals
28 raise(ArgumentError, 'Must specify a reference') unless reference
29 if !params || params == ''
34 if Puppet::Util::Package.versioncmp(Puppet.version, '4.6.0') >= 0
35 # Workaround for PE-20308
36 if reference.is_a?(String)
37 type_name, title = Puppet::Resource.type_and_title(reference, nil)
38 type = Puppet::Pops::Evaluator::Runtime3ResourceSupport.find_resource_type_or_class(find_global_scope, type_name.downcase)
39 elsif reference.is_a?(Puppet::Resource)
41 title = reference.title
43 raise(ArgumentError, "Reference is not understood: '#{reference.class}'")
51 resource = findresource(type, title)
53 matches = params.map do |key, value|
54 # eql? avoids bugs caused by monkeypatching in puppet
55 resource_is_undef = resource[key].eql?(:undef) || resource[key].nil?
56 value_is_undef = value.eql?(:undef) || value.nil?
57 (resource_is_undef && value_is_undef) || (resource[key] == value)
59 ret = params.empty? || !matches.include?(false)
61 Puppet.debug("Resource #{reference} was not determined to be defined")