0

我使用 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资源定义。但我从未将其视为任何文档中的要求。从文档中,我假设 Chefattr_accessor在资源/提供者编译期间负责设置任何属性。谁能确认我的发现或解释到底发生了什么?

谢谢。

4

1 回答 1

1
def load_current_resource
  @current_resource = Chef::Resource::Foo.new @new_resource.name
  @current_resource.name = @new_resource.name
end

您的问题在这里,name应该是不变的(用于匹配当前和新资源),因为它识别资源,您不应该尝试设置@current_resource.name.

删除此行,没有访问器应该没问题。

于 2015-04-24T13:23:23.403 回答