summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortyler-ball <tyleraball@gmail.com>2014-10-16 10:07:40 -0500
committertyler-ball <tyleraball@gmail.com>2014-10-16 10:33:02 -0500
commit72dd653d9481848a7489845a46851c66964c5a37 (patch)
tree82b3be7d0c343182ac9fc294459495ff2f70b911
parent322eaf62464f306e2e3b3fec5b9fed15d17fc50b (diff)
downloadchef-72dd653d9481848a7489845a46851c66964c5a37.tar.gz
Refactoring ResourceCollection interface to be backwards compatiable, all specs continue to pass, removing ResourceSet and ResourceList from consumer knowledge
-rw-r--r--lib/chef/dsl/recipe.rb3
-rw-r--r--lib/chef/formatters/doc.rb2
-rw-r--r--lib/chef/formatters/minimal.rb2
-rw-r--r--lib/chef/json_compat.rb6
-rw-r--r--lib/chef/provider/breakpoint.rb4
-rw-r--r--lib/chef/provider/deploy.rb3
-rw-r--r--lib/chef/provider/lwrp_base.rb2
-rw-r--r--lib/chef/provider/route.rb2
-rw-r--r--lib/chef/recipe.rb2
-rw-r--r--lib/chef/resource.rb28
-rw-r--r--lib/chef/resource_collection.rb63
-rw-r--r--lib/chef/resource_collection/resource_collection_serialization.rb19
-rw-r--r--lib/chef/resource_list.rb116
-rw-r--r--lib/chef/resource_set.rb236
-rw-r--r--lib/chef/run_context.rb8
-rw-r--r--lib/chef/run_status.rb4
-rw-r--r--lib/chef/runner.rb4
-rw-r--r--lib/chef/shell/ext.rb4
-rw-r--r--lib/chef/shell/shell_session.rb4
-rw-r--r--spec/unit/resource_collection_spec.rb4
-rw-r--r--spec/unit/resource_spec.rb4
-rw-r--r--spec/unit/shell/shell_ext_spec.rb2
22 files changed, 286 insertions, 236 deletions
diff --git a/lib/chef/dsl/recipe.rb b/lib/chef/dsl/recipe.rb
index 8578f41d92..b110371036 100644
--- a/lib/chef/dsl/recipe.rb
+++ b/lib/chef/dsl/recipe.rb
@@ -86,8 +86,7 @@ class Chef
resource = build_resource(type, name, created_at, &resource_attrs_block)
- run_context.resource_set.insert_as(type, name, resource)
- run_context.resource_list << resource
+ run_context.resource_collection.insert(resource, type, name)
resource
end
diff --git a/lib/chef/formatters/doc.rb b/lib/chef/formatters/doc.rb
index 2e102a82c6..4a08b9d095 100644
--- a/lib/chef/formatters/doc.rb
+++ b/lib/chef/formatters/doc.rb
@@ -143,7 +143,7 @@ class Chef
# Called before convergence starts
def converge_start(run_context)
- puts_line "Converging #{run_context.resource_list.all_resources.size} resources"
+ puts_line "Converging #{run_context.resource_collection.all_resources.size} resources"
end
# Called when the converge phase is finished.
diff --git a/lib/chef/formatters/minimal.rb b/lib/chef/formatters/minimal.rb
index eaf7e8f40f..a189cc67eb 100644
--- a/lib/chef/formatters/minimal.rb
+++ b/lib/chef/formatters/minimal.rb
@@ -144,7 +144,7 @@ class Chef
# Called before convergence starts
def converge_start(run_context)
- puts "Converging #{run_context.resource_list.all_resources.size} resources"
+ puts "Converging #{run_context.resource_collection.all_resources.size} resources"
end
# Called when the converge phase is finished.
diff --git a/lib/chef/json_compat.rb b/lib/chef/json_compat.rb
index 3350da0c13..0796984ab2 100644
--- a/lib/chef/json_compat.rb
+++ b/lib/chef/json_compat.rb
@@ -39,6 +39,8 @@ class Chef
CHEF_SANDBOX = "Chef::Sandbox".freeze
CHEF_RESOURCE = "Chef::Resource".freeze
CHEF_RESOURCECOLLECTION = "Chef::ResourceCollection".freeze
+ CHEF_RESOURCESET = "Chef::ResourceCollection::ResourceSet".freeze
+ CHEF_RESOURCELIST = "Chef::ResourceCollection::ResourceList".freeze
class <<self
@@ -145,6 +147,10 @@ class Chef
Chef::Resource
when CHEF_RESOURCECOLLECTION
Chef::ResourceCollection
+ when CHEF_RESOURCESET
+ Chef::ResourceCollection::ResourceSet
+ when CHEF_RESOURCELIST
+ Chef::ResourceCollection::ResourceList
when /^Chef::Resource/
Chef::Resource.find_subclass_by_name(json_class)
else
diff --git a/lib/chef/provider/breakpoint.rb b/lib/chef/provider/breakpoint.rb
index caa8bd5231..224e2758eb 100644
--- a/lib/chef/provider/breakpoint.rb
+++ b/lib/chef/provider/breakpoint.rb
@@ -25,9 +25,9 @@ class Chef
def action_break
if defined?(Shell) && Shell.running?
- run_context.resource_list.iterator.pause
+ run_context.resource_collection.iterator.pause
@new_resource.updated_by_last_action(true)
- run_context.resource_list.iterator
+ run_context.resource_collection.iterator
end
end
diff --git a/lib/chef/provider/deploy.rb b/lib/chef/provider/deploy.rb
index d55dc35f94..b30f7ed17e 100644
--- a/lib/chef/provider/deploy.rb
+++ b/lib/chef/provider/deploy.rb
@@ -375,8 +375,7 @@ class Chef
def gem_resource_collection_runner
gems_collection = Chef::ResourceCollection.new
- # We don't need to populated the resource_set because converge simply runs through each resource and applies them
- gem_packages.each { |rbgem| gems_collection.resource_list << rbgem }
+ gem_packages.each { |rbgem| gems_collection.insert(rbgem) }
gems_run_context = run_context.dup
gems_run_context.resource_collection = gems_collection
Chef::Runner.new(gems_run_context)
diff --git a/lib/chef/provider/lwrp_base.rb b/lib/chef/provider/lwrp_base.rb
index ce55b00729..135a3f6b7c 100644
--- a/lib/chef/provider/lwrp_base.rb
+++ b/lib/chef/provider/lwrp_base.rb
@@ -62,7 +62,7 @@ class Chef
return_value
ensure
@run_context = saved_run_context
- if temp_run_context.resource_list.any? {|r| r.updated? }
+ if temp_run_context.resource_collection.any? {|r| r.updated? }
new_resource.updated_by_last_action(true)
end
end
diff --git a/lib/chef/provider/route.rb b/lib/chef/provider/route.rb
index d430860f04..208a4f4139 100644
--- a/lib/chef/provider/route.rb
+++ b/lib/chef/provider/route.rb
@@ -162,7 +162,7 @@ class Chef::Provider::Route < Chef::Provider
case node[:platform]
when "centos", "redhat", "fedora"
# walk the collection
- run_context.resource_list.each do |resource|
+ run_context.resource_collection.each do |resource|
if resource.is_a? Chef::Resource::Route
# default to eth0
if resource.device
diff --git a/lib/chef/recipe.rb b/lib/chef/recipe.rb
index 599c451ce4..32578da5ab 100644
--- a/lib/chef/recipe.rb
+++ b/lib/chef/recipe.rb
@@ -79,7 +79,7 @@ class Chef
# Used by the DSL to look up resources when executing in the context of a
# recipe.
def resources(*args)
- run_context.resource_set.find(*args)
+ run_context.resource_collection.find(*args)
end
# This was moved to Chef::Node#tag, redirecting here for compatability
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index 377a0c4aef..cfa478c389 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -27,7 +27,6 @@ require 'chef/guard_interpreter/resource_guard_interpreter'
require 'chef/resource/conditional'
require 'chef/resource/conditional_action_not_nothing'
require 'chef/resource_collection'
-require 'chef/resource_set'
require 'chef/resource_platform_map'
require 'chef/node'
require 'chef/platform'
@@ -49,22 +48,22 @@ class Chef
# If resource and/or notifying_resource is not a resource object, this will look them up in the resource collection
# and fix the references from strings to actual Resource objects.
- def resolve_resource_reference(resource_set)
+ def resolve_resource_reference(resource_collection)
return resource if resource.kind_of?(Chef::Resource) && notifying_resource.kind_of?(Chef::Resource)
if not(resource.kind_of?(Chef::Resource))
- fix_resource_reference(resource_set)
+ fix_resource_reference(resource_collection)
end
if not(notifying_resource.kind_of?(Chef::Resource))
- fix_notifier_reference(resource_set)
+ fix_notifier_reference(resource_collection)
end
end
# This will look up the resource if it is not a Resource Object. It will complain if it finds multiple
# resources, can't find a resource, or gets invalid syntax.
- def fix_resource_reference(resource_set)
- matching_resource = resource_set.find(resource)
+ def fix_resource_reference(resource_collection)
+ matching_resource = resource_collection.find(resource)
if Array(matching_resource).size > 1
msg = "Notification #{self} from #{notifying_resource} was created with a reference to multiple resources, "\
"but can only notify one resource. Notifying resource was defined on #{notifying_resource.source_line}"
@@ -92,8 +91,8 @@ F
# This will look up the notifying_resource if it is not a Resource Object. It will complain if it finds multiple
# resources, can't find a resource, or gets invalid syntax.
- def fix_notifier_reference(resource_set)
- matching_notifier = resource_set.find(notifying_resource)
+ def fix_notifier_reference(resource_collection)
+ matching_notifier = resource_collection.find(notifying_resource)
if Array(matching_notifier).size > 1
msg = "Notification #{self} from #{notifying_resource} was created with a reference to multiple notifying "\
"resources, but can only originate from one resource. Destination resource was defined "\
@@ -300,10 +299,11 @@ F
def load_prior_resource(resource_type, instance_name)
begin
- prior_resource = run_context.resource_set.lookup(resource_type, instance_name)
+ key = ::Chef::ResourceCollection::ResourceSet.create_key(resource_type, instance_name)
+ prior_resource = run_context.resource_collection.lookup(key)
# if we get here, there is a prior resource (otherwise we'd have jumped
# to the rescue clause).
- Chef::Log.warn("Cloning resource attributes for #{::Chef::ResourceSet.create_key(resource_type, resource_name)} from prior resource (CHEF-3694)")
+ Chef::Log.warn("Cloning resource attributes for #{key} from prior resource (CHEF-3694)")
Chef::Log.warn("Previous #{prior_resource}: #{prior_resource.source_line}") if prior_resource.source_line
Chef::Log.warn("Current #{self}: #{self.source_line}") if self.source_line
prior_resource.instance_variables.each do |iv|
@@ -445,8 +445,8 @@ F
# resolve_resource_reference on each in turn, causing them to
# resolve lazy/forward references.
def resolve_notification_references
- run_context.immediate_notifications(self).each { |n| n.resolve_resource_reference(run_context.resource_set) }
- run_context.delayed_notifications(self).each {|n| n.resolve_resource_reference(run_context.resource_set) }
+ run_context.immediate_notifications(self).each { |n| n.resolve_resource_reference(run_context.resource_collection) }
+ run_context.delayed_notifications(self).each {|n| n.resolve_resource_reference(run_context.resource_collection) }
end
def notifies_immediately(action, resource_spec)
@@ -466,7 +466,7 @@ F
end
def resources(*args)
- run_context.resource_set.find(*args)
+ run_context.resource_collection.find(*args)
end
def subscribes(action, resources, timing=:delayed)
@@ -484,7 +484,7 @@ F
end
def validate_resource_spec!(resource_spec)
- ::Chef::ResourceSet.validate_lookup_spec!(resource_spec)
+ run_context.resource_collection.validate_lookup_spec!(resource_spec)
end
def is(*args)
diff --git a/lib/chef/resource_collection.rb b/lib/chef/resource_collection.rb
index f62245686a..8b6cf79afd 100644
--- a/lib/chef/resource_collection.rb
+++ b/lib/chef/resource_collection.rb
@@ -22,33 +22,76 @@ require 'chef/resource_list'
require 'chef/resource_collection/resource_collection_serialization'
##
-# TODO add class documentation
+# ResourceCollection currently handles two tasks:
+# 1) Keeps an ordered list of resources to use when converging the node
+# 2) Keeps a unique list of resources (keyed as `type[name]`) used for notifications
class Chef
class ResourceCollection
include ResourceCollectionSerialization
- attr_reader :resource_set, :resource_list
-
def initialize
@resource_set = ResourceSet.new
@resource_list = ResourceList.new
end
- # TODO fundamentally we want to write objects into 2 different data containers. We can proxy reads, but it is
- # much harder to proxy writes through 1 object.
+ # @param resource [Chef::Resource] The resource to insert
+ # @param resource_type [String,Symbol] If known, the resource type used in the recipe, Eg `package`, `execute`
+ # @param instance_name [String] If known, the recource name as used in the recipe, IE `vim` in `package 'vim'`
+ # @param at_location [Integer] If know, a location in the @resource_list to insert resource
+ # If you know the at_location but not the resource_type or instance_name, pass them in as nil
+ # This method is meant to be the 1 insert method necessary in the future. It should support all known use cases
+ # for writing into the ResourceCollection.
+ def insert(resource, resource_type=nil, instance_name=nil, at_location=nil)
+ if at_location
+ @resource_list.insert_at(at_location, resource)
+ else
+ @resource_list.insert(resource)
+ end
+ unless resource_type.nil? || instance_name.nil?
+ @resource_set.insert_as(resource, resource_type, instance_name)
+ else
+ @resource_set.insert_as(resource)
+ end
+ end
- # TODO insert calls we might need?
- # :insert, :insert_at, :[]=, :<<, :push
- # :insert_as
+ # @param insert_at_index [Integer] Location to insert resources
+ # @param resources [Chef::Resource] Resources to insert
+ # @depreciated Callers should use the insert method above and loop through their resources as necessary
+ def insert_at(insert_at_index, *resources)
+ @resource_list.insert_at(insert_at_index, *resources)
+ resources.each do |resource|
+ @resource_set.insert_as(resource)
+ end
+ end
+
+ # @depreciated
+ def []=(index, resource)
+ @resource_list[index] = resource
+ @resource_set.insert_as(resource)
+ end
+
+ # @depreciated
+ def <<(*resources)
+ resources.flatten.each do |res|
+ insert(res)
+ end
+ self
+ end
+
+ # @depreciated
+ alias_method :push, :<<
# TODO when there were 2 resources with the same key in resource_set, how do we handle notifications since they get copied?
# Did the old class only keep the last seen reference?
# TODO do we need to implement a dup method? Run_context was shallowly copying resource_collection before
+ # Read-only methods are simple to proxy - doing that below
+
RESOURCE_LIST_METHODS = Enumerable.instance_methods +
- [:all_resources, :[], :each, :execute_each_resource, :each_index, :empty?]
- RESOURCE_SET_METHODS = [:lookup, :find, :resources]
+ [:iterator, :all_resources, :[], :each, :execute_each_resource, :each_index, :empty?] -
+ [:find] # find needs to run on the set
+ RESOURCE_SET_METHODS = [:lookup, :find, :resources, :keys, :validate_lookup_spec!]
def method_missing(name, *args, &block)
if RESOURCE_LIST_METHODS.include?(name)
diff --git a/lib/chef/resource_collection/resource_collection_serialization.rb b/lib/chef/resource_collection/resource_collection_serialization.rb
index 708e72d848..8c11b9beeb 100644
--- a/lib/chef/resource_collection/resource_collection_serialization.rb
+++ b/lib/chef/resource_collection/resource_collection_serialization.rb
@@ -16,6 +16,7 @@
# limitations under the License.
#
class Chef
+ # TODO move into subfolder until we promote these to top level classes
class ResourceCollection
module ResourceCollectionSerialization
# Serialize this object as a hash
@@ -34,15 +35,21 @@ class Chef
Chef::JSONCompat.to_json(to_hash, *a)
end
- def self.json_create(o)
- collection = self.new()
- o["instance_vars"].each do |k,v|
- collection.instance_variable_set(k.to_sym, v)
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ def json_create(o)
+ collection = self.new()
+ o["instance_vars"].each do |k,v|
+ collection.instance_variable_set(k.to_sym, v)
+ end
+ collection
end
- collection
end
- def is_chef_resource(arg)
+ def is_chef_resource!(arg)
unless arg.kind_of?(Chef::Resource)
raise ArgumentError, "Cannot insert a #{arg.class} into a resource collection: must be a subclass of Chef::Resource"
end
diff --git a/lib/chef/resource_list.rb b/lib/chef/resource_list.rb
index 1392c6b557..50b5a41e58 100644
--- a/lib/chef/resource_list.rb
+++ b/lib/chef/resource_list.rb
@@ -21,84 +21,80 @@ require 'chef/resource_collection/stepable_iterator'
require 'chef/resource_collection/resource_collection_serialization'
class Chef
- class ResourceList
- include ResourceCollection::ResourceCollectionSerialization
- include Enumerable
+ class ResourceCollection
+ class ResourceList
+ include ResourceCollection::ResourceCollectionSerialization
+ include Enumerable
- attr_reader :iterator
+ attr_reader :iterator
- def initialize
- @resources = Array.new
- @insert_after_idx = nil
- end
-
- def all_resources
- @resources
- end
+ def initialize
+ @resources = Array.new
+ @insert_after_idx = nil
+ end
- def [](index)
- @resources[index]
- end
+ # TODO the differences between these 2 insert methods is very confusing
+ def insert(resource)
+ if @insert_after_idx
+ # in the middle of executing a run, so any resources inserted now should
+ # be placed after the most recent addition done by the currently executing
+ # resource
+ insert_at(@insert_after_idx += 1, resource)
+ else
+ is_chef_resource!(resource)
+ @resources << resource
+ end
+ end
- def []=(index, arg)
- is_chef_resource(arg)
- @resources[index] = arg
- end
+ # TODO this did not adjust @insert_after_idx in the old class - add test case and ask JohnK
+ def insert_at(index, *resources)
+ resources.each do |resource|
+ is_chef_resource!(resource)
+ end
+ @resources.insert(index, *resources)
+ end
- def <<(*args)
- args.flatten.each do |a|
- is_chef_resource(a)
- @resources << a
+ # @depreciated
+ def []=(index, resource)
+ @resources[index] = resource
end
- self
- end
- # 'push' is an alias method to <<
- alias_method :push, :<<
+ def all_resources
+ @resources
+ end
- def insert(resource)
- if @insert_after_idx
- # in the middle of executing a run, so any resources inserted now should
- # be placed after the most recent addition done by the currently executing
- # resource
- insert_at(@insert_after_idx + 1, resource)
- @insert_after_idx += 1
- else
- is_chef_resource(resource)
- @resources << resource
+ def [](index)
+ @resources[index]
end
- end
- def insert_at(insert_at_index, *resources)
- resources.each do |resource|
- is_chef_resource(resource)
+ def each
+ @resources.each do |resource|
+ yield resource
+ end
end
- @resources.insert(insert_at_index, *resources)
- end
- def each
- @resources.each do |resource|
- yield resource
+ # TODO I would like to rename this to something that illustrates it sets the @insert_after_idx variable, then alias this old name
+ # TODO or perhaps refactor it to have 2 pointers - 1 for the end of the list and 1 for resources we have processed
+ # so far, and then move that logic up into the ResourceCollection class to simplify this class
+ def execute_each_resource(&resource_exec_block)
+ @iterator = ResourceCollection::StepableIterator.for_collection(@resources)
+ @iterator.each_with_index do |resource, idx|
+ @insert_after_idx = idx
+ yield resource
+ end
end
- end
- def execute_each_resource(&resource_exec_block)
- @iterator = ResourceCollection::StepableIterator.for_collection(@resources)
- @iterator.each_with_index do |resource, idx|
- @insert_after_idx = idx
- yield resource
+ def each_index
+ @resources.each_index do |i|
+ yield i
+ end
end
- end
- def each_index
- @resources.each_index do |i|
- yield i
+ def empty?
+ @resources.empty?
end
- end
- def empty?
- @resources.empty?
end
-
end
end
+
diff --git a/lib/chef/resource_set.rb b/lib/chef/resource_set.rb
index e0e2038f78..8c39065e75 100644
--- a/lib/chef/resource_set.rb
+++ b/lib/chef/resource_set.rb
@@ -20,141 +20,149 @@ require 'chef/resource'
require 'chef/resource_collection/resource_collection_serialization'
class Chef
- class ResourceSet
- include ResourceCollection::ResourceCollectionSerialization
+ # TODO move into subfolder until we promote these to top level classes
+ class ResourceCollection
+ class ResourceSet
+ include ResourceCollection::ResourceCollectionSerialization
- # Matches a multiple resource lookup specification,
- # e.g., "service[nginx,unicorn]"
- MULTIPLE_RESOURCE_MATCH = /^(.+)\[(.+?),(.+)\]$/
+ # Matches a multiple resource lookup specification,
+ # e.g., "service[nginx,unicorn]"
+ MULTIPLE_RESOURCE_MATCH = /^(.+)\[(.+?),(.+)\]$/
- # Matches a single resource lookup specification,
- # e.g., "service[nginx]"
- SINGLE_RESOURCE_MATCH = /^(.+)\[(.+)\]$/
+ # Matches a single resource lookup specification,
+ # e.g., "service[nginx]"
+ SINGLE_RESOURCE_MATCH = /^(.+)\[(.+)\]$/
- def initialize
- @resources_by_key = Hash.new
- end
+ def initialize
+ @resources_by_key = Hash.new
+ end
- def keys
- @resources_by_key.keys
- end
+ def keys
+ @resources_by_key.keys
+ end
- def insert_as(resource_type, instance_name, resource)
- is_chef_resource(resource)
- key = ResourceSet.create_key(resource_type, instance_name)
- @resources_by_key[key] = resource
- end
+ def insert_as(resource, resource_type=resource.resource_name, instance_name=resource.name)
+ is_chef_resource!(resource)
+ key = ResourceSet.create_key(resource_type, instance_name)
+ @resources_by_key[key] = resource
+ end
+
+ def lookup(key)
+ case
+ when key.kind_of?(String)
+ lookup_by = key
+ when key.kind_of?(Chef::Resource)
+ lookup_by = ResourceSet.create_key(key.resource_name, key.name)
+ else
+ raise ArgumentError, "Must pass a Chef::Resource or String to lookup"
+ end
- # TODO when do we need to lookup a resource by resource? That smells like we should still have the resource say how its indexed, or we shouldn't be doing that.
- def lookup(resource_type, instance_name)
- raise ArgumentError, "Must pass resource_type as a String or Symbol and instance_name as a String" unless
- (resource_type.kind_of?(String) || resource_type.kind_of?(Symbol)) && instance_name.kind_of?(String)
- key = ResourceSet.create_key(resource_type, instance_name)
- res = @resources_by_key[key]
- unless res
- raise Chef::Exceptions::ResourceNotFound, "Cannot find a resource matching #{key} (did you define it first?)"
+ res = @resources_by_key[lookup_by]
+ unless res
+ raise Chef::Exceptions::ResourceNotFound, "Cannot find a resource matching #{lookup_by} (did you define it first?)"
+ end
+ res
end
- res
- end
- # Find existing resources by searching the list of existing resources. Possible
- # forms are:
- #
- # find(:file => "foobar")
- # find(:file => [ "foobar", "baz" ])
- # find("file[foobar]", "file[baz]")
- # find("file[foobar,baz]")
- #
- # Returns the matching resource, or an Array of matching resources.
- #
- # Raises an ArgumentError if you feed it bad lookup information
- # Raises a Runtime Error if it can't find the resources you are looking for.
- def find(*args)
- results = Array.new
- args.each do |arg|
- case arg
+ # Find existing resources by searching the list of existing resources. Possible
+ # forms are:
+ #
+ # find(:file => "foobar")
+ # find(:file => [ "foobar", "baz" ])
+ # find("file[foobar]", "file[baz]")
+ # find("file[foobar,baz]")
+ #
+ # Returns the matching resource, or an Array of matching resources.
+ #
+ # Raises an ArgumentError if you feed it bad lookup information
+ # Raises a Runtime Error if it can't find the resources you are looking for.
+ def find(*args)
+ results = Array.new
+ args.each do |arg|
+ case arg
+ when Hash
+ results << find_resource_by_hash(arg)
+ when String
+ results << find_resource_by_string(arg)
+ else
+ msg = "arguments to #{self.class.name}#find should be of the form :resource => 'name' or 'resource[name]'"
+ raise Chef::Exceptions::InvalidResourceSpecification, msg
+ end
+ end
+ flat_results = results.flatten
+ flat_results.length == 1 ? flat_results[0] : flat_results
+ end
+
+ # resources is a poorly named, but we have to maintain it for back
+ # compat.
+ alias_method :resources, :find
+
+ # Returns true if +query_object+ is a valid string for looking up a
+ # resource, or raises InvalidResourceSpecification if not.
+ # === Arguments
+ # * query_object should be a string of the form
+ # "resource_type[resource_name]", a single element Hash (e.g., :service =>
+ # "apache2"), or a Chef::Resource (this is the happy path). Other arguments
+ # will raise an exception.
+ # === Returns
+ # * true returns true for all valid input.
+ # === Raises
+ # * Chef::Exceptions::InvalidResourceSpecification for all invalid input.
+ def validate_lookup_spec!(query_object)
+ case query_object
+ when Chef::Resource
+ true
+ when SINGLE_RESOURCE_MATCH, MULTIPLE_RESOURCE_MATCH
+ true
when Hash
- results << find_resource_by_hash(arg)
+ true
when String
- results << find_resource_by_string(arg)
+ raise Chef::Exceptions::InvalidResourceSpecification,
+ "The string `#{query_object}' is not valid for resource collection lookup. Correct syntax is `resource_type[resource_name]'"
else
- msg = "arguments to #{self.class.name}#find should be of the form :resource => 'name' or 'resource[name]'"
- raise Chef::Exceptions::InvalidResourceSpecification, msg
+ raise Chef::Exceptions::InvalidResourceSpecification,
+ "The object `#{query_object.inspect}' is not valid for resource collection lookup. " +
+ "Use a String like `resource_type[resource_name]' or a Chef::Resource object"
end
end
- flat_results = results.flatten
- flat_results.length == 1 ? flat_results[0] : flat_results
- end
- # resources is a poorly named, but we have to maintain it for back
- # compat.
- alias_method :resources, :find
-
- # Returns true if +query_object+ is a valid string for looking up a
- # resource, or raises InvalidResourceSpecification if not.
- # === Arguments
- # * query_object should be a string of the form
- # "resource_type[resource_name]", a single element Hash (e.g., :service =>
- # "apache2"), or a Chef::Resource (this is the happy path). Other arguments
- # will raise an exception.
- # === Returns
- # * true returns true for all valid input.
- # === Raises
- # * Chef::Exceptions::InvalidResourceSpecification for all invalid input.
- def self.validate_lookup_spec!(query_object)
- case query_object
- when Chef::Resource
- true
- when SINGLE_RESOURCE_MATCH, MULTIPLE_RESOURCE_MATCH
- true
- when Hash
- true
- when String
- raise Chef::Exceptions::InvalidResourceSpecification,
- "The string `#{query_object}' is not valid for resource collection lookup. Correct syntax is `resource_type[resource_name]'"
- else
- raise Chef::Exceptions::InvalidResourceSpecification,
- "The object `#{query_object.inspect}' is not valid for resource collection lookup. " +
- "Use a String like `resource_type[resource_name]' or a Chef::Resource object"
+ def self.create_key(resource_type, instance_name)
+ "#{resource_type}[#{instance_name}]"
end
- end
- def self.create_key(resource_type, instance_name)
- "#{resource_type}[#{instance_name}]"
- end
-
- private
+ private
- def find_resource_by_hash(arg)
- results = Array.new
- arg.each do |resource_type, name_list|
- instance_names = name_list.kind_of?(Array) ? name_list : [ name_list ]
- instance_names.each do |instance_name|
- results << lookup(resource_type, instance_name)
+ def find_resource_by_hash(arg)
+ results = Array.new
+ arg.each do |resource_type, name_list|
+ instance_names = name_list.kind_of?(Array) ? name_list : [ name_list ]
+ instance_names.each do |instance_name|
+ results << lookup(ResourceSet.create_key(resource_type, instance_name))
+ end
end
+ return results
end
- return results
- end
- def find_resource_by_string(arg)
- results = Array.new
- case arg
- when MULTIPLE_RESOURCE_MATCH
- resource_type = $1
- arg =~ /^.+\[(.+)\]$/
- resource_list = $1
- resource_list.split(",").each do |instance_name|
- results << lookup(resource_type, instance_name)
- end
- when SINGLE_RESOURCE_MATCH
- resource_type = $1
- name = $2
- results << lookup(resource_type, name)
- else
- raise ArgumentError, "Bad string format #{arg}, you must have a string like resource_type[name]!"
+ def find_resource_by_string(arg)
+ results = Array.new
+ case arg
+ when MULTIPLE_RESOURCE_MATCH
+ resource_type = $1
+ arg =~ /^.+\[(.+)\]$/
+ resource_list = $1
+ resource_list.split(",").each do |instance_name|
+ results << lookup(ResourceSet.create_key(resource_type, instance_name))
+ end
+ when SINGLE_RESOURCE_MATCH
+ resource_type = $1
+ name = $2
+ results << lookup(ResourceSet.create_key(resource_type, name))
+ else
+ raise ArgumentError, "Bad string format #{arg}, you must have a string like resource_type[name]!"
+ end
+ return results
end
- return results
- end
+ end
end
end
diff --git a/lib/chef/run_context.rb b/lib/chef/run_context.rb
index b77a2af9b0..bbe2f9eba0 100644
--- a/lib/chef/run_context.rb
+++ b/lib/chef/run_context.rb
@@ -50,14 +50,6 @@ class Chef
# recipes, which is triggered by #load. (See also: CookbookCompiler)
attr_accessor :resource_collection
- def resource_set
- resource_collection.resource_set
- end
-
- def resource_list
- resource_collection.resource_list
- end
-
# A Hash containing the immediate notifications triggered by resources
# during the converge phase of the chef run.
attr_accessor :immediate_notification_collection
diff --git a/lib/chef/run_status.rb b/lib/chef/run_status.rb
index d47a35459c..0f181426b0 100644
--- a/lib/chef/run_status.rb
+++ b/lib/chef/run_status.rb
@@ -70,13 +70,13 @@ class Chef::RunStatus
# The list of all resources in the current run context's +resource_collection+
def all_resources
- @run_context && @run_context.resource_list.all_resources
+ @run_context && @run_context.resource_collection.all_resources
end
# The list of all resources in the current run context's +resource_collection+
# that are marked as updated
def updated_resources
- @run_context && @run_context.resource_list.select { |r| r.updated }
+ @run_context && @run_context.resource_collection.select { |r| r.updated }
end
# The backtrace from +exception+, if any
diff --git a/lib/chef/runner.rb b/lib/chef/runner.rb
index e27e250b21..6125fe59e1 100644
--- a/lib/chef/runner.rb
+++ b/lib/chef/runner.rb
@@ -72,12 +72,12 @@ class Chef
# +run_action+ for each resource in turn.
def converge
# Resolve all lazy/forward references in notifications
- run_context.resource_list.each do |resource|
+ run_context.resource_collection.each do |resource|
resource.resolve_notification_references
end
# Execute each resource.
- run_context.resource_list.execute_each_resource do |resource|
+ run_context.resource_collection.execute_each_resource do |resource|
Array(resource.action).each {|action| run_action(resource, action)}
end
diff --git a/lib/chef/shell/ext.rb b/lib/chef/shell/ext.rb
index ea11d9d1a8..fd785e2f79 100644
--- a/lib/chef/shell/ext.rb
+++ b/lib/chef/shell/ext.rb
@@ -244,7 +244,7 @@ E
:skip_back => "move back in the run list",
:skip_forward => "move forward in the run list"
def chef_run
- Shell.session.resource_list.iterator
+ Shell.session.resource_collection.iterator
end
desc "resets the current recipe"
@@ -547,7 +547,7 @@ E
desc "list all the resources on the current recipe"
def resources(*args)
if args.empty?
- pp run_context.resource_set.keys
+ pp run_context.resource_collection.keys
else
pp resources = original_resources(*args)
resources
diff --git a/lib/chef/shell/shell_session.rb b/lib/chef/shell/shell_session.rb
index 6486a5ebeb..73e6c34ebb 100644
--- a/lib/chef/shell/shell_session.rb
+++ b/lib/chef/shell/shell_session.rb
@@ -69,8 +69,8 @@ module Shell
@node.consume_attributes(@node_attributes)
end
- def resource_list
- run_context.resource_list
+ def resource_collection
+ run_context.resource_collection
end
def run_context
diff --git a/spec/unit/resource_collection_spec.rb b/spec/unit/resource_collection_spec.rb
index cf119f1ab0..631e3730d8 100644
--- a/spec/unit/resource_collection_spec.rb
+++ b/spec/unit/resource_collection_spec.rb
@@ -300,13 +300,13 @@ describe Chef::ResourceCollection do
describe "provides access to the raw resources array" do
it "returns the resources via the all_resources method" do
- @rc.all_resources.should equal(@rc.instance_variable_get(:@resources))
+ @rc.all_resources.should equal(@rc.instance_variable_get(:@resource_list).instance_variable_get(:@resources))
end
end
describe "provides access to stepable iterator" do
it "returns the iterator object" do
- @rc.instance_variable_set(:@iterator, :fooboar)
+ @rc.instance_variable_get(:@resource_list).instance_variable_set(:@iterator, :fooboar)
@rc.iterator.should == :fooboar
end
end
diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb
index 692345c943..aa69861630 100644
--- a/spec/unit/resource_spec.rb
+++ b/spec/unit/resource_spec.rb
@@ -174,12 +174,12 @@ describe Chef::Resource do
end
it "should load the attributes of a prior resource" do
- @resource.load_prior_resource
+ @resource.load_prior_resource(@resource.resource_name, @resource.name)
@resource.supports.should == { :funky => true }
end
it "should not inherit the action from the prior resource" do
- @resource.load_prior_resource
+ @resource.load_prior_resource(@resource.resource_name, @resource.name)
@resource.action.should_not == @prior_resource.action
end
end
diff --git a/spec/unit/shell/shell_ext_spec.rb b/spec/unit/shell/shell_ext_spec.rb
index c24acbca3e..8485b66d23 100644
--- a/spec/unit/shell/shell_ext_spec.rb
+++ b/spec/unit/shell/shell_ext_spec.rb
@@ -121,7 +121,7 @@ describe Shell::Extensions do
Shell.session.stub(:rebuild_context)
events = Chef::EventDispatch::Dispatcher.new
run_context = Chef::RunContext.new(Chef::Node.new, {}, events)
- run_context.resource_collection.instance_variable_set(:@iterator, :the_iterator)
+ run_context.resource_collection.instance_variable_get(:@resource_list).instance_variable_set(:@iterator, :the_iterator)
Shell.session.run_context = run_context
@root_context.chef_run.should == :the_iterator
end