Custom LWRP Un-initialized Constant error

Good Afternoon/evening/morning,
I’m having a little trouble writing a custom LWRP. It’s pretty simple as I’m trying to get my head round how @current_resource works.
Here’s the code followed by the error; any ideas what I’m doing wrong? (this LWRP is contained in an library cookbook called AirWatch)

Resource:
#test lwrp
actions :create, :remove
default_action :create

#attributes to use such as path
attribute :path, :kind_of => String, :default => "c:\"
attribute :filename, :kind_of => String, :name_attribute => true

#attr_accessor :exists

Provider:
#test lwrp

#define actions
action :create do
if @current_resource.exists(true)
Chef::Log.info "#{ @new_resource } already exists, nowt to do chief"
else
resource current_resource.filename do
#copy_file
::File.new(@current_resource)
Chef::Log.info "#{ @new_resource } created"
end
end
end

action :delete do
if @current_resource.exists(true)
#delete_file
::File.delete(@current_resource)
Chef::Log.info "#{ @new_resource } has been removed"
else
Chef::Log.info "#{ @new_resource } not found, nowt to do"
end
end

#define current_resource
def load_current_resource
@current_resource = Chef::Resource::AirWatchchfile.new(new_resource)
@current_resource.path(new_resource.path)
@current_resource.filename(new_resource.filename)
@current_resource

if (::File.exist?(@current_resource)) then
Chef::Log.info “#{ @current_resource } has been found”
@current_resource.exists = true
else
Chef::Log.info “#{ @current_resource } has not been found”
@current_resource.exists = false
end
end

Recipe calling it:

include_recipe 'chef_handler’
include_recipe 'windows’
include_recipe ‘AirWatch’

#map to managment server and copy AirWatch installer to upgrade to

AirWatch_chfile ‘chtest.txt’ do
action :create
end

#perform some post upgrade tests - check port etc

#done

Error:
[cid:image001.png@01D03B1F.BF1B0B70]

Any help greatly appreaciated.
Thanks

Chris

On Wednesday, January 28, 2015 at 9:27 AM, ChristopherHall@air-watch.com wrote:

Good Afternoon/evening/morning,
I’m having a little trouble writing a custom LWRP. It’s pretty simple as I’m trying to get my head round how @current_resource works.
Here’s the code followed by the error; any ideas what I’m doing wrong? (this LWRP is contained in an library cookbook called AirWatch)

You’re really close. What’s going on here is that Chef prepends the cookbook name to the LWRP name to generate the class name for the resource. E.g., in the java cookbook there is an ark LWRP, which becomes ‘JavaArk’. Based on your mail, it looks like your resource is in air_watch/resources/test.rb ? In that case it should be AirWatchTest.

HTH,

--
Daniel DeLeo

Hi Daniel,
Thanks for that, but it's still not working.
The library cookbook that this LWRP is declared in is called AirWatch. The provider/resource file are both called chfile.rb (and located in their respective directories).
The cookbook calling the lwrp is called awcm_upgrade and is including the AirWatch cookbook.
If I use Chef::Resource::AirWatchChfile.new I'm still getting an error "uninitialized constant" when executing the awcm_upgrade on a test node.
Am I calling it correctly from the awcm_upgrade cookbook?

AirWatch_Chfile 'chtest.txt' do
action :create
end

Thanks

-----Original Message-----
From: Daniel DeLeo [mailto:ddeleo@kallistec.com] On Behalf Of Daniel DeLeo
Sent: 28 January 2015 17:35
To: chef@lists.opscode.com
Subject: [chef] Re: Custom LWRP Un-initialized Constant error

On Wednesday, January 28, 2015 at 9:27 AM, ChristopherHall@air-watch.com wrote:

Good Afternoon/evening/morning,
I’m having a little trouble writing a custom LWRP. It’s pretty simple as I’m trying to get my head round how @current_resource works.
Here’s the code followed by the error; any ideas what I’m doing wrong? (this LWRP is contained in an library cookbook called AirWatch)

You’re really close. What’s going on here is that Chef prepends the cookbook name to the LWRP name to generate the class name for the resource. E.g., in the java cookbook there is an ark LWRP, which becomes ‘JavaArk’. Based on your mail, it looks like your resource is in air_watch/resources/test.rb ? In that case it should be AirWatchTest.

HTH,

--
Daniel DeLeo

On Wednesday, January 28, 2015 at 10:01 AM, ChristopherHall@air-watch.com wrote:

Hi Daniel,
Thanks for that, but it's still not working.
The library cookbook that this LWRP is declared in is called AirWatch. The provider/resource file are both called chfile.rb (and located in their respective directories).
The cookbook calling the lwrp is called awcm_upgrade and is including the AirWatch cookbook.
If I use Chef::Resource::AirWatchChfile.new I'm still getting an error "uninitialized constant" when executing the awcm_upgrade on a test node.
Am I calling it correctly from the awcm_upgrade cookbook?

AirWatch_Chfile 'chtest.txt' do
action :create
end

This should be snake-cased and all lower case. In Ruby, method names (declaring a resource in a recipe is a method call on a Recipe object) generally need to be lower case (there are exceptions, but they’re for weirdos who like confusing code :stuck_out_tongue: ). Upper case is for constants (which is what class and module names really are). So you want to use air_watch_chfile.

Thanks
HTH,

--
Daniel DeLeo

Thanks very much for the help Daniel, I'm off and running now :slight_smile:

-----Original Message-----
From: Daniel DeLeo [mailto:ddeleo@kallistec.com] On Behalf Of Daniel DeLeo
Sent: 28 January 2015 19:42
To: chef@lists.opscode.com
Subject: [chef] Re: RE: Re: Custom LWRP Un-initialized Constant error

On Wednesday, January 28, 2015 at 10:01 AM, ChristopherHall@air-watch.com wrote:

Hi Daniel,
Thanks for that, but it's still not working.
The library cookbook that this LWRP is declared in is called AirWatch. The provider/resource file are both called chfile.rb (and located in their respective directories).
The cookbook calling the lwrp is called awcm_upgrade and is including the AirWatch cookbook.
If I use Chef::Resource::AirWatchChfile.new I'm still getting an error "uninitialized constant" when executing the awcm_upgrade on a test node.
Am I calling it correctly from the awcm_upgrade cookbook?

AirWatch_Chfile 'chtest.txt' do
action :create
end

This should be snake-cased and all lower case. In Ruby, method names (declaring a resource in a recipe is a method call on a Recipe object) generally need to be lower case (there are exceptions, but they’re for weirdos who like confusing code :stuck_out_tongue: ). Upper case is for constants (which is what class and module names really are). So you want to use air_watch_chfile.

Thanks
HTH,

--
Daniel DeLeo