Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongodb_replicaset and mongodb_shard: Use error code instead of error text #55639

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/ansible/modules/database/mongodb/mongodb_replicaset.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@
except ImportError:
HAS_PYMONGO = False

MONGO_ERRCODE_UNAUTHORIZED = 13

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six import binary_type, text_type
from ansible.module_utils.six.moves import configparser
Expand Down Expand Up @@ -357,8 +359,8 @@ def main():

try:
check_compatibility(module, client)
except Exception as excep:
if "not authorized on" not in str(excep) and "there are no users authenticated" not in str(excep):
except OperationFailure as excep:
if excep.code != MONGO_ERRCODE_UNAUTHORIZED:
raise excep
if login_user is None or login_password is None:
raise excep
Expand All @@ -375,8 +377,8 @@ def main():

try:
client['admin'].command('listDatabases', 1.0) # if this throws an error we need to authenticate
except Exception as excep:
if "not authorized on" in str(excep) or "command listDatabases requires authentication" in str(excep):
except OperationFailure as excep:
if excep.code == MONGO_ERRCODE_UNAUTHORIZED:
if login_user is not None and login_password is not None:
client.admin.authenticate(login_user, login_password, source=login_database)
else:
Expand Down
20 changes: 10 additions & 10 deletions lib/ansible/modules/database/mongodb/mongodb_shard.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@
else:
pymongo_found = True

MONGO_ERRCODE_UNAUTHORIZED = 13

from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.six import binary_type, text_type
from ansible.module_utils.six.moves import configparser
Expand Down Expand Up @@ -268,15 +270,13 @@ def main():

try:
check_compatibility(module, client)
except Exception as excep:
if "not authorized on" in str(excep) or "there are no users authenticated" in str(excep):
if login_user is not None and login_password is not None:
client.admin.authenticate(login_user, login_password, source=login_database)
check_compatibility(module, client)
else:
raise excep
else:
except OperationFailure as excep:
if excep.code != MONGO_ERRCODE_UNAUTHORIZED:
raise excep
if login_user is None or login_password is None:
raise excep
client.admin.authenticate(login_user, login_password, source=login_database)
check_compatibility(module, client)

if login_user is None and login_password is None:
mongocnf_creds = load_mongocnf()
Expand All @@ -288,8 +288,8 @@ def main():

try:
client['admin'].command('listDatabases', 1.0) # if this throws an error we need to authenticate
except Exception as excep:
if "not authorized on" in str(excep) or "command listDatabases requires authentication" in str(excep):
except OperationFailure as excep:
if excep.code == MONGO_ERRCODE_UNAUTHORIZED:
if login_user is not None and login_password is not None:
client.admin.authenticate(login_user, login_password, source=login_database)
else:
Expand Down