Skip to content

Commit 210e3c7

Browse files
author
Kevin Paulisse
committed
Introduce support for v3 PuppetDB API for fact retrieval
1 parent 5263d00 commit 210e3c7

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

lib/octocatalog-diff/facts/puppetdb.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ module OctocatalogDiff
66
class Facts
77
# Deal with facts in PuppetDB
88
class PuppetDB
9+
# Supporting multiple versions of the PuppetDB API.
10+
PUPPETDB_QUERY_FACTS_URL = {
11+
'3' => '/v3/nodes/<NODE>/facts',
12+
'4' => '/pdb/query/v4/nodes/<NODE>/facts'
13+
}.freeze
14+
915
# Retrieve facts from PuppetDB for a specified node.
1016
# @param :puppetdb_url [String|Array] => URL to PuppetDB
1117
# @param :retry [Fixnum] => Retry after timeout (default 0 retries, can be more)
@@ -15,7 +21,8 @@ def self.fact_retriever(options = {}, node)
1521
# Set up some variables from options
1622
raise ArgumentError, 'puppetdb_url is required' unless options[:puppetdb_url].is_a?(String)
1723
raise ArgumentError, 'node must be a non-empty string' unless node.is_a?(String) && node != ''
18-
uri = "/pdb/query/v4/nodes/#{node}/facts"
24+
puppetdb_api_version = options.fetch(:puppetdb_api_version, 4)
25+
uri = PUPPETDB_QUERY_FACTS_URL.fetch(puppetdb_api_version.to_s).gsub('<NODE>', node)
1926
retries = options.fetch(:retry, 0).to_i
2027

2128
# Construct puppetdb object and options

spec/octocatalog-diff/tests/facts/puppetdb_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,48 @@
3030
end
3131
end
3232

33+
context 'PuppetDB API compatibility layer' do
34+
before(:each) do
35+
clazz = double('OctocatalogDiff::PuppetDB')
36+
allow(clazz).to receive(:get) { |args| [{ 'certname' => 'foo.bar.com', 'name' => 'uri', 'value' => args }] }
37+
allow(OctocatalogDiff::PuppetDB).to receive(:new).and_return(clazz)
38+
end
39+
40+
it 'should use the correct URL for API v3' do
41+
opts = {
42+
puppetdb_api_version: 3,
43+
puppetdb_url: 'https://foo.bar.baz:8081'
44+
}
45+
result = OctocatalogDiff::Facts::PuppetDB.fact_retriever(opts, 'foo.bar.com')
46+
expect(result).to eq('name' => 'foo.bar.com', 'values' => { 'uri' => '/v3/nodes/foo.bar.com/facts' })
47+
end
48+
49+
it 'should use the correct URL for API v4' do
50+
opts = {
51+
puppetdb_api_version: 4,
52+
puppetdb_url: 'https://foo.bar.baz:8081'
53+
}
54+
result = OctocatalogDiff::Facts::PuppetDB.fact_retriever(opts, 'foo.bar.com')
55+
expect(result).to eq('name' => 'foo.bar.com', 'values' => { 'uri' => '/pdb/query/v4/nodes/foo.bar.com/facts' })
56+
end
57+
58+
it 'should default to API v4' do
59+
opts = {
60+
puppetdb_url: 'https://foo.bar.baz:8081'
61+
}
62+
result = OctocatalogDiff::Facts::PuppetDB.fact_retriever(opts, 'foo.bar.com')
63+
expect(result).to eq('name' => 'foo.bar.com', 'values' => { 'uri' => '/pdb/query/v4/nodes/foo.bar.com/facts' })
64+
end
65+
66+
it 'should fail if an unrecognized API version is provided' do
67+
opts = {
68+
puppetdb_api_version: 9000,
69+
puppetdb_url: 'https://foo.bar.baz:8081'
70+
}
71+
expect { OctocatalogDiff::Facts::PuppetDB.fact_retriever(opts, 'foo.bar.com') }.to raise_error(KeyError)
72+
end
73+
end
74+
3375
context 'mocking methods for error testing' do
3476
describe '#fact_retriever' do
3577
let(:opts) { { puppetdb_url: 'https://mocked-puppetdb.somedomain.xyz:8081', node: 'valid-facts' } }

0 commit comments

Comments
 (0)