diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2016-10-12 10:12:55 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-12 10:12:55 -0700 |
commit | be176596352b85ef7736fdfe86421a55f9d3e4d5 (patch) | |
tree | 80a190de06664348f5ec3b38c13d49bbf0737e68 | |
parent | 5e7c3b96f125c6a07c6c48c0d27899100b3be9a6 (diff) | |
parent | f5ec0e2c99716e2353a2fbd4b215d154bae24143 (diff) | |
download | chef-be176596352b85ef7736fdfe86421a55f9d3e4d5.tar.gz |
Merge pull request #5443 from chef/lcg/delayed_action
Core: add delayed_action to the Chef::Resource API
-rw-r--r-- | lib/chef/resource.rb | 25 | ||||
-rw-r--r-- | spec/integration/recipes/accumulator_spec.rb | 8 |
2 files changed, 27 insertions, 6 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index d11fa1c80c..8048330ce1 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -181,6 +181,31 @@ class Chef alias_method :action=, :action # + # Force a delayed notification into this resource's run_context. + # + # This should most likely be paired with action :nothing + # + # @param arg [Array[Symbol], Symbol] A list of actions (e.g. `:create`) + # @return [Array[Symbol]] the list of actions. + # + def delayed_action(arg = nil) + if arg + arg = Array(arg).map(&:to_sym) + arg.each do |action| + validate( + { action: action }, + { action: { kind_of: Symbol, equal_to: allowed_actions } } + ) + # the resource effectively sends a delayed notification to itself + run_context.add_delayed_action(Notification.new(self, action, self)) + end + @delayed_action = arg + else + @delayed_action + end + end + + # # Sets up a notification that will run a particular action on another resource # if and when *this* resource is updated by an action. # diff --git a/spec/integration/recipes/accumulator_spec.rb b/spec/integration/recipes/accumulator_spec.rb index b2107b2c02..e6afe09b8c 100644 --- a/spec/integration/recipes/accumulator_spec.rb +++ b/spec/integration/recipes/accumulator_spec.rb @@ -46,11 +46,9 @@ describe "Accumulators" do variables[:aliases][new_resource.address] ||= [] variables[:aliases][new_resource.address] += new_resource.recipients action :nothing + delayed_action :create end end - log "force delayed notification" do - notifies :create, "template[#{aliases_temppath}]", :delayed - end end EOM @@ -148,13 +146,11 @@ describe "Accumulators" do source "aliases.erb" variables[:aliases] = {} action :nothing + delayed_action :create end end r.variables[:aliases][address] ||= [] r.variables[:aliases][address] += new_resource.recipients - log "force delayed notification" do - notifies :create, "template[#{aliases_temppath}]", :delayed - end end EOM |