diff options
author | Mathieu Bornoz <mathieu.bornoz@camptocamp.com> | 2010-11-25 10:19:40 +0100 |
---|---|---|
committer | Mathieu Bornoz <mathieu.bornoz@camptocamp.com> | 2010-11-25 14:12:52 +0100 |
commit | 6e339f4a82d590ec28e9c81d39d12ec7923d9207 (patch) | |
tree | 0a73e4962221f11178b95516f100d3187047c166 /lib/puppet/parser/functions/couchdblookup.rb | |
parent | 1cca7f47a1508dc3c05b4eb33445d2f730de382c (diff) |
Revert "removed couchdb lookup"
This reverts commit ea97fa67482e3597b32ffc3a21eb7a3361cc5515.
Diffstat (limited to 'lib/puppet/parser/functions/couchdblookup.rb')
-rw-r--r-- | lib/puppet/parser/functions/couchdblookup.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/couchdblookup.rb b/lib/puppet/parser/functions/couchdblookup.rb new file mode 100644 index 0000000..807c138 --- /dev/null +++ b/lib/puppet/parser/functions/couchdblookup.rb @@ -0,0 +1,38 @@ +# +# A basic function to retrieve data in couchdb +# + +require 'json' +require 'open-uri' + +module Puppet::Parser::Functions + newfunction(:couchdblookup, :type => :rvalue) do |args| + + url = args[0] + key = args[1] + + raise Puppet::ParseError, ("couchdblookup(): wrong number of arguments (#{args.length}; must be == 2)") if args.length != 2 + + begin + json = JSON.parse(open(URI.parse(url)).read) + rescue OpenURI::HTTPError => error + raise Puppet::ParseError, "couchdblookup(): fetching URL #{url} failed with status #{error.message}" + end + + result = nil + if json.has_key?("rows") and json['total_rows'] > 0 and json['rows'][0].has_key?(key) + result = Array.new + json['rows'].each do |x| + result.push(x[key]) + end + else + if json.has_key?(key) + result = json[key] + end + end + + result or raise Puppet::ParseError, "couchdblookup(): key '#{key}' not found in JSON object !" + + end +end + |