How can a chef LW resource attribute default value refer to another attribute?
chef-infra, lwrp, ruby
Solution
Since those are class-level methods, you need to use the `lazy` attribute:
attribute :service_name, kind_of: String, default: lazy { |r| r.name }
It's also worth noting that:
attribute :name, kind_of: String, required: true, name_attribute: true
is entirely redundant. That's the default...
Problem
I'm trying to set the default value of one resource attribute to the value of another attribute. I'm defining a resource with the following definitions in it for a tomcat cookbook I'm building. I want to have "name" and "service_name" attributes that can be set independently. When service name is not set, I want it to default to whatever is provided for "name." The following does not work as I would expect: ``` attribute :name, :kind_of => String, :required => true, :name_attribute => true attribute :service_name, :kind_of => String, :default => :name ``` Notice the ":default => :name" at the end of the second line. When I refer to my resource in a new block in a recipe as such ``` my_tomcat "install tomcat" do name "foo_bar" end ``` The attribute values get assigned as ``` name = "foo_bar" service_name = "name" ``` which is not what I expected. I wanted service_name to be "foo_bar" unless it was explicitly set. I've tried ``` attribute :service_name, :kind_of => String, :default => new_resource.name attribute :service_name, :kind_of => String, :default => @new_resource.name ``` but those don't compile. Is there a way to do what I'm trying to do?