From ceeea0b080eb3c00314d12513f1c06fb917a5bd3 Mon Sep 17 00:00:00 2001 From: ryanohnemus Date: Mon, 4 Sep 2017 18:20:19 -0500 Subject: [PATCH 1/2] allow partial search with chef.Search From: https://docs.chef.io/api_chef_server.html#id76 added an optional filter_results parameter to get a minimal/filtered set of attributes when searching Note: this doesn't give back a full ChefObject since the partial search only returns the parts of the object you request, so the data is returned in the row['data'] attribute closes #17 --- chef/search.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/chef/search.py b/chef/search.py index ee1d940..46218d6 100644 --- a/chef/search.py +++ b/chef/search.py @@ -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 @@ -108,6 +124,7 @@ def index(self, name): raise ValueError('%s not in search'%name) def __call__(self, query): + print "__call__" return self.query(query) @classmethod From 00f81605dc91289a80cc62dfbf674f71d744da87 Mon Sep 17 00:00:00 2001 From: ryanohnemus Date: Mon, 4 Sep 2017 18:26:22 -0500 Subject: [PATCH 2/2] removed an extra print line i added by accident --- chef/search.py | 1 - 1 file changed, 1 deletion(-) diff --git a/chef/search.py b/chef/search.py index 46218d6..37fc2fe 100644 --- a/chef/search.py +++ b/chef/search.py @@ -124,7 +124,6 @@ def index(self, name): raise ValueError('%s not in search'%name) def __call__(self, query): - print "__call__" return self.query(query) @classmethod