lib/chef/resource_collection.rb in chef-11.4.4 vs lib/chef/resource_collection.rb in chef-11.6.0.hotfix.1
- old
+ new
@@ -22,10 +22,18 @@
class Chef
class ResourceCollection
include Enumerable
+ # 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 = /^(.+)\[(.+)\]$/
+
attr_reader :iterator
def initialize
@resources = Array.new
@resources_by_name = Hash.new
@@ -50,12 +58,16 @@
args.flatten.each do |a|
is_chef_resource(a)
@resources << a
@resources_by_name[a.to_s] = @resources.length - 1
end
+ self
end
+ # 'push' is an alias method to <<
+ alias_method :push, :<<
+
def insert(resource)
is_chef_resource(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
@@ -71,18 +83,10 @@
@resources << resource
@resources_by_name[resource.to_s] = @resources.length - 1
end
end
- def push(*args)
- args.flatten.each do |arg|
- is_chef_resource(arg)
- @resources.push(arg)
- @resources_by_name[arg.to_s] = @resources.length - 1
- end
- end
-
def each
@resources.each do |resource|
yield resource
end
end
@@ -152,10 +156,40 @@
# 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
+ 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"
+ end
+ end
+
# Serialize this object as a hash
def to_json(*a)
instance_vars = Hash.new
self.instance_variables.each do |iv|
instance_vars[iv] = self.instance_variable_get(iv)
@@ -190,24 +224,24 @@
end
def find_resource_by_string(arg)
results = Array.new
case arg
- when /^(.+)\[(.+?),(.+)\]$/
+ when MULTIPLE_RESOURCE_MATCH
resource_type = $1
arg =~ /^.+\[(.+)\]$/
resource_list = $1
resource_list.split(",").each do |name|
resource_name = "#{resource_type}[#{name}]"
results << lookup(resource_name)
end
- when /^(.+)\[(.+)\]$/
+ when SINGLE_RESOURCE_MATCH
resource_type = $1
name = $2
resource_name = "#{resource_type}[#{name}]"
results << lookup(resource_name)
else
- raise ArgumentError, "You must have a string like resource_type[name]!"
+ raise ArgumentError, "Bad string format #{arg}, you must have a string like resource_type[name]!"
end
return results
end
def is_chef_resource(arg)