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
?
Asked
Active
Viewed 802 times
0

blueowl
- 35
- 6
2 Answers
1
You can do it exactly the same way you would, if you wanted to use it in your recipe.
- 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
- 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 inline
cookbook it was declared.)
# check/resources/myresource.rb
action :some_action do
line_resource [...] do
[...]
end
end

Draco Ater
- 20,820
- 8
- 62
- 86
-
Thank you very much for your answer. I tried this and it gives me an undefined method error in the *line_resource* line. I used the actual `replace_or_add` resource from line cookbook which is in [link] (https://github.com/sous-chefs/line/blob/master/resources/replace_or_add.rb) I used `line_replace_or_add [...] do ` and it gave undefined method error. Any comments on this, please? – blueowl Jun 17 '20 at 22:19
-
As you see on line 10 https://github.com/sous-chefs/line/blob/master/resources/replace_or_add.rb#L10 the actual resource name is `replace_or_add`. – Draco Ater Jun 18 '20 at 06:38
-
when I use `replace_or_add [..] do` I still get the undefined method error. – blueowl Jun 18 '20 at 08:19
0
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.

blueowl
- 35
- 6