Skip to content

Commit

Permalink
Wrap boto3 calls in try/except to cleanly catch failures
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble committed Oct 14, 2021
1 parent ddd26fe commit 1eb4f53
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions plugins/modules/ec2_win_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,23 @@ def ec2_win_password(module):

ec2 = module.client('ec2', retry_decorator=AWSRetry.jittered_backoff())

if wait:
start = datetime.datetime.now()
end = start + datetime.timedelta(seconds=wait_timeout)

while datetime.datetime.now() < end:
try:
if wait:
start = datetime.datetime.now()
end = start + datetime.timedelta(seconds=wait_timeout)

while datetime.datetime.now() < end:
data = ec2.get_password_data(InstanceId=instance_id)['PasswordData']
decoded = b64decode(data)
if not decoded:
time.sleep(5)
else:
break
else:
data = ec2.get_password_data(InstanceId=instance_id)['PasswordData']
decoded = b64decode(data)
if not decoded:
time.sleep(5)
else:
break
else:
data = ec2.get_password_data(InstanceId=instance_id)['PasswordData']
decoded = b64decode(data)
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json_aws(e, msg='Failed to get password data')

if wait and datetime.datetime.now() >= end:
module.fail_json(msg="wait for password timeout after %d seconds" % wait_timeout)
Expand Down

0 comments on commit 1eb4f53

Please sign in to comment.