
    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d Zedk(  r e        yy# e$ r Y <w xY w)a?  
---
module: s3_logging
version_added: 1.0.0
short_description: Manage logging facility of an s3 bucket in AWS
description:
  - Manage logging facility of an s3 bucket in AWS
author:
  - Rob White (@wimnat)
options:
  name:
    description:
      - "Name of the s3 bucket."
    required: true
    type: str
  state:
    description:
      - "Enable or disable logging."
    default: present
    choices: [ 'present', 'absent' ]
    type: str
  target_bucket:
    description:
      - "The bucket to log to. Required when state=present."
    type: str
  target_prefix:
    description:
      - "The prefix that should be prepended to the generated log files written to the target_bucket."
    default: ""
    type: str
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.boto3
z # a  
# Note: These examples do not set authentication details, see the AWS Guide for details.

- name: Enable logging of s3 bucket mywebsite.com to s3 bucket mylogs
  community.aws.s3_logging:
    name: mywebsite.com
    target_bucket: mylogs
    target_prefix: logs/mywebsite.com
    state: present

- name: Remove logging on an s3 bucket
  community.aws.s3_logging:
    name: mywebsite.com
    state: absent
    N)camel_dict_to_snake_dict)is_boto3_error_code)AWSRetry)AnsibleCommunityAWSModulec                 ^    | j                  dd      s|ryy| d   }|d   |k7  ry|d   |k7  ryy)NLoggingEnabledFTTargetBucketTargetPrefix)get)bucket_loggingtarget_buckettarget_prefixloggings       l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/s3_logging.pycompare_bucket_loggingr   J   sJ    .6-.G~-/~-/    c                    	 | j                  d|      }|d   }d	d
ddd}D ]	  }||k(  s	 y |j                  ryt              }t        |      }	|	j                  |       |	|d<   |d= 	 | j                  d||       y# t        d      $ r |j                  d| d       Y 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 }~yd }~ww xY w)NT	aws_retryBucketGrantsNoSuchBucketzTarget Bucket '' not foundmsgz!Failed to fetch target bucket ACLz.http://acs.amazonaws.com/groups/s3/LogDeliveryGroup)URITypeFULL_CONTROL)Grantee
PermissionFResponseMetadata)r   r   AccessControlPolicyz8Failed to update target bucket ACL to allow log delivery)get_bucket_aclr   	fail_jsonbotocore
exceptionsBotoCoreErrorClientErrorfail_json_aws
check_modedictlistappendput_bucket_acl)

connectionmoduler   current_aclcurrent_grantserequired_grantgrantupdated_aclupdated_grantss
             r   verify_aclsr9   X   s|   	I //$}/U$X. LU\]$N
   N" {#K.)N.)*K&'`!!D\g!h = ~. K}o[IJ))'' I 	Q$GHH	I2 --x/B/B/N/NO `Q$^__`s5   B -C> $C;(6C;C66C;>7E5EEc                    |j                   j                  d      }|j                   j                  d      }|j                   j                  d      }d}	 | j                  d|      }	 |t        | ||      z  }t        ||      st        |      } |j                  dd|i| |j                  r|j                  d       | j                  d|d||di      }t              } |j                  dddi| y # t        d      $ r |j	                  d| d	
       Y 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)Nnamer   r   FTr   r   zBucket 'r   r   &Failed to fetch current logging statuschangedr=   r   )r	   r
   )r   r   BucketLoggingStatuszFailed to enable bucket logging )paramsr   get_bucket_loggingr   r%   r&   r'   r(   r)   r*   r9   r   r   	exit_jsonr+   put_bucket_logging)	r0   r1   bucket_namer   r   r=   r   r4   results	            r   enable_bucket_loggingrG   }   s   --##F+KMM%%o6MMM%%o6MGN#66k6ZG;z6=AA%nm]S5nENF?W??T*.. $1$1#! / 	
 &f-FF,T,V,C ~. Bx}K@A))'' N 	Q$LMM	N6 --x/B/B/N/NO GQ$EFFGs7   C< )A3E9 <$E6"6E6E11E697G0GGc                    |j                   j                  d      }	 | j                  d|      }t        d d       s|j                  d       |j                  r|j                  d       	   t        j                  dg	      | j                        |i 
       |j                  d       y # 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)Nr;   Tr   r<   r   Fr>   InvalidTargetBucketForLogging)catch_extra_error_codes)r   r?   z Failed to disable bucket logging)rA   r   rB   r&   r'   r(   r)   r*   r   rC   r+   r   jittered_backoffrD   )r0   r1   rE   r   r4   s        r   disable_bucket_loggingrL      sC   --##F+KN#66k6Z ".$='&H	
\!!;Z:[\))	
"	6 T"! --x/B/B/N/NO NQ$LMMN --x/B/B/N/NO HQ$FGGHs/   B0 ./D 07D'C??D7E>EEc                  `   t        t        d      t        dd       t        dd      t        ddddg      	      } t        | d
      }|j                  dt        j                               }|j
                  j                  d      }|dk(  rt        ||       y |dk(  rt        ||       y y )NT)requiredF)rN   default presentabsent)rN   rO   choices)r;   r   r   state)argument_specsupports_check_modes3)retry_decoratorrT   )	r,   AnsibleAWSModuleclientr   rK   rA   r   rG   rL   )rU   r1   r0   rT   s       r   mainr[      s    4 E48E26E9y(>ST	M MtTFtX5N5N5PQJMMg&E	j&1	(	z62 
r   __main__)DOCUMENTATIONRETURNEXAMPLESr&   ImportError0ansible.module_utils.common.dict_transformationsr   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr   >ansible_collections.community.aws.plugins.module_utils.modulesr   rY   r   r9   rG   rL   r[   __name__r@   r   r   <module>rf      sv   "H 
 	 V \ P x"J)-X#03& zF k  		s   A A	A	