Good example of a lwrp

I ran a test to answer my own question. new_resource is already an instance
of the LWRP generated resource class. So I suggest that instead of this:

def load_current_resource
@smbuser = Chef::Resource::SambaUser.new(new_resource.name)

Chef::Log.debug("Checking for smbuser #{new_resource.name}")
u = shell_out("pdbedit -Lv -u #{new_resource.name}")
exists = u.stdout.include?(new_resource.name)
disabled = u.stdout.include?("Account Flags.*[D")
@smbuser.exists(exists)
@smbuser.disabled(disabled)
end

I could do this:

def load_current_resource
Chef::Log.debug("Checking for smbuser #{new_resource.name}")
u = shell_out("pdbedit -Lv -u #{new_resource.name}")
exists = u.stdout.include?(new_resource.name)
disabled = u.stdout.include?("Account Flags.*[D")
new_resource.exists(exists)
new_resource.disabled(disabled)
end

Does anyone see a problem with this?

Thanks,
Kevin Christen

On Sat, Dec 1, 2012 at 9:00 AM, Kevin Christen kevin.christen@gmail.comwrote:

Joshua,

Thanks for providing these examples; I learned a few things from them. One
question for you or anyone else on the list though. This line from the
samba provider's load_current_resource method confuses me:

@smbuser = Chef::Resource::SambaUser.new(new_resource.name)

I assume that Chef::Resource::SambaUser is the class generated from the
LWRP resource. So isn't new_resource already an instance of that class?

Thanks,
Kevin Christen

On Fri, Nov 30, 2012 at 11:32 AM, Joshua Timberman joshua@opscode.comwrote:

Ohai,

On 11/30/12 9:31 AM, "Maven User" maven.2.user@gmail.com wrote:

I'm still on my quest to simplify our deployments and was wondering if
there is a good demo/example/sample I can look at to create my own LWRP?

I can see how to create the resource, but the provider bit is kinda
confusing.

First, LWRPs are specifically the lightweight DSL that doesn't require the
ruby class structure. "Heavyweight" resources/providers are like the ones
that come with Chef itself. The "database" cookbook implements its
resources/providers as heavyweight due the way that the LWRPs are
automatically named by the cookbook and the filenames, and we wanted to
name

Second, the key is to make your resources idempotent. This is easy if
you're using resources inside the action blocks in the provider, and the
examples (below) have some of that. In some cases, like when you're
chaining together a bunch of command execution, you'll want to use
"load_current_resource" to check the current state of the system.

There are more advanced considerations, such as why-run support, and
handling updated resources, but for now stick to a simple use case.

These LWRPs from Opscode's cookbooks are relatively simple examples:

https://github.com/opscode-cookbooks/cron/blob/master/providers/d.rb

https://github.com/opscode-cookbooks/samba/blob/master/providers/user.rb

https://github.com/opscode-cookbooks/daemontools/blob/master/providers/serv
ice.rb

As an exercise, I'd probably start by doing an LWRP that simply handles
dropping off a "conf.d" style of configuration file.

Hope this helps!

--
Opscode, Inc
Joshua Timberman, Technical Community Manager
IRC, Skype, Twitter, Github: jtimberman

Hello all,

I'm starting to implement my own LWRPs as well, and in addition to reading
existing implementations as suggested by Joshua, I found the Wiki page to
be quite helpful in understanding what they were, and how they contrast
with "heavy-weight" resources and providers:
http://wiki.opscode.com/display/chef/Lightweight+Resources+and+Providers+(LWRP)
It was officially moved to the Chef Docs just 3 weeks ago, so I'm guessing
the code examples of the Wiki (which are absent from Chef Docs) should
remain valid for a little while.

Best,

Loïc

On Fri, Nov 30, 2012 at 6:32 PM, Joshua Timberman joshua@opscode.comwrote:

Ohai,

On 11/30/12 9:31 AM, "Maven User" maven.2.user@gmail.com wrote:

I'm still on my quest to simplify our deployments and was wondering if
there is a good demo/example/sample I can look at to create my own LWRP?

I can see how to create the resource, but the provider bit is kinda
confusing.

First, LWRPs are specifically the lightweight DSL that doesn't require the
ruby class structure. "Heavyweight" resources/providers are like the ones
that come with Chef itself. The "database" cookbook implements its
resources/providers as heavyweight due the way that the LWRPs are
automatically named by the cookbook and the filenames, and we wanted to
name

Second, the key is to make your resources idempotent. This is easy if
you're using resources inside the action blocks in the provider, and the
examples (below) have some of that. In some cases, like when you're
chaining together a bunch of command execution, you'll want to use
"load_current_resource" to check the current state of the system.

There are more advanced considerations, such as why-run support, and
handling updated resources, but for now stick to a simple use case.

These LWRPs from Opscode's cookbooks are relatively simple examples:

https://github.com/opscode-cookbooks/cron/blob/master/providers/d.rb

https://github.com/opscode-cookbooks/samba/blob/master/providers/user.rb

https://github.com/opscode-cookbooks/daemontools/blob/master/providers/serv
ice.rb

As an exercise, I'd probably start by doing an LWRP that simply handles
dropping off a "conf.d" style of configuration file.

Hope this helps!

--
Opscode, Inc
Joshua Timberman, Technical Community Manager
IRC, Skype, Twitter, Github: jtimberman

--
Loïc ANTOINE-GOMBEAUD
IT Engineer I
Plinga GmbH | www.plinga.com
Saarbrücker Straße 20/21 | 10405 Berlin | Germany

E-Mail: loic.gombeaud@plinga.com | Skype:loic.plinga
Telefon : +49(0)160-92286573

Geschäftsführer: Johannes Kreibohm, Florian Schmidt-Amelung
Eingetragen beim Amtsgericht Charlottenburg, HRB 119994