Commit 9c81086b authored by Andrew Roetker's avatar Andrew Roetker

(PDB-2186) Add PuppetDB utility function for querying PuppetDB

This commit adds a PuppetDB terminus utility function to query PuppetDB
at the `root` endpoint. This essentially provides `puppetdb-termini`
consumers a main entripoint into the module.
parent 2895b872
......@@ -6,6 +6,7 @@ require 'puppet/util/puppetdb/command'
require 'puppet/util/puppetdb/config'
require 'digest/sha1'
require 'time'
require 'json'
require 'fileutils'
module Puppet::Util::Puppetdb
......@@ -64,6 +65,21 @@ module Puppet::Util::Puppetdb
end
end
# Query PuppetDB.
#
# @param query [String, Array] The PQL or AST query for PuppetDB
# @return [Array<Hash>]
def query_puppetdb(query)
profile("Submitted query '#{query}'", [:puppetdb, :query, query]) do
headers = { "Accept" => "application/json",
"Content-Type" => "application/json; charset=UTF-8" }
response = Puppet::Util::Puppetdb::Http.action("/pdb/query/v4", :query) do |http_instance, path|
http_instance.post(path, { 'query' => query }.to_json, headers)
end
JSON.parse(response.body)
end
end
# Profile a block of code and log the time it took to execute.
#
# This outputs logs entries to the Puppet masters logging destination
......
......@@ -147,7 +147,7 @@ module Puppet::Util::Puppetdb
request_exception = with_http_error_logging(server_url, route) {
http = Puppet::Network::HttpPool.http_instance(server_url.host, server_url.port)
response = timeout(config.server_url_timeout) do
response = Timeout.timeout(config.server_url_timeout) do
http_callback.call(http, route)
end
}
......@@ -182,7 +182,7 @@ module Puppet::Util::Puppetdb
request_exception = with_http_error_logging(server_url, route) {
http = Puppet::Network::HttpPool.http_instance(server_url.host, server_url.port)
response = timeout(config.server_url_timeout) do
response = Timeout.timeout(config.server_url_timeout) do
http_callback.call(http, route)
end
}
......
......@@ -5,23 +5,30 @@ require 'spec_helper'
require 'digest/sha1'
require 'puppet/util/puppetdb'
require 'puppet/util/puppetdb/command_names'
require 'puppet/util/puppetdb/http'
require 'json'
Command = Puppet::Util::Puppetdb::Command
class FakeHttpResponse
def initialize(body)
@body = body
end
attr_reader :body
end
describe Puppet::Util::Puppetdb do
subject { Object.new.extend described_class }
describe "#submit_command" do
let(:payload) { {'resistance' => 'futile', 'opinion' => 'irrelevant'} }
let(:command1) { Command.new("OPEN SESAME", 1, 'foo.localdomain',
payload.merge(:uniqueprop => "command1")) }
let(:payload) { {'resistance' => 'futile', 'opinion' => 'irrelevant'} }
let(:command1) { Puppet::Util::Puppetdb::Command.new("OPEN SESAME", 1, 'foo.localdomain',
payload.merge(:uniqueprop => "command1")) }
it "should submit the command" do
# careful here... since we're going to stub Command.new, we need to
# make sure we reference command1 first, because it calls Command.new.
command1.expects(:submit).once
Command.expects(:new).once.returns(command1)
Puppet::Util::Puppetdb::Command.expects(:new).once.returns(command1)
subject.submit_command(command1.certname,
command1.payload,
command1.command,
......@@ -30,4 +37,17 @@ describe Puppet::Util::Puppetdb do
end
describe "#query_puppetdb" do
let(:response) { JSON.generate({'certname' => 'futile', 'status' => 'irrelevant'}) }
let(:query) { ["=", "type", "Foo"] }
let(:http_response) { FakeHttpResponse.new(response) }
it "should query PuppetDB" do
# careful here... since we're going to stub Command.new, we need to
# make sure we reference command1 first, because it calls Command.new.
Puppet::Util::Puppetdb::Http.expects(:action).once.returns(http_response)
subject.query_puppetdb(query)
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment