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

Chef Search Query Using Brackets Fails #69

Closed
isuftin opened this issue Mar 2, 2017 · 7 comments
Closed

Chef Search Query Using Brackets Fails #69

isuftin opened this issue Mar 2, 2017 · 7 comments

Comments

@isuftin
Copy link

isuftin commented Mar 2, 2017

Looks like searching with [ ] for ranges fails. Assuming it's an escaping issue. Works if I manually escape.

PyChef 0.3.0
Python 2.7.13 on CentOS

Working:

>>> len(chef.Search('node', q="ohai_time:\[0 to 1488474332\]"))
DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): owichef.usgs.chs.ead
/var/lib/rundeck/var/tmp/project/Chef/venv/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py:852: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
DEBUG:requests.packages.urllib3.connectionpool:https://cheftest.url.org:443 "GET /organizations/test/search/node?q=ohai_time%3A%5C%5B0+to+1488474332%5C%5D&start=0&rows=1000 HTTP/1.1" 200 None
79

Not working:

>>> len(chef.Search('node', q="ohai_time:[0 to 1488474332]"))
DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): owichef.usgs.chs.ead
/var/lib/rundeck/var/tmp/project/Chef/venv/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py:852: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
DEBUG:requests.packages.urllib3.connectionpool:https://cheftest.url.org:443 "GET /organizations/test/search/node?q=ohai_time%3A%5B0+to+1488474332%5D&start=0&rows=1000 HTTP/1.1" 400 65
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/var/lib/rundeck/var/tmp/project/Chef/venv/lib/python2.7/site-packages/chef/search.py", line 83, in __len__
    return len(self.data['rows'])
  File "/var/lib/rundeck/var/tmp/project/Chef/venv/lib/python2.7/site-packages/chef/search.py", line 60, in data
    self._data = self.api[self.url]
  File "/var/lib/rundeck/var/tmp/project/Chef/venv/lib/python2.7/site-packages/chef/api.py", line 221, in __getitem__
    return self.api_request('GET', path)
  File "/var/lib/rundeck/var/tmp/project/Chef/venv/lib/python2.7/site-packages/chef/api.py", line 217, in api_request
    response = self.request(method, path, headers, data)
  File "/var/lib/rundeck/var/tmp/project/Chef/venv/lib/python2.7/site-packages/chef/api.py", line 207, in request
    raise ChefServerError.from_error(response.reason, code=response.status_code)
chef.exceptions.ChefServerError: Bad Request
@coderanger
Copy link
Owner

That search doesn't do what you think anyway, TO ranges in Lucene operate on text ranges, not numeric ranges. Chef does not support numeric range searches.

@isuftin
Copy link
Author

isuftin commented Mar 2, 2017

@isuftin
Copy link
Author

isuftin commented Mar 2, 2017

@coderanger When doing the search via knife search node, I do get the correct data back that I expect. Example knife search node "ohai_time:[0 TO 148847433]"

@isuftin
Copy link
Author

isuftin commented Mar 2, 2017

Also figured out my own issue. It was not due to escaping. I had lower-case to.

len(chef.Search('node', q="ohai_time:[0 TO 148847433]"))
2

Perfect!

@coderanger
Copy link
Owner

Just as long as you are aware that won't always do what you want :-) I would highly recommend implementing the filter in Python code if it is something important.

@isuftin
Copy link
Author

isuftin commented Mar 2, 2017

@coderanger Mainly, I'm looking to not get all nodes (and node info) back from the Chef server each time I run a search. I could just run Search('node') and then parse through that via python but I'd rather use the server's back-end to do the searching for me.

My code here is just the first step in a node sweeping process. Step two would take each item in that list and check if the machine is still running, so there is that safety ;)

@coderanger
Copy link
Owner

👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants