diff options
author | John Keiser <john@johnkeiser.com> | 2015-05-01 22:58:03 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-05-07 17:02:13 -0700 |
commit | d88ff5a988831d880225db151c3557dd07e3fa92 (patch) | |
tree | ddd554f01a12e1ab02b16c9dbfcc29813afef803 | |
parent | ba1dfd97f0c5c5dfcd687cf3801378a3f6abaf8c (diff) | |
download | chef-d88ff5a988831d880225db151c3557dd07e3fa92.tar.gz |
Move deprecation back into resource_for_short_name
-rw-r--r-- | lib/chef/resource.rb | 20 | ||||
-rw-r--r-- | lib/chef/resource_resolver.rb | 64 | ||||
-rw-r--r-- | spec/support/lib/chef/resource/cat.rb | 1 | ||||
-rw-r--r-- | spec/support/lib/chef/resource/one_two_three_four.rb | 2 | ||||
-rw-r--r-- | spec/support/lib/chef/resource/zen_follower.rb | 1 | ||||
-rw-r--r-- | spec/support/lib/chef/resource/zen_master.rb | 2 |
6 files changed, 42 insertions, 48 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index ee940c98d4..7e0dd6395a 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -974,8 +974,9 @@ class Chef end def self.provides(name, *args, &block) - super + result = super Chef::DSL::Resources.add_resource_dsl(name) + result end # Helper for #notifies @@ -1108,6 +1109,7 @@ class Chef # === Returns # <Chef::Resource>:: returns the proper Chef::Resource class def self.resource_for_node(short_name, node) + require 'chef/resource_resolver' klass = Chef::ResourceResolver.new(node, short_name).resolve raise Chef::Exceptions::NoSuchResourceType.new(short_name, node) if klass.nil? klass @@ -1123,8 +1125,20 @@ class Chef # # === Returns # <Chef::Resource>:: returns the proper Chef::Resource class + # + # @deprecated Chef::Resource::FooBar will no longer mean anything special in + # Chef 13. Use `resource_for_node` instead. def self.resource_matching_short_name(short_name) - Chef::ResourceResolver.new(Chef::Node.new, short_name).resolve + require 'chef/resource_resolver' + begin + rname = convert_to_class_name(short_name.to_s) + result = Chef::Resource.const_get(rname) + Chef::Log.deprecation("Class Chef::Resource::#{rname} does not declare `provides #{short_name.inspect}`.") + Chef::Log.deprecation("This will no longer work in Chef 13: you must use `provides` to provide DSL.") + result + rescue NameError + nil + end end private @@ -1142,5 +1156,3 @@ class Chef end end end - -require 'chef/resource_resolver' diff --git a/lib/chef/resource_resolver.rb b/lib/chef/resource_resolver.rb index c9bf2e189a..4560bac347 100644 --- a/lib/chef/resource_resolver.rb +++ b/lib/chef/resource_resolver.rb @@ -39,7 +39,22 @@ class Chef end def resolve - # log this so we know what resources will work for the generic resource on the node (early cut) + maybe_dynamic_resource_resolution || + maybe_chef_platform_lookup + end + + # this cut looks at if the resource can handle the resource type on the node + def enabled_handlers + @enabled_handlers ||= + resources.select do |klass| + klass.provides?(node, resource) + end.sort {|a,b| a.to_s <=> b.to_s } + end + + private + + # try dynamically finding a resource based on querying the resources to see what they support + def maybe_dynamic_resource_resolution # log this so we know what resources will work for the generic resource on the node (early cut) Chef::Log.debug "resources for generic #{resource} resource enabled on node include: #{enabled_handlers}" # if none of the resources specifically support the resource, we still need to pick one of the resources that are @@ -62,55 +77,16 @@ class Chef raise Chef::Exceptions::AmbiguousResourceResolution.new(resource, handlers) if handlers.count >= 2 - Chef::Log.debug "dynamic resource resolver FAILED to resolve a resouce" if handlers.empty? + Chef::Log.debug "dynamic resource resolver FAILED to resolve a resource" if handlers.empty? return nil if handlers.empty? handlers[0] end - # this cut looks at if the resource can handle the resource type on the node - def enabled_handlers - @enabled_handlers ||= begin - enabled_handlers = resources.select do |klass| - klass.provides?(node, resource) - end.sort {|a,b| a.to_s <=> b.to_s } - - # Add Chef::Resource::X as a possibility if it is not a handler already - check_for_deprecated_chef_resource_class(enabled_handlers) - - enabled_handlers - end - end - - private - - # - # Checks if the Chef::Resource::* class corresponding to the DSL name - # exists, emits a deprecation warning, marks it as providing the given - # short name and adds it to the DSL. This is used for method_missing - # deprecation and ResourceResolver checking, when people have created - # anonymous classes and assigned them to Chef::Resource::X. - # - # Returns the matched class, if it exists. - # - # @api private - def check_for_deprecated_chef_resource_class(enabled_handlers) - # If Chef::Resource::MyResource exists, but was not set, it won't have a - # DSL name. Add the DSL method and warn about this pattern. - class_name = convert_to_class_name(resource.to_s) - if Chef::Resource.const_defined?(class_name) - # If Chef::Resource::X already exists, and is *not* already marked as - # providing this resource, mark it as providing the resource and add it - # to the list of handlers for next time. - resource_class = Chef::Resource.const_get(class_name) - if resource_class <= Chef::Resource && !enabled_handlers.include?(resource_class) - enabled_handlers << resource_class - Chef::Log.warn("Class Chef::Resource::#{class_name} does not declare `provides #{resource.inspect}`.") - Chef::Log.warn("This will no longer work in Chef 13: you must use `provides` to provide DSL.") - resource_class.provides resource.to_sym - end - end + # try the old static lookup of resources by mangling name to resource klass + def maybe_chef_platform_lookup + Chef::Resource.resource_matching_short_name(resource) end # dep injection hooks diff --git a/spec/support/lib/chef/resource/cat.rb b/spec/support/lib/chef/resource/cat.rb index ecca50cb53..641ce28795 100644 --- a/spec/support/lib/chef/resource/cat.rb +++ b/spec/support/lib/chef/resource/cat.rb @@ -19,6 +19,7 @@ class Chef class Resource class Cat < Chef::Resource + provides :cat attr_accessor :action diff --git a/spec/support/lib/chef/resource/one_two_three_four.rb b/spec/support/lib/chef/resource/one_two_three_four.rb index 296d2cd970..d7e5ea095e 100644 --- a/spec/support/lib/chef/resource/one_two_three_four.rb +++ b/spec/support/lib/chef/resource/one_two_three_four.rb @@ -19,6 +19,8 @@ class Chef class Resource class OneTwoThreeFour < Chef::Resource + provides :one_two_three_four + attr_reader :i_can_count def initialize(name, run_context) diff --git a/spec/support/lib/chef/resource/zen_follower.rb b/spec/support/lib/chef/resource/zen_follower.rb index ddc289e48d..590aa0827b 100644 --- a/spec/support/lib/chef/resource/zen_follower.rb +++ b/spec/support/lib/chef/resource/zen_follower.rb @@ -21,6 +21,7 @@ require 'chef/json_compat' class Chef class Resource class ZenFollower < Chef::Resource + provides :zen_follower provides :follower, platform: "zen" diff --git a/spec/support/lib/chef/resource/zen_master.rb b/spec/support/lib/chef/resource/zen_master.rb index d47d174e28..145dd70e64 100644 --- a/spec/support/lib/chef/resource/zen_master.rb +++ b/spec/support/lib/chef/resource/zen_master.rb @@ -22,6 +22,8 @@ require 'chef/json_compat' class Chef class Resource class ZenMaster < Chef::Resource + provides :zen_master + attr_reader :peace def initialize(name, run_context=nil) |