Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 7.x] (#9292) New command line flag for puppet resource, fail #9300

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/puppet/application/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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 @@ -106,6 +107,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 @@ -233,8 +237,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 @@ -144,6 +151,9 @@ def exists?
def string
Puppet::Util::Execution::ProcessOutput.new('test', 0)
end

def string=(value)
end
end

@resource_app.options[:to_yaml] = true
Expand Down