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

[WIP] update lamba_alias and lambda_event to use AWSModule.client() #205

Closed
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
20 changes: 6 additions & 14 deletions plugins/modules/lambda_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@

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 get_aws_connection_info


class AWSConnection:
Expand All @@ -164,28 +162,22 @@ class AWSConnection:
def __init__(self, ansible_obj, resources, boto3_=True):

try:
self.region, self.endpoint, aws_connect_kwargs = get_aws_connection_info(ansible_obj, boto3=boto3_)

self.region = ansible_obj.region
self.resource_client = dict()
if not resources:
resources = ['lambda']

resources.append('iam')

for resource in resources:
aws_connect_kwargs.update(dict(region=self.region,
endpoint=self.endpoint,
conn_type='client',
resource=resource
))
self.resource_client[resource] = boto3_conn(ansible_obj, **aws_connect_kwargs)
self.resource_client[resource] = ansible_obj.client(resource)

# if region is not provided, then get default profile/session region
if not self.region:
self.region = self.resource_client['lambda'].meta.region_name

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

try:
self.account_id = self.resource_client['iam'].get_user()['User']['Arn'].split(':')[4]
Expand Down Expand Up @@ -315,7 +307,7 @@ def lambda_alias(module, aws):
try:
results = client.update_alias(**api_params)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json(msg='Error updating function alias: {0}'.format(e))
module.fail_json_aws(e, msg='Error updating function alias')

else:
# create new function alias
Expand All @@ -326,7 +318,7 @@ def lambda_alias(module, aws):
results = client.create_alias(**api_params)
changed = True
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json(msg='Error creating function alias: {0}'.format(e))
module.fail_json_aws(e, msg='Error creating function alias')

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

return dict(changed=changed, **dict(results or facts))

Expand Down
37 changes: 16 additions & 21 deletions plugins/modules/lambda_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,13 @@
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.ec2 import boto3_conn
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import get_aws_connection_info


# ---------------------------------------------------------------------------------------------------
Expand All @@ -143,7 +141,7 @@ class AWSConnection:
def __init__(self, ansible_obj, resources, use_boto3=True):

try:
self.region, self.endpoint, aws_connect_kwargs = get_aws_connection_info(ansible_obj, boto3=use_boto3)
self.region = ansible_obj.region

self.resource_client = dict()
if not resources:
Expand All @@ -152,24 +150,21 @@ def __init__(self, ansible_obj, resources, use_boto3=True):
resources.append('iam')

for resource in resources:
aws_connect_kwargs.update(dict(region=self.region,
endpoint=self.endpoint,
conn_type='client',
resource=resource
))
self.resource_client[resource] = boto3_conn(ansible_obj, **aws_connect_kwargs)
self.resource_client[resource] = ansible_obj.client(resource)

# if region is not provided, then get default profile/session region
if not self.region:
self.region = self.resource_client['lambda'].meta.region_name

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

# set account ID
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) as e:
self.account_id = ''
except (ValueError, KeyError, IndexError):
self.account_id = ''

def client(self, resource='lambda'):
Expand Down Expand Up @@ -323,8 +318,8 @@ def lambda_event_stream(module, aws):
facts = client.list_event_source_mappings(**api_params)['EventSourceMappings']
if facts:
current_state = 'present'
except ClientError as e:
module.fail_json(msg='Error retrieving stream event notification configuration: {0}'.format(e))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg='Error retrieving stream event notification configuration')

if state == 'present':
if current_state == 'absent':
Expand All @@ -347,8 +342,8 @@ def lambda_event_stream(module, aws):
if not module.check_mode:
facts = client.create_event_source_mapping(**api_params)
changed = True
except (ClientError, ParamValidationError, MissingParametersError) as e:
module.fail_json(msg='Error creating stream source event mapping: {0}'.format(e))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg='Error creating stream source event mapping')

else:
# current_state is 'present'
Expand Down Expand Up @@ -377,8 +372,8 @@ def lambda_event_stream(module, aws):
if not module.check_mode:
facts = client.update_event_source_mapping(**api_params)
changed = True
except (ClientError, ParamValidationError, MissingParametersError) as e:
module.fail_json(msg='Error updating stream source event mapping: {0}'.format(e))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg='Error updating stream source event mapping')

else:
if current_state == 'present':
Expand All @@ -389,8 +384,8 @@ def lambda_event_stream(module, aws):
if not module.check_mode:
facts = client.delete_event_source_mapping(**api_params)
changed = True
except (ClientError, ParamValidationError, MissingParametersError) as e:
module.fail_json(msg='Error removing stream source event mapping: {0}'.format(e))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg='Error removing stream source event mapping')

return camel_dict_to_snake_dict(dict(changed=changed, events=facts))

Expand Down