coderanger / pychef

Python library to interact with the Chef server API
Other
195 stars 129 forks source link

Chef Search Query Using Brackets Fails #69

Closed isuftin closed 7 years ago

isuftin commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

@coderanger Seems that it does operate on numbers: https://brettscott.wordpress.com/2011/11/19/lucene-number-range-search-integers-floats/

isuftin commented 7 years ago

@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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

👍