jcarapet
jcarapet

Reputation: 15

Chef library helper method is undefined

I am trying to write an LWRP where based on conditionals it will call methods from a helper library. I am having problem with the syntax in getting the provider to read the external method.

The provider is pretty straightforward

# providers/provider.rb
require_relative '../libraries/acx_shared'
include Acx

action :create  do
  Chef::Log.debug('{jefflibrary->lwrp->user} - start')

if @new_resource.shared == true
  Acx::User::Shared.true_shared()
else
  Acx::User::Shared.false_shared()
end

if @new_resource.sudo == true
  Chef::Log.error('I HAVE THE POWER')
else
  Chef::Log.error('my power is weak and feeble')
end
if @new_resource.password == true
  Chef::Log.error('the secret password is 12345')
else
  Chef::Log.error('I will never tell you the secret to the airlock')
end

  Chef::Log.debug('{jefflibrary->lwrp->user} - end')
end

along with the helper library

#libraries/acx_shared.rb
module Acx
  module User
    module Shared
    def true_shared
      #puts blah
      Chef::Log.error('I am pulling this from a library reference')
    end
    def false_shared
      Chef::Log.error('I am not very good at sharing')
    end
  end
end
end

but whenever I try to run it regardless of resource attributes set, I keep getting

NoMethodError
           -------------
           undefined method `false_shared' for Acx::User::Shared:Module

I am clearly missing something in the documentation on writing a helper library, but I am not sure what. Tried moving a few things around but starting to run out of ideas.

Upvotes: 0

Views: 1942

Answers (1)

Mircea
Mircea

Reputation: 10566

try removing the include Acx

What might happen is that because you are doing that it's actually looking inside Acx for another module name Acx. So either remove the include or remove the Acx:: from calls to *shared methods. You also cannot call an instance variable directly from the module as you are attempting. You need to have a class include the module and after that call the method on an object of that class.
As an alternative you can promote the methods to class methods (self.) and you can call them directly.

Something like:

# providers/provider.rb
require_relative '../libraries/acx_shared'

action :create  do
  Chef::Log.debug('{jefflibrary->lwrp->user} - start')

acx = Class.new.exted(Acx::User::Shared)
if @new_resource.shared == true
  acx.true_shared()
else
  acx.false_shared()
end

if @new_resource.sudo == true
  Chef::Log.error('I HAVE THE POWER')
else
  Chef::Log.error('my power is weak and feeble')
end
if @new_resource.password == true
  Chef::Log.error('the secret password is 12345')
else
  Chef::Log.error('I will never tell you the secret to the airlock')
end

  Chef::Log.debug('{jefflibrary->lwrp->user} - end')
end

or

#libraries/acx_shared.rb
module Acx
  module User
    module Shared
    def self.true_shared
      #puts blah
      Chef::Log.error('I am pulling this from a library reference')
    end
    def self.false_shared
      Chef::Log.error('I am not very good at sharing')
    end
  end
end
end

Upvotes: 2

Related Questions