Undefined local variable or method 'new_resource' inside action_class

Hi,

So I was trying to create a custom resource for the first time and read the documentation about how to use action-class

But when I try to use it inside action_class it throws an error:

================================================================================
Recipe Compile Error in c:/chef/cache/cookbooks/eg_provisioning/resources/eg_data_bag.rb
================================================================================

NameError
---------
undefined local variable or method 'new_resource' for #<Class:0x115a4528> action provider

Cookbook Trace:
---------------
  c:/chef/cache/cookbooks/eg_provisioning/resources/eg_data_bag.rb:51:in `block in class_from_file'
  c:/chef/cache/cookbooks/eg_provisioning/resources/eg_data_bag.rb:48:in `class_from_file'

Relevant File Content:
----------------------
c:/chef/cache/cookbooks/eg_provisioning/resources/eg_data_bag.rb:

 44:      action :delete
 45:    end
 46:  end
 47:
 48:  action_class do
 49:    include EC2Handler
 50:    require 'chef/provisioning'
 51>>   Default_Data = new_resource.region_data
 52:    VPC_Data     = new_resource.vpc_data
 53:

Platform:
---------
i386-mingw32

Chef version is 12.14.89

I am not sure what is wrong. Please help!

Thanks!

Any chance you can include the complete contents of the resource file?

Sure! Just clipping out implementation of some methods and other similar things. The methods inside action_class also need to have access to new_resource properties. Thanks!

resource_name :eg_data_bag

property :bag_item_name, String, name_property: true, required: true
property :bag_name, String, required: true
property :bag_type, String, equal_to: %w(DU EG)
property :vpc_data, Hash, required: true
property :region_data, Hash, required: true

# Action Create
action :create do
  bag_item_data = add_common_info
  bag_item_data = add_du_info(bag_item_data) if bag_type == 'DU'
  bag_item_data = add_eg_info(bag_item_data) if bag_type == 'EG'

  # Create DU data bag
  chef_data_bag new_resource.bag_name do
    action :create
  end

  # Create DU Data bag item
  chef_data_bag_item new_resource.bag_item_name do
    data_bag new_resource.bag_name
    raw_data bag_item_data
    action :create
  end
end

# Action delete
action :delete do
  # Delete DU Data bag item
  chef_data_bag_item new_resource.bag_item_name do
    data_bag new_resource.bag_name
    action :delete
  end
end

action_class do
  include EC2Handler
  require 'chef/provisioning'
  Default_Data = new_resource.region_data
  VPC_Data     = new_resource.vpc_data
  
  def add_common_info
  end

  def add_du_info(input_hash)
 
  end

  def add_eg_info(input_hash)

  end

  def get_ip_address(env, subnet_cidr)
  end
end

Also adding Stacktrace.out below:

Generated at 2018-01-30 22:05:45 +0000
NameError: undefined local variable or method `new_resource' for #<Class:0x11441760> action provider
c:/chef/cache/cookbooks/eg_provisioning/resources/eg_data_bag.rb:51:in `block in class_from_file'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/resource.rb:1224:in `class_eval'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/resource.rb:1224:in `declare_action_class'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/resource.rb:1199:in `action_class'
c:/chef/cache/cookbooks/eg_provisioning/resources/eg_data_bag.rb:48:in `class_from_file'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/mixin/from_file.rb:42:in `class_eval'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/mixin/from_file.rb:42:in `class_from_file'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/resource/lwrp_base.rb:58:in `build_from_file'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:222:in `load_lwrp_resource'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:207:in `block in load_lwrps_from_cookbook'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:206:in `each'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:206:in `load_lwrps_from_cookbook'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:121:in `block in compile_lwrps'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:120:in `each'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:120:in `compile_lwrps'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:73:in `compile'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/run_context.rb:176:in `load'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/policy_builder/expand_node_object.rb:97:in `setup_run_context'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/client.rb:510:in `setup_run_context'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/client.rb:280:in `run'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application.rb:284:in `run_with_graceful_exit_option'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application.rb:260:in `block in run_chef_client'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/local_mode.rb:44:in `with_server_connectivity'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application.rb:243:in `run_chef_client'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application/client.rb:464:in `sleep_then_run_chef_client'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application/client.rb:451:in `block in interval_run_chef_client'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application/client.rb:450:in `loop'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application/client.rb:450:in `interval_run_chef_client'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application/client.rb:434:in `run_application'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/lib/chef/application.rb:60:in `run'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.14.89-universal-mingw32/bin/chef-client:26:in `<top (required)>'
C:/opscode/chefdk/bin/chef-client:68:in `load'
C:/opscode/chefdk/bin/chef-client:68:in `<main>'

The block for action_class is evaluated at the class level for the provider subclass. So at that time, there is no specific resource object instance available. You would have to query those things from inside the methods, using them later on in the lifecycle.

1 Like

I need a bunch of similar variables to be available to multiple methods inside action_class. I could add these inside getter/setter methods but then I will first have to call those methods from action blocks?