Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow module to work with puppet 6.16 and higher #127

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/hiera/backend/puppetdb_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/functions/query_facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/puppet/functions/query_nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/parser/functions/query_facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/puppet/parser/functions/query_nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/puppet/parser/functions/query_resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 19 additions & 20 deletions lib/puppetdb/connection.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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