salt icon indicating copy to clipboard operation
salt copied to clipboard

ext pillar mongo fails with pymongo 3.0.x

Open trevor-h opened this issue 11 years ago • 4 comments

There is some deprecated usage of pymongo that results in errors connecting and requesting specific fields in the find_one statement. This is due to deprecated usage becoming removed from latest releases. The following is observed with pymongo 3.0.2.

From master log on connection:

[salt.pillar      ][ERROR   ] Failed to load ext_pillar mongo: 'module' object has no attribute 'Connection'
Traceback (most recent call last):

  File "/usr/lib/python2.6/site-packages/salt/pillar/__init__.py", line 526, in ext_pillar
    key)
  File "/usr/lib/python2.6/site-packages/salt/pillar/__init__.py", line 487, in _external_pillar_data
    ext = self.ext_pillars[key](self.opts['id'], pillar, **val)
  File "/usr/lib/python2.6/site-packages/salt/pillar/mongo.py", line 119, in ext_pillar
    conn = pymongo.Connection(host, port)
AttributeError: 'module' object has no attribute 'Connection'

From master log with find_one if 'Connection' replaced with 'MongoClient':

[salt.pillar      ][ERROR   ] Failed to load ext_pillar mongo: __init__() got an unexpected keyword argument 'fields'
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/salt/pillar/__init__.py", line 526, in ext_pillar
    key)
  File "/usr/lib/python2.6/site-packages/salt/pillar/__init__.py", line 487, in _external_pillar_data
    ext = self.ext_pillars[key](self.opts['id'], pillar, **val)
  File "/usr/lib/python2.6/site-packages/salt/pillar/mongo.py", line 142, in ext_pillar
    result = mdb[collection].find_one({id_field: minion_id}, fields=fields)
  File "/usr/lib64/python2.6/site-packages/pymongo/collection.py", line 796, in find_one
    *args, **kwargs).max_time_ms(max_time_ms)
  File "/usr/lib64/python2.6/site-packages/pymongo/collection.py", line 929, in find
    return Cursor(self, *args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'fields'

trevor-h avatar May 25 '15 17:05 trevor-h

The above pull request resolves the issue and could be easily included in all stable releases.

trevor-h avatar May 25 '15 18:05 trevor-h

@trevor-h, thanks for the report. Do we need to support pymongo < 3.0? Or do we need to enforce pymongo >= 3.0?

jfindlay avatar May 25 '15 18:05 jfindlay

Pymongo > 3.0 introduces some breaking changes but using 'Connection' instead of 'MongoClient' has been deprecated since pymongo-2.4 about 3 years back. It would take a really old pymongo for this to be a breaking change for users.

Up until pymongo 2.8.1 Connection was still supported (http://api.mongodb.org/python/2.8.1/api/pymongo/connection.html#module-pymongo.connection) but it's now totally absent and forcing usage of MongoClient.

The more "breaking" change will be the fields list in find_one. In pymongo 2.x this was 'fields' and now is 'projection' and no backwards compatibility is provided.

Testing on pymongo 2.8.1 the usage of 'projection' in place of 'fields' just leads to it being ignored. The side effect is the whole document gets returned, not just the pillar field specified by the user. This may be enough to place a requirement on pymongo > 3.0 though. The merged pull request above allows users with 'fields' in their master config and pymongo 3.x to get the filtered down field list.

I left the config as 'fields' in the master config to not break people's master config file, and it just uses projection in the background. A future change to consider will be using 'projection' instead of 'fields' in the master configuration.

trevor-h avatar May 25 '15 20:05 trevor-h

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.

stale[bot] avatar Nov 10 '17 21:11 stale[bot]