Method: Chef::DSL::DeclareResource#edit_resource

Defined in:
lib/chef/dsl/declare_resource.rb

#edit_resource(type, name, created_at = nil, run_context: self.run_context, &resource_attrs_block) ⇒ Chef::Resource

Lookup a resource in the resource collection by name. If it exists, return it. If it does not exist, create it. This is a useful function for accumulator patterns. In CRUD terminology this is an “upsert” operation and is used to assert that the resource must exist with the specified properties.

Examples:

resource = edit_resource(:template, '/x/y.txy') do
  source "y.txy.erb"
  variables {}
end
resource.variables.merge!({ home: "/home/klowns"  })

Parameters:

  • type (Symbol)

    The type of resource (e.g. ‘:file` or `:package`)

  • name (String)

    The name of the resource (e.g. ‘/x/y.txt’ or ‘apache2’)

  • created_at (String) (defaults to: nil)

    The caller of the resource. Use ‘caller` to get the caller of your function. Defaults to the caller of this function.

  • run_context (Chef::RunContext) (defaults to: self.run_context)

    the run_context of the resource collection to operate on

  • resource_attrs_block

    A block that lets you set attributes of the resource (it is instance_eval’d on the resource instance).

Returns:



149
150
151
152
153
# File 'lib/chef/dsl/declare_resource.rb', line 149

def edit_resource(type, name, created_at = nil, run_context: self.run_context, &resource_attrs_block)
  edit_resource!(type, name, created_at, run_context: run_context, &resource_attrs_block)
rescue Chef::Exceptions::ResourceNotFound
  declare_resource(type, name, created_at, run_context: run_context, &resource_attrs_block)
end