summaryrefslogtreecommitdiff
path: root/lib/chef/resource_resolver.rb
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-06-08 11:05:12 -0700
committerJohn Keiser <john@johnkeiser.com>2015-06-08 11:05:12 -0700
commitd795db97bb8a79b179cd2f61355f04a856df3bd8 (patch)
tree58690703f387ce0d3d9c71f3e41f4c47f04e648c /lib/chef/resource_resolver.rb
parent4936ddb3b2391a6cab7adf9f3883ab97a3c57272 (diff)
downloadchef-d795db97bb8a79b179cd2f61355f04a856df3bd8.tar.gz
Make resource_for_short_name look up the canonical resourcejk/automatic-automatic-name
Diffstat (limited to 'lib/chef/resource_resolver.rb')
-rw-r--r--lib/chef/resource_resolver.rb105
1 files changed, 68 insertions, 37 deletions
diff --git a/lib/chef/resource_resolver.rb b/lib/chef/resource_resolver.rb
index 22fed7e587..31b39f7e24 100644
--- a/lib/chef/resource_resolver.rb
+++ b/lib/chef/resource_resolver.rb
@@ -21,64 +21,92 @@ require 'chef/platform/resource_priority_map'
class Chef
class ResourceResolver
+ #
+ # Resolve a resource by name.
+ #
+ # @param resource_name [Symbol] The resource DSL name (e.g. `:file`).
+ # @param node [Chef::Node] The node against which to resolve. `nil` causes
+ # platform filters to be ignored.
+ #
+ def self.resolve(resource_name, node: nil, canonical: nil)
+ new(node, resource_name, canonical: canonical).resolve
+ end
+
+ #
+ # Resolve a list of all resources that implement the given DSL (in order of
+ # preference).
+ #
+ # @param resource_name [Symbol] The resource DSL name (e.g. `:file`).
+ # @param node [Chef::Node] The node against which to resolve. `nil` causes
+ # platform filters to be ignored.
+ # @param canonical [Boolean] `true` or `false` to match canonical or
+ # non-canonical values only. `nil` to ignore canonicality.
+ #
+ def self.list(resource_name, node: nil, canonical: nil)
+ new(node, resource_name, canonical: canonical).list
+ end
+
+
include Chef::Mixin::ConvertToClassName
+ # @api private
attr_reader :node
- attr_reader :resource
+ # @api private
+ attr_reader :resource_name
+ # @api private
+ def resource
+ Chef::Log.deprecation("Chef::ResourceResolver.resource deprecated. Use resource_name instead.")
+ resource_name
+ end
+ # @api private
attr_reader :action
+ # @api private
attr_reader :canonical
- def initialize(node, resource)
+ #
+ # Create a resolver.
+ #
+ # @param node [Chef::Node] The node against which to resolve. `nil` causes
+ # platform filters to be ignored.
+ # @param resource_name [Symbol] The resource DSL name (e.g. `:file`).
+ # @param canonical [Boolean] `true` or `false` to match canonical or
+ # non-canonical values only. `nil` to ignore canonicality. Default: `nil`
+ #
+ # @api private use Chef::ResourceResolver.resolve or .list instead.
+ def initialize(node, resource_name, canonical: nil)
@node = node
- @resource = resource.to_sym
+ @resource_name = resource_name.to_sym
+ @canonical = canonical
end
+ # @api private use Chef::ResourceResolver.resolve instead.
def resolve
# 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}"
+ Chef::Log.debug "Resources for generic #{resource_name} resource enabled on node include: #{prioritized_handlers}"
handler = prioritized_handlers.first
if handler
- Chef::Log.debug "Resource for #{resource} is #{handler}"
+ Chef::Log.debug "Resource for #{resource_name} is #{handler}"
else
- Chef::Log.debug "Dynamic resource resolver FAILED to resolve a resource for #{resource}"
+ Chef::Log.debug "Dynamic resource resolver FAILED to resolve a resource for #{resource_name}"
end
handler
end
+ # @api private
def list
- Chef::Log.debug "Resources for generic #{resource} resource enabled on node include: #{enabled_handlers}"
- Chef::Log.debug "Resources for #{resource}: #{prioritized_handlers}"
+ Chef::Log.debug "Resources for generic #{resource_name} resource enabled on node include: #{prioritized_handlers}"
prioritized_handlers
end
- def provided_by?(resource_class)
- !prioritized_handlers.include?(resource_class)
- end
-
#
- # Resolve a resource by name.
+ # Whether this DSL is provided by the given resource_class.
#
- # @param resource_name [Symbol] The resource DSL name (e.g. `:file`).
- # @param node [Chef::Node] The node on which the resource will run. If not
- # passed, will return the first match.
- #
- def self.resolve(resource_name, node: nil)
- new(node, resource_name).resolve
- end
-
- #
- # Resolve a list of all resources that implement the given DSL (in order of
- # preference).
- #
- # @param resource_name [Symbol] The resource DSL name (e.g. `:file`).
- # @param node [Chef::Node] The node on which the resource will run. If not
- # passed, will return all resources (ignoring filters).
- #
- def self.list(resource_name, node: nil)
- new(node, resource_name).list
+ # @api private
+ def provided_by?(resource_class)
+ !prioritized_handlers.include?(resource_class)
end
protected
@@ -89,7 +117,7 @@ class Chef
def prioritized_handlers
@prioritized_handlers ||=
- priority_map.list_handlers(node, resource)
+ priority_map.list_handlers(node, resource_name, canonical: canonical)
end
module Deprecated
@@ -102,19 +130,22 @@ class Chef
# A list of all handlers
# @deprecated Now prioritized_handlers does its own work
def enabled_handlers
- resources.select { |klass| klass.provides?(node, resource) }
+ Chef::Log.deprecation("enabled_handlers is deprecated. If you are implementing a ResourceResolver, use provided_handlers. If you are not, use Chef::ResourceResolver.list(#{resource_name.inspect}, node: <node>)")
+ resources.select { |klass| klass.provides?(node, resource_name) }
end
protected
- # If there are no providers for a DSL, we search through the
+ # A list of all handlers for the given DSL. If there are no handlers in
+ # the map, we still check all descendants of Chef::Resource for backwards
+ # compatibility purposes.
def prioritized_handlers
@prioritized_handlers ||= super ||
resources.select do |klass|
- # Don't bother calling provides? unless it's overriden. We already
+ # Don't bother calling provides? unless it's overridden. We already
# know prioritized_handlers
- if klass.method(:provides?).owner != Chef::Resource && klass.provides?(node, resource)
- Chef::Log.deprecation("Resources #{provided.join(", ")} are marked as providing DSL #{resource}, but provides #{resource.inspect} was never called!")
+ if klass.method(:provides?).owner != Chef::Resource && klass.provides?(node, resource_name)
+ Chef::Log.deprecation("Resources #{provided.join(", ")} are marked as providing DSL #{resource_name}, but provides #{resource_name.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.")
true
end