Skip to content

Commit cf6cb77

Browse files
committed
(#9292) New command line flag for puppet resource, fail
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.
1 parent 1a53bf7 commit cf6cb77

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/puppet/application/resource.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def preinit
1515
option("--verbose", "-v")
1616
option("--edit", "-e")
1717
option("--to_yaml", "-y")
18+
option('--fail', '-f')
1819

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

238242
# save returns [resource that was saved, transaction log from applying the resource]
239-
save_result = Puppet::Resource.indirection.save(resource, key)
240-
[save_result.first]
243+
save_result, report = Puppet::Resource.indirection.save(resource, key)
244+
status = report.resource_statuses[resource.ref]
245+
raise "Failed to manage resource #{resource.ref}" if status&.failed? && options[:fail]
246+
247+
[save_result]
241248
end
242249
else
243250
if type == "file"

spec/unit/application/resource_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,19 @@
118118
@resource_app.main
119119
end
120120

121+
before :each do
122+
allow(@res).to receive(:ref).and_return("type/name")
123+
end
124+
121125
it "should add given parameters to the object" do
122126
allow(@resource_app.command_line).to receive(:args).and_return(['type','name','param=temp'])
123127

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

131+
resource_status = instance_double('Puppet::Resource::Status')
132+
allow(@report).to receive(:resource_statuses).and_return({'type/name' => resource_status})
133+
allow(resource_status).to receive(:failed?).and_return(false)
127134
@resource_app.main
128135
end
129136
end
@@ -140,6 +147,9 @@ def exists?
140147
true
141148
end
142149

150+
def string=(value)
151+
end
152+
143153
def string
144154
Puppet::Util::Execution::ProcessOutput.new('test', 0)
145155
end

0 commit comments

Comments
 (0)