chef lwrp 的资源定义是否需要 attr_accessor?
Is attr_accessor required in resource definition of chef lwrp?
我使用 chefdk 创建了一个 LWRP,并严格按照文档和几篇博文进行操作。
资源
actions :install
default_action :install
provides :foo
attribute :name, :kind_of => String, :name_attribute => true
提供商
provides :foo
def whyrun_supported?
true
end
use_inline_resources
action :install do
converge_by("install: #{@new_resource}") do
execute "installation of: #{@new_resource.name}" do
command "foo install #{@new_resource.name}"
end
end
end
def load_current_resource
@current_resource = Chef::Resource::Foo.new @new_resource.name
@current_resource.name = @new_resource.name
end
在食谱中使用此 LWRP 时,我会收到以下错误:
undefined method `name=' for Chef::Resource::Foo
我唯一可以修复它的方法是将 attr_accessor :name
添加到资源定义中。但我从未将此视为任何文档中的要求。根据文档,我假设 Chef 在 resource/provider 编译期间负责设置任何属性的 attr_accessor
。谁能确认我发现了什么或解释一下到底发生了什么?
谢谢。
def load_current_resource
@current_resource = Chef::Resource::Foo.new @new_resource.name
@current_resource.name = @new_resource.name
end
你的问题在这里,name
应该是不变的(为了匹配当前和新的资源),因为它标识了资源,你不应该尝试设置 @current_resource.name
。
把这行去掉,不用accessor应该就可以了。
我使用 chefdk 创建了一个 LWRP,并严格按照文档和几篇博文进行操作。
资源
actions :install
default_action :install
provides :foo
attribute :name, :kind_of => String, :name_attribute => true
提供商
provides :foo
def whyrun_supported?
true
end
use_inline_resources
action :install do
converge_by("install: #{@new_resource}") do
execute "installation of: #{@new_resource.name}" do
command "foo install #{@new_resource.name}"
end
end
end
def load_current_resource
@current_resource = Chef::Resource::Foo.new @new_resource.name
@current_resource.name = @new_resource.name
end
在食谱中使用此 LWRP 时,我会收到以下错误:
undefined method `name=' for Chef::Resource::Foo
我唯一可以修复它的方法是将 attr_accessor :name
添加到资源定义中。但我从未将此视为任何文档中的要求。根据文档,我假设 Chef 在 resource/provider 编译期间负责设置任何属性的 attr_accessor
。谁能确认我发现了什么或解释一下到底发生了什么?
谢谢。
def load_current_resource
@current_resource = Chef::Resource::Foo.new @new_resource.name
@current_resource.name = @new_resource.name
end
你的问题在这里,name
应该是不变的(为了匹配当前和新的资源),因为它标识了资源,你不应该尝试设置 @current_resource.name
。
把这行去掉,不用accessor应该就可以了。