From 252df8685a9b61051c60946a2dfc39ef53624e43 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 25 Mar 2015 21:40:13 -0400 Subject: [PATCH] refactrored connection exception handling, added check to see if we actually get a connection before proceeding Fixes #964 --- lib/ansible/modules/cloud/amazon/s3.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/s3.py b/lib/ansible/modules/cloud/amazon/s3.py index 0f91f4ba457..306e4f0d224 100644 --- a/lib/ansible/modules/cloud/amazon/s3.py +++ b/lib/ansible/modules/cloud/amazon/s3.py @@ -320,8 +320,8 @@ def main(): # Look at s3_url and tweak connection settings # if connecting to Walrus or fakes3 - if is_fakes3(s3_url): - try: + try: + if is_fakes3(s3_url): fakes3 = urlparse.urlparse(s3_url) s3 = boto.connect_s3( aws_access_key, @@ -330,19 +330,18 @@ def main(): host=fakes3.hostname, port=fakes3.port, calling_format=OrdinaryCallingFormat()) - except boto.exception.NoAuthHandlerFound, e: - module.fail_json(msg = str(e)) - elif is_walrus(s3_url): - try: + elif is_walrus(s3_url): walrus = urlparse.urlparse(s3_url).hostname s3 = boto.connect_walrus(walrus, aws_access_key, aws_secret_key) - except boto.exception.NoAuthHandlerFound, e: - module.fail_json(msg = str(e)) - else: - try: + else: s3 = boto.s3.connect_to_region(location, aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key, is_secure=True, calling_format=OrdinaryCallingFormat()) - except boto.exception.NoAuthHandlerFound, e: - module.fail_json(msg = str(e)) + except boto.exception.NoAuthHandlerFound, e: + module.fail_json(msg='No Authentication Handler found: %s ' % str(e)) + except Exception, e: + module.fail_json(msg='Failed to connect to S3: %s' % str(e)) + + if s3 is None: # this should never happen + module.fail_json(msg ='Unknown error, failed to create s3 connection, no information from boto.') # If our mode is a GET operation (download), go through the procedure as appropriate ... if mode == 'get':