Chef LWRP and new_resource.updated_by_last_action and use_inline_resources

I think that I am almost at the understanding that I need in regards to
LWRPs and notifications and use_inline_resources - I just want to verify
that I truly get this subject given what I think is it's importance and the
somewhat fuzzy docs that seems to exist around it.

I write a good number of LWRPs and originally used the below general
paradigm as I learned from the customizing chef o'reilly book.

def whyrun_supported?
true
end

action :create do

_user = new_resource.user ? new_resource.user : "root"
_group = new_resource.group ? new_resource.group : "root"

directory ::File.dirname(new_resource.name) do
owner _user
group _group
mode "0700"
action :create
recursive true
end

new_resource.updated_by_last_action(true)
end

I realized soon that there is a a serious flaw to this original paradigm
which is that by specifying new_resource.updated_by_last_action(true) this
resource always thinks that it does something and if I happen to perform
any notification based on it - they will always fire. (Took a a bit of time
to figure out why a service was restarting on every Chef run)

I use Chef 11 all around so I realized that I could change my overall
paradigm to

def whyrun_supported?
true
end

use_inline_resources

action :create do

_user = new_resource.user ? new_resource.user : "root"
_group = new_resource.group ? new_resource.group : "root"

directory ::File.dirname(new_resource.name) do
owner _user
group _group
mode "0700"
action :create
recursive true
end
end

Now I get the behavior that I want which is that when the underlying
resource actually triggers then the LWRP will send notifies rather than all
the time. I realize that the use of use_inline_resources means that I can
only notify other resources within my LWRP - but that is fine by me.

MY FIRST REAL QUESTION: If I happen to only use already existing resources
in my then I never have call new_resource.updated_by_last_action(true). Is
this accurate?

SECOND QUESTION: If I need to call new_resource.updated_by_last_action(true)
because I do something outside and existing resource -- can I mix
use_inline_resources and new_resource.updated_by_last_action(true) in the
same provider?

THIRD QUESTION: I have seen the below code which I know is "old" but I
think should work but does not seem to work. Is this paradigm now longer
valid? Even when the resource creates the directory it seems
that d.updated_by_last_action? always evaluates to false

def whyrun_supported?
true
end

action :create do

_user = new_resource.user ? new_resource.user : "root"
_group = new_resource.group ? new_resource.group : "root"

d = directory ::File.dirname(new_resource.name) do
owner _user
group _group
mode "0700"
action :create
recursive true
end

new_resource.updated_by_last_action(d.updated_by_last_action?)
end

Thanks for listening.

Q1:
yeah, assuming you have use_inline_resource in place. if you your lwrp
provider are purely based of chef core resources they you are good. in fact
i have proposed a composite resource based on this[1]
Q2:
yes, if you are altering the system state using something other that chef
resources then yes, in fact you’ll do the checks (which allow you to decide
whether to mutate/change the system or not) will be outside provider and
the actual change logic will be inside converge_by block. that will
automatically update the resource as well iirc. that how we roll our own
hwrp/lwrp
Q3: it should work, but i think you have to add d.run_action(:create)
before passing it to updated_by_last_action

hth
[1]https://github.com/chef/chef/pull/3584

On Tue, Jun 30, 2015 at 5:37 PM, Mark Selby mselby@thenextbigsound.com
wrote:

http://stackoverflow.com/questions/25964635/chef-notify-global-service-from-inside-of-lwrp-provider-code

I think that I am almost at the understanding that I need in regards to
LWRPs and notifications and use_inline_resources - I just want to verify
that I truly get this subject given what I think is it’s importance and the
somewhat fuzzy docs that seems to exist around it.

I write a good number of LWRPs and originally used the below general
paradigm as I learned from the customizing chef o’reilly book.

def whyrun_supported?
true
end

action :create do

_user = new_resource.user ? new_resource.user : “root”
_group = new_resource.group ? new_resource.group : “root”

directory ::File.dirname(new_resource.name) do
owner _user
group _group
mode "0700"
action :create
recursive true
end

new_resource.updated_by_last_action(true)
end

I realized soon that there is a a serious flaw to this original paradigm
which is that by specifying new_resource.updated_by_last_action(true) this
resource always thinks that it does something and if I happen to perform
any notification based on it - they will always fire. (Took a a bit of time
to figure out why a service was restarting on every Chef run)

I use Chef 11 all around so I realized that I could change my overall
paradigm to

def whyrun_supported?
true
end

use_inline_resources

action :create do

_user = new_resource.user ? new_resource.user : “root”
_group = new_resource.group ? new_resource.group : “root”

directory ::File.dirname(new_resource.name) do
owner _user
group _group
mode "0700"
action :create
recursive true
end
end

Now I get the behavior that I want which is that when the underlying
resource actually triggers then the LWRP will send notifies rather than all
the time. I realize that the use of use_inline_resources means that I can
only notify other resources within my LWRP - but that is fine by me.

MY FIRST REAL QUESTION: If I happen to only use already existing resources
in my then I never have call new_resource.updated_by_last_action(true). Is
this accurate?

SECOND QUESTION: If I need to call new_resource.updated_by_last_action(true)
because I do something outside and existing resource – can I mix
use_inline_resources and new_resource.updated_by_last_action(true) in the
same provider?

THIRD QUESTION: I have seen the below code which I know is “old” but I
think should work but does not seem to work. Is this paradigm now longer
valid? Even when the resource creates the directory it seems
that d.updated_by_last_action? always evaluates to false

def whyrun_supported?
true
end

action :create do

_user = new_resource.user ? new_resource.user : “root”
_group = new_resource.group ? new_resource.group : “root”

d = directory ::File.dirname(new_resource.name) do
owner _user
group _group
mode "0700"
action :create
recursive true
end

new_resource.updated_by_last_action(d.updated_by_last_action?)
end

Thanks for listening.