Add idempotency to mongodb_user module

reviewable/pr18780/r1
George Miroshnykov 11 years ago
parent 3ddfc8d471
commit 58e34edce3

@ -188,6 +188,18 @@ def main():
try: try:
client = MongoClient(login_host, int(login_port)) client = MongoClient(login_host, int(login_port))
except ConnectionFailure, e:
module.fail_json(msg='unable to connect to database, check login_host and login_port are correct')
# try to authenticate as a target user to check if it already exists
try:
client[db_name].authenticate(user, password)
if state == 'present':
module.exit_json(changed=False, user=user)
except OperationFailure:
if state == 'absent':
module.exit_json(changed=False, user=user)
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:
@ -199,9 +211,6 @@ def main():
if login_user is not None and login_password is not None: if login_user is not None and login_password is not None:
client.admin.authenticate(login_user, login_password) client.admin.authenticate(login_user, login_password)
except ConnectionFailure, e:
module.fail_json(msg='unable to connect to database, check login_user and login_password are correct')
if state == 'present': if state == 'present':
if password is None: if password is None:
module.fail_json(msg='password parameter required when adding a user') module.fail_json(msg='password parameter required when adding a user')

Loading…
Cancel
Save