| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: cloudwatchlogs_log_group_info |
| 10 |
version_added: 5.0.0 |
| 11 |
short_description: Get information about log_group in CloudWatchLogs |
| 12 |
description: |
| 13 |
- Lists the specified log groups. You can list all your log groups or filter the results by prefix. |
| 14 |
- This module was originally added to C(community.aws) in release 1.0.0. |
| 15 |
author: |
| 16 |
- Willian Ricardo (@willricardo) <[email protected]> |
| 17 |
options: |
| 18 |
log_group_name: |
| 19 |
description: |
| 20 |
- The name or prefix of the log group to filter by. |
| 21 |
type: str |
| 22 |
extends_documentation_fragment: |
| 23 |
- amazon.aws.common.modules |
| 24 |
- amazon.aws.region.modules |
| 25 |
- amazon.aws.boto3 |
| 26 |
""" |
| 27 |
|
| 28 |
EXAMPLES = r""" |
| 29 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 30 |
- amazon.aws.cloudwatchlogs_log_group_info: |
| 31 |
log_group_name: test-log-group |
| 32 |
""" |
| 33 |
|
| 34 |
RETURN = r""" |
| 35 |
log_groups: |
| 36 |
description: Return the list of complex objects representing log groups. |
| 37 |
returned: success |
| 38 |
type: complex |
| 39 |
contains: |
| 40 |
log_group_name: |
| 41 |
description: The name of the log group. |
| 42 |
returned: always |
| 43 |
type: str |
| 44 |
creation_time: |
| 45 |
description: The creation time of the log group. |
| 46 |
returned: always |
| 47 |
type: int |
| 48 |
retention_in_days: |
| 49 |
description: The number of days to retain the log events in the specified log group. |
| 50 |
returned: always |
| 51 |
type: int |
| 52 |
metric_filter_count: |
| 53 |
description: The number of metric filters. |
| 54 |
returned: always |
| 55 |
type: int |
| 56 |
arn: |
| 57 |
description: The Amazon Resource Name (ARN) of the log group. |
| 58 |
returned: always |
| 59 |
type: str |
| 60 |
stored_bytes: |
| 61 |
description: The number of bytes stored. |
| 62 |
returned: always |
| 63 |
type: str |
| 64 |
kms_key_id: |
| 65 |
description: The Amazon Resource Name (ARN) of the CMK to use when encrypting log data. |
| 66 |
returned: always |
| 67 |
type: str |
| 68 |
tags: |
| 69 |
description: A dictionary representing the tags on the log group. |
| 70 |
returned: always |
| 71 |
type: dict |
| 72 |
version_added: 4.0.0 |
| 73 |
version_added_collection: community.aws |
| 74 |
""" |
| 75 |
|
| 76 |
try: |
| 77 |
import botocore |
| 78 |
except ImportError: |
| 79 |
pass # Handled by AnsibleAWSModule |
| 80 |
|
| 81 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 82 |
|
| 83 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 84 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 85 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 86 |
|
| 87 |
|
| 88 |
@AWSRetry.exponential_backoff() |
| 89 |
def list_tags_log_group_with_backoff(client, log_group_name): |
| 90 |
return client.list_tags_log_group(logGroupName=log_group_name) |
| 91 |
|
| 92 |
|
| 93 |
@AWSRetry.exponential_backoff() |
| 94 |
def describe_log_groups_with_backoff(client, **kwargs): |
| 95 |
paginator = client.get_paginator("describe_log_groups") |
| 96 |
return paginator.paginate(**kwargs).build_full_result() |
| 97 |
|
| 98 |
|
| 99 |
def describe_log_group(client, log_group_name, module): |
| 100 |
params = {} |
| 101 |
if log_group_name: |
| 102 |
params["logGroupNamePrefix"] = log_group_name |
| 103 |
try: |
| 104 |
desc_log_group = describe_log_groups_with_backoff(client, **params) |
| 105 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 106 |
module.fail_json_aws(e, msg=f"Unable to describe log group {log_group_name}") |
| 107 |
|
| 108 |
for log_group in desc_log_group["logGroups"]: |
| 109 |
log_group_name = log_group["logGroupName"] |
| 110 |
try: |
| 111 |
tags = list_tags_log_group_with_backoff(client, log_group_name) |
| 112 |
except is_boto3_error_code("AccessDeniedException"): |
| 113 |
tags = {} |
| 114 |
module.warn(f"Permission denied listing tags for log group {log_group_name}") |
| 115 |
except ( |
| 116 |
botocore.exceptions.ClientError, |
| 117 |
botocore.exceptions.BotoCoreError, |
| 118 |
) as e: # pylint: disable=duplicate-except |
| 119 |
module.fail_json_aws(e, msg=f"Unable to describe tags for log group {log_group_name}") |
| 120 |
log_group["tags"] = tags.get("tags", {}) |
| 121 |
|
| 122 |
return desc_log_group |
| 123 |
|
| 124 |
|
| 125 |
def main(): |
| 126 |
argument_spec = dict( |
| 127 |
log_group_name=dict(), |
| 128 |
) |
| 129 |
|
| 130 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 131 |
|
| 132 |
try: |
| 133 |
logs = module.client("logs") |
| 134 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 135 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 136 |
|
| 137 |
desc_log_group = describe_log_group(client=logs, log_group_name=module.params["log_group_name"], module=module) |
| 138 |
final_log_group_snake = [] |
| 139 |
|
| 140 |
for log_group in desc_log_group["logGroups"]: |
| 141 |
final_log_group_snake.append(camel_dict_to_snake_dict(log_group, ignore_list=["tags"])) |
| 142 |
|
| 143 |
desc_log_group_result = dict(changed=False, log_groups=final_log_group_snake) |
| 144 |
module.exit_json(**desc_log_group_result) |
| 145 |
|
| 146 |
|
| 147 |
if __name__ == "__main__": |
| 148 |
main() |