Skip to content

Commit 3cf0142

Browse files
Merge pull request #9300 from joshcooper/backport-9297-to-7.x
[Backport 7.x] (#9292) New command line flag for puppet resource, fail
2 parents c6e5c59 + f71d334 commit 3cf0142

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
@@ -13,6 +13,7 @@ def preinit
1313
option("--verbose","-v")
1414
option("--edit","-e")
1515
option("--to_yaml","-y")
16+
option('--fail', '-f')
1617

1718
option("--types", "-t") do |arg|
1819
env = Puppet.lookup(:environments).get(Puppet[:environment]) || create_default_environment
@@ -106,6 +107,9 @@ def help
106107
Output found resources in yaml format, suitable to use with Hiera and
107108
create_resources.
108109
110+
* --fail:
111+
Fails and returns an exit code of 1 if the resource could not be modified.
112+
109113
EXAMPLE
110114
-------
111115
This example uses `puppet resource` to return a Puppet configuration for
@@ -233,8 +237,11 @@ def find_or_save_resources(type, name, params)
233237
resource = Puppet::Resource.new( type, name, :parameters => params )
234238

235239
# save returns [resource that was saved, transaction log from applying the resource]
236-
save_result = Puppet::Resource.indirection.save(resource, key)
237-
[ save_result.first ]
240+
save_result, report = Puppet::Resource.indirection.save(resource, key)
241+
status = report.resource_statuses[resource.ref]
242+
raise "Failed to manage resource #{resource.ref}" if status&.failed? && options[:fail]
243+
244+
[save_result]
238245
end
239246
else
240247
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
@@ -144,6 +151,9 @@ def exists?
144151
def string
145152
Puppet::Util::Execution::ProcessOutput.new('test', 0)
146153
end
154+
155+
def string=(value)
156+
end
147157
end
148158

149159
@resource_app.options[:to_yaml] = true

0 commit comments

Comments
 (0)