Why does the LWRP custom definition gives me undefined method for nil:NilClass

喜你入骨 提交于 2019-12-05 11:55:34

Ahh so close! Your load_current_resource method isn't quite right. You need to set the instance_variable, but you've only set a local variable:

def load_current_resource
  @current_resource = Chef::Resource::AppCookbookUserPostgresql.new(new_resource.name)

  begin
    @current_resource.name(new_resource.name)
    @current_resource.username(new_resource.username)
    @current_resource.password(new_resource.password)
    @current_resource.database_name(new_resource.database_name)
    @current_resource
  rescue
    Chef::Log.debug("Cannot find #{new_resource} in the swarm")
  end
end

I've also moved the initial creation outside of the begin block. You still want to create an empty resource, even if something fails. That being said, that rescue block is entirely unnecessary. It should just be:

def load_current_resource
  @current_resource = Chef::Resource::AppCookbookUserPostgresql.new(new_resource.name)
  @current_resource.username(new_resource.username)
  @current_resource.password(new_resource.password)
  @current_resource.database_name(new_resource.database_name)
  @current_resource
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!