Skip to content

Commit

Permalink
Cleanup - use is_boto3_error_(message|code) (ansible-collections#268)
Browse files Browse the repository at this point in the history
* Reorder imports
* Make use of is_boto3_error_message
* Mass-migration over to is_boto3_error_code
* Remove unused imports
* unused vars in exception
* Improve consistency around catching BotoCoreError and ClientError
* Remove unused imports
* Remove unused 'PolicyError' from iam_policy_info
* Avoid catching botocore.exceptions.ClientError when we only want some error codes
* Import camel_dict_to_snake_dict/snake_dict_to_camel_dict from ansible.module_utils.common.dict_transformations

This commit was initially merged in https://github.com/ansible-collections/community.aws
See: ansible-collections/community.aws@4cf52ef
  • Loading branch information
tremble authored and alinabuzachis committed Sep 16, 2022
1 parent 6502d63 commit 1c77f57
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 96 deletions.
26 changes: 13 additions & 13 deletions plugins/modules/lambda_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,15 @@
import re

try:
from botocore.exceptions import ClientError, ParamValidationError, MissingParametersError
import botocore
except ImportError:
pass # Handled by AnsibleAWSModule

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_conn
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import get_aws_connection_info


Expand Down Expand Up @@ -182,12 +184,12 @@ def __init__(self, ansible_obj, resources, boto3_=True):
if not self.region:
self.region = self.resource_client['lambda'].meta.region_name

except (ClientError, ParamValidationError, MissingParametersError) as e:
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
ansible_obj.fail_json(msg="Unable to connect, authorize or access resource: {0}".format(e))

try:
self.account_id = self.resource_client['iam'].get_user()['User']['Arn'].split(':')[4]
except (ClientError, ValueError, KeyError, IndexError):
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError, ValueError, KeyError, IndexError):
self.account_id = ''

def client(self, resource='lambda'):
Expand Down Expand Up @@ -269,12 +271,10 @@ def get_lambda_alias(module, aws):
# check if alias exists and get facts
try:
results = client.get_alias(**api_params)

except (ClientError, ParamValidationError, MissingParametersError) as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
results = None
else:
module.fail_json(msg='Error retrieving function alias: {0}'.format(e))
except is_boto3_error_code('ResourceNotFoundException'):
results = None
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg='Error retrieving function alias')

return results

Expand Down Expand Up @@ -314,7 +314,7 @@ def lambda_alias(module, aws):
if not module.check_mode:
try:
results = client.update_alias(**api_params)
except (ClientError, ParamValidationError, MissingParametersError) as e:
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json(msg='Error updating function alias: {0}'.format(e))

else:
Expand All @@ -325,7 +325,7 @@ def lambda_alias(module, aws):
if not module.check_mode:
results = client.create_alias(**api_params)
changed = True
except (ClientError, ParamValidationError, MissingParametersError) as e:
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json(msg='Error creating function alias: {0}'.format(e))

else: # state = 'absent'
Expand All @@ -337,7 +337,7 @@ def lambda_alias(module, aws):
if not module.check_mode:
results = client.delete_alias(**api_params)
changed = True
except (ClientError, ParamValidationError, MissingParametersError) as e:
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json(msg='Error deleting function alias: {0}'.format(e))

return dict(changed=changed, **dict(results or facts))
Expand Down
65 changes: 30 additions & 35 deletions plugins/modules/lambda_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,21 @@
returned: success
type: dict
'''

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict
import json
import datetime
import sys
import re


try:
from botocore.exceptions import ClientError
import botocore
except ImportError:
pass # caught by AnsibleAWSModule

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code


def fix_return(node):
"""
Expand Down Expand Up @@ -147,11 +148,10 @@ def alias_details(client, module):
params['Marker'] = module.params.get('next_marker')
try:
lambda_facts.update(aliases=client.list_aliases(FunctionName=function_name, **params)['Aliases'])
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
lambda_facts.update(aliases=[])
else:
module.fail_json_aws(e, msg="Trying to get aliases")
except is_boto3_error_code('ResourceNotFoundException'):
lambda_facts.update(aliases=[])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Trying to get aliases")
else:
module.fail_json(msg='Parameter function_name required for query=aliases.')

Expand Down Expand Up @@ -201,11 +201,10 @@ def config_details(client, module):
if function_name:
try:
lambda_facts.update(client.get_function_configuration(FunctionName=function_name))
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
lambda_facts.update(function={})
else:
module.fail_json_aws(e, msg="Trying to get {0} configuration".format(function_name))
except is_boto3_error_code('ResourceNotFoundException'):
lambda_facts.update(function={})
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Trying to get {0} configuration".format(function_name))
else:
params = dict()
if module.params.get('max_items'):
Expand All @@ -216,11 +215,10 @@ def config_details(client, module):

try:
lambda_facts.update(function_list=client.list_functions(**params)['Functions'])
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
lambda_facts.update(function_list=[])
else:
module.fail_json_aws(e, msg="Trying to get function list")
except is_boto3_error_code('ResourceNotFoundException'):
lambda_facts.update(function_list=[])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Trying to get function list")

functions = dict()
for func in lambda_facts.pop('function_list', []):
Expand Down Expand Up @@ -257,11 +255,10 @@ def mapping_details(client, module):

try:
lambda_facts.update(mappings=client.list_event_source_mappings(**params)['EventSourceMappings'])
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
lambda_facts.update(mappings=[])
else:
module.fail_json_aws(e, msg="Trying to get source event mappings")
except is_boto3_error_code('ResourceNotFoundException'):
lambda_facts.update(mappings=[])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Trying to get source event mappings")

if function_name:
return {function_name: camel_dict_to_snake_dict(lambda_facts)}
Expand All @@ -288,11 +285,10 @@ def policy_details(client, module):
try:
# get_policy returns a JSON string so must convert to dict before reassigning to its key
lambda_facts.update(policy=json.loads(client.get_policy(FunctionName=function_name)['Policy']))
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
lambda_facts.update(policy={})
else:
module.fail_json_aws(e, msg="Trying to get {0} policy".format(function_name))
except is_boto3_error_code('ResourceNotFoundException'):
lambda_facts.update(policy={})
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Trying to get {0} policy".format(function_name))
else:
module.fail_json(msg='Parameter function_name required for query=policy.')

Expand Down Expand Up @@ -321,11 +317,10 @@ def version_details(client, module):

try:
lambda_facts.update(versions=client.list_versions_by_function(FunctionName=function_name, **params)['Versions'])
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
lambda_facts.update(versions=[])
else:
module.fail_json_aws(e, msg="Trying to get {0} versions".format(function_name))
except is_boto3_error_code('ResourceNotFoundException'):
lambda_facts.update(versions=[])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Trying to get {0} versions".format(function_name))
else:
module.fail_json(msg='Parameter function_name required for query=versions.')

Expand Down
65 changes: 30 additions & 35 deletions plugins/modules/lambda_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,20 @@
returned: success
type: dict
'''

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict
import json
import datetime
import re


try:
from botocore.exceptions import ClientError
import botocore
except ImportError:
pass # caught by AnsibleAWSModule

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code


def fix_return(node):
"""
Expand Down Expand Up @@ -136,11 +137,10 @@ def alias_details(client, module):
params['Marker'] = module.params.get('next_marker')
try:
lambda_info.update(aliases=client.list_aliases(FunctionName=function_name, **params)['Aliases'])
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
lambda_info.update(aliases=[])
else:
module.fail_json_aws(e, msg="Trying to get aliases")
except is_boto3_error_code('ResourceNotFoundException'):
lambda_info.update(aliases=[])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Trying to get aliases")
else:
module.fail_json(msg='Parameter function_name required for query=aliases.')

Expand Down Expand Up @@ -190,11 +190,10 @@ def config_details(client, module):
if function_name:
try:
lambda_info.update(client.get_function_configuration(FunctionName=function_name))
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
lambda_info.update(function={})
else:
module.fail_json_aws(e, msg="Trying to get {0} configuration".format(function_name))
except is_boto3_error_code('ResourceNotFoundException'):
lambda_info.update(function={})
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Trying to get {0} configuration".format(function_name))
else:
params = dict()
if module.params.get('max_items'):
Expand All @@ -205,11 +204,10 @@ def config_details(client, module):

try:
lambda_info.update(function_list=client.list_functions(**params)['Functions'])
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
lambda_info.update(function_list=[])
else:
module.fail_json_aws(e, msg="Trying to get function list")
except is_boto3_error_code('ResourceNotFoundException'):
lambda_info.update(function_list=[])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Trying to get function list")

functions = dict()
for func in lambda_info.pop('function_list', []):
Expand Down Expand Up @@ -246,11 +244,10 @@ def mapping_details(client, module):

try:
lambda_info.update(mappings=client.list_event_source_mappings(**params)['EventSourceMappings'])
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
lambda_info.update(mappings=[])
else:
module.fail_json_aws(e, msg="Trying to get source event mappings")
except is_boto3_error_code('ResourceNotFoundException'):
lambda_info.update(mappings=[])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Trying to get source event mappings")

if function_name:
return {function_name: camel_dict_to_snake_dict(lambda_info)}
Expand All @@ -277,11 +274,10 @@ def policy_details(client, module):
try:
# get_policy returns a JSON string so must convert to dict before reassigning to its key
lambda_info.update(policy=json.loads(client.get_policy(FunctionName=function_name)['Policy']))
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
lambda_info.update(policy={})
else:
module.fail_json_aws(e, msg="Trying to get {0} policy".format(function_name))
except is_boto3_error_code('ResourceNotFoundException'):
lambda_info.update(policy={})
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Trying to get {0} policy".format(function_name))
else:
module.fail_json(msg='Parameter function_name required for query=policy.')

Expand Down Expand Up @@ -310,11 +306,10 @@ def version_details(client, module):

try:
lambda_info.update(versions=client.list_versions_by_function(FunctionName=function_name, **params)['Versions'])
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
lambda_info.update(versions=[])
else:
module.fail_json_aws(e, msg="Trying to get {0} versions".format(function_name))
except is_boto3_error_code('ResourceNotFoundException'):
lambda_info.update(versions=[])
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Trying to get {0} versions".format(function_name))
else:
module.fail_json(msg='Parameter function_name required for query=versions.')

Expand Down
23 changes: 10 additions & 13 deletions plugins/modules/lambda_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,16 @@

import json
import re
from ansible.module_utils._text import to_native
from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule

try:
from botocore.exceptions import ClientError
import botocore
except ImportError:
pass # caught by AnsibleAWSModule

from ansible.module_utils._text import to_native
from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code


def pc(key):
"""
Expand Down Expand Up @@ -285,14 +287,9 @@ def get_policy_statement(module, client):
# check if function policy exists
try:
policy_results = client.get_policy(**api_params)
except ClientError as e:
try:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
return {}
except AttributeError: # catches ClientErrors without response, e.g. fail before connect
pass
module.fail_json_aws(e, msg="retrieving function policy")
except Exception as e:
except is_boto3_error_code('ResourceNotFoundException'):
return {}
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="retrieving function policy")

# get_policy returns a JSON string so must convert to dict before reassigning to its key
Expand Down Expand Up @@ -328,7 +325,7 @@ def add_policy_permission(module, client):
if not module.check_mode:
try:
client.add_permission(**api_params)
except Exception as e:
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="adding permission to policy")
changed = True

Expand Down Expand Up @@ -356,7 +353,7 @@ def remove_policy_permission(module, client):
if not module.check_mode:
client.remove_permission(**api_params)
changed = True
except Exception as e:
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="removing permission from policy")

return changed
Expand Down

0 comments on commit 1c77f57

Please sign in to comment.