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>

This commit was initially merged in https://github.com/ansible-collections/community.aws
See: ansible-collections/community.aws@5235a3b
  • Loading branch information
ngoduykhanh authored and goneri committed Sep 21, 2022
1 parent ae18553 commit 9243cb2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
34 changes: 31 additions & 3 deletions plugins/modules/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
9 changes: 9 additions & 0 deletions tests/integration/targets/lambda/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@
tracing_mode: Active
handler: mini_lambda.handler
role: '{{ lambda_role_name }}'
tags:
CamelCase: 'ACamelCaseValue'
snake_case: 'a_snake_case_value'
'Spaced key': 'A value with spaces'
register: update_result
- name: assert that update succeeded
assert:
Expand All @@ -145,6 +149,10 @@
tracing_mode: Active
handler: mini_lambda.handler
role: '{{ lambda_role_name }}'
tags:
CamelCase: 'ACamelCaseValue'
snake_case: 'a_snake_case_value'
'Spaced key': 'A value with spaces'
register: update_result
- name: assert that update succeeded
assert:
Expand Down Expand Up @@ -189,6 +197,7 @@
- lambda_infos_all.function[lambda_function_name].description == ""
- lambda_infos_all.function[lambda_function_name].function_arn is defined
- lambda_infos_all.function[lambda_function_name].handler == "mini_lambda.handler"
- lambda_infos_all.function[lambda_function_name].tags is defined

- name: lambda_info | Gather version infos for given lambda function
lambda_info:
Expand Down

0 comments on commit 9243cb2

Please sign in to comment.