Skip to content

Commit

Permalink
Add tag query to lambda_info module (ansible-collections#375)
Browse files Browse the repository at this point in the history
Add tag query to lambda_info module

SUMMARY

This PR to add tags query into the lambda_info module. It is helpful to be able to query the Lambda function's tag list so we can integrate future actions.
This PR also fix some missing parameter

ISSUE TYPE


Bugfix Pull Request
Feature Pull Request

COMPONENT NAME


lambda_info

ADDITIONAL INFORMATION

Reviewed-by: Mark Chappell <None>
Reviewed-by: Jill R <None>
Reviewed-by: None <None>
  • Loading branch information
ngoduykhanh authored and abikouo committed Sep 18, 2023
1 parent c274ab9 commit 1034885
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions lambda_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
query:
description:
- Specifies the resource type for which to gather information. Leave blank to retrieve all information.
choices: [ "aliases", "all", "config", "mappings", "policy", "versions" ]
choices: [ "aliases", "all", "config", "mappings", "policy", "versions", "tags" ]
default: "all"
type: str
function_name:
Expand Down Expand Up @@ -164,6 +164,7 @@ def all_details(client, module):
lambda_info[function_name].update(policy_details(client, module)[function_name])
lambda_info[function_name].update(version_details(client, module)[function_name])
lambda_info[function_name].update(mapping_details(client, module)[function_name])
lambda_info[function_name].update(tags_details(client, module)[function_name])
else:
lambda_info.update(config_details(client, module))

Expand Down Expand Up @@ -199,6 +200,7 @@ def config_details(client, module):

functions = dict()
for func in lambda_info.pop('function_list', []):
func['tags'] = client.get_function(FunctionName=func['FunctionName']).get('Tags', {})
functions[func['FunctionName']] = camel_dict_to_snake_dict(func)
return functions

Expand Down Expand Up @@ -288,6 +290,31 @@ def version_details(client, module):
return {function_name: camel_dict_to_snake_dict(lambda_info)}


def tags_details(client, module):
"""
Returns tag details for one or all lambda functions.
:param client: AWS API client reference (boto3)
:param module: Ansible module reference
:return dict:
"""

lambda_info = dict()

function_name = module.params.get('function_name')
if function_name:
try:
lambda_info.update(tags=client.get_function(aws_retry=True, FunctionName=function_name).get('Tags', {}))
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} tags".format(function_name))
else:
module.fail_json(msg='Parameter function_name required for query=tags.')

return {function_name: camel_dict_to_snake_dict(lambda_info)}


def main():
"""
Main entry point.
Expand All @@ -296,8 +323,8 @@ def main():
"""
argument_spec = dict(
function_name=dict(required=False, default=None, aliases=['function', 'name']),
query=dict(required=False, choices=['aliases', 'all', 'config', 'mappings', 'policy', 'versions'], default='all'),
event_source_arn=dict(required=False, default=None)
query=dict(required=False, choices=['aliases', 'all', 'config', 'mappings', 'policy', 'versions', 'tags'], default='all'),
event_source_arn=dict(required=False, default=None),
)

module = AnsibleAWSModule(
Expand Down Expand Up @@ -326,6 +353,7 @@ def main():
mappings='mapping_details',
policy='policy_details',
versions='version_details',
tags='tags_details',
)

this_module_function = globals()[invocations[module.params['query']]]
Expand Down

0 comments on commit 1034885

Please sign in to comment.