Search code examples
chef-infradependency-managementchef-custom-resource

Is it possible to use a custom resource from some cookbook to create a custom resource in your own cookbook?


I have a cookbook let's say the name of my cookbook is check and I am trying to build a custom resource by having the file in the following directory structure : check/resources/myresource.rb. In this myresource.rb file I need to use a custom resource from another cookbook line. How do I use the resource from line cookbook in myresource.rb?


Solution

  • Based on what @Draco already mentioned, the two steps described by him are required steps. In addition to that, the inclusion of the cookbook needs to be done when you call the custom resource in your recipe.

    # check/resources/myresource.rb
    
    resource_name :myresource
    property :cookbook_inclusion, String
    property :some_name, String, name_property: true
    
    action :some_action do
      include_recipe new_resource.cookbook_inclusion
      line_resource [...] do
        [...]
      end
    end
    

    Then while calling it in the recipe you can mention the name of the cookbook that you want to include.

    # check/recipes/default.rb
    
    myresource 'include' do
      cookbook_inclusion 'line'
    end
    

    In this way, at convergence, all resources will be available for operations.