How to use Chef DSL inside LWRP's action

150 views Asked by At

I have LWRP which should create cookbook_file inside its create action

resource_name :vuy
property :vu_name, String, name_property :true
actions :create

action :create do
  log "vuy:@new_resource.vu_name-#{@new_resource.vu_name}"
  cookbook_file "c:/temp/test.xml" do
    source "#{@new_resource.vu_name}"
  end
end

And test recipe

vuy 'text.txt'

chef-client execution fails with error NoMethodError: undefined method 'vu_name' for nil:NilClass

When I remove cookbook_file from create method, log correctly shows: INFO: vu:@new_resource.vu_name-text.txt In next step, I replaced

    source "#{@new_resource.vu_name}"

with same value as specified in test recipe

    source "text.txt"

and file was fetched. To me, it looks that cookbook_file inner ruby block does not get copy of new_resource and it becomes nil.

How to use properties of LWRP as arguments of resources declared inside action?

1

There are 1 answers

0
coderanger On

That should be new_resource.vu_name, i.e. without the @. With the at sign it's a local variable lookup, without it is a method call. Chef automatically promotes the method call so that even inside the cookbook_file you get the right thing, but it cannot do this with an instance variable lookup.