Skip to content

Commit

Permalink
Merge pull request #9424 from tvpartytonight/PUP-12046_send_facts_cat…
Browse files Browse the repository at this point in the history
…alog_download

(PUP-12046) Send facts for `catalog download`
  • Loading branch information
joshcooper authored Jul 29, 2024
2 parents d6151b2 + c47de03 commit 7080b6a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
32 changes: 24 additions & 8 deletions lib/puppet/face/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,27 @@

deactivate_action(:destroy)
deactivate_action(:search)
find = get_action(:find)
find.summary "Retrieve the catalog for the node from which the command is run."
find.arguments "<certname>"
find.returns <<-'EOT'
A serialized catalog. When used from the Ruby API, returns a
Puppet::Resource::Catalog object.
EOT
action(:find) do
summary _("Retrieve the catalog for the node from which the comamand is run.")
arguments "<certname>, <facts>"
option("--facts_for_catalog") do
summary _("Not yet implemented for the CLI; facts will be collected internally.")
end
returns <<-'EOT'
A serialized catalog. When used from the Ruby API, returns a
Puppet::Resource::Catalog object.
EOT

when_invoked do |*args|
# Default the key to Puppet[:certname] if none is supplied
if args.length == 1
key = Puppet[:certname]
else
key = args.shift
end
call_indirection_method :find, key, args.first
end
end

action(:apply) do
summary "Find and apply a catalog."
Expand Down Expand Up @@ -135,9 +149,11 @@
when_invoked do |_options|
Puppet::Resource::Catalog.indirection.terminus_class = :rest
Puppet::Resource::Catalog.indirection.cache_class = nil
facts = Puppet::Face[:facts, '0.0.1'].find(Puppet[:certname])
catalog = nil
retrieval_duration = thinmark do
catalog = Puppet::Face[:catalog, '0.0.1'].find(Puppet[:certname])
catalog = Puppet::Face[:catalog, '0.0.1'].find(Puppet[:certname],
{ facts_for_catalog: facts })
end
catalog.retrieval_duration = retrieval_duration
catalog.write_class_file
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/interface/action_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Puppet::Interface::ActionManager
# @dsl Faces
def action(name, &block)
@actions ||= {}
Puppet.warning _("Redefining action %{name} for %{self}") % { name: name, self: self } if action?(name)
Puppet.debug _("Redefining action %{name} for %{self}") % { name: name, self: self } if action?(name)

action = Puppet::Interface::ActionBuilder.build(self, name, &block)

Expand Down
39 changes: 39 additions & 0 deletions spec/unit/face/catalog_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper'
require 'puppet/face'
require 'puppet/indirector/facts/facter'
require 'puppet/indirector/facts/rest'

describe Puppet::Face[:catalog, '0.0.1'] do

describe '#download' do
let(:model) { Puppet::Node::Facts }
let(:test_data) { model.new('puppet.node.test', {test_fact: 'catalog_face_request_test_value'}) }
let(:catalog) { Puppet::Resource::Catalog.new('puppet.node.test', Puppet::Node::Environment.remote(Puppet[:environment].to_sym)) }

before(:each) do
Puppet[:facts_terminus] = :memory
Puppet::Node::Facts.indirection.save(test_data)
allow(Puppet::Face[:catalog, "0.0.1"]).to receive(:save).once

Puppet.settings.parse_config(<<-CONF)
[main]
server=puppet.server.test
certname=puppet.node.test
CONF

# Faces start in :user run mode
Puppet.settings.preferred_run_mode = :user
end

it "adds facts to the catalog request" do
stub_request(:post, 'https://puppet.server.test:8140/puppet/v3/catalog/puppet.node.test?environment=*root*')
.with(
headers: { 'Content-Type' => 'application/x-www-form-urlencoded' },
body: hash_including(facts: URI.encode_www_form_component(Puppet::Node::Facts.indirection.find('puppet.node.test').to_json))
).to_return(:status => 200, :body => catalog.render(:json), :headers => {'Content-Type' => 'application/json'})
subject.download
end
end
end


0 comments on commit 7080b6a

Please sign in to comment.