Skip to content

Commit 7043207

Browse files
committed
Updating search to support attributes and not running a block.
1 parent b5418af commit 7043207

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

chef/lib/chef/recipe.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,17 @@ def resources(*args)
8989
@collection.resources(*args)
9090
end
9191

92-
def search(type, query, &block)
92+
def search(type, query, attributes=[], &block)
9393
Chef::Log.debug("Searching #{type} index with #{query}")
9494
r = Chef::REST.new(Chef::Config[:search_url])
95-
results = r.get_rest("search/#{type}?q=#{query}")
95+
results = r.get_rest("search/#{type}?q=#{query}&a=#{attributes.join(',')}")
9696
Chef::Log.debug("Searching #{type} index with #{query} returned #{results.length} entries")
97-
results.each do |sr|
98-
block.call(sr)
97+
if block
98+
results.each do |sr|
99+
block.call(sr)
100+
end
101+
else
102+
results
99103
end
100104
end
101105

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Cookbook Name:: CHEF-90
3+
# Recipe:: default
4+
#
5+
# Copyright 2009, Example Com
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
results = search(:node, '*', [ 'ipaddress', 'hostname' ])
21+
Chef::Log.info(results.inspect)
22+
23+
search(:node, '*', [ 'ipaddress', 'hostname' ]) do |n|
24+
Chef::Log.info("#{n['ipaddress']} #{n['hostname']}")
25+
end
26+
27+
Chef::Log.info(search(:node, '*').inspect)

0 commit comments

Comments
 (0)