From b2366a11103a53518704b54ab58c5b9b8e45a241 Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Tue, 31 Mar 2020 18:01:44 -0700 Subject: Add after_resource to pair with current_resource For custom resources which correctly implement load_current_value the new after_resource comes for "free" and load_current_value will be called twice to load the current_resource (should be renamed the "before_resource" but that ship sailed) and then the after_resource. Appropriate wiring is added for a new event and capturing that into the action_collection and then the data_collector. The resource_reporter has not and will not be updated. For old style resources which are difficult to convert to custom resources (thinking here particularly of the 1:N model of the service resource in core chef, or stuff that just may be a lot of work like the file resource) then they can override the load_after_resource to manually wire up their own after_resource. Signed-off-by: Lamont Granquist --- lib/chef/action_collection.rb | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'lib/chef/action_collection.rb') diff --git a/lib/chef/action_collection.rb b/lib/chef/action_collection.rb index 7b1997cfaf..eb0d3bef58 100644 --- a/lib/chef/action_collection.rb +++ b/lib/chef/action_collection.rb @@ -1,5 +1,5 @@ # -# Copyright:: Copyright 2018-2019, Chef Software Inc. +# Copyright:: Copyright 2018-2020, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,10 +24,6 @@ class Chef class ActionRecord - # XXX: this is buggy since we (ab)use this resource for "after" state and it may be - # inaccurate and it may be mutated by the user. A third after_resource should be added - # to new_resource + current_resource to properly implement this. - # # @return [Chef::Resource] The declared resource state. # attr_accessor :new_resource @@ -38,6 +34,10 @@ class Chef # implementation, but must be handled), or for unprocessed resources. attr_accessor :current_resource + # @return [Chef::Resource] the after_resource object (after-state). This can be nil for + # non custom-resources or resources that do not implement load_after_resource. + attr_accessor :after_resource + # @return [Symbol] # The action that was run (or scheduled to run in the case of "unprocessed" resources). attr_accessor :action @@ -161,7 +161,7 @@ class Chef pending_updates << ActionRecord.new(new_resource, action, pending_updates.length) end - # Hook called after a resource is loaded. If load_current_resource fails, this hook will + # Hook called after a current resource is loaded. If load_current_resource fails, this hook will # not be called and current_resource will be nil, and the resource_failed hook will be called. # # (see EventDispatch::Base#) @@ -172,6 +172,17 @@ class Chef current_record.current_resource = current_resource end + # Hook called after an after resource is loaded. If load_after_resource fails, this hook will + # not be called and after_resource will be nil, and the resource_failed hook will be called. + # + # (see EventDispatch::Base#) + # + def resource_after_state_loaded(new_resource, action, after_resource) + return if consumers.empty? + + current_record.after_resource = after_resource + end + # Hook called after an action is determined to be up to date. # # (see EventDispatch::Base#) -- cgit v1.2.1