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 /lib | |
parent | 6aa94e70cde907e9c890307a82fb50af3d46f9df (diff) | |
download | chef-05c59d70345598da5bb0e7ccc2f4ebaf85adcb73.tar.gz |
Add current_value_does_not_exist! API, pretty up the output
Diffstat (limited to 'lib')
-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 |
5 files changed, 31 insertions, 10 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 |