Modelling a collection of resources as an LWRP

I have a set of resources that I want to invoke as a single unit, that will
notify other resources if any of the sub-resources would do so.

One such set could be modeled as its own recipe, I want to be able to
include multiple instances in other recipes - and you can only include
another given recipe once.

So I was thinking an LWRP. Say I want to create a group, a user who belongs
to that group, a file owned by that user, and a service that requires that
file to exist, in one swell foop. The provider could look like this:

action :create do
resources = Hash.new []
resources[:create] <<
group “foo” do … end <<
user “bar” do … end <<
file “baz” do … end
resources[:enable] = resources[:start] << service “zoo” do … end

resources.each do |action, res|
    res.run_action action
    @new_resource.updated_by_last_action(

@new_resource.updated_by_last_action? || res.updated_by_last_action? )
end
end

It seems kind of hacky to force all the resources to run immediately just so
I can query their updated_by_last_action status. Is there a better way?

Mark J. Reed markjreed@gmail.com

On Mon, Feb 14, 2011 at 1:01 PM, Mark J. Reed markjreed@gmail.com wrote:

I have a set of resources that I want to invoke as a single unit, that will
notify other resources if any of the sub-resources would do so.
One such set could be modeled as its own recipe, I want to be able to
include multiple instances in other recipes - and you can only include
another given recipe once.
So I was thinking an LWRP. Say I want to create a group, a user who belongs
to that group, a file owned by that user, and a service that requires that
file to exist, in one swell foop. The provider could look like this:
action :create do
resources = Hash.new
resources[:create] <<
group "foo" do ... end <<
user "bar" do ... end <<
file "baz" do ... end
resources[:enable] = resources[:start] << service "zoo" do ... end
resources.each do |action, res|
res.run_action action
@new_resource.updated_by_last_action(
@new_resource.updated_by_last_action? || res.updated_by_last_action? )
end
end
It seems kind of hacky to force all the resources to run immediately just so
I can query their updated_by_last_action status. Is there a better way?

If the only real goal here is to fire a notification if a set of
resources change, you should just include the notification on all the
resources - either through being explicit about it (specifying it in
each resource declaration) or in a loop. No need to run the actions
in advance, Chef is smart enough to group together the notifications.

Adam

--
Opscode, Inc.
Adam Jacob, Chief Product Officer
T: (206) 619-7151 E: adam@opscode.com