summaryrefslogtreecommitdiff
path: root/lib/chef/dsl/recipe.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chef/dsl/recipe.rb')
-rw-r--r--lib/chef/dsl/recipe.rb80
1 files changed, 49 insertions, 31 deletions
diff --git a/lib/chef/dsl/recipe.rb b/lib/chef/dsl/recipe.rb
index d70266c14c..7b467fd867 100644
--- a/lib/chef/dsl/recipe.rb
+++ b/lib/chef/dsl/recipe.rb
@@ -19,6 +19,7 @@
require 'chef/mixin/convert_to_class_name'
require 'chef/exceptions'
+require 'chef/resource_builder'
class Chef
module DSL
@@ -72,10 +73,31 @@ class Chef
new_recipe.instance_eval(&new_def.recipe)
end
+ #
# Instantiates a resource (via #build_resource), then adds it to the
# resource collection. Note that resource classes are looked up directly,
# so this will create the resource you intended even if the method name
# corresponding to that resource has been overridden.
+ #
+ # @param type [Symbol] The type of resource (e.g. `:file` or `:package`)
+ # @param name [String] The name of the resource (e.g. '/x/y.txt' or 'apache2')
+ # @param created_at [String] The caller of the resource. Use `caller[0]`
+ # to get the caller of your function. Defaults to the caller of this
+ # function.
+ # @param resource_attrs_block A block that lets you set attributes of the
+ # resource (it is instance_eval'd on the resource instance).
+ #
+ # @return [Chef::Resource] The new resource.
+ #
+ # @example
+ # declare_resource(:file, '/x/y.txy', caller[0]) do
+ # action :delete
+ # end
+ # # Equivalent to
+ # file '/x/y.txt' do
+ # action :delete
+ # end
+ #
def declare_resource(type, name, created_at=nil, &resource_attrs_block)
created_at ||= caller[0]
@@ -85,44 +107,40 @@ class Chef
resource
end
+ #
# Instantiate a resource of the given +type+ with the given +name+ and
# attributes as given in the +resource_attrs_block+.
#
# The resource is NOT added to the resource collection.
+ #
+ # @param type [Symbol] The type of resource (e.g. `:file` or `:package`)
+ # @param name [String] The name of the resource (e.g. '/x/y.txt' or 'apache2')
+ # @param created_at [String] The caller of the resource. Use `caller[0]`
+ # to get the caller of your function. Defaults to the caller of this
+ # function.
+ # @param resource_attrs_block A block that lets you set attributes of the
+ # resource (it is instance_eval'd on the resource instance).
+ #
+ # @return [Chef::Resource] The new resource.
+ #
+ # @example
+ # build_resource(:file, '/x/y.txy', caller[0]) do
+ # action :delete
+ # end
+ #
def build_resource(type, name, created_at=nil, &resource_attrs_block)
created_at ||= caller[0]
- # Checks the new platform => short_name => resource mapping initially
- # then fall back to the older approach (Chef::Resource.const_get) for
- # backward compatibility
- resource_class = resource_class_for(type)
-
- raise ArgumentError, "You must supply a name when declaring a #{type} resource" if name.nil?
-
- resource = resource_class.new(name, run_context)
- resource.source_line = created_at
- resource.declared_type = type
- # If we have a resource like this one, we want to steal its state
- # This behavior is very counter-intuitive and should be removed.
- # See CHEF-3694, https://tickets.opscode.com/browse/CHEF-3694
- # Moved to this location to resolve CHEF-5052, https://tickets.opscode.com/browse/CHEF-5052
- resource.load_prior_resource(type, name)
- resource.cookbook_name = cookbook_name
- resource.recipe_name = recipe_name
- # Determine whether this resource is being created in the context of an enclosing Provider
- resource.enclosing_provider = self.is_a?(Chef::Provider) ? self : nil
-
- # XXX: This is very crufty, but it's required for resource definitions
- # to work properly :(
- resource.params = @params
-
- # Evaluate resource attribute DSL
- resource.instance_eval(&resource_attrs_block) if block_given?
-
- # Run optional resource hook
- resource.after_created
-
- resource
+ Chef::ResourceBuilder.new(
+ type: type,
+ name: name,
+ created_at: created_at,
+ params: @params,
+ run_context: run_context,
+ cookbook_name: cookbook_name,
+ recipe_name: recipe_name,
+ enclosing_provider: self.is_a?(Chef::Provider) ? self : nil
+ ).build(&resource_attrs_block)
end
def resource_class_for(snake_case_name)