On Thursday, February 19, 2015 at 6:36 PM, Medya wrote:
Thanks for the reply Daniel,
but cant I have the same effect with normal attributes?
like the case of the package, if I run action install on a package, I can change the a normal attribute to the version of the installed version ? why do I have to declare it as state attribute?
State attributes are just an annotation. For example if you look at the package resource here: https://github.com/chef/chef/blob/master/lib/chef/resource/package.rb
We define the ‘version’ attribute (the long way, with a method definition) here: https://github.com/chef/chef/blob/a7f5c92960aedf8d5bfc71abbce430ab075e016a/lib/chef/resource/package.rb#L53
But we also have to specify that it’s a state attribute here: https://github.com/chef/chef/blob/a7f5c92960aedf8d5bfc71abbce430ab075e016a/lib/chef/resource/package.rb#L28
What happens behind the scenes is that the resource class (in this case Chef::Resource::Package) just keeps an array of the things we told it are ‘state attributes.’
In a LWRP, you’d have to use
attribute to define the attribute method and also call
state_attrs to add the attribute to the array.
For example: https://github.com/opscode-cookbooks/aws/blob/master/resources/ebs_volume.rb
and another question ,
how can I access a state attribute ? can I access them through node objects ? are they stored on the chef server or on the client side ?
Hopefully it’s clear from the above that a ’state attribute’ is just a regular attribute that happens to have its name in an array called “state_attrs.”
As I noted in the previous email, the client side reporting code loops over the state attributes to convert a resource to a hash, keeping only the data relevant to changes chef made to the system. If you have the reporting add-on installed, the reporting server will store it so you can keep track of every change made by chef to any system it manages.
More about reporting/analytics here: http://docs.chef.io/server/reporting.html
As I noted, the server side component is a premium feature, though you can install it for free for up to 25 nodes (or use the free trial of hosted chef to play around with it), though there are open source tools that use Chef’s client side reporting code to store data to custom server/data store.