Skip to content

Commit 09ff5f9

Browse files
author
Sergei Antipov
committed
Fixed problem with wrong pymongo package
1 parent 94ebb6e commit 09ff5f9

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

library/mongodb_user.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@
148148
# MongoDB module specific support methods.
149149
#
150150

151+
def check_compatibility(module, client):
152+
srv_info = client.server_info()
153+
if LooseVersion(srv_info['version']) >= LooseVersion('3.0') and LooseVersion(PyMongoVersion) <= LooseVersion('3.0'):
154+
module.fail_json(msg=' (Note: you must use pymongo 3.0+ with MongoDB >= 3.0)')
155+
elif LooseVersion(srv_info['version']) >= LooseVersion('2.6') and LooseVersion(PyMongoVersion) <= LooseVersion('2.7'):
156+
module.fail_json(msg=' (Note: you must use pymongo 2.7.x-2.9.x with MongoDB 2.6)')
157+
elif LooseVersion(PyMongoVersion) <= LooseVersion('2.5'):
158+
module.fail_json(msg=' (Note: you must be on mongodb 2.4+ and pymongo 2.5+ to use the roles param)')
159+
151160
def user_find(client, user):
152161
for mongo_user in client["admin"].system.users.find():
153162
if mongo_user['user'] == user:
@@ -165,13 +174,6 @@ def user_add(module, client, db_name, user, password, roles):
165174
db.add_user(user, password, None, roles=roles)
166175
except OperationFailure, e:
167176
err_msg = str(e)
168-
srv_info = client.server_info()
169-
if LooseVersion(srv_info['version']) >= LooseVersion('3.0') and LooseVersion(PyMongoVersion) <= LooseVersion('3.0'):
170-
err_msg = err_msg + ' (Note: you must use pymongo 3.0+ with MongoDB >= 3.0)'
171-
elif LooseVersion(srv_info['version']) >= LooseVersion('2.6') and LooseVersion(PyMongoVersion) <= LooseVersion('2.7'):
172-
err_msg = err_msg + ' (Note: you must use pymongo 2.7.x-2.9.x with MongoDB 2.6)'
173-
elif LooseVersion(PyMongoVersion) <= LooseVersion('2.5'):
174-
err_msg = err_msg + ' (Note: you must be on mongodb 2.4+ and pymongo 2.5+ to use the roles param)'
175177
module.fail_json(msg=err_msg)
176178

177179
def user_remove(module, client, db_name, user):
@@ -262,6 +264,8 @@ def main():
262264
except ConnectionFailure, e:
263265
module.fail_json(msg='unable to connect to database: %s' % str(e))
264266

267+
check_compatibility(module, client)
268+
265269
if state == 'present':
266270
if password is None and update_password == 'always':
267271
module.fail_json(msg='password parameter required when adding a user unless update_password is set to on_create')

0 commit comments

Comments
 (0)