Sha256: ebb84a0cb3818e79d327863e5818c4db724a6f46d2584bbfe4b6de2106476878

Contents?: true

Size: 1.38 KB

Versions: 22

Compression:

Stored size: 1.38 KB

Contents

module Puppet::Parser::Functions
  newfunction(:pdbquery, :type => :rvalue, :doc => "\
    Perform a PuppetDB query

    The first argument is the URL path that should be queried, for
    example 'nodes' or 'status/nodes/<nodename>'.
    The second argument if supplied if the query parameter, if it is
    a string it is assumed to be JSON formatted and sent as is,
    anything else is converted to JSON and then sent.

    Example: pdbquery('nodes', ['=', ['node', 'active'], true ])") do |args|

    raise(Puppet::ParseError, "pdbquery(): Wrong number of arguments " +
      "given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2

    require 'puppet/network/http_pool'
    require 'uri'
    require 'puppet/util/puppetdb'
    require 'puppet/indirector/rest'

    # Query type (URL path)
    t, q = args

    # Query contents
    if q then
      # Convert to JSON if it isn't already
      q=q.to_pson unless q.is_a? String
      params = URI.escape("?query=#{q}")
    else
      params = ''
    end

    conn = Puppet::Network::HttpPool.http_instance(Puppet::Util::Puppetdb.server, Puppet::Util::Puppetdb.port, use_ssl = true)
    response = conn.get("/v1/#{t}#{params}", { "Accept" => "application/json",})

    unless response.kind_of?(Net::HTTPSuccess)
      raise Puppet::ParseError, "PuppetDB query error: [#{response.code}] #{response.msg}"
    end
    PSON.load(response.body)
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
ruby-puppetdb-1.6.1 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.6.0 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.5.3 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.5.2 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.5.1 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.5.0 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.4.0 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.3.3 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.3.2 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.3.1 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.3.0 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.2.0 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.1.1 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.1.0 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.0.4 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.0.3 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.0.2 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.0.1 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.0.0 lib/puppet/parser/functions/pdbquery.rb
ruby-puppetdb-1.0.0.pre5 lib/puppet/parser/functions/pdbquery.rb