Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Chef "search" method in library code (in module method)?

Tags:

chef-infra

I encountered a problem about using "search" method in library code: libraries/helpers.rb

Bcpc
  Helper
    extend self
    def help(node=node)
      search(:node, "....")
    end
  end
end
Chef::Recipe.send(:include, Bcpc::Helper)

Chef::Resource.send(:include, Bcpc::Helper) for the recipes using module methods.

Then use this module method in recipes like this: Bcpc::Helper.help(node) When I run it, it report the error that search method is not defined in Bcpc::Helper:Module

I found the search method is defined in Chef::Search::Query class. Then I tried use the full name of search in my library code like: Chef::Search::Query.search(:node, "...."). But it reported search is undefined in Chef::Search::Query. Should this search method be a static method that can be called with its class name?

How can I use the Chef provided "search" method in my library code in this case? Thanks!

like image 298
Ray Avatar asked Jan 12 '15 21:01

Ray


1 Answers

You want something like this.

Chef::Search::Query.new.search(:node, 'foo:bar') do |n|
  # something with n
end
like image 200
coderanger Avatar answered Sep 22 '22 10:09

coderanger