LWRP load_current_resourse questions


#1

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.


#2

Please see the answers to your questions below.

Regards,
Tom

On January 31, 2014 at 2:14:52 AM, anikeev1988@yandex.ru (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.