Skip to content

Commit

Permalink
(#9292) New command line flag for puppet resource, fail
Browse files Browse the repository at this point in the history
See discussion in issue #9292.
What this PR adds is the ability for on the command line if --fail is passed then
the `puppet resource` command will exit with a non zero return code.
  • Loading branch information
cthorn42 committed Mar 15, 2024
1 parent 1a53bf7 commit cf6cb77
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/puppet/application/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def preinit
option("--verbose", "-v")
option("--edit", "-e")
option("--to_yaml", "-y")
option('--fail', '-f')

option("--types", "-t") do |_arg|
env = Puppet.lookup(:environments).get(Puppet[:environment]) || create_default_environment
Expand Down Expand Up @@ -109,6 +110,9 @@ def help
Output found resources in yaml format, suitable to use with Hiera and
create_resources.
* --fail:
Fails and returns an exit code of 1 if the resource could not be modified.
EXAMPLE
-------
This example uses `puppet resource` to return a Puppet configuration for
Expand Down Expand Up @@ -236,8 +240,11 @@ def find_or_save_resources(type, name, params)
resource = Puppet::Resource.new(type, name, :parameters => params)

# save returns [resource that was saved, transaction log from applying the resource]
save_result = Puppet::Resource.indirection.save(resource, key)
[save_result.first]
save_result, report = Puppet::Resource.indirection.save(resource, key)
status = report.resource_statuses[resource.ref]
raise "Failed to manage resource #{resource.ref}" if status&.failed? && options[:fail]

[save_result]
end
else
if type == "file"
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/application/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,19 @@
@resource_app.main
end

before :each do
allow(@res).to receive(:ref).and_return("type/name")
end

it "should add given parameters to the object" do
allow(@resource_app.command_line).to receive(:args).and_return(['type','name','param=temp'])

expect(Puppet::Resource.indirection).to receive(:save).with(@res, 'type/name').and_return([@res, @report])
expect(Puppet::Resource).to receive(:new).with('type', 'name', {:parameters => {'param' => 'temp'}}).and_return(@res)

resource_status = instance_double('Puppet::Resource::Status')
allow(@report).to receive(:resource_statuses).and_return({'type/name' => resource_status})
allow(resource_status).to receive(:failed?).and_return(false)
@resource_app.main
end
end
Expand All @@ -140,6 +147,9 @@ def exists?
true
end

def string=(value)
end

def string
Puppet::Util::Execution::ProcessOutput.new('test', 0)
end
Expand Down

0 comments on commit cf6cb77

Please sign in to comment.