Skip to content

Commit b8fa3a4

Browse files
(PUP-10435) Facts provided in a file can't be used for classification
5cde767 added the ability to use facts from a fact file, but those facts were added after the classification happened, meaning that any facts used as rules for classification couldn't be overridden with the facts from the fact file. This commit fixes this issue by querying pdb to get the target node's facts, and merge the facts provided from the fact file, before the classification happens.
1 parent aaae117 commit b8fa3a4

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

lib/puppet/application/lookup.rb

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -337,18 +337,6 @@ def generate_scope
337337
Puppet.settings[:facts_terminus] = 'facter'
338338
end
339339

340-
unless node.is_a?(Puppet::Node) # to allow unit tests to pass a node instance
341-
ni = Puppet::Node.indirection
342-
tc = ni.terminus_class
343-
if tc == :plain || options[:compile]
344-
node = ni.find(node)
345-
else
346-
ni.terminus_class = :plain
347-
node = ni.find(node)
348-
ni.terminus_class = tc
349-
end
350-
end
351-
352340
fact_file = options[:fact_file]
353341

354342
if fact_file
@@ -364,7 +352,26 @@ def generate_scope
364352
unless given_facts.instance_of?(Hash)
365353
raise _("Incorrectly formatted data in %{fact_file} given via the --facts flag (only accepts yaml and json files)") % { fact_file: fact_file }
366354
end
367-
node.add_extra_facts(given_facts)
355+
end
356+
357+
unless node.is_a?(Puppet::Node) # to allow unit tests to pass a node instance
358+
facts = Puppet::Node::Facts.indirection.find(node, :environment => Puppet.lookup(:current_environment))
359+
360+
facts = Puppet::Node::Facts.new(node, {}) if facts.nil?
361+
facts.add_extra_values(given_facts) if given_facts
362+
363+
ni = Puppet::Node.indirection
364+
tc = ni.terminus_class
365+
366+
if tc == :plain || options[:compile]
367+
node = ni.find(node, facts: facts)
368+
else
369+
ni.terminus_class = :plain
370+
node = ni.find(node, facts: facts)
371+
ni.terminus_class = tc
372+
end
373+
else
374+
node.add_extra_facts(given_facts) if given_facts
368375
end
369376

370377
Puppet[:code] = 'undef' unless options[:compile]

spec/integration/application/lookup_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
include PuppetSpec::Files
88

99
context 'with an environment' do
10+
let(:facts) { Puppet::Node::Facts.new("facts", {}) }
1011
let(:env_name) { 'spec' }
1112
let(:env_dir) { tmpdir('environments') }
1213
let(:environment_files) do
@@ -98,6 +99,8 @@ def explain(key, options = {})
9899

99100
it 'skip loading of external facts when run with --node' do
100101
app.options[:node] = "random_node"
102+
103+
expect(Puppet::Node::Facts.indirection).to receive(:find).and_return(facts)
101104
expect(Facter).to receive(:load_external).once.with(false)
102105
expect(Facter).to receive(:load_external).once.with(true)
103106
lookup('a')

0 commit comments

Comments
 (0)