Class: Chef::ProviderResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/provider_resolver.rb

Overview

Provider Resolution

Provider resolution is the process of taking a Resource object and an action, and determining the Provider class that should be instantiated to handle the action.

If the resource has its ‘provider` set, that is used.

Otherwise, we take the lists of Providers that have registered as providing the DSL through ‘provides :dsl_name, <filters>` or `Chef.set_resource_priority_array :dsl_name, <filters>`. We filter each list of Providers through:

  1. The filters it was registered with (such as ‘os: ’linux’‘ or `platform_family: ’debian’‘)

  2. ‘provides?(node, resource)`

  3. ‘supports?(resource, action)`

Anything that passes the filter and returns ‘true` to provides and supports, is considered a match. The first matching Provider in the *most recently registered list* is selected and returned.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, resource, action) ⇒ ProviderResolver

Returns a new instance of ProviderResolver.



53
54
55
56
57
# File 'lib/chef/provider_resolver.rb', line 53

def initialize(node, resource, action)
  @node = node
  @resource = resource
  @action = action
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



51
52
53
# File 'lib/chef/provider_resolver.rb', line 51

def action
  @action
end

#nodeObject (readonly)

Returns the value of attribute node.



49
50
51
# File 'lib/chef/provider_resolver.rb', line 49

def node
  @node
end

#resourceObject (readonly)

Returns the value of attribute resource.



50
51
52
# File 'lib/chef/provider_resolver.rb', line 50

def resource
  @resource
end

Instance Method Details

#enabled_handlersObject



78
79
80
# File 'lib/chef/provider_resolver.rb', line 78

def enabled_handlers
  @enabled_handlers ||= potential_handlers.select { |handler| !overrode_provides?(handler) || handler.provides?(node, resource) }
end

#provided_by?(provider_class) ⇒ Boolean

Does NOT call provides? on the resource (it is assumed this is being called from provides?).

Returns:

  • (Boolean)


74
75
76
# File 'lib/chef/provider_resolver.rb', line 74

def provided_by?(provider_class)
  potential_handlers.include?(provider_class)
end

#resolveObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/chef/provider_resolver.rb', line 59

def resolve
  resolved = maybe_explicit_provider(resource) ||
    maybe_custom_resource(resource) ||
    maybe_dynamic_provider_resolution(resource, action)

  if resolved.nil?
    raise(Chef::Exceptions::ProviderNotFound, "Cannot find a provider for #{resource}") if node.nil?

    raise(Chef::Exceptions::ProviderNotFound, "Cannot find a provider for #{resource} on #{node["platform"]} version #{node["platform_version"]}")
  end
  resolved
end

#supported_handlersObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO deprecate this and allow actions to be passed as a filter to ‘provides` so we don’t have to have two separate things.



85
86
87
# File 'lib/chef/provider_resolver.rb', line 85

def supported_handlers
  enabled_handlers.select { |handler| handler.supports?(resource, action) }
end