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

1k views Asked by At

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?

2

There are 2 answers

0
blueowl On BEST ANSWER

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.

3
Draco Ater On

You can do it exactly the same way you would, if you wanted to use it in your recipe.

  1. Depend on the cookbook that has another custom resource defined:
# metadata.rb

depends 'line', '~> X.Y' # add this line, replacing X and Y with line cookbook version
  1. Use custom resource. You can use it in recipe or in your custom resource, anywhere you can generally use resources. (I used line_resource as an example, the real name is different, depending in what file in line cookbook it was declared.)
# check/resources/myresource.rb

action :some_action do
  line_resource [...] do
    [...]
  end
end