简体   繁体   English

厨师LW资源属性默认值如何引用另一个属性?

[英]How can a chef LW resource attribute default value refer to another attribute?

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. 我正在为我正在构建的tomcat cookbook定义一个具有以下定义的资源。 I want to have "name" and "service_name" attributes that can be set independently. 我想拥有可以独立设置的“name”和“service_name”属性。 When service name is not set, I want it to default to whatever is provided for "name." 如果未设置服务名称,我希望它默认为“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. 注意第二行末尾的“:default =>:name”。 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. 除非明确设置,否则我希望service_name为“foo_bar”。

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? 有没有办法做我想做的事情?

Since those are class-level methods, you need to use the lazy attribute: 由于这些是类级方法,您需要使用lazy属性:

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... 那是默认的......

I was unable to use Seth's "lazy" evaluation, but was able to simulate by creating a dynamic accessor method. 我无法使用Seth的“懒惰”评估,但能够通过创建动态访问器方法进行模拟。

This other post was useful: How to implement a dynamic attribute default in chef LWRP definition 这篇文章非常有用: 如何在chef LWRP定义中实现动态属性默认值

First, the definition in my resource definition file: 首先,我的资源定义文件中的定义:

attribute :service_name,        :kind_of => String, default: nil

Next, the accessor block at the bottom of the same resource definition file: 接下来,同一资源定义文件底部的访问者块:

def service_name( arg=nil )
  if arg.nil? and @service_name.nil?
    set_or_return( :service_name, @name, :kind_of => String)
  else
    set_or_return( :service_name, arg, :kind_of => String )
  end
end

Which effectively sets the value of "service_name" the first time it is used in my provider code. 这是第一次在我的提供者代码中使用时有效设置“service_name”的值。

This works for all combinations of 这适用于所有组合

resource "title" do
  # name defaults to "title"
  # service_name defaults to "title"
end
resource "title" do
  name "my_name"
  # service_name defaults to "my_name"
end
resource "title" do
  name "my_name"
  service_name "my_service_name"
end

Thanks, again, for your help. 再次感谢你的帮助。 I hope someone else finds this useful in the future. 我希望其他人在将来发现这有用。

Rich 丰富

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM