diff options
author | John Keiser <john@johnkeiser.com> | 2015-05-06 14:07:43 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-06-02 09:53:39 -0700 |
commit | fc710c26a95e74aa66bf0bee8923ee104593c97a (patch) | |
tree | b41e2ed1f80fa139d8b9c171b8e11eb4eed82c5f /lib/chef/resource_resolver.rb | |
parent | 9028823f7b046c4c081b1cb1df005d61fbfa1db2 (diff) | |
download | chef-fc710c26a95e74aa66bf0bee8923ee104593c97a.tar.gz |
Narrow resolvers to only look at parts of the map we support
Diffstat (limited to 'lib/chef/resource_resolver.rb')
-rw-r--r-- | lib/chef/resource_resolver.rb | 84 |
1 files changed, 47 insertions, 37 deletions
diff --git a/lib/chef/resource_resolver.rb b/lib/chef/resource_resolver.rb index 47d98154a8..da34f832eb 100644 --- a/lib/chef/resource_resolver.rb +++ b/lib/chef/resource_resolver.rb @@ -33,22 +33,13 @@ class Chef @resource = resource.to_sym end - # return a deterministically sorted list of Chef::Resource subclasses - def resources - @resources ||= Chef::Resource.descendants - end - def resolve 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 } + def provided_by?(resource_class) + !prioritized_handlers.include?(resource_class) end # @@ -61,32 +52,22 @@ class Chef new(node, resource_name).resolve end - private + protected # 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 - # enabled on the node to handle the why-run use case. - handlers = enabled_handlers - - if handlers.size >= 2 - # this magic stack ranks the resources by where they appear in the resource_priority_map - priority_list = [ get_priority_array(node, resource) ].flatten.compact - handlers = handlers.sort_by { |x| i = priority_list.index x; i.nil? ? Float::INFINITY : i } - handlers = handlers[0..0] - end - - Chef::Log.debug "resources that survived replacement include: #{handlers}" + 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}" - raise Chef::Exceptions::AmbiguousResourceResolution.new(resource, handlers) if handlers.count >= 2 + handler = prioritized_handlers.first - Chef::Log.debug "dynamic resource resolver FAILED to resolve a resource" if handlers.empty? - - return nil if handlers.empty? + if handler + Chef::Log.debug "Resource for #{resource} is #{handler}" + else + Chef::Log.debug "Dynamic resource resolver FAILED to resolve a resource for #{resource}" + end - handlers[0] + handler end # try the old static lookup of resources by mangling name to resource klass @@ -94,13 +75,42 @@ class Chef Chef::Resource.resource_matching_short_name(resource) end - # dep injection hooks - def get_priority_array(node, resource_name) - resource_priority_map.get_priority_array(node, resource_name) + def priority_map + Chef::Platform::ResourcePriorityMap.instance end - def resource_priority_map - Chef::Platform::ResourcePriorityMap.instance + def prioritized_handlers + @prioritized_handlers ||= + priority_map.list_handlers(node, resource) + end + + module Deprecated + # return a deterministically sorted list of Chef::Resource subclasses + def resources + @resources ||= Chef::Resource.descendants + 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 + + protected + + # If there are no providers for a DSL, we search through the + def prioritized_handlers + @prioritized_handlers ||= super || begin + if !enabled_handlers.empty? + Chef::Log.deprecation("#{resource} is marked as providing DSL #{resource}, but provides #{resource.inspect} was never called!") + Chef::Log.deprecation("In Chef 13, this will break: you must call provides to mark the names you provide, even if you also override provides? yourself.") + end + enabled_handlers + end + end end + prepend Deprecated end end |