
    Vh)                         d Z dZdZ	 ddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ d	 Zd
 Zd Zd Zedk(  r e        yy# e$ r Y 9w xY w)a  
---
module: cloudtrail_info
version_added: 5.0.0
short_description: Gather information about trails in AWS Cloud Trail
description:
  - Gather information about trails in AWS CloudTrail.
author: "Gomathi Selvi Srinivasan (@GomathiselviS)"
options:
  trail_names:
    type: list
    elements: str
    default: []
    description:
      - Specifies a list of trail names, trail ARNs, or both, of the trails to describe.
      - If an empty list is specified, information for the trail in the current region is returned.
  include_shadow_trails:
    type: bool
    default: true
    description: Specifies whether to include shadow trails in the response.
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.boto3
a=  
# Note: These examples do not set authentication details, see the AWS Guide for details.

# Gather information about all trails
- amazon.aws.cloudtrail_info:

# Gather information about a particular trail
- amazon.aws.cloudtrail_info:
    trail_names:
      - arn:aws:cloudtrail:us-east-2:123456789012:trail/MyTrail
a3  
trail_list:
    description: List of trail objects. Each element consists of a dict with all the information related to that cloudtrail.
    type: list
    elements: dict
    returned: always
    contains:
        name:
            description: Name of the trail.
            type: str
            sample: "MyTrail"
        s3_bucket_name:
            description: Name of the Amazon S3 bucket into which CloudTrail delivers the trail files.
            type: str
            sample: "aws-cloudtrail-logs-xxxx"
        s3_key_prefix:
            description: Amazon S3 key prefix that comes after the name of the bucket that is designated for log file delivery.
            type: str
            sample: "xxxx"
        sns_topic_arn:
            description: ARN of the Amazon SNS topic that CloudTrail uses to send notifications when log files are delivered.
            type: str
            sample: "arn:aws:sns:us-east-2:123456789012:MyTopic"
        include_global_service_events:
            description: If True, AWS API calls from AWS global services such as IAM are included.
            type: bool
            sample: true
        is_multi_region_trail:
            description: Specifies whether the trail exists only in one region or exists in all regions.
            type: bool
            sample: true
        home_region:
            description: The region in which the trail was created.
            type: str
            sample: "us-east-1"
        trail_arn:
            description: Specifies the ARN of the trail.
            type: str
            sample: "arn:aws:cloudtrail:us-east-2:123456789012:trail/MyTrail"
        log_file_validation_enabled:
            description: Specifies whether log file validation is enabled.
            type: bool
            sample: true
        cloud_watch_logs_log_group_arn:
            description: Specifies an ARN, that represents the log group to which CloudTrail logs will be delivered.
            type: str
            sample: "arn:aws:sns:us-east-2:123456789012:Mylog"
        cloud_watch_logs_role_arn:
            description: Specifies the role for the CloudWatch Logs endpoint to assume to write to a user's log group.
            type: str
            sample: "arn:aws:sns:us-east-2:123456789012:Mylog"
        kms_key_id:
            description: Specifies the KMS key ID that encrypts the logs delivered by CloudTrail.
            type: str
            sample: "arn:aws:kms:us-east-2:123456789012:key/12345678-1234-1234-1234-123456789012"
        has_custom_event_selectors:
            description: Specifies if the trail has custom event selectors.
            type: bool
            sample: true
        has_insight_selectors:
            description: Specifies whether a trail has insight types specified in an InsightSelector list.
            type: bool
            sample: true
        is_organization_trail:
            description: Specifies whether the trail is an organization trail.
            type: bool
            sample: true
        is_logging:
            description: Whether the CloudTrail is currently logging AWS API calls.
            type: bool
            sample: true
        latest_delivery_error:
            description: Displays any Amazon S3 error that CloudTrail encountered when attempting to deliver log files to the designated bucket.
            type: str
        latest_notification_attempt_time:
            description: Specifies the date and time that CloudTrail last attempt to deliver a notification.
            type: str
        latest_notification_attempt_succeeded:
            description: Specifies the date and time that CloudTrail last successful attempt to deliver a notification.
            type: str
        latest_notification_error:
            description: Displays any Amazon SNS error that CloudTrail encountered when attempting to send a notification.
            type: str
        latest_delivery_attempt_succeeded:
            description: Specifies the date and time that CloudTrail last successful attempt to deliver log files to an account's Amazon S3 bucket.
            type: str
        latest_delivery_attempt_time:
            description: Specifies the date and time that CloudTrail last attempt to deliver log files to an account's Amazon S3 bucket.
            type: str
        latest_delivery_time:
            description: Specifies the date and time that CloudTrail last delivered log files to an account's Amazon S3 bucket.
            type: str
        start_logging_time:
            description: Specifies the most recent date and time when CloudTrail started recording API calls for an AWS account.
            type: str
        stop_logging_time:
            description: Specifies the most recent date and time when CloudTrail stopped recording API calls for an AWS account.
            type: str
        latest_cloud_watch_logs_delivery_error:
            description: Displays any CloudWatch Logs error that CloudTrail encountered when attempting to deliver logs to CloudWatch Logs.
            type: str
        latest_cloud_watch_logs_delivery_time:
            description: Displays the most recent date and time when CloudTrail delivered logs to CloudWatch Logs.
            type: str
        latest_digest_delivery_time:
            description: Specifies the date and time that CloudTrail last delivered a digest file to an account's Amazon S3 bucket.
            type: str
        latest_digest_delivery_error:
            description: Displays any Amazon S3 error that CloudTrail encountered when attempting to deliver a digest file to the designated bucket.
            type: str
        resource_id:
            description: Specifies the ARN of the resource.
            type: str
        tags:
            description: Any tags assigned to the cloudtrail.
            type: dict
            returned: always
            sample: "{ 'my_tag_key': 'my_tag_value' }"
    N)camel_dict_to_snake_dict)AnsibleAWSModule)AWSRetry)boto3_tag_list_to_ansible_dictc                 :   g }	 | j                  d      }j                         D ]  }|j                  t        |              |S # t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d }~d }~ww xY w)Nlist_trailsFailed to get the trails.msg)	get_paginatorbotocore
exceptionsClientErrorBotoCoreErrorfail_json_awspaginateextendlist_cloud_trails)
connectionmodule
all_trailsresultetrails         n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/amazon/aws/plugins/modules/cloudtrail_info.py
get_trailsr      s    JA))-8 " 4+E234	 ++X-@-@-N-NO AQ$?@@As   A 7B=BBc                 8    | d   D cg c]  }|d   	 c}S c c}w )NTrailsTrailARN )
trail_dictxs     r   r   r      s    #-h#78aAjM888s   c                 ,   i }|j                   j                  d      }|j                   j                  d      }|st        | |      }	 | j                  ||d      }g }d   D ]w  }	 | j                  |d   d	      }	|j                  |	       	 | j                  |d   g      }
|
d   D ]  }|j                  |        	 |j                  t        |             y |D ]$  }d|v rt        |d   dd      |d<   |d= d|v s"|d= & ||d<   |S # t        j
                  j                  t        j
                  j                  f$ r}|j                  |d       Y d }~d }~ww xY w# t        j
                  j                  t        j
                  j                  f$ r}|j                  |d
       Y d }~'d }~ww xY w# t        j
                  j                  t        j
                  j                  f$ r}|j                  d|        Y d }~Md }~ww xY w)Ntrail_namesinclude_shadow_trailsT)trailNameListincludeShadowTrails	aws_retryr	   r
   	trailListr   )Namer(   zFailed to get the trail status)ResourceIdListResourceTagListzFailed to get the trail tags - 	tags_listkeyvaluetagsresponse_metadata
trail_list)paramsgetr   describe_trailsr   r   r   r   r   get_trail_statusupdate	list_tagswarnappendr   r   )r   r   outputtrail_name_listr%   r   r   snaked_cloud_trailcloud_trailstatus_dicttag_listtag_dicttrs                r   get_trail_detailrC      s7   Fmm''6O"MM--.EF$Z8A++)?T`d , 
 k* I	J$55;z;R^b5cK{+	?!++K
<S;T+UH$%67 -""8,- 	!!":;"GHI ! ("7;PWXBvJ;"$&'( .F<M5 ++X-@-@-N-NO AQ$?@@A ##//1D1D1R1RS 	J  (H II	J ##//1D1D1R1RS 	?KK9!=>>	?sH   D ''E%1F=7E"EE"%7F:F55F:=7H4HHc                     t        t        ddg       t        dd            } t        | d      }	 |j                  d	t        j                         
      }t        |      } |j                  di | y # t
        j                  j                  t
        j                  j                  f$ r}|j                  |d       Y d }~nd }~ww xY w)Nliststr)typeelementsdefaultboolT)rG   rI   )r$   r%   )argument_specsupports_check_mode
cloudtrail)retry_decoratorzFailed to connect to AWSr
   r    )dictr   clientr   jittered_backoffr   r   r   r   r   rC   	exit_json)rK   r   r   r   r   s        r   mainrS      s    fubA"=M
 MtTF@]]<AZAZA\]]
 j&1FFv ++X-@-@-N-NO @Q$>??@s   %A7 77C.CC__main__)DOCUMENTATIONEXAMPLESRETURNr   ImportError0ansible.module_utils.common.dict_transformationsr   ;ansible_collections.amazon.aws.plugins.module_utils.modulesr   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr   ;ansible_collections.amazon.aws.plugins.module_utils.taggingr   r   r   rC   rS   __name__r        r   <module>r_      sq   4
v
p	 V X P f9$N  zF a  		s   > AA