Skip to content

Commit

Permalink
Adds backward compatibility to retrieve catalogs from older PuppetDB …
Browse files Browse the repository at this point in the history
…version 2.3 used in Puppet 3 in the latest version of puppet-catalog-diff. This is neccesary when you want to use puppet-catalog-diff to compare the cataloges between a Puppet 3 and Puppet 5 Master, as we did when we upgraded to Puppet 5. This code adds a fallback to the older /v4/nodes/?query PuppetDB API if the latest version of the API, that is /pdb/query/v4/nodes?, fails. This means that the code is compatible with both versions and thus allows the latest version of puppet-catalog-diff to still be used together with Puppet 3 servers, which is useful for those who still hasn't upgraded to newer version of Puppet.

We needed to use this code modification in combination with voxpupuli#38 in our environment, but this change does not require this pull request as long as the encoding is valid UTF-8 in the cataloges stored in PuppetDB on the Puppet 3 Master server.
  • Loading branch information
JohnEricson committed Oct 5, 2020
1 parent 66f962e commit 1839b06
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions lib/puppet/catalog-diff/searchfacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ def find_nodes(options = {})
active_nodes
end

def find_nodes_puppetdb(env)
require 'puppet/util/puppetdb'
server_url = Puppet::Util::Puppetdb.config.server_urls[0]
port = server_url.port
use_ssl = port != 8080
connection = Puppet::Network::HttpPool.http_instance(server_url.host, port, use_ssl)
def build_query(env, version)
base_query = ['and', ['=', ['node', 'active'], true]]
base_query.concat([['=', 'catalog_environment', env]]) if env
if version == 'latest'
query_field_catalog_environment = 'catalog_environment'
else
query_field_catalog_environment = 'catalog-environment'
end
base_query.concat([['=', query_field_catalog_environment, env]]) if env
real_facts = @facts.reject { |_k, v| v.nil? }
query = base_query.concat(real_facts.map { |k, v| ['=', ['fact', k], v] })
classes = Hash[@facts.select { |_k, v| v.nil? }].keys
Expand All @@ -43,9 +43,28 @@ def find_nodes_puppetdb(env)
['=', 'title', capit]]]]]],
)
end
json_query = URI.encode_www_form_component(query.to_json)
query
end

def find_nodes_puppetdb(env)
require 'puppet/util/puppetdb'
puppetdb_version = 'latest'
server_url = Puppet::Util::Puppetdb.config.server_urls[0]
port = server_url.port
use_ssl = port != 8080
connection = Puppet::Network::HttpPool.http_instance(server_url.host, port, use_ssl)
query = build_query(env, puppetdb_version)
json_query = URI.escape(query.to_json)
begin
filtered = PSON.parse(connection.request_get("/pdb/query/v4/nodes?query=#{json_query}", 'Accept' => 'application/json').body)
result = connection.request_get("/pdb/query/v4/nodes?query=#{json_query}", 'Accept' => 'application/json')
if result.code.to_i >= 400
puppetdb_version = '2.3'
Puppet::debug("Query returned HTTP code #{result.code}. Falling back to older version of API used in PuppetDB version #{puppetdb_version}.")
query = build_query(env, puppetdb_version)
json_query = URI.escape(query.to_json)
result = connection.request_get("/v4/nodes/?query=#{json_query}", 'Accept' => 'application/json')
end
filtered = PSON.parse(result.body)
rescue PSON::ParserError => e
raise "Error parsing json output of puppet search: #{e.message}"
end
Expand Down

0 comments on commit 1839b06

Please sign in to comment.