Reputation: 2929
I've forked the chocolatey-cookbook in an attempt to add the ability to set which user you want to execute the install scripts with.
So, I added to resources/default.rb
:
attribute :user, :kind_of => String
and I added to providers/default.rb
:
@current_resource.user = (@new_resource.user)
to load_current_Resource
function so I can then just do
user @current_resource.user if @current_resource.user
in the execute blocks it uses to actually install a package.
I thought that would work instead all I'm getting is undefined method user=
for Chef::Resource::Chocolatey`.
So, what am I doing wrong? I've looked but I can't find anything definitive on how resource attributes are declared and used within the provider and I'm kinda stumped.
EDIT: I added :user
to the attr_accessor
in the resource file but that just gave a different error: wrong number of arguments (1 for 0)
EDIT:
Ok, I don't know why but for some reason @current_resource
wasn't being returned from the load_current_resource
function; I worked that out when I was able to get vagrant to do a full stack trace and realised it was erroring where I was setting the user on the execute block for the install action of the chocolatey provider.
All I did was ensure that it was returned by making sure the last line was @current_resource
. and now it works, I mean sure it errored but for an entirely different reason.
ok, so that didn't fix it, I suddenly got undefined method 'user' for nil:NilClass
error again.
EDIT:
After logging I found that @current_resource.user
is not nil
when I log it in load_current_resource
but it I think it errors before it gets to the execute script that will run the chocolaty installation of the package.
How is the provider class built? and does @current_resource
exist in only certain instances?
EDIT:
Ok, so just to see what would happen I changed @current_resource.user
to @current_resource.instance_variable_get(:@user)
and it worked, no undefined method 'user' for nil:NilClass
error. Whats going on?
Why does @current_resource.instance_variable_get(:@user)
work but not @current_resource.user
?
Upvotes: 0
Views: 81
Reputation: 54181
So Chef's resource attribute don't work like Ruby's accessors (and you shouldn't mix them). The way to do the set in the provider is @current_resource.user(@new_resource.user)
. This is because the attribute methods are really optimized for use in the recipe DSL blocks (where you would have something like user 'foo'
).
Upvotes: 1