Mercurial > repos > other > Puppet
annotate modules/stdlib/lib/puppet/parser/functions/defined_with_params.rb @ 424:4a2ee7e3b110
Update stdlib in case it fixed deprecation
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 09 Oct 2022 10:34:32 +0100 |
parents | d9352a684e62 |
children | adf6fe9bbc17 |
rev | line source |
---|---|
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 # Test whether a given class or definition is defined |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 require 'puppet/parser/functions' |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 Puppet::Parser::Functions.newfunction(:defined_with_params, |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 :type => :rvalue, |
275
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
6 :doc => <<-DOC |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
7 @summary |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
8 Takes a resource reference and an optional hash of attributes. |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 |
275
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
10 Returns `true` if a resource with the specified attributes has already been added |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
11 to the catalog, and `false` otherwise. |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 |
275
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
13 ``` |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
14 user { 'dan': |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
15 ensure => present, |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
16 } |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 |
275
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
18 if ! defined_with_params(User[dan], {'ensure' => 'present' }) { |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
19 user { 'dan': ensure => present, } |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
20 } |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
21 ``` |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
22 |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
23 @return [Boolean] |
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
24 returns `true` or `false` |
272 | 25 DOC |
26 ) do |vals| | |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 reference, params = vals |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 raise(ArgumentError, 'Must specify a reference') unless reference |
272 | 29 if !params || params == '' |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 params = {} |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 end |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 ret = false |
272 | 33 |
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) | |
275
d9352a684e62
Mass update of modules to remove deprecation warnings
IBBoard <dev@ibboard.co.uk>
parents:
272
diff
changeset
|
40 type = reference.type |
272 | 41 title = reference.title |
42 else | |
43 raise(ArgumentError, "Reference is not understood: '#{reference.class}'") | |
44 end | |
45 # end workaround | |
46 else | |
47 type = reference.to_s | |
48 title = nil | |
49 end | |
50 | |
424
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
51 resources = if title.empty? |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
52 catalog.resources.select { |r| r.type == type } |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
53 else |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
54 [findresource(type, title)] |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
55 end |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
56 |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
57 resources.compact.each do |res| |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
58 # If you call this from within a defined type, it will find itself |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
59 next if res.to_s == resource.to_s |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
60 |
272 | 61 matches = params.map do |key, value| |
62 # eql? avoids bugs caused by monkeypatching in puppet | |
424
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
63 res_is_undef = res[key].eql?(:undef) || res[key].nil? |
272 | 64 value_is_undef = value.eql?(:undef) || value.nil? |
424
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
65 found_match = (res_is_undef && value_is_undef) || (res[key] == value) |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
66 |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
67 Puppet.debug("Matching resource is #{res}") if found_match |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
68 |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
69 found_match |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
70 end |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
71 ret = params.empty? || !matches.include?(false) |
424
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
72 |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
73 break if ret |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
74 end |
424
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
75 |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
76 Puppet.debug("Resource #{reference} was not determined to be defined") unless ret |
4a2ee7e3b110
Update stdlib in case it fixed deprecation
IBBoard <dev@ibboard.co.uk>
parents:
275
diff
changeset
|
77 |
0
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
78 ret |
956e484adc12
Initial public release of Puppet configs
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
79 end |