LWRP: How to set attribute default to another attribute


#1

Ohai Chefs,

Finally settled in today and wrote my first LWRP. Lamenting not getting off my arse and doing this earlier as it’s pretty simple and super powerful! Anyway, running into minor issue and could use some guidance. Within my resource I tried and failed at setting defaults of some attributes to another attribute in resources.rb, like so:

resources/instance.rb

actions :create, :delete

attribute :instance_name, :name_attribute => true, :kind_of => String, :required => true
attribute :project_name, :kind_of => String, :required => true
attribute :webdav_host, :kind_of => String, :required => true
attribute :webdav_path, :kind_of => String, :default => "/files/#{@project_name}"
attribute :db_host, :kind_of => String, :required => true
attribute :db_name, :kind_of => String, :default => @project_name

I wasn’t able to figure out how to “get to” @project_name from within resources.rb so I could set it as a default for some other attributes. I tried a number of different ways and all gave me nil. Is this possible and/or advisable? Curious how the cool kids handle this one. Thanks much.

::toddmichael


#2

I don’t think it’s possible to access the project_name there, which
would be nil during the resource definition phase anyway, creating
something like “/files/”. You’re actually wanting to compute that
value at recipe compile (or converge) time – I’d suggest the latter.

You can use the ‘load_current_resource’ method of the provider to set
the current state, and if necessary, compute dynamic resource
parameters. I’d suggest defaulting this particular resource parameter
to an empty path.

Here is an example of l_c_r in a provider, although this one is in a
library, it is usable from the LWRP DSL as well:

Cheers,

AJ

On 22 April 2013 18:19, Todd Michael Bushnell toddmichael@gmail.com wrote:

Ohai Chefs,

Finally settled in today and wrote my first LWRP. Lamenting not getting off
my arse and doing this earlier as it’s pretty simple and super powerful!
Anyway, running into minor issue and could use some guidance. Within my
resource I tried and failed at setting defaults of some attributes to
another attribute in resources.rb, like so:

resources/instance.rb

actions :create, :delete

attribute :instance_name, :name_attribute => true, :kind_of => String,
:required => true
attribute :project_name, :kind_of => String, :required => true
attribute :webdav_host, :kind_of => String, :required => true
attribute :webdav_path, :kind_of => String, :default =>
"/files/#{@project_name}"
attribute :db_host, :kind_of => String, :required => true
attribute :db_name, :kind_of => String, :default => @project_name

I wasn’t able to figure out how to “get to” @project_name from within
resources.rb so I could set it as a default for some other attributes. I
tried a number of different ways and all gave me nil. Is this possible
and/or advisable? Curious how the cool kids handle this one. Thanks much.

::toddmichael