Fix mongodb user compatibility check (#2731)

- Check the compatibility asap
reviewable/pr18780/r1
THEBAULT Julien 8 years ago committed by René Moser
parent 48e55c9d7a
commit 085321e3a8

@ -174,13 +174,26 @@ else:
# #
def check_compatibility(module, client): def check_compatibility(module, client):
srv_info = client.server_info() """Check the compatibility between the driver and the database.
if LooseVersion(srv_info['version']) >= LooseVersion('3.2') and LooseVersion(PyMongoVersion) <= LooseVersion('3.2'):
See: https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#python-driver-compatibility
Args:
module: Ansible module.
client (cursor): Mongodb cursor on admin database.
"""
loose_srv_version = LooseVersion(client.server_info()['version'])
loose_driver_version = LooseVersion(PyMongoVersion)
if loose_srv_version >= LooseVersion('3.2') and loose_driver_version <= LooseVersion('3.2'):
module.fail_json(msg=' (Note: you must use pymongo 3.2+ with MongoDB >= 3.2)') module.fail_json(msg=' (Note: you must use pymongo 3.2+ with MongoDB >= 3.2)')
elif LooseVersion(srv_info['version']) >= LooseVersion('3.0') and LooseVersion(PyMongoVersion) <= LooseVersion('2.8'):
elif loose_srv_version >= LooseVersion('3.0') and loose_driver_version <= LooseVersion('2.8'):
module.fail_json(msg=' (Note: you must use pymongo 2.8+ with MongoDB 3.0)') module.fail_json(msg=' (Note: you must use pymongo 2.8+ with MongoDB 3.0)')
elif LooseVersion(srv_info['version']) >= LooseVersion('2.6') and LooseVersion(PyMongoVersion) <= LooseVersion('2.7'):
elif loose_srv_version >= LooseVersion('2.6') and loose_srv_version <= LooseVersion('2.7'):
module.fail_json(msg=' (Note: you must use pymongo 2.7+ with MongoDB 2.6)') module.fail_json(msg=' (Note: you must use pymongo 2.7+ with MongoDB 2.6)')
elif LooseVersion(PyMongoVersion) <= LooseVersion('2.5'): elif LooseVersion(PyMongoVersion) <= LooseVersion('2.5'):
module.fail_json(msg=' (Note: you must be on mongodb 2.4+ and pymongo 2.5+ to use the roles param)') module.fail_json(msg=' (Note: you must be on mongodb 2.4+ and pymongo 2.5+ to use the roles param)')
@ -339,6 +352,10 @@ def main():
client = MongoClient(**connection_params) client = MongoClient(**connection_params)
# NOTE: this check must be done ASAP.
# We doesn't need to be authenticated.
check_compatibility(module, client)
if login_user is None and login_password is None: if login_user is None and login_password is None:
mongocnf_creds = load_mongocnf() mongocnf_creds = load_mongocnf()
if mongocnf_creds is not False: if mongocnf_creds is not False:
@ -357,8 +374,6 @@ def main():
except Exception, e: except Exception, e:
module.fail_json(msg='unable to connect to database: %s' % str(e)) module.fail_json(msg='unable to connect to database: %s' % str(e))
check_compatibility(module, client)
if state == 'present': if state == 'present':
if password is None and update_password == 'always': if password is None and update_password == 'always':
module.fail_json(msg='password parameter required when adding a user unless update_password is set to on_create') module.fail_json(msg='password parameter required when adding a user unless update_password is set to on_create')

Loading…
Cancel
Save