diff options
author | Christian Kaenzig <christian.kaenzig@camptocamp.com> | 2012-09-19 14:46:09 +0200 |
---|---|---|
committer | Christian Kaenzig <christian.kaenzig@camptocamp.com> | 2012-09-20 10:30:39 +0200 |
commit | e97e408116525f28b53162b89e6b582fb71020d2 (patch) | |
tree | 834603e94f6bbdbe59c4129af686483dd7843ffd /lib/puppet/parser/functions | |
parent | cca28df8d5ef7154bc16581d77193ddf10395252 (diff) |
couchdblookup: add support for default value
Add an optional 3rd parameters to couchdblookup that specifies a default
value that is returned if the key doesn't exist in the document.
Diffstat (limited to 'lib/puppet/parser/functions')
-rw-r--r-- | lib/puppet/parser/functions/couchdblookup.rb | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/lib/puppet/parser/functions/couchdblookup.rb b/lib/puppet/parser/functions/couchdblookup.rb index 7f920a8..132c488 100644 --- a/lib/puppet/parser/functions/couchdblookup.rb +++ b/lib/puppet/parser/functions/couchdblookup.rb @@ -8,10 +8,11 @@ module Puppet::Parser::Functions require 'json' require 'open-uri' + raise Puppet::ParseError, ("couchdblookup(): wrong number of arguments (#{args.length}; must be 2 or 3)") unless args.length.between?(2, 3) + url = args[0] key = args[1] - - raise Puppet::ParseError, ("couchdblookup(): wrong number of arguments (#{args.length}; must be == 2)") if args.length != 2 + default = args[2] if args.length >= 3 begin json = JSON.parse(open(URI.parse(url)).read) @@ -39,7 +40,7 @@ module Puppet::Parser::Functions result = json[key] end - result or raise Puppet::ParseError, "couchdblookup(): key '#{key}' not found in JSON object !" + result or default or raise Puppet::ParseError, "couchdblookup(): key '#{key}' not found in JSON object !" end end |