# LWRP load\_current\_resourse questions

**URL:** <https://discourse.chef.io/t/lwrp-load-current-resourse-questions/4958>\
**Category:** Chef Infra (archive)\
**Created:** [January 31, 2014, 8:14am UTC](https://discourse.chef.io/t/lwrp-load-current-resourse-questions/4958 "2014-01-31T08:14:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anikeev1988](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Anikeev1988](https://discourse.chef.io/u/Anikeev1988)\
**Post date:** [January 31, 2014, 8:14am UTC](https://discourse.chef.io/t/lwrp-load-current-resourse-questions/4958/1 "2014-01-31T08:14:38Z")

</div>

Greetings!

In the LWRP Chef docs regarding provider DSL, i saw load\_current\_resourse  
method, which i can’t fully understand.  
I would like to terminate that lack of knowledge and i would appreciate any  
help in this.

It’s description from official docs :  
The load\_current\_resource method is used to find a resource on a node based on  
a collection of attributes. These attributes are defined in a lightweight  
resource and are loaded by the chef-client when processing a recipe during a  
chef-client run. This method will ask the chef-client to look on the node to  
see if a resource exists with specific matching attributes.

But what it actually does when it finds matching resource on a node or can’t  
find it?

My guess :  
load\_current\_resourse is processed by provider when it compiled new\_resource  
object it simply just tries to find  
installed resource on a node matching compiled object? If it can find it it, it  
does nothing, otherwise it installs compiled object?

I’m not quite sure how it process load\_current\_resourse when i saw this 2  
examples from chef repository.

Code below tells us to install new\_resource every time?  
#ruby\_block provider  
def load\_current\_resource  
true  
end

Code below tells us to install new\_resourse if it has different path attribute?  
#directory provider  
def load\_current\_resource  
@current\_resource = Chef::Resource::Directory.new(@new\_resource.name)  
@current\_resource.path(@new\_resource.path)  
if ::File.exists?(@current\_resource.path) && @action !=  
:create\_if\_missing  
load\_resource\_attributes\_from\_file(@current\_resource)  
end  
@current\_resource  
end

Main confusion i experience when i’m analyzing those 2 strings of code :  
@current\_resource =  
Chef::Resource::SomeUndeclaredClass.new(@new\_resource.name) # It creates object  
of class “SomeUndeclaredClass” passing to it new\_resource.name attribute  
and assigns object to current\_resourse instance variable? Also as i understood  
when initializing new resource class i can use any name instead of  
SomeUndeclaredClass place?

```
@current_resource.path(@new_resource.path) # it just assigns

```

new\_resourse.path to current\_resourse.path attribute?

And if load\_current\_resource method successfully found matching resource on a  
node it does nothing,otherwise it installes new compiled resource?

That is a list of questions i would like to resolve and i would appreciate any  
help with that.

Best Regards.

---

<div class="post-metadata">

**Author:** ![tomduffield\_getchef](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/tomduffield_getchef/32/218_2.png) [@tomduffield\_getchef](https://discourse.chef.io/u/tomduffield_getchef)\
**Post date:** [January 31, 2014, 4:39pm UTC](https://discourse.chef.io/t/lwrp-load-current-resourse-questions/4958/2 "2014-01-31T16:39:22Z")

</div>

Please see the answers to your questions below.

Regards,  
Tom

On January 31, 2014 at 2:14:52 AM, [anikeev1988@yandex.ru](mailto:anikeev1988@yandex.ru) ([anikeev1988@yandex.ru](mailto:anikeev1988@yandex.ru)) wrote:

Greetings!

In the LWRP Chef docs regarding provider DSL, i saw load\_current\_resourse  
method, which i can’t fully understand.  
I would like to terminate that lack of knowledge and i would appreciate any  
help in this.

It’s description from official docs :  
The load\_current\_resource method is used to find a resource on a node based on  
a collection of attributes. These attributes are defined in a lightweight  
resource and are loaded by the chef-client when processing a recipe during a  
chef-client run. This method will ask the chef-client to look on the node to  
see if a resource exists with specific matching attributes.

But what it actually does when it finds matching resource on a node or can’t  
find it?

My guess :  
load\_current\_resourse is processed by provider when it compiled new\_resource  
object it simply just tries to find  
installed resource on a node matching compiled object? If it can find it it, it  
does nothing, otherwise it installs compiled object?  
Close. load\_current\_resource builds a current\_resource object by inspecting the current state of the system. If new\_resource is different than current\_resource then chef-client takes action and enforces the state declared in new\_resource. Otherwise it does nothing.

I’m not quite sure how it process load\_current\_resourse when i saw this 2  
examples from chef repository.

Code below tells us to install new\_resource every time?  
#ruby\_block provider  
def load\_current\_resource  
true  
end  
Basically yes. In the case of the ruby\_block provider you don’t really have an existing resource to inspect so the code is basically relying on the resource guards to determine whether or not is should run.

Code below tells us to install new\_resourse if it has different path attribute?  
#directory provider  
def load\_current\_resource  
@current\_resource = Chef::Resource::Directory.new(@new\_resource.name)  
@current\_resource.path(@new\_resource.path)  
if ::File.exists?(@current\_resource.path) && @action !=  
:create\_if\_missing  
load\_resource\_attributes\_from\_file(@current\_resource)  
end  
@current\_resource  
end  
That is one aspect, yes. But in reality the load\_resource\_attributes\_from\_file is also playing a part in deciding if the new\_resource should be enforced.

Main confusion i experience when i’m analyzing those 2 strings of code :  
@current\_resource =  
Chef::Resource::SomeUndeclaredClass.new(@new\_resource.name) # It creates object  
of class “SomeUndeclaredClass” passing to it new\_resource.name attribute  
and assigns object to current\_resourse instance variable? Also as i understood

when initializing new resource class i can use any name instead of  
SomeUndeclaredClass place?  
Yes, this is the point where you are creating the current\_resource object I mentioned above so that it can compare it to new\_resource.

Each chef resource will have an associated Ruby class with it. You will want to replace SomeUndeclaredClass with that class.

@current\_resource.path(@new\_resource.path) # it just assigns  
new\_resourse.path to current\_resourse.path attribute?  
Yes.

And if load\_current\_resource method successfully found matching resource on a  
node it does nothing,otherwise it installes new compiled resource?  
Yes.

That is a list of questions i would like to resolve and i would appreciate any  
help with that.

Best Regards.
