class Chef::ResourceCollection::ResourceSet

Constants

MULTIPLE_RESOURCE_MATCH

Matches a multiple resource lookup specification, e.g., “service

NAMELESS_RESOURCE_MATCH

Matches e.g. “apt_update” with no name

SINGLE_RESOURCE_MATCH

Matches a single resource lookup specification, e.g., “service

Public Class Methods

from_hash(o) click to toggle source
# File lib/chef/resource_collection/resource_set.rb, line 138
def self.from_hash(o)
  collection = new()
  rl = o["instance_vars"]["@resources_by_key"]
  resources = rl.merge(rl) { |k, r| Chef::Resource.from_hash(r) }
  collection.instance_variable_set(:@resources_by_key, resources)
  collection
end
new() click to toggle source
# File lib/chef/resource_collection/resource_set.rb, line 38
def initialize
  @resources_by_key = Hash.new
end

Public Instance Methods

delete(key) click to toggle source
# File lib/chef/resource_collection/resource_set.rb, line 64
def delete(key)
  raise ArgumentError, "Must pass a Chef::Resource or String to delete" unless key.is_a?(String) || key.is_a?(Chef::Resource)
  key = key.to_s
  res = @resources_by_key.delete(key)

  if res == @resources_by_key.default
    raise Chef::Exceptions::ResourceNotFound, "Cannot find a resource matching #{key} (did you define it first?)"
  end
  res
end
find(*args) click to toggle source

Find existing resources by searching the list of existing resources. Possible forms are:

find(:file => “foobar”) find(:file => [ “foobar”, “baz” ]) find(“file”, “file”) find(“file”)

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.

# File lib/chef/resource_collection/resource_set.rb, line 87
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
Also aliased as: resources
insert_as(resource, resource_type = nil, instance_name = nil) click to toggle source
# File lib/chef/resource_collection/resource_set.rb, line 46
def insert_as(resource, resource_type = nil, instance_name = nil)
  is_chef_resource!(resource)
  resource_type ||= resource.resource_name
  instance_name ||= resource.name
  key = create_key(resource_type, instance_name)
  @resources_by_key[key] = resource
end
keys() click to toggle source
# File lib/chef/resource_collection/resource_set.rb, line 42
def keys
  @resources_by_key.keys
end
lookup(key) click to toggle source
# File lib/chef/resource_collection/resource_set.rb, line 54
def lookup(key)
  raise ArgumentError, "Must pass a Chef::Resource or String to lookup" unless key.is_a?(String) || key.is_a?(Chef::Resource)
  key = key.to_s
  res = @resources_by_key[key]
  unless res
    raise Chef::Exceptions::ResourceNotFound, "Cannot find a resource matching #{key} (did you define it first?)"
  end
  res
end
resources(*args)

@deprecated resources is a poorly named, but we have to maintain it for back compat.

Alias for: find
validate_lookup_spec!(query_object) click to toggle source

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”, 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

# File lib/chef/resource_collection/resource_set.rb, line 120
def validate_lookup_spec!(query_object)
  case query_object
    when Chef::Resource
      true
    when SINGLE_RESOURCE_MATCH, MULTIPLE_RESOURCE_MATCH, NAMELESS_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

Private Instance Methods

create_key(resource_type, instance_name) click to toggle source
# File lib/chef/resource_collection/resource_set.rb, line 148
def create_key(resource_type, instance_name)
  "#{resource_type}[#{instance_name}]"
end
find_resource_by_hash(arg) click to toggle source
# File lib/chef/resource_collection/resource_set.rb, line 152
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(create_key(resource_type, instance_name))
    end
  end
  results
end
find_resource_by_string(arg) click to toggle source
# File lib/chef/resource_collection/resource_set.rb, line 163
def find_resource_by_string(arg)
  begin
    if arg =~ SINGLE_RESOURCE_MATCH
      resource_type = $1
      name = $2
      return [ lookup(create_key(resource_type, name)) ]
    end
  rescue Chef::Exceptions::ResourceNotFound => e
    if arg =~ MULTIPLE_RESOURCE_MATCH
      begin
        results = Array.new
        resource_type = $1
        arg =~ /^.+\[(.+)\]$/
        resource_list = $1
        resource_list.split(",").each do |instance_name|
          results << lookup(create_key(resource_type, instance_name))
        end
        Chef.deprecated(:multiresource_match, "The resource_collection multi-resource syntax is deprecated")
        return results
      rescue  Chef::Exceptions::ResourceNotFound
        raise e
      end
    else
      raise e
    end
  end

  if arg =~ NAMELESS_RESOURCE_MATCH
    resource_type = $1
    name = ""
    return [ lookup(create_key(resource_type, name)) ]
  end

  raise ArgumentError, "Bad string format #{arg}, you must have a string like resource_type[name]!"
end