diff options
author | Jeff McCune <jeff@puppetlabs.com> | 2012-10-25 10:58:27 -0700 |
---|---|---|
committer | Jeff McCune <jeff@puppetlabs.com> | 2012-10-25 10:58:27 -0700 |
commit | 12a146058ee885e9ad295e978b15465259a905c5 (patch) | |
tree | edb57c2cf326241c762052ab6431d96f464f6033 /lib/puppet/parser/functions/defined_with_params.rb | |
parent | 9693c04c9d877e0f877418bc41e16f01aaf784cd (diff) | |
parent | c2573298a5a58ca902d1d04ca2f91dd85bef6581 (diff) | |
download | puppet-stdlib-12a146058ee885e9ad295e978b15465259a905c5.tar.gz puppet-stdlib-12a146058ee885e9ad295e978b15465259a905c5.tar.bz2 |
Merge branch 'maint/2.5.x/pick_compatible_new_functionality' into 2.5.x
* maint/2.5.x/pick_compatible_new_functionality:
Explicitly load functions used by ensure_resource
re-formatting
Add better docs about duplicate resource failures
Handle undef for parameter argument
Add function ensure_resource and defined_with_params
Diffstat (limited to 'lib/puppet/parser/functions/defined_with_params.rb')
-rw-r--r-- | lib/puppet/parser/functions/defined_with_params.rb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/defined_with_params.rb b/lib/puppet/parser/functions/defined_with_params.rb new file mode 100644 index 0000000..d7df306 --- /dev/null +++ b/lib/puppet/parser/functions/defined_with_params.rb @@ -0,0 +1,35 @@ +# Test whether a given class or definition is defined +require 'puppet/parser/functions' + +Puppet::Parser::Functions.newfunction(:defined_with_params, + :type => :rvalue, + :doc => <<-'ENDOFDOC' +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, } + } +ENDOFDOC +) do |vals| + reference, params = vals + raise(ArgumentError, 'Must specify a reference') unless reference + if (! params) || params == '' + params = {} + end + ret = false + if resource = findresource(reference.to_s) + matches = params.collect do |key, value| + resource[key] == value + end + ret = params.empty? || !matches.include?(false) + end + Puppet.debug("Resource #{reference} was not determined to be defined") + ret +end |