diff --git a/lib/hiera/backend/puppetdb_backend.rb b/lib/hiera/backend/puppetdb_backend.rb index b870c07..8acc334 100644 --- a/lib/hiera/backend/puppetdb_backend.rb +++ b/lib/hiera/backend/puppetdb_backend.rb @@ -45,10 +45,10 @@ def lookup(key, scope, order_override, _resolution_type) if fact query = @parser.facts_query query, [fact] - @puppetdb.query(:facts, query).collect { |f| f['value'] }.sort + @puppetdb.query(:facts, query, { :source => 'function' }).collect { |f| f['value'] }.sort else query = @parser.parse query, :nodes if query.is_a? String - @puppetdb.query(:nodes, query).collect { |n| n['name'] } + @puppetdb.query(:nodes, query, { :source => 'function' }).collect { |n| n['name'] } end end end diff --git a/lib/puppet/functions/query_facts.rb b/lib/puppet/functions/query_facts.rb index eceb1db..25c596f 100644 --- a/lib/puppet/functions/query_facts.rb +++ b/lib/puppet/functions/query_facts.rb @@ -33,6 +33,6 @@ def query_facts(query, facts) puppetdb = PuppetDB::Connection.new(uri.host, uri.port, uri.scheme == 'https') parser = PuppetDB::Parser.new query = parser.facts_query query, facts_for_query if query.is_a? String - parser.facts_hash(puppetdb.query(:facts, query, :extract => [:certname, :name, :value]), facts) + parser.facts_hash(puppetdb.query(:facts, query, { :extract => [:certname, :name, :value], :source => 'function' }), facts) end end diff --git a/lib/puppet/functions/query_nodes.rb b/lib/puppet/functions/query_nodes.rb index 412355e..7eafb0c 100644 --- a/lib/puppet/functions/query_nodes.rb +++ b/lib/puppet/functions/query_nodes.rb @@ -42,7 +42,7 @@ def puppetdb def query_nodes(query) query = parser.parse(query, :nodes) if query.is_a? String - puppetdb.query(:nodes, query, :extract => :certname).collect do |n| + puppetdb.query(:nodes, query, { :extract => :certname, :source => 'function' }).collect do |n| n['certname'] end end @@ -51,7 +51,7 @@ def query_nodes_fact(query, fact) fact_for_query = fact.split('.').first query = parser.facts_query(query, [fact_for_query]) - response = puppetdb.query(:facts, query, :extract => :value) + response = puppetdb.query(:facts, query, { :extract => :value, :source => 'function' }) if fact.split('.').size > 1 parser.extract_nested_fact(response, fact.split('.')[1..-1]) diff --git a/lib/puppet/parser/functions/query_facts.rb b/lib/puppet/parser/functions/query_facts.rb index db73817..4487838 100644 --- a/lib/puppet/parser/functions/query_facts.rb +++ b/lib/puppet/parser/functions/query_facts.rb @@ -34,5 +34,5 @@ puppetdb = PuppetDB::Connection.new(uri.host, uri.port, uri.scheme == 'https') parser = PuppetDB::Parser.new query = parser.facts_query query, facts_for_query if query.is_a? String - parser.facts_hash(puppetdb.query(:facts, query, :extract => [:certname, :name, :value]), facts) + parser.facts_hash(puppetdb.query(:facts, query, { :extract => [:certname, :name, :value], :source => 'function' }), facts) end diff --git a/lib/puppet/parser/functions/query_nodes.rb b/lib/puppet/parser/functions/query_nodes.rb index 83898bc..056fe50 100644 --- a/lib/puppet/parser/functions/query_nodes.rb +++ b/lib/puppet/parser/functions/query_nodes.rb @@ -36,7 +36,7 @@ parser = PuppetDB::Parser.new if fact_for_query query = parser.facts_query(query, [fact_for_query]) - response = puppetdb.query(:facts, query, :extract => :value) + response = puppetdb.query(:facts, query, { :extract => :value, :source => 'function' }) if fact.split('.').size > 1 parser.extract_nested_fact(response, fact.split('.')[1..-1]) @@ -45,6 +45,6 @@ end else query = parser.parse(query, :nodes) if query.is_a? String - puppetdb.query(:nodes, query, :extract => :certname).collect { |n| n['certname'] } + puppetdb.query(:nodes, query, { :extract => :certname, :source => 'function' }).collect { |n| n['certname'] } end end diff --git a/lib/puppet/parser/functions/query_resources.rb b/lib/puppet/parser/functions/query_resources.rb index 5f86359..16d6f41 100644 --- a/lib/puppet/parser/functions/query_resources.rb +++ b/lib/puppet/parser/functions/query_resources.rb @@ -61,7 +61,7 @@ end # Fetch the results - results = puppetdb.query(:resources, q) + results = puppetdb.query(:resources, q, { :source => 'function' }) # If grouphosts is true create a nested hash with nodes and resources if grouphosts diff --git a/lib/puppetdb/connection.rb b/lib/puppetdb/connection.rb index 426b265..e6810b5 100644 --- a/lib/puppetdb/connection.rb +++ b/lib/puppetdb/connection.rb @@ -1,12 +1,14 @@ +# frozen_string_literal: true + require 'puppetdb' +require 'rubygems' +require 'puppetdb/parser' +require 'uri' +require 'puppet' +require 'puppet/util/logging' +require 'json' class PuppetDB::Connection - require 'rubygems' - require 'puppetdb/parser' - require 'uri' - require 'puppet' - require 'puppet/util/logging' - include Puppet::Util::Logging def initialize(host = 'puppetdb', port = 443, use_ssl = true) @@ -34,11 +36,11 @@ def self.check_version # @param options [Hash] specify extract values or http connection # @return [Array] the results of the query def query(endpoint, query = nil, options = {}, version = :v4) - require 'json' default_options = { :http => nil, # A HTTP object to be used for the connection - :extract => nil # An array of fields to extract + :extract => nil, # An array of fields to extract + :source => 'face' # Source of request (face or function) } if options.is_a? Hash @@ -47,25 +49,22 @@ def query(endpoint, query = nil, options = {}, version = :v4) Puppet.deprecation_warning 'Specify http object with :http key instead' options = default_options.merge(:http => options) end - http = options[:http] - unless http - require 'puppet/network/http_pool' - http = Puppet::Network::HttpPool.http_instance(@host, @port, @use_ssl) - end + source = options[:source] + http = options[:http] || Puppet.runtime[:http] headers = { 'Accept' => 'application/json' } - if options[:extract] - query = PuppetDB::ParserHelper.extract(*Array(options[:extract]), query) - end + query = PuppetDB::ParserHelper.extract(*Array(options[:extract]), query) if options[:extract] - uri = "/pdb/query/#{version}/#{endpoint}" - uri += URI.escape "?query=#{query.to_json}" unless query.nil? || query.empty? + uri = URI("#{@use_ssl ? 'https' : 'http'}://#{@host}:#{@port}/pdb/query/#{version}/#{endpoint}") + _query = "#{query.to_json}" unless query.nil? || query.empty? + uri.query = (source == 'function' ? URI.encode_www_form(query: _query) : "query=#{_query}") unless _query.empty? + debug("PuppetDB uri: #{uri.to_s}") debug("PuppetDB query: #{query.to_json}") - resp = http.get(uri, headers) - fail "PuppetDB query error: [#{resp.code}] #{resp.msg}, query: #{query.to_json}" unless resp.is_a?(Net::HTTPSuccess) + resp = http.get(uri, headers: headers) + raise "PuppetDB query error: [#{resp.code}] #{resp.reason}, url: #{uri.to_s}, query: #{query.to_json}" unless resp.success? JSON.parse(resp.body) end end