Skip to content

Commit dedfdf3

Browse files
authored
Merge pull request #24 from github/kpaulisse-puppetdb-v3
Support puppetdb API v3
2 parents 4aceadc + f22a62e commit dedfdf3

5 files changed

Lines changed: 126 additions & 30 deletions

File tree

doc/optionsref.md

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,52 +67,53 @@ Usage: octocatalog-diff [command line options]
6767
--puppet-binary STRING Full path to puppet binary globally
6868
--to-puppet-binary STRING Full path to puppet binary for the to branch
6969
--from-puppet-binary STRING Full path to puppet binary for the from branch
70-
--[no-]from-puppetdb Pull "from" catalog from PuppetDB instead of compiling
7170
--[no-]parallel Enable or disable parallel processing
72-
--puppetdb-ssl-ca FILENAME CA certificate that signed the PuppetDB certificate
73-
--puppetdb-ssl-client-cert FILENAME
74-
SSL client certificate to connect to PuppetDB
71+
--[no-]from-puppetdb Pull "from" catalog from PuppetDB instead of compiling
7572
--puppetdb-ssl-client-password-file FILENAME
7673
Read password for SSL client key from a file
7774
--puppetdb-ssl-client-password PASSWORD
7875
Password for SSL client key to connect to PuppetDB
79-
--facts-terminus STRING Facts terminus: one of yaml, facter
80-
--puppetdb-url URL PuppetDB base URL
8176
--puppetdb-ssl-client-key FILENAME
8277
SSL client key to connect to PuppetDB
83-
--puppet-master-ssl-client-key STRING
84-
Full path to key file for SSL client auth to Puppet Master globally
85-
--to-puppet-master-ssl-client-key STRING
86-
Full path to key file for SSL client auth to Puppet Master for the to branch
87-
--from-puppet-master-ssl-client-key STRING
88-
Full path to key file for SSL client auth to Puppet Master for the from branch
89-
--puppet-master STRING Hostname or Hostname:PortNumber for Puppet Master globally
90-
--to-puppet-master STRING Hostname or Hostname:PortNumber for Puppet Master for the to branch
91-
--from-puppet-master STRING Hostname or Hostname:PortNumber for Puppet Master for the from branch
92-
--puppet-master-api-version STRING
93-
Puppet Master API version (2 for Puppet 3.x, 3 for Puppet 4.x) globally
94-
--to-puppet-master-api-version STRING
95-
Puppet Master API version (2 for Puppet 3.x, 3 for Puppet 4.x) for the to branch
96-
--from-puppet-master-api-version STRING
97-
Puppet Master API version (2 for Puppet 3.x, 3 for Puppet 4.x) for the from branch
78+
--puppetdb-ssl-ca FILENAME CA certificate that signed the PuppetDB certificate
79+
--puppetdb-ssl-client-cert FILENAME
80+
SSL client certificate to connect to PuppetDB
81+
--facts-terminus STRING Facts terminus: one of yaml, facter
82+
--puppetdb-url URL PuppetDB base URL
83+
--puppetdb-api-version N Version of PuppetDB API (3 or 4)
9884
--puppet-master-ssl-ca STRING
9985
Full path to CA certificate that signed the Puppet Master certificate globally
10086
--to-puppet-master-ssl-ca STRING
10187
Full path to CA certificate that signed the Puppet Master certificate for the to branch
10288
--from-puppet-master-ssl-ca STRING
10389
Full path to CA certificate that signed the Puppet Master certificate for the from branch
104-
--puppet-master-ssl-client-cert STRING
105-
Full path to certificate file for SSL client auth to Puppet Master globally
106-
--to-puppet-master-ssl-client-cert STRING
107-
Full path to certificate file for SSL client auth to Puppet Master for the to branch
108-
--from-puppet-master-ssl-client-cert STRING
109-
Full path to certificate file for SSL client auth to Puppet Master for the from branch
90+
--puppet-master-ssl-client-key STRING
91+
Full path to key file for SSL client auth to Puppet Master globally
92+
--to-puppet-master-ssl-client-key STRING
93+
Full path to key file for SSL client auth to Puppet Master for the to branch
94+
--from-puppet-master-ssl-client-key STRING
95+
Full path to key file for SSL client auth to Puppet Master for the from branch
11096
--fact-override STRING1[,STRING2[,...]]
11197
Override fact globally
11298
--to-fact-override STRING1[,STRING2[,...]]
11399
Override fact for the to branch
114100
--from-fact-override STRING1[,STRING2[,...]]
115101
Override fact for the from branch
102+
--puppet-master-ssl-client-cert STRING
103+
Full path to certificate file for SSL client auth to Puppet Master globally
104+
--to-puppet-master-ssl-client-cert STRING
105+
Full path to certificate file for SSL client auth to Puppet Master for the to branch
106+
--from-puppet-master-ssl-client-cert STRING
107+
Full path to certificate file for SSL client auth to Puppet Master for the from branch
108+
--puppet-master-api-version STRING
109+
Puppet Master API version (2 for Puppet 3.x, 3 for Puppet 4.x) globally
110+
--to-puppet-master-api-version STRING
111+
Puppet Master API version (2 for Puppet 3.x, 3 for Puppet 4.x) for the to branch
112+
--from-puppet-master-api-version STRING
113+
Puppet Master API version (2 for Puppet 3.x, 3 for Puppet 4.x) for the from branch
114+
--puppet-master STRING Hostname or Hostname:PortNumber for Puppet Master globally
115+
--to-puppet-master STRING Hostname or Hostname:PortNumber for Puppet Master for the to branch
116+
--from-puppet-master STRING Hostname or Hostname:PortNumber for Puppet Master for the from branch
116117
--pe-enc-url URL Base URL for Puppet Enterprise ENC endpoint
117118
--pe-enc-token-file PATH Path containing token for PE node classifier, relative or absolute
118119
--pe-enc-token TOKEN Token to access the Puppet Enterprise ENC API
@@ -124,10 +125,10 @@ Usage: octocatalog-diff [command line options]
124125
--no-ignore-tags Disable ignoring based on tags
125126
--ignore-tags STRING1[,STRING2[,...]]
126127
Specify tags to ignore
127-
--[no-]suppress-absent-file-details
128-
Suppress certain attributes of absent files
129128
--pass-env-vars VAR1[,VAR2[,...]]
130129
Environment variables to pass
130+
--[no-]suppress-absent-file-details
131+
Suppress certain attributes of absent files
131132
132133
```
133134

@@ -828,6 +829,19 @@ https://your-pe-console-server:4433/classifier-api (<a href="../lib/octocatalog-
828829
</td>
829830
</tr>
830831

832+
<tr>
833+
<td valign=top>
834+
<pre><code>--puppetdb-api-version N</code></pre>
835+
</td>
836+
<td valign=top>
837+
Version of PuppetDB API (3 or 4)
838+
</td>
839+
<td valign=top>
840+
Specify the API version to use for the PuppetDB. The current values supported are '3' or '4', and '4' is
841+
the default. (<a href="../lib/octocatalog-diff/catalog-diff/cli/options/puppetdb_api_version.rb">puppetdb_api_version.rb</a>)
842+
</td>
843+
</tr>
844+
831845
<tr>
832846
<td valign=top>
833847
<pre><code>--puppetdb-ssl-ca FILENAME</code></pre>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Specify the API version to use for the PuppetDB. The current values supported are '3' or '4', and '4' is
2+
# the default.
3+
# @param parser [OptionParser object] The OptionParser argument
4+
# @param options [Hash] Options hash being constructed; this is modified in this method.
5+
OctocatalogDiff::CatalogDiff::Cli::Options::Option.newoption(:puppetdb_api_version) do
6+
has_weight 319
7+
8+
def parse(parser, options)
9+
parser.on('--puppetdb-api-version N', OptionParser::DecimalInteger, 'Version of PuppetDB API (3 or 4)') do |x|
10+
options[:puppetdb_api_version] = x
11+
raise ArgumentError, 'Only PuppetDB versions 3 and 4 are supported' unless [3, 4].include?(x)
12+
end
13+
end
14+
end

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
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require_relative '../options_helper'
2+
3+
describe OctocatalogDiff::CatalogDiff::Cli::Options do
4+
describe '#opt_puppetdb_api_version' do
5+
it 'should handle --puppetdb-api-version with API version 3' do
6+
result = run_optparse(['--puppetdb-api-version', '3'])
7+
expect(result[:puppetdb_api_version]).to eq(3)
8+
end
9+
10+
it 'should handle --puppetdb-api-version with API version 4' do
11+
result = run_optparse(['--puppetdb-api-version', '4'])
12+
expect(result[:puppetdb_api_version]).to eq(4)
13+
end
14+
15+
it 'should fail if --puppetdb-api-version is unsupported' do
16+
expect { run_optparse(['--puppetdb-api-version', '9000']) }.to raise_error(ArgumentError)
17+
end
18+
end
19+
end

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)