Puppet Function: defined_with_params

Defined in:
modules/stdlib/lib/puppet/parser/functions/defined_with_params.rb
Function type:
Ruby 3.x API

Overview

defined_with_params()Boolean

Returns `true` if a resource with the specified attributes has already been added to the catalog, and `false` otherwise.

```
user { 'dan':
  ensure => present,
}

if ! defined_with_params(User[dan], {'ensure' => 'present' }) {
  user { 'dan': ensure => present, }
}
```

Returns:

  • (Boolean)

    returns `true` or `false`



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'modules/stdlib/lib/puppet/parser/functions/defined_with_params.rb', line 4

Puppet::Parser::Functions.newfunction(:defined_with_params,
                                      :type => :rvalue,
                                      :doc => <<-DOC
    @summary
      Takes a resource reference and an optional hash of attributes.

    Returns `true` if a resource with the specified attributes has already been added
    to the catalog, and `false` otherwise.

      ```
      user { 'dan':
        ensure => present,
      }

      if ! defined_with_params(User[dan], {'ensure' => 'present' }) {
        user { 'dan': ensure => present, }
      }
      ```

    @return [Boolean]
      returns `true` or `false`
DOC
                                     ) do |vals|
  reference, params = vals
  raise(ArgumentError, 'Must specify a reference') unless reference
  if !params || params == ''
    params = {}
  end
  ret = false

  if Puppet::Util::Package.versioncmp(Puppet.version, '4.6.0') >= 0
    # Workaround for PE-20308
    if reference.is_a?(String)
      type_name, title = Puppet::Resource.type_and_title(reference, nil)
      type = Puppet::Pops::Evaluator::Runtime3ResourceSupport.find_resource_type_or_class(find_global_scope, type_name.downcase)
    elsif reference.is_a?(Puppet::Resource)
      type = reference.type
      title = reference.title
    else
      raise(ArgumentError, "Reference is not understood: '#{reference.class}'")
    end
    # end workaround
  else
    type = reference.to_s
    title = nil
  end

  resources = if title.empty?
                catalog.resources.select { |r| r.type == type }
              else
                [findresource(type, title)]
              end

  resources.compact.each do |res|
    # If you call this from within a defined type, it will find itself
    next if res.to_s == resource.to_s

    matches = params.map do |key, value|
      # eql? avoids bugs caused by monkeypatching in puppet
      res_is_undef = res[key].eql?(:undef) || res[key].nil?
      value_is_undef = value.eql?(:undef) || value.nil?
      found_match = (res_is_undef && value_is_undef) || (res[key] == value)

      Puppet.debug("Matching resource is #{res}") if found_match

      found_match
    end
    ret = params.empty? || !matches.include?(false)

    break if ret
  end

  Puppet.debug("Resource #{reference} was not determined to be defined") unless ret

  ret
end