Class: Chef::ResourceCollection::ResourceSet
- Inherits:
-
Object
- Object
- Chef::ResourceCollection::ResourceSet
- Includes:
- ResourceCollectionSerialization
- Defined in:
- lib/chef/resource_collection/resource_set.rb
Constant Summary collapse
- MULTIPLE_RESOURCE_MATCH =
Matches a multiple resource lookup specification, e.g., “service”
/^(.+)\[(.+?),(.+)\]$/
- SINGLE_RESOURCE_MATCH =
Matches a single resource lookup specification, e.g., “service”
/^(.+)\[(.+)\]$/
Instance Method Summary collapse
-
#find(*args) ⇒ Object
(also: #resources)
Find existing resources by searching the list of existing resources.
-
#initialize ⇒ ResourceSet
constructor
A new instance of ResourceSet.
- #insert_as(resource, resource_type = nil, instance_name = nil) ⇒ Object
- #keys ⇒ Object
- #lookup(key) ⇒ Object
-
#validate_lookup_spec!(query_object) ⇒ Object
Returns true if
query_object
is a valid string for looking up a resource, or raises InvalidResourceSpecification if not.
Methods included from ResourceCollectionSerialization
included, #is_chef_resource!, #to_hash, #to_json
Constructor Details
#initialize ⇒ ResourceSet
Returns a new instance of ResourceSet.
35 36 37 |
# File 'lib/chef/resource_collection/resource_set.rb', line 35 def initialize @resources_by_key = Hash.new end |
Instance Method Details
#find(*args) ⇒ Object Also known as: resources
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.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/chef/resource_collection/resource_set.rb', line 80 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 |
#insert_as(resource, resource_type = nil, instance_name = nil) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/chef/resource_collection/resource_set.rb', line 43 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 ⇒ Object
39 40 41 |
# File 'lib/chef/resource_collection/resource_set.rb', line 39 def keys @resources_by_key.keys end |
#lookup(key) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/chef/resource_collection/resource_set.rb', line 51 def lookup(key) case when key.kind_of?(String) lookup_by = key when key.kind_of?(Chef::Resource) lookup_by = create_key(key.resource_name, key.name) else raise ArgumentError, "Must pass a Chef::Resource or String to lookup" end 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 |
#validate_lookup_spec!(query_object) ⇒ Object
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
-
Chef::Exceptions::InvalidResourceSpecification for all invalid input.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/chef/resource_collection/resource_set.rb', line 113 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 |