diff options
author | John Keiser <john@johnkeiser.com> | 2015-07-29 15:31:36 -0600 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-07-31 12:42:28 -0600 |
commit | 05c59d70345598da5bb0e7ccc2f4ebaf85adcb73 (patch) | |
tree | b9054251567a2ecff2b810b1d9cac55f486d8d69 | |
parent | 6aa94e70cde907e9c890307a82fb50af3d46f9df (diff) | |
download | chef-05c59d70345598da5bb0e7ccc2f4ebaf85adcb73.tar.gz |
Add current_value_does_not_exist! API, pretty up the output
-rw-r--r-- | lib/chef/dsl/recipe.rb | 6 | ||||
-rw-r--r-- | lib/chef/exceptions.rb | 3 | ||||
-rw-r--r-- | lib/chef/provider.rb | 5 | ||||
-rw-r--r-- | lib/chef/resource.rb | 8 | ||||
-rw-r--r-- | lib/chef/resource/action_provider.rb | 19 | ||||
-rw-r--r-- | spec/integration/recipes/resource_converge_if_changed_spec.rb | 124 |
6 files changed, 94 insertions, 71 deletions
diff --git a/lib/chef/dsl/recipe.rb b/lib/chef/dsl/recipe.rb index d00d0df247..29cfcd478c 100644 --- a/lib/chef/dsl/recipe.rb +++ b/lib/chef/dsl/recipe.rb @@ -19,12 +19,10 @@ require 'chef/mixin/convert_to_class_name' require 'chef/exceptions' -require 'chef/resource_builder' require 'chef/mixin/shell_out' require 'chef/mixin/powershell_out' require 'chef/dsl/resources' require 'chef/dsl/definitions' -require 'chef/resource' class Chef module DSL @@ -198,6 +196,10 @@ class Chef end end +# Avoid circular references for things that are only used in instance methods +require 'chef/resource_builder' +require 'chef/resource' + # **DEPRECATED** # This used to be part of chef/mixin/recipe_definition_dsl_core. Load the file to activate the deprecation code. require 'chef/mixin/recipe_definition_dsl_core' diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb index e2e36e8162..16f985acaa 100644 --- a/lib/chef/exceptions.rb +++ b/lib/chef/exceptions.rb @@ -105,6 +105,9 @@ class Chef class VerificationNotFound < RuntimeError; end class InvalidEventType < ArgumentError; end class MultipleIdentityError < RuntimeError; end + # Used in Resource::ActionProvider#load_current_resource to denote that + # the resource doesn't actually exist (for example, the file does not exist) + class CurrentValueDoesNotExist < RuntimeError; end # Can't find a Resource of this type that is valid on this platform. class NoSuchResourceType < NameError diff --git a/lib/chef/provider.rb b/lib/chef/provider.rb index 8c51acb71f..3ea0c13f97 100644 --- a/lib/chef/provider.rb +++ b/lib/chef/provider.rb @@ -207,11 +207,12 @@ class Chef # Print the pretty green text and run the block property_size = modified.map { |p| p.size }.max modified = modified.map { |p| " set #{p.to_s.ljust(property_size)} to #{new_resource.send(p).inspect} (was #{current_resource.send(p).inspect})" } - converge_by([ "update #{new_resource.to_s}" ] + modified, &converge_block) + converge_by([ "update #{current_resource.identity}" ] + modified, &converge_block) else # The resource doesn't exist. Mark that we are *creating* this, and # write down any properties we are setting. + property_size = properties.map { |p| p.size }.max created = [] properties.each do |property| if new_resource.property_is_set?(property) @@ -221,7 +222,7 @@ class Chef end end - converge_by([ "create #{new_resource.to_s}" ] + created, &converge_block) + converge_by([ "create #{new_resource.identity}" ] + created, &converge_block) end true end diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index b6355dab55..39fd05305f 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -18,6 +18,7 @@ # limitations under the License. # +require 'chef/exceptions' require 'chef/mixin/params_validate' require 'chef/dsl/platform_introspection' require 'chef/dsl/data_query' @@ -1384,9 +1385,16 @@ class Chef # created resource with its identity values filled in. # def self.load_current_value(&load_block) + include LoadCurrentValueDSL define_method(:load_current_value!, &load_block) end + module LoadCurrentValueDSL + def current_value_does_not_exist! + raise Chef::Exceptions::CurrentValueDoesNotExist + end + end + # # Get the current actual value of this resource. # diff --git a/lib/chef/resource/action_provider.rb b/lib/chef/resource/action_provider.rb index c756a81b6f..abbab79311 100644 --- a/lib/chef/resource/action_provider.rb +++ b/lib/chef/resource/action_provider.rb @@ -16,6 +16,8 @@ # limitations under the License. # +require 'chef/exceptions' + class Chef class Resource module ActionProvider @@ -36,14 +38,19 @@ class Chef end end - if current_resource.method(:load_current_value!).arity > 0 - current_resource.load_current_value!(new_resource) - else - current_resource.load_current_value! + # Call the actual load_current_value! method. If it raises + # CurrentValueDoesNotExist, set current_resource to `nil`. + begin + if current_resource.method(:load_current_value!).arity > 0 + current_resource.load_current_value!(new_resource) + else + current_resource.load_current_value! + end + rescue Chef::Exceptions::CurrentValueDoesNotExist + current_resource = nil end - elsif superclass.public_instance_method?(:load_current_resource) - super end + @current_resource = current_resource end diff --git a/spec/integration/recipes/resource_converge_if_changed_spec.rb b/spec/integration/recipes/resource_converge_if_changed_spec.rb index ef768a693e..1c77ad8152 100644 --- a/spec/integration/recipes/resource_converge_if_changed_spec.rb +++ b/spec/integration/recipes/resource_converge_if_changed_spec.rb @@ -36,7 +36,7 @@ describe "Resource::ActionProvider#converge_if_changed" do property :identity1, identity: true, default: 'default_identity1' property :control1, desired_state: false, default: 'default_control1' property :state1, default: 'default_state1' - property :state2, default: 'default_identity1' + property :state2, default: 'default_state2' attr_accessor :converged def initialize(*args) super @@ -101,7 +101,7 @@ Recipe: basic_chef_client::block expect(converge_recipe.stdout).to eq <<-EOM Recipe: basic_chef_client::block * #{resource_name}[blah] action create - - update #{resource_name}[blah] + - update default_identity1 - set state1 to "new_state1" (was "current_state1") EOM end @@ -124,7 +124,7 @@ Recipe: basic_chef_client::block expect(converge_recipe.stdout).to eq <<-EOM Recipe: basic_chef_client::block * #{resource_name}[blah] action create - - update #{resource_name}[blah] + - update default_identity1 - set state1 to "new_state1" (was "current_state1") - set state2 to "new_state2" (was "current_state2") EOM @@ -148,7 +148,7 @@ EOM expect(converge_recipe.stdout).to eq <<-EOM Recipe: basic_chef_client::block * #{resource_name}[blah] action create - - update #{resource_name}[blah] + - update default_identity1 - set state2 to "new_state2" (was "current_state2") EOM end @@ -226,65 +226,67 @@ EOM expect(converge_recipe.stdout).to eq <<-EOM Recipe: basic_chef_client::block * #{resource_name}[blah] action create - - update #{resource_name}[blah] + - update current_identity1 - set identity1 to "new_identity1" (was "current_identity1") EOM end end end -# context "and has no current_resource" do -# before :each do -# resource_class.load_current_value do -# value_does_not_exist! -# end -# end -# -# context "and nothing is set" do -# let(:converge_recipe) { -# resource_name = self.resource_name -# converge { -# public_send(resource_name, 'blah') -# } -# } -# -# it "the resource is created" do -# expect(resource.converged).to eq 1 -# expect(resource.updated?).to be_truthy -# expect(converge_recipe.stdout).to eq <<-EOM -# Recipe: basic_chef_client::block -# * #{resource_name}[blah] action create -# - create #{resource_name}[blah] -# - default state1 to "default_state1" -# - default state2 to "default_state2" -# EOM -# end -# end -# -# context "and state1 and state2 are set" do -# let(:converge_recipe) { -# resource_name = self.resource_name -# converge { -# public_send(resource_name, 'blah') do -# state1 'new_state1' -# state2 'new_state2' -# end -# } -# } -# -# it "the resource is created" do -# expect(resource.converged).to eq 1 -# expect(resource.updated?).to be_truthy -# expect(converge_recipe.stdout).to eq <<-EOM -# Recipe: basic_chef_client::block -# * #{resource_name}[blah] action create -# - create #{resource_name}[blah] -# - set state1 to "new_state1" -# - set state2 to "new_state2" -# EOM -# end -# end -# end + context "and has no current_resource" do + before :each do + resource_class.load_current_value do + current_value_does_not_exist! + end + end + + context "and nothing is set" do + let(:converge_recipe) { + resource_name = self.resource_name + converge { + public_send(resource_name, 'blah') + } + } + + it "the resource is created" do + expect(resource.converged).to eq 1 + expect(resource.updated?).to be_truthy + expect(converge_recipe.stdout).to eq <<-EOM +Recipe: basic_chef_client::block + * #{resource_name}[blah] action create + - create default_identity1 + - default identity1 to "default_identity1" + - default state1 to "default_state1" + - default state2 to "default_state2" +EOM + end + end + + context "and state1 and state2 are set" do + let(:converge_recipe) { + resource_name = self.resource_name + converge { + public_send(resource_name, 'blah') do + state1 'new_state1' + state2 'new_state2' + end + } + } + + it "the resource is created" do + expect(resource.converged).to eq 1 + expect(resource.updated?).to be_truthy + expect(converge_recipe.stdout).to eq <<-EOM +Recipe: basic_chef_client::block + * #{resource_name}[blah] action create + - create default_identity1 + - default identity1 to "default_identity1" + - set state1 to "new_state1" + - set state2 to "new_state2" +EOM + end + end + end end context "and separate converge_if_changed :state1 and converge_if_changed :state2" do @@ -341,7 +343,7 @@ EOM expect(converge_recipe.stdout).to eq <<-EOM Recipe: basic_chef_client::block * #{resource_name}[blah] action create - - update #{resource_name}[blah] + - update default_identity1 - set state1 to "new_state1" (was "current_state1") EOM end @@ -364,9 +366,9 @@ EOM expect(converge_recipe.stdout).to eq <<-EOM Recipe: basic_chef_client::block * #{resource_name}[blah] action create - - update #{resource_name}[blah] + - update default_identity1 - set state1 to "new_state1" (was "current_state1") - - update #{resource_name}[blah] + - update default_identity1 - set state2 to "new_state2" (was "current_state2") EOM end @@ -389,7 +391,7 @@ EOM expect(converge_recipe.stdout).to eq <<-EOM Recipe: basic_chef_client::block * #{resource_name}[blah] action create - - update #{resource_name}[blah] + - update default_identity1 - set state2 to "new_state2" (was "current_state2") EOM end |