summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions/defined_with_params.rb
diff options
context:
space:
mode:
authorJeff McCune <jeff@puppetlabs.com>2012-10-25 10:54:39 -0700
committerJeff McCune <jeff@puppetlabs.com>2012-10-25 10:54:39 -0700
commit699d520e72014a3a17395c52c04f5b521d1318e6 (patch)
treece291f0e86475f9ec60f07f85ef5ab130d210607 /lib/puppet/parser/functions/defined_with_params.rb
parent9693c04c9d877e0f877418bc41e16f01aaf784cd (diff)
parent444393bf6b1e21da4e31f9037d17ea417b9b473b (diff)
downloadpuppet-stdlib-699d520e72014a3a17395c52c04f5b521d1318e6.tar.gz
puppet-stdlib-699d520e72014a3a17395c52c04f5b521d1318e6.tar.bz2
Merge branch 'maint/2.5.x/pick_pr86_ensure_resource' into maint/2.5.x/pick_compatible_new_functionality
* maint/2.5.x/pick_pr86_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.rb35
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