Skip to content
This repository was archived by the owner on May 9, 2020. It is now read-only.

allow partial search with chef.Search #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions chef/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,36 @@ class Search(collections.Sequence):
print row.object.name

.. versionadded:: 0.1

You can also do partial searches if you are using chef server >= 12.

Example::
keys = {
'name' : ['name'],
'ip' : ['ipaddress'],
'kernel_name' : ['kernel', 'name']
}

for row in Search('node', 'roles:app', filter_result=keys):
print row['data']['kernel_name']
"""

url = '/search'

def __init__(self, index, q='*:*', rows=1000, start=0, api=None):
def __init__(self, index, q='*:*', rows=1000, start=0, api=None, filter_result=None):
self.name = index
self.api = api or ChefAPI.get_global()
self._args = dict(q=q, rows=rows, start=start)
self.url = self.__class__.url + '/' + self.name + '?' + six.moves.urllib.parse.urlencode(self._args)
self._filter_result = filter_result

@property
def data(self):
if not hasattr(self, '_data'):
self._data = self.api[self.url]
if not self._filter_result:
self._data = self.api[self.url]
else:
self._data = self.api.api_request('POST', self.url, data=self._filter_result)
return self._data

@property
Expand Down