]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/parser/functions/defined_with_params.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / defined_with_params.rb
1 # Test whether a given class or definition is defined
2 require 'puppet/parser/functions'
3
4 Puppet::Parser::Functions.newfunction(:defined_with_params,
5                                       :type => :rvalue,
6                                       :doc => <<-'ENDOFDOC'
7 Takes a resource reference and an optional hash of attributes.
8
9 Returns true if a resource with the specified attributes has already been added
10 to 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     }
19 ENDOFDOC
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       resource[key] == value
30     end
31     ret = params.empty? || !matches.include?(false)
32   end
33   Puppet.debug("Resource #{reference} was not determined to be defined")
34   ret
35 end