diff options
author | Xabier de Zuazo <xabier@zuazo.org> | 2013-01-18 13:11:37 +0100 |
---|---|---|
committer | Bryan McLellan <btm@opscode.com> | 2013-06-19 14:18:43 -0700 |
commit | 2c25de2828f8a959acd5542730ad15a0136fd3ee (patch) | |
tree | e949192f41ab0482102374a95f8722eacc03f68a /lib | |
parent | 91f9b6aef4d6ff0454ba92fe163aab4921eb83b5 (diff) | |
download | chef-2c25de2828f8a959acd5542730ad15a0136fd3ee.tar.gz |
[CHEF-972] created a ConditionalAction class to be used in resource#should_skip? method
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/formatters/doc.rb | 2 | ||||
-rw-r--r-- | lib/chef/resource.rb | 13 | ||||
-rw-r--r-- | lib/chef/resource/conditional.rb | 4 | ||||
-rw-r--r-- | lib/chef/resource/conditional_action.rb | 83 |
4 files changed, 93 insertions, 9 deletions
diff --git a/lib/chef/formatters/doc.rb b/lib/chef/formatters/doc.rb index 6a969404c1..300311819f 100644 --- a/lib/chef/formatters/doc.rb +++ b/lib/chef/formatters/doc.rb @@ -162,7 +162,7 @@ class Chef # Called when a resource action has been skipped b/c of a conditional def resource_skipped(resource, action, conditional) # TODO: more info about conditional - puts " (skipped due to #{conditional.positivity})" + puts " (skipped due to #{conditional.short_description})" end # Called after #load_current_resource has run. diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 264840a357..2b62ebcedd 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -23,6 +23,7 @@ require 'chef/dsl/data_query' require 'chef/dsl/registry_helper' require 'chef/mixin/convert_to_class_name' require 'chef/resource/conditional' +require 'chef/resource/conditional_action' require 'chef/resource_collection' require 'chef/resource_platform_map' require 'chef/node' @@ -672,16 +673,12 @@ F # "fails" its check. Subsequent conditionals are not evaluated, so in # general it's not a good idea to rely on side effects from not_if or # only_if commands/blocks being evaluated. + # + # Also skips conditional checking when the action is :nothing def should_skip?(action) - if (action == :nothing) - events.resource_skipped(self, action, Conditional.not_if('action == :nothing')) - Chef::Log.debug("Skipping #{self} due to :nothing action") - return true - end - - conditionals = only_if + not_if - return false if conditionals.empty? + conditional_action = ConditionalAction.not_if(action, :nothing) + conditionals = [ conditional_action ] + only_if + not_if conditionals.find do |conditional| if conditional.continue? false diff --git a/lib/chef/resource/conditional.rb b/lib/chef/resource/conditional.rb index 3f892af827..60f65e14e2 100644 --- a/lib/chef/resource/conditional.rb +++ b/lib/chef/resource/conditional.rb @@ -83,6 +83,10 @@ class Chef @block.call end + def short_description + @positivity + end + def description cmd_or_block = @command ? "command `#{@command}`" : "ruby block" "#{@positivity} #{cmd_or_block}" diff --git a/lib/chef/resource/conditional_action.rb b/lib/chef/resource/conditional_action.rb new file mode 100644 index 0000000000..9cf385a6f7 --- /dev/null +++ b/lib/chef/resource/conditional_action.rb @@ -0,0 +1,83 @@ +# +# Author:: Xabier de Zuazo (<xabier@onddo.com>) +# Copyright:: Copyright (c) 2013 Onddo Labs, SL. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +class Chef + class Resource + class ConditionalAction + + # We only create these via the `not_if` or `only_if` constructors, and + # not the default constructor + class << self + private :new + end + + def self.not_if(current_action, required_action) + new(:not_if, current_action, required_action) + end + + def self.only_if(current_action, required_action) + new(:only_if, current_action, required_action) + end + + attr_reader :positivity + attr_reader :required_action + attr_reader :current_action + + def initialize(positivity, current_action, required_action) + @positivity = positivity + @current_action = current_action + @required_action = required_action + end + + def continue? + case @positivity + when :only_if + evaluate + when :not_if + !evaluate + else + raise "Cannot evaluate resource conditional of type #{@positivity}" + end + end + + def evaluate + @required_action == @current_action + end + + def short_description + description + end + + def description + case @positivity + when :only_if + "action not being #{@required_action.inspect}" + when :not_if + "action #{@required_action.inspect}" + else + raise "Cannot describe resource conditional of type #{@positivity}" + end + end + + def to_text + "#{@positivity} { action == #{@required_action.inspect} }" + end + + end + end +end |