
    Vh                        d Z dZdZddlZddlZddlmZ ddlmZ ddlm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dlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  dede!dee"e#f   fdZ$dede!dee"e#f   fdZ%dede!dee"e#f   fdZ&dede!dee"e#f   fdZ'dede!dee"e#f   fdZ(dede!dee"e#f   fdZ)dede!dee"e#f   fdZ*dede!dee"e#f   fdZ+dede!de#fd Z,dede!dee"e"f   fd!Z-dede!dee"e#f   fd"Z.dede!dee"e#f   fd#Z/defd$Z0d%e!de"fd&Z1 ejd                  d'(      djd%e!d)e!d*e"de"fd+       Z3 ejd                  d'd,d-g.      d/        Z4 ejd                  d'd,d-g.      d0        Z5 ejd                  d'd,d-g.      d1        Z6 ejd                  d'd,d-g.      d2        Z7 ejd                  d'd,d-g.      de"fd3       Z8 ejd                  d'd,d-g.      d%e!d4e#ddfd5       Z9 ejd                  d'd,d-g.      d%e!d6e#fd7       Z: ejd                  d'd,d-g.      d%e!d8e!ddfd9       Z; ejd                  d'd,d-g.      d%e!d:e#fd;       Z< ejd                  d'd,d-g.      d%e!fd<       Z= ejd                  d'd,d-g.      d%e!de!fd=       Z> ejd                  d'd,d-g.      d%e!d>e!fd?       Z? ejd                  d'd,d-g.      d%e!de!fd@       Z@ ejd                  d'd,d-g.      d%e!de#fdA       ZA ejd                  d'd,d-g.      d%e!dBe!fdC       ZB ejd                  d'd,d-g.      d%e!de"fdD       ZC ejd                  d'd,d-g.      d%e!de#fdE       ZD ejd                  d'd,d-g.      d%e!de"fdF       ZEdede!dGe#de#fdHZF ejd                  d'd,d-g.      d%e!dIe#ddfdJ       ZGdede!dGe"de#fdKZH ejd                  d'd,d-g.      d%e!dIe"ddfdL       ZI ejd                  d'd,d-g.      d%e!ddfdM       ZJ ejd                  d'd,d-g.      d%e!ddfdN       ZK ejd                  dOd-g.      d%e!ddfdP       ZL ejd                  d'd,d-g.      d%e!dQe#ddfdR       ZM ejd                  d'd,d-g.      d%e!ddfdS       ZN ejd                  d'd,d-g.      d%e!ddfdT       ZO ejd                  d'd,d-g.      d%e!dUe!ddfdV       ZP	 dkded%e!dWe#dXe"de#f
dYZQ	 dkded%e!dZe"de!fd[ZR	 dlded%e!dGe#de#fd\ZS	 dlded%e!dGe"de"fd]ZTded%e!dBe#de#fd^ZUded%e!d_e#de#fd`ZVd%e!de#fdaZWd%e!de#fdbZXd%e!de!fdcZYdeee!      fddZZdeeee!e!f         fdeZ[dede!ddfdfZ\deddfdgZ]dh Z^e_dik(  r e^        yy# e
$ r Y fw xY w)mau+  
---
module: s3_bucket
version_added: 1.0.0
short_description: Manage S3 buckets in AWS, DigitalOcean, Ceph, Walrus, FakeS3 and StorageGRID
description:
  - Manage S3 buckets.
  - Compatible with AWS, DigitalOcean, Ceph, Walrus, FakeS3 and StorageGRID.
  - When using non-AWS services, O(endpoint_url) should be specified.
author:
  - Rob White (@wimnat)
  - Aubin Bikouo (@abikouo)
options:
  force:
    description:
      - When trying to delete a bucket, delete all keys (including versions and delete markers)
        in the bucket first (an S3 bucket must be empty for a successful deletion).
    type: bool
    default: false
  name:
    description:
      - Name of the S3 bucket.
    required: true
    type: str
  policy:
    description:
      - The JSON policy as a string. Set to the string V("null") to force the absence of a policy.
    type: json
  ceph:
    description:
      - Enable API compatibility with Ceph RGW.
      - It takes into account the S3 API subset working with Ceph in order to provide the same module
        behaviour where possible.
      - Requires O(endpoint_url) if O(ceph=true).
    aliases: ['rgw']
    type: bool
    default: false
  requester_pays:
    description:
      - With Requester Pays buckets, the requester instead of the bucket owner pays the cost
        of the request and the data download from the bucket.
    type: bool
  state:
    description:
      - Create or remove the S3 bucket.
    required: false
    default: present
    choices: [ 'present', 'absent' ]
    type: str
  versioning:
    description:
      - Whether versioning is enabled or disabled (note that once versioning is enabled, it can only be suspended).
    type: bool
  encryption:
    description:
      - Describes the default server-side encryption to apply to new objects in the bucket.
        In order to remove the server-side encryption, the encryption needs to be set to 'none' explicitly.
      - "Note: Since January 2023 Amazon S3 doesn't support disabling encryption on S3 buckets."
    choices: [ 'none', 'AES256', 'aws:kms' ]
    type: str
  encryption_key_id:
    description:
      - KMS master key ID to use for the default encryption.
      - If not specified then it will default to the AWS provided KMS key.
      - This parameter is only supported if O(encryption) is V(aws:kms).
    type: str
  bucket_key_enabled:
    description:
      - Enable S3 Bucket Keys for SSE-KMS on new objects.
      - See the AWS documentation for more information
        U(https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucket-key.html).
      - Bucket Key encryption is only supported if O(encryption=aws:kms).
    required: false
    type: bool
    version_added: 4.1.0
  public_access:
    description:
      - Configure public access block for S3 bucket.
      - This option cannot be used together with O(delete_public_access).
      - |
        Note: At the end of April 2023 Amazon updated the default settings to block public access by
        default.  While the defaults for this module remain unchanged, it is necessary to explicitly
        pass the O(public_access) parameter to enable public access ACLs.
    suboptions:
      block_public_acls:
        description: Sets BlockPublicAcls value.
        type: bool
        default: false
      block_public_policy:
        description: Sets BlockPublicPolicy value.
        type: bool
        default: false
      ignore_public_acls:
        description: Sets IgnorePublicAcls value.
        type: bool
        default: false
      restrict_public_buckets:
        description: Sets RestrictPublicAcls value.
        type: bool
        default: false
    type: dict
    version_added: 1.3.0
  delete_public_access:
    description:
      - Delete public access block configuration from bucket.
      - This option cannot be used together with a O(public_access) definition.
    default: false
    type: bool
    version_added: 1.3.0
  object_ownership:
    description:
      - Allow bucket's ownership controls.
      - V(BucketOwnerEnforced) - ACLs are disabled and no longer affect access permissions to your
        bucket. Requests to set or update ACLs fail. However, requests to read ACLs are supported.
        Bucket owner has full ownership and control. Object writer no longer has full ownership and
        control.
      - V(BucketOwnerPreferred) - Objects uploaded to the bucket change ownership to the bucket owner
        if the objects are uploaded with the bucket-owner-full-control canned ACL.
      - V(ObjectWriter) - The uploading account will own the object
        if the object is uploaded with the bucket-owner-full-control canned ACL.
      - This option cannot be used together with a O(delete_object_ownership) definition.
      - V(BucketOwnerEnforced) has been added in version 3.2.0.
      - "Note: At the end of April 2023 Amazon updated the default setting to V(BucketOwnerEnforced)."
    choices: [ 'BucketOwnerEnforced', 'BucketOwnerPreferred', 'ObjectWriter' ]
    type: str
    version_added: 2.0.0
  object_lock_enabled:
    description:
      - Whether S3 Object Lock to be enabled.
      - Defaults to V(false) when creating a new bucket.
    type: bool
    version_added: 5.3.0
  delete_object_ownership:
    description:
      - Delete bucket's ownership controls.
      - This option cannot be used together with a O(object_ownership) definition.
    default: false
    type: bool
    version_added: 2.0.0
  acl:
    description:
      - The canned ACL to apply to the bucket.
      - If your bucket uses the bucket owner enforced setting for S3 Object Ownership,
        ACLs are disabled and no longer affect permissions.
    choices: [ 'private', 'public-read', 'public-read-write', 'authenticated-read' ]
    type: str
    version_added: 3.1.0
  validate_bucket_name:
    description:
      - Whether the bucket name should be validated to conform to AWS S3 naming rules.
      - On by default, this may be disabled for S3 backends that do not enforce these rules.
      - See U(https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html).
    type: bool
    version_added: 3.1.0
    default: true
  dualstack:
    description:
      - Enables Amazon S3 Dual-Stack Endpoints, allowing S3 communications using both IPv4 and IPv6.
      - Mutually exclusive with O(endpoint_url).
    type: bool
    default: false
    version_added: 6.0.0
  accelerate_enabled:
    description:
      - Enables Amazon S3 Transfer Acceleration, sent data will be routed to Amazon S3 over an optimized network path.
      - Transfer Acceleration is not available in AWS GovCloud (US).
      - See U(https://docs.aws.amazon.com/govcloud-us/latest/UserGuide/govcloud-s3.html#govcloud-S3-diffs).
    type: bool
    version_added: 8.1.0
  object_lock_default_retention:
    description:
      - Default Object Lock configuration that will be applied by default to
        every new object placed in the specified bucket.
      - O(object_lock_enabled) must be included and set to V(True).
      - Object lock retention policy can't be removed.
    suboptions:
      mode:
        description: Type of retention modes.
        choices: [ "GOVERNANCE", "COMPLIANCE"]
        required: true
        type: str
      days:
        description:
            - The number of days that you want to specify for the default retention period.
            - Mutually exclusive with O(object_lock_default_retention.years).
        type: int
      years:
        description:
            - The number of years that you want to specify for the default retention period.
            - Mutually exclusive with O(object_lock_default_retention.days).
        type: int
    type: dict
    version_added: 8.1.0
  inventory:
    description:
      - Enable S3 Inventory, saving list of the objects and their corresponding
        metadata on a daily or weekly basis for an S3 bucket.
    type: list
    elements: dict
    suboptions:
      destination:
        description: Contains information about where to publish the inventory results.
        type: dict
        required: True
        suboptions:
          account_id:
            description: The account ID that owns the destination S3 bucket. If no account ID is provided, the owner is not validated before exporting data.
            type: str
          bucket:
            description: The Amazon Resource Name (ARN) of the bucket where inventory results will be published.
            type: str
            required: True
          format:
            description: Specifies the output format of the inventory results.
            type: str
            choices: [ 'CSV', 'ORC', 'Parquet' ]
            required: True
          prefix:
            description: The prefix that is prepended to all inventory results.
            type: str
      filter:
        description: The prefix that an object must have to be included in the inventory results.
        type: str
      id:
        description: The ID used to identify the inventory configuration.
        type: str
        required: True
      schedule:
        description: Specifies the schedule for generating inventory results.
        type: str
        choices: [ 'Daily', 'Weekly' ]
        required: True
      included_object_versions:
        description: |
            Object versions to include in the inventory list. If set to All, the list includes all the object versions,
            which adds the version-related fields VersionId, IsLatest, and DeleteMarker to the list. If set to Current,
            the list does not contain these version-related fields.
        type: str
        required: True
        choices: [ 'All', 'Current' ]
      optional_fields:
        description: Contains the optional fields that are included in the inventory results.
        type: list
        elements: str
        choices: [ "Size", "LastModifiedDate", "StorageClass", "ETag",
            "IsMultipartUploaded", "ReplicationStatus", "EncryptionStatus",
            "ObjectLockRetainUntilDate", "ObjectLockMode",
            "ObjectLockLegalHoldStatus", "IntelligentTieringAccessTier",
            "BucketKeyStatus", "ChecksumAlgorithm", "ObjectAccessControlList",
            "ObjectOwner" ]
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.tags
  - amazon.aws.boto3

notes:
  - If C(requestPayment), C(policy), C(tagging) or C(versioning)
    operations/API aren't implemented by the endpoint, module doesn't fail
    if each parameter satisfies the following condition.
    O(requester_pays) is V(false), O(policy), O(tags), and O(versioning) are V(None).
  - In release 5.0.0 the O(s3_url) parameter was merged into the O(endpoint_url) parameter,
    O(s3_url) remains as an alias for O(endpoint_url).
  - For Walrus O(endpoint_url) should be set to the FQDN of the endpoint with neither scheme nor path.
  - Support for the E(S3_URL) environment variable has been
    deprecated and will be removed in a release after 2024-12-01, please use the O(endpoint_url) parameter
    or the E(AWS_URL) environment variable.
a  
# Note: These examples do not set authentication details, see the AWS Guide for details.

# Create a simple S3 bucket
- amazon.aws.s3_bucket:
    name: mys3bucket
    state: present

# Create a simple S3 bucket on Ceph Rados Gateway
- amazon.aws.s3_bucket:
    name: mys3bucket
    endpoint_url: http://your-ceph-rados-gateway-server.xxx
    ceph: true

# Remove an S3 bucket and any keys it contains
- amazon.aws.s3_bucket:
    name: mys3bucket
    state: absent
    force: true

# Create a bucket, add a policy from a file, enable requester pays, enable versioning and tag
- amazon.aws.s3_bucket:
    name: mys3bucket
    policy: "{{ lookup('file','policy.json') }}"
    requester_pays: true
    versioning: true
    tags:
      example: tag1
      another: tag2

# Create a simple DigitalOcean Spaces bucket using their provided regional endpoint
- amazon.aws.s3_bucket:
    name: mydobucket
    endpoint_url: 'https://nyc3.digitaloceanspaces.com'

# Create a bucket with AES256 encryption
- amazon.aws.s3_bucket:
    name: mys3bucket
    state: present
    encryption: "AES256"

# Create a bucket with aws:kms encryption, KMS key
- amazon.aws.s3_bucket:
    name: mys3bucket
    state: present
    encryption: "aws:kms"
    encryption_key_id: "arn:aws:kms:us-east-1:1234/5678example"

# Create a bucket with aws:kms encryption, Bucket key
- amazon.aws.s3_bucket:
    name: mys3bucket
    bucket_key_enabled: true
    encryption: "aws:kms"

# Create a bucket with aws:kms encryption, default key
- amazon.aws.s3_bucket:
    name: mys3bucket
    state: present
    encryption: "aws:kms"

# Create a bucket with public policy block configuration
- amazon.aws.s3_bucket:
    name: mys3bucket
    state: present
    public_access:
      block_public_acls: true
      ignore_public_acls: true
      ## keys == 'false' can be omitted, undefined keys defaults to 'false'
      # block_public_policy: false
      # restrict_public_buckets: false

# Delete public policy block from bucket
- amazon.aws.s3_bucket:
    name: mys3bucket
    state: present
    delete_public_access: true

# Create a bucket with object ownership controls set to ObjectWriter
- amazon.aws.s3_bucket:
    name: mys3bucket
    state: present
    object_ownership: ObjectWriter

# Delete onwership controls from bucket
- amazon.aws.s3_bucket:
    name: mys3bucket
    state: present
    delete_object_ownership: true

# Delete a bucket policy from bucket
- amazon.aws.s3_bucket:
    name: mys3bucket
    state: present
    policy: "null"

# This example grants public-read to everyone on bucket using ACL
- amazon.aws.s3_bucket:
    name: mys3bucket
    state: present
    acl: public-read

# Enable transfer acceleration
- amazon.aws.s3_bucket:
    name: mys3bucket
    state: present
    accelerate_enabled: true

# Default Object Lock retention
- amazon.aws.s3_bucket:
    name: mys3bucket
    state: present
    object_lock_enabled: true
    object_lock_default_retention:
      mode: governance
      days: 1
# Bucket with inventory configuration:
- amazon.aws.s3_bucket:
    name: mys3bucket
    state: present
    inventory:
      - id: mys3bucket-inventory-id
        destination:
          bucket: "arn:aws:s3:::mys3inventorybucket"
        optional_fields:
          - "Size"
        included_object_versions: "All"
        schedule: "Weekly"
a  
encryption:
    description: Server-side encryption of the objects in the S3 bucket.
    type: dict
    returned: when O(state=present)
    sample: {
                "SSEAlgorithm": "AES256"
            }
name:
    description: Bucket name.
    returned: when O(state=present)
    type: str
    sample: "a-testing-bucket-name"
object_ownership:
    description: S3 bucket's ownership controls.
    type: str
    returned: when O(state=present)
    sample: "BucketOwnerPreferred"
object_lock_default_retention:
    description: S3 bucket's object lock retention policy.
    type: dict
    returned: when O(state=present)
    sample: {
        "Days": 1,
        "Mode": "GOVERNANCE",
        "Years": 0,
    }
policy:
    description: S3 bucket's policy.
    type: dict
    returned: when O(state=present)
    sample: {
        "Statement": [
            {
                "Action": "s3:GetObject",
                "Effect": "Allow",
                "Principal": "*",
                "Resource": "arn:aws:s3:::2d3ce10a8210d36d6b4d23b822892074complex/*",
                "Sid": "AddPerm"
            }
        ],
        "Version": "2012-10-17"
    }
requester_pays:
    description: Indicates that the requester was successfully charged for the request.
    type: bool
    returned: when O(state=present)
    sample: true
tags:
    description: S3 bucket's tags.
    type: dict
    returned: when O(state=present)
    sample: {
        "Tag1": "tag1",
        "Tag2": "tag2"
    }
versioning:
    description: S3 bucket's versioning configuration.
    type: dict
    returned: when O(state=present)
    sample: {
        "MfaDelete": "Disabled",
        "Versioning": "Enabled"
    }
    contains:
        MfaDelete:
            description: Specifies whether MFA delete is enabled in the bucket versioning configuration.
            returned: when O(state=presnet) and MfaDelete configured on bucket.
            type: str
        Versioning:
            description: The versioning state of the bucket.
            type: str
            returned: always
acl:
    description: S3 bucket's canned ACL.
    type: dict
    returned: when O(state=present).
    sample: "public-read"
object_lock_enabled:
    description: Whether S3 Object Lock is enabled.
    type: bool
    returned: when O(state=present)
    sample: false
public_access_block:
    description: Bucket public access block configuration.
    returned: when O(state=present)
    type: dict
    sample: {
                "PublicAccessBlockConfiguration": {
                    "BlockPublicAcls": true,
                    "BlockPublicPolicy": true,
                    "IgnorePublicAcls": true,
                    "RestrictPublicBuckets": true
                }
            }
    contains:
        PublicAccessBlockConfiguration:
            description: The PublicAccessBlock configuration currently in effect for this Amazon S3 bucket.
            type: dict
            contains:
                BlockPublicAcls:
                    description: Specifies whether Amazon S3 should block public access control lists (ACLs) for this bucket and objects in this bucket.
                    type: bool
                BlockPublicPolicy:
                    description: Specifies whether Amazon S3 should block public bucket policies for this bucket.
                    type: bool
                IgnorePublicAcls:
                    description: Specifies whether Amazon S3 should ignore public ACLs for this bucket and objects in this bucket.
                    type: bool
                RestrictPublicBuckets:
                    description: Specifies whether Amazon S3 should restrict public bucket policies for this bucket.
                    type: bool
accelerate_enabled:
    description: S3 bucket acceleration status.
    type: bool
    returned: O(state=present)
    sample: true
bucket_inventory:
    description: S3 bucket inventory configuration.
    type: list
    returned: when O(state=present)
    sample: [
        {
            "IsEnabled": true,
            "Id": "9c2a337ba5fd64de777f499441f83093-inventory-target",
            "Destination": {
                "S3BucketDestination": {
                    "Bucket": "arn:aws:s3:::9c2a337ba5fd64de777f499441f83093-inventory-target",
                    "Format": "CSV"
                    }
                },
            "IncludedObjectVersions": "All",
            "Schedule": {
                "Frequency": "Daily"
            },
            "OptionalFields": []
        }
        ]
    N)Iterator)List)Tupleto_text)snake_dict_to_camel_dict)string_types)is_boto3_error_code)AnsibleAWSModule)compare_policies)AWSRetry)get_s3_bucket_location)$list_bucket_inventory_configurations)s3_extra_params)validate_bucket_name)ansible_dict_to_boto3_tag_list)boto3_tag_list_to_ansible_dictmodulenamereturnc                    |j                   j                  d      }d}i }	 t        | |      }|Sd}|r|j                  d      dk7  rd}n|s|j                  d      dk(  rd}|r	 t        | ||       d}t        || ||      }|j                  dd
      |j                  dd
      d}||fS # t        j
                  j                  t        j
                  j                  f$ r}|j                  |d	       Y d}~d}~ww xY w# t        ddg      $ r"}||j                  |d	       Y d}~|fS d}~wt        d      $ r3}||j                  |d	       |j                  d       Y d}~|fS d}~wt        j
                  j                  t        j
                  j                  f$ r }|j                  |d	       Y d}~|fS d}~ww xY w)a  
    Manage versioning for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        module (AnsibleAWSModule): The Ansible module object.
        name (str): The name of the bucket to handle versioning for.
    Returns:
        A tuple containing a boolean indicating whether versioning
        was changed and a dictionary containing the updated versioning status.
    
versioningFNStatusEnabled	SuspendedTz"Failed to update bucket versioningmsgDisabled	MFADelete)
Versioning	MfaDeleteNotImplementedXNotImplementedz=Bucket versioning is not supported by the current S3 EndpointAccessDeniedzFailed to get bucket versioningz'AccessDenied fetching bucket versioning)paramsgetget_bucket_versioningput_bucket_versioningbotocore
exceptionsBotoCoreErrorClientErrorfail_json_awswait_versioning_is_appliedr
   warn)		s3_clientr   r   r   versioning_changedversioning_statusrequired_versioningeversioning_results	            h/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/amazon/aws/plugins/modules/s3_bucket.pyhandle_bucket_versioningr7   <  s    ""<0J"
1)TB !"&/33H=J&/#$5$9$9($Cy$P&1#"V))T;NO)-& %?vyRVXk$l! ,//*E*..{JG

 000 !++998;N;N;Z;Z[ V((0T(UUV1  02CDE i!  (g h@ 000? ~. ?!  (I J=>>8 0005 	))'' G 	Q$EFF. 0007GsG   D %B+ +7C?"C::C?GD//G &E..:G(GGc                    |j                   j                  d      }d}i }	 t        | |      }|I|rdnd}||k7  r>t        | ||       t	        || ||d      }|t        | ||       t	        || ||d      }d}||fS # t        dd	g      $ r"}||j                  |d
       Y d}~||fS d}~wt        d      $ r3}||j                  |d       |j                  d       Y d}~||fS d}~wt        j                  j                  t        j                  j                  f$ r }|j                  |d       Y d}~||fS d}~ww xY w)a  
    Manage requester pays setting for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        module (AnsibleAWSModule): The Ansible module object.
        name (str): The name of the bucket to handle requester pays setting for.
    Returns:
        A tuple containing a boolean indicating whether requester pays setting
        was changed and a dictionary containing the updated requester pays status.
    requester_paysFN	RequesterBucketOwnershould_failTr"   r#   zBBucket request payment is not supported by the current S3 Endpointr   r$   $Failed to get bucket request paymentz,AccessDenied fetching bucket request payment)r%   r&   get_bucket_request_paymentput_bucket_request_paymentwait_payer_is_appliedr
   r-   r/   r)   r*   r+   r,   )r0   r   r   r9   requester_pays_changedrequester_pays_statuspayerr4   s           r6   handle_bucket_requester_paysrE   s  s    ]]&&'78N". :9d K %#1K}E$-*9dEB(=fiQUW\jo(p%(0 /y$F,A&)UY[`nr,s))-&!>111  02CDE n%  (l m, ">11+ ~. D%  (N OBCC$ ">11! 	))'' L 	Q$JKK ">11#Ls/   A< <EB))E:&C((:E"D==Ec                    |j                   j                  d      }|j                   j                  d      }d}i }i }	 t        | |      }|&t        |d      }||k(  r|}nt	        | ||       d}|}|r|i k(  r|}||fS t        | |       d}i }||fS # t        ddg      $ r"}	||j                  |	d	
       Y d}	~	||fS d}	~	wt        d      $ r3}	||j                  |	d
       |j                  d       Y d}	~	||fS d}	~	wt        j                  j                  t        j                  j                  f$ r }	|j                  |	d
       Y d}	~	||fS d}	~	ww xY w)a  
    Manage public access configuration for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        module (AnsibleAWSModule): The Ansible module object.
        name (str): The name of the bucket to handle public access configuration for.
    Returns:
        A tuple containing a boolean indicating whether public access configuration
        was changed and a dictionary containing the updated public access configuration.
    public_accessdelete_public_accessFNTcapitalize_firstr"   r#   zJBucket public access settings are not supported by the current S3 Endpointr   r$   z0Failed to get bucket public access configurationz3AccessDenied fetching bucket public access settings)r%   r&   get_bucket_public_accessr   put_bucket_public_accessdelete_bucket_public_accessr
   r-   r/   r)   r*   r+   r,   )
r0   r   r   rG   rH   public_access_changedpublic_access_resultcurrent_public_accesscamel_public_blockr4   s
             r6   "handle_bucket_public_access_configrR     s    MM%%o6M!==,,-CD!!* 8D I $!9-Z^!_$(::'<$(D:LM(,%'9$  $*'<$ !"666 ,It<(,%')$ !"666E  02CDE v$  (t u@ !"666? ~. K$  (Z [IJJ8 !"6665 	))'' X 	Q$VWW. !"6667Xs/   B E#C  E&C??:E9EEc                 ~   |j                   j                  d      }d}d}	 t        | |      }|t        |t              rt        j                  |      }|s#|r!	 t        | |       t        || ||      }d}||fS t        ||      r?	 t        | ||       t        || ||d      }|t        | ||       t        || ||d      }d}||f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!        d	d
g      $ r"}||j                  |d       Y d}~||fS d}~wt!        d      $ r3}||j                  |d       |j#                  d       Y d}~||fS d}~wt        j                  j                  t        j                  j                  f$ r }|j                  |d       Y d}~||fS d}~ww xY w)a  
    Manage bucket policy for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        module (AnsibleAWSModule): The Ansible module object.
        name (str): The name of the bucket to handle the policy for.
    Returns:
        A tuple containing a boolean indicating whether the bucket policy
        was changed and a dictionary containing the updated bucket policy.
    policyFNzFailed to delete bucket policyr   TzFailed to update bucket policyr<   r"   r#   z9Bucket policy is not supported by the current S3 Endpointr$   Failed to get bucket policyz#AccessDenied fetching bucket policy)r%   r&   get_bucket_policy
isinstancer	   jsonloadsdelete_bucket_policyr)   r*   r+   r,   r-   wait_policy_is_appliedr   put_bucket_policyr
   r/   )r0   r   r   rT   policy_changedcurrent_policyr4   s          r6   handle_bucket_policyr_     sj    ]]x(FNN%&*9d; &,/F+nR(D9 "8	4QW!X!% >)) ".&9R%iv> "8	4QWej!k!) &iv>%;FItU[im%nN!%>))# !++998;N;N;Z;Z[ R((0P(QQR !++998;N;N;Z;Z[ R((0P(QQR7  02CDE e  (c dF >))E ~. ;  (E F9::> >)); 	))'' C 	Q$ABB4 >))=Cs_   E6 C D 7D?DD7E3E..E36H<F##H<4&G"":H<H77H<c                 ~   |j                   j                  d      }|j                   j                  d      }d}d}	 t        | |      }|zt        d |j	                         D              }|s#|j                         }|j                  |       |}||k7  r0|r	 t        | ||       n|r	 t        | |       t        || ||      }d	}||fS # t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d}~cd}~ww xY w# t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d}~d}~ww xY w# t        d
dg      $ r"}||j                  |d       Y d}~||fS d}~wt        d      $ r3}||j                  |d       |j!                  d       Y d}~||fS d}~wt        j                  j                  t        j                  j                  f$ r }|j                  |d       Y d}~||fS d}~ww xY w)a  
    Manage tags for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        module (AnsibleAWSModule): The Ansible module object.
        name (str): The name of the bucket to handle tags for.
    Returns:
        A tuple containing a boolean indicating whether tags were changed
        and a dictionary containing the updated tags.
    tags
purge_tagsFNc              3   N   K   | ]  \  }}t        |      t        |      f  y w)Nr   ).0kvs      r6   	<genexpr>z%handle_bucket_tags.<locals>.<genexpr>5  s!     JTQWQZ0Js   #%zFailed to update bucket tagsr   zFailed to delete bucket tagsTr"   r#   z:Bucket tagging is not supported by the current S3 Endpointr$   zFailed to get bucket tagsz!AccessDenied fetching bucket tags)r%   r&   get_current_bucket_tags_dictdictitemscopyupdateput_bucket_taggingr)   r*   r+   r,   r-   delete_bucket_taggingwait_tags_are_appliedr
   r/   )	r0   r   r   ra   rb   bucket_tags_changedcurrent_tags_dictcurrent_copyr4   s	            r6   handle_bucket_tagsrs     sX    ==V$D""<0J$+8DI JTZZ\JJD0557##D)# D(T*9dDA "X1)TB %:&)TSW$X!&*# 111 %//==x?R?R?^?^_ T,,Q4R,SST !) 3 3 A A8CVCVCbCbc X"008V0WWX?  02CDE f  (d eD  111C ~. 9  (C D788<  1119 	))'' A 	Q$?@@2  111;As_   E6 C (D 7D?DD7E3E..E36H<F##H<4&G"":H<H77H<c                    |j                   j                  d      }|j                   j                  d      }|j                   j                  d      }d}d}	 t        | |      }||r|j                  d      nd}|r|j                  d      nd}	|dk(  r |[	 t        | |       t        || |d      }d}n=||k7  s
|dk(  r3|	|k7  r.d|i}|dk(  r||j                  d|i       t        || ||      }d}|D|r|j                  d      nd}|dk(  r*t        | |      |k7  rt        |      }t        || ||      }d}||fS # t        j
                  j                  t        j
                  j                  f$ r}
|j                  |
d	
       Y d}
~
d}
~
ww xY w# t        ddg      $ r"}
||j                  |
d
       Y d}
~
||fS d}
~
wt        d      $ r3}
||j                  |
d
       |j!                  d       Y d}
~
||fS d}
~
wt        j
                  j                  t        j
                  j                  f$ r }
|j                  |
d
       Y d}
~
||fS d}
~
ww xY w)a  
    Manage encryption settings for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        module (AnsibleAWSModule): The Ansible module object.
        name (str): The name of the bucket to handle encryption for.
    Returns:
        A tuple containing a boolean indicating whether encryption settings
        were changed and a dictionary containing the updated encryption settings.
    
encryptionencryption_key_idbucket_key_enabledFNSSEAlgorithmKMSMasterKeyIDnonez"Failed to delete bucket encryptionr   Taws:kmsr"   r#   z=Bucket encryption is not supported by the current S3 Endpointr$   z(Failed to get bucket encryption settingsz0AccessDenied fetching bucket encryption settings)r%   r&   get_bucket_encryptiondelete_bucket_encryptionr)   r*   r+   r,   r-   wait_encryption_is_appliedrl    put_bucket_encryption_with_retryget_bucket_keyboolput_bucket_key_with_retryr
   r/   )r0   r   r   ru   rv   rw   encryption_changedcurrent_encryptioncurrent_encryption_algorithmcurrent_encryption_keyr4   expected_encryptions               r6   handle_bucket_encryptionr   M  s    ""<0J))*=>**+?@*.29dC !Ug+=+A+A.+Qmq(Qc%7%;%;<L%Mim"V#/;Z0DA *DFIW[]a)b&)-&">>)+0FJ[0[+9:*F'!Y.3D3P+224DFW3XY)I&R[]acv)w&)-&)Ug+=+A+A.+Qmq(+y8!)T26HH*./A*B')B69VZ\o)p&)-&111- %//==x?R?R?^?^_ Z,,Q4X,YYZ)  02CDE i!  (g hP 111O ~. H!  (R SFGGH 111E 	))'' P 	Q$NOO> 111GPsH   F D; ;7F2F

FI"F??I&G>>:I8IIc                    |j                   j                  d      }|j                   j                  d      }d}i }	 t        | |      }|}|r|,t        | |       d}d}||fS |||k7  rt	        | ||       d}|}||fS # t
        $ r$}|s||j                  |d       Y d}~||fS d}~wt        dd	g      $ r$}|s||j                  |d
       Y d}~||fS d}~wt        d      $ r5}|s||j                  |d       |j                  d       Y d}~||fS d}~wt        j                  j                  t        j                  j                  f$ r }|j                  |d       Y d}~||fS d}~ww xY w)a  
    Manage ownership settings for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        module (AnsibleAWSModule): The Ansible module object.
        name (str): The name of the bucket to handle ownership for.
    Returns:
        A tuple containing a boolean indicating whether ownership settings were changed
        and a dictionary containing the updated ownership settings.
    delete_object_ownershipobject_ownershipFNTz.Failed to get bucket object ownership settingsr   r"   r#   zCBucket object ownership is not supported by the current S3 Endpointr$   z6AccessDenied fetching bucket object ownership settings)r%   r&   get_bucket_ownership_cntrldelete_bucket_ownershipput_bucket_ownershipKeyErrorr-   r
   r/   r)   r*   r+   r,   )	r0   r   r   r   r   bucket_ownership_changedbucket_ownership_resultbucket_ownershipr4   s	            r6   handle_bucket_ownershipr     s    %mm//0IJ}}(();<$ !;5iF"2( #+'	48+/(*.' $%<<< )#33$Y6FG+/(*:'#%<<<A  Z #&6&B  (X Y6 $%<<<5  02CDE o"&6&B  (m n0 $%<<</ ~. N"&6&B  (X YLMM( $%<<<% 	))'' V 	Q$TUU $%<<<'Vs;   A> >	E3B&&E39CE3)(D:E3E..E3c                 :   |j                   j                  d      }d}i }|r	 | j                  ||       |}d}||fS ||fS # t        $ r }|j	                  |d       Y d}~||fS d}~wt        dd	g      $ r }|j	                  |d
       Y d}~||fS d}~wt        d      $ r }|j	                  |d       Y d}~||fS d}~wt        j                  j                  t        j                  j                  f$ r }|j	                  |d       Y d}~||fS d}~ww xY w)a  
    Manage Access Control List (ACL) for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        module (AnsibleAWSModule): The Ansible module object.
        name (str): The name of the bucket to handle ACL for.
    Returns:
        A tuple containing a boolean indicating whether ACL was changed and a dictionary containing the updated ACL.
    aclF)BucketACLTzFailed to get bucket acl blockr   Nr"   r#   z7Bucket ACLs ar not supported by the current S3 Endpointr$   z)Access denied trying to update bucket ACLzFailed to update bucket ACL)
r%   r&   put_bucket_aclr   r-   r
   r)   r*   r+   r,   )r0   r   r   r   bucket_acl_changedbucket_acl_resultr4   s          r6   handle_bucket_aclr     se    --

E
"C
	G$$Dc$: #!%  000000  	J   (H II 000 #$46G#HI 	c  (a bb 000 #>2 	U  (S TT 000 --++
 	G   (E FF000	Gs;   A 	DA&&D9BD%C  :D:DDc                 J   |j                   j                  d      }i }	 t        | |      }|}|,|s|r|j                  d       |r|s|j                  d       |S # t	        ddg      $ r ||j                  d       Y |S t	        d	      $ r |r|j                  d       d
}Y |S t	        d      $ r ||j                  d       Y |S t
        j                  j                  t
        j                  j                  f$ r}|j                  |d       Y d}~|S d}~ww xY w)aJ  
    Manage object lock configuration for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        module (AnsibleAWSModule): The Ansible module object.
        name (str): The name of the bucket to handle object lock for.
    Returns:
        The updated object lock configuration.
    object_lock_enabledNz;Disabling object lock for existing buckets is not supportedr   z:Enabling object lock for existing buckets is not supportedr"   r#   z2Fetching bucket object lock state is not supported$ObjectLockConfigurationNotFoundErrorFr$   z7Permission denied fetching object lock state for bucketz(Failed to fetch bucket object lock state)
r%   r&   get_bucket_object_lock_enabled	fail_jsonr
   r)   r*   r+   r,   r-   )r0   r   r   r   object_lock_resultobject_lock_statusr4   s          r6   handle_bucket_object_lockr     sh    !--++,ABc;ItL/" )&+=  %b c"+=  %a b-  02CDE W*!UV( ' EF #!]^"   ~. \*!Z[  	))'' P 	Q$NOO Ps)   A $D"#D"*!D"6D"DD"c                 6   |j                   j                  d      }d}d}	 t        | |      }|}|-	 |s|rt        | |       d}d}|r|st	        | |       d}d}||fS ||fS # t
        j                  j                  t
        j                  j                  f$ r }|j                  |d       Y d}~||fS d}~ww xY w# t        ddg      $ r"}||j                  |d	       Y d}~||fS d}~wt        d
dg      $ r |j                  d       d}Y ||fS t        d      $ r"}||j                  |d       Y d}~||fS d}~wt
        j                  j                  t
        j                  j                  f$ r }|j                  |d       Y d}~||fS d}~ww xY w)a  
    Manage transfer accelerate for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        module (AnsibleAWSModule): The Ansible module object.
        name (str): The name of the bucket to handle transfer accelerate for.
    Returns:
        A tuple containing a boolean indicating whether transfer accelerate setting was changed
        and a boolean indicating the transfer accelerate status.
    accelerate_enabledFNTz-Failed to update bucket transfer accelerationr   r"   r#   z<Fetching bucket transfer acceleration state is not supportedUnsupportedArgumentMethodNotAllowedz:Tranfer acceleration is not available in S3 bucket region.r$   z;Permission denied fetching transfer acceleration for bucketz2Failed to fetch bucket transfer acceleration state)r%   r&   get_bucket_accelerate_status&delete_bucket_accelerate_configuration#put_bucket_accelerate_configurationr)   r*   r+   r,   r-   r
   r/   )r0   r   r   r   accelerate_enabled_resultaccelerate_enabled_changedaccelerate_statusr4   s           r6   handle_bucket_accelerater     s     **+?@ %!&!]8DI$5!( )
]).?:9dK15.05-%.?7	4H15.04- &'@@@%'@@@ ''55x7J7J7V7VW ]$$Q,[$\\%'@@@];  02CDE h)  (f g: &'@@@9  57IJK *
 	PQ$)!, &'@@@+ ~. g)  (e f& &'@@@# 	))'' Z 	Q$XYY &'@@@%ZsL   B< (A" "7B9B44B9<FC))&FF!D>>:F8FFc                    |j                   j                  d      }|j                   j                  d      }i }d}	 |rt        | |      }ni }|s|i k7  r|j                  d       |St	        |d      }|j                         D 	
ci c]  \  }	}
|
s	|	|
 }}	}
	 |r||k7  rt        | ||       d}|}n|}||fS ||fS c c}
}	w # t        j                  j                  t        j                  j                  f$ r }|j                  |d	       Y d}~||fS d}~ww xY w# t        d
dg      $ r"}||j                  |d       Y d}~||fS d}~wt        d      $ r"}||j                  |d       Y d}~||fS d}~wt        j                  j                  t        j                  j                  f$ r }|j                  |d       Y d}~||fS d}~ww xY w)a  
    Manage object lock retention configuration for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        module (AnsibleAWSModule): The Ansible module object.
        name (str): The name of the bucket to handle object lock for.
    Returns:
        A tuple containing a boolean indicating whether the bucket object lock
        retention configuration was changed and a dictionary containing the change.
    r   object_lock_default_retentionFz7Removing object lock default retention is not supportedr   NTrI   z5Failed to update bucket object lock default retentionr"   r#   z>Fetching bucket object lock default retention is not supportedr$   zCPermission denied fetching object lock default retention for bucketz:Failed to fetch bucket object lock default retention state)r%   r&   get_object_lock_configurationr   r   rj   put_object_lock_configurationr)   r*   r+   r,   r-   r
   )r0   r   r   r   r   $object_lock_default_retention_result%object_lock_default_retention_changed object_lock_configuration_statusconfre   rf   r4   s               r6   #handle_bucket_object_lock_retentionr   I  s(    !--++,AB$*MM$5$56U$V!+-(,1)e/LYX\/],/1, -1QUW1W!Z[(4+,I\`aD%)ZZ\7TQQAqD7D7e05UY]5]1)T4H<@9;X8;X8 12VVV02VVV 8 ''55x7J7J7V7VW e$$Q,c$dd02VVVe1  02CDE j(4  (h i2 12VVV1 ~. o(4  (m n, 12VVV) 	))'' b 	Q$`aa" 12VVV+bsS   D 
B?B?C 7D<DDG/EGE:::G4GGc           
      *   |j                   j                  d      }g }d}|d|fS 	 t        | |      D ci c]  }|d   |
 }}|D ]  }	t        |	j                  di       d      }
d|	j                  d      d|
j                         D ci c]  \  }}|	|| c}}i|	j                  d      d|	j                  d      ig d}|	j                  dg       D ]  }|d   j                  |        |	j                  d      d|	j                  d      i|d<   j                  |d   d      }||k7  r	 t        | ||       d}|j                  |        j                         D ]  }	 t        | ||       d} ||fS c c}w # t        ddg      $ r }||j	                  |d       Y d}~jd}~wt        d	      $ r }||j	                  |d
       Y d}~d}~wt
        j                  j                  t
        j                  j                  f$ r}|j	                  |d       Y d}~d}~ww xY wc c}}w # t        d      $ r}|j	                  |d       Y d}~!d}~wt
        j                  j                  t
        j                  j                  f$ r}|j	                  |d       Y d}~td}~ww xY w# t
        j                  j                  t
        j                  j                  f$ r}|j	                  |d       Y d}~d}~ww xY w)a  
    Manage inventory configuration for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        module (AnsibleAWSModule): The Ansible module object.
        name (str): The name of the bucket to handle inventory for.
    Returns:
        A tuple containing a boolean indicating whether inventory settings were changed
        and a dictionary containing the updated inventory.
    	inventoryFNIdr"   r#   z,Fetching bucket inventories is not supportedr   r$   z-Permission denied fetching bucket inventoriesz"Failed to fetch bucket inventoriesdestinationTidS3BucketDestinationincluded_object_versions	Frequencyschedule)	IsEnabledr   DestinationIncludedObjectVersionsScheduleOptionalFieldsoptional_fieldsr   filterPrefixFilterInvalidS3DestinationBucketzInvalid destination bucket ARNz&Failed to set bucket inventory settingz!Failed to delete bucket inventory)r%   r&   r   r
   r-   r)   r*   r+   r,   r   rj   appendpopput_bucket_inventorykeysdelete_bucket_inventory)r0   r   r   declared_inventoriesresultsbucket_changedipresent_inventoriesr4   declared_inventorycamel_destinationre   rf   declared_inventory_apifieldpresent_inventoryinventory_ids                    r6   handle_bucket_inventoryr   }  sR    "==,,[9GN#wJ3WXacg3hiaqwzii 3 /45G5K5KM[]5^`de$((.1EVE\E\E^3pTQbcboAqD3pq&8&<&<=W&X$&8&<&<Z&HI "
 (++,=rB 	CE"#34;;EB	C!!(+708:L:P:PQY:Z/["8,/334J44PRVW!%66	V$Y6LM!% 	-.;/> ,002 	M#It\B  7""g j 02CDE X+  (V W~. Y+  (W X))'' J 	Q$HII	J 4q  ''CD N$$Q,L$MM##11##// V $$Q,T$UU	V ##1183F3F3R3RS 	M  (K LL	Ms   F E>F 
H5H52H;)J=>F H2F..H2?G:H2H--H2;J:	I"":J:J55J:=7L4LLc                    |j                   j                  d      }|j                   j                  d      }t        |      }d}i }	 t        | |      }s6	 t        | |||      }	| j                  d      j                  |	       |xs |	}t        | ||      \  }
}||d<   t!        | ||      \  }}||d<   t#        | ||      \  }}||d<   t%        | ||      \  }}||d<   t'        | ||      \  }}||d<   t)        | ||      \  }}||d<   t+        | ||      \  }}||d<   t-        | ||      \  }}||d<   t/        | ||      }||d<   t1        | ||      \  }}||d<   t3        | ||      \  }}||d<   t5        | ||      \  }} | |d<   |xs* |
xs& |xs" |xs |xs |xs |xs |xs |xs
 |xs |xs |} |j6                  d||d| y# t        j
                  j                  $ r*}|j                  |dt        |              Y d}~d}~wt        j
                  j                  t        j
                  j                  f$ r}|j                  |d       Y d}~
d}~ww xY w# t        j
                  j                  $ r}|j                  |d
       Y d}~d}~wt        j
                  j                  t        j
                  j                  f$ r}|j                  |d       Y d}~cd}~ww xY w)aB  
    Create or update an S3 bucket along with its associated configurations.
    This function creates a new S3 bucket if it does not already exist, and updates its configurations,
    such as versioning, requester pays, public access block configuration, policy, tags, encryption, bucket ownership,
    ACL, and object lock settings. It returns whether any changes were made and the updated configurations.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        module (AnsibleAWSModule): The Ansible module object.
    Returns:
        None
    r   r   FInvalid endpoint provided: r   NFailed to check bucket presencebucket_existsr   z<An error occurred waiting for the bucket to become availablezFailed while creating bucketr   r9   public_access_blockrT   ra   ru   r   r   r   r   bucket_inventory)changedr    )r%   r&   r   r   r)   r*   EndpointConnectionErrorr-   r   r+   r,   create_bucket
get_waiterwaitWaiterErrorr7   rE   rR   r_   rs   r   r   r   r   r   r   r   	exit_json)!r0   r   r   r   locationr   resultbucket_is_presentr4   r   r1   r5   rB   requester_pays_resultpublic_access_config_changedpublic_access_config_resultr]   r^   tags_changedrq   r   r   r   object_ownership_resultr   r   bucket_object_lock_resultbucket_accelerate_changedbucket_accelerate_result$bucket_object_lock_retention_changed#bucket_object_lock_retention_resultbucket_inventory_changedbucket_inventory_results!                                    r6   create_or_update_bucketr     s    ==V$D --++,AB%f-HGFG))T: 	H*9dHFYZN  166d6C/G -EYPVX\,])),F< 5QQZ\bdh4i114F Ac64A= "= %@F ! &:)VT%R"NN%F8 '9FD&Q#L#&F6N .FiQWY]-^**-F< 9PPY[acg8h55!8F ->iQU,V))%F5M !:)VT R$=F ! ;SS\^dfj:k77#;F  Qt64QM(*M /RF*+8OPY[acg8h55!8F 	 	$	$!	$ (	$ 		$
 	$ 	$ $	$ 	$ %	$ 0	$ $  F:W4:6:c 66 PQ&A'!*$NOO--x/B/B/N/NO GQ$EFFG "".. 	h  (f gg##1183F3F3R3RS 	H  (F GG	HsH   G 5I% I"%H

:I"II"%K3J:K3K..K3bucket_namec                 R    	 | j                  |       y# t        d      $ r Y yw xY w)a  
    Checks if a given bucket exists in an AWS S3 account.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the bucket to check for existence.
    Returns:
        True if the bucket exists, False otherwise.
    r   T404F)head_bucketr
   r0   r   s     r6   r   r   /  s2    [1u%     &&x   )	max_delayr   r   c                     	 d|i}i }|dvr||d<   |r||d<   |||d<    | j                   d	i | y# t        d      $ r Y yw xY w)
a  
    Create an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the bucket to create.
        location (str): The AWS region where the bucket should be created. If None, it defaults to "us-east-1".
        object_lock_enabled (bool): Whether to enable object lock for the bucket. Defaults to False.
    Returns:
        True if the bucket was successfully created, False otherwise.
    r   )z	us-east-1NLocationConstraintCreateBucketConfigurationObjectLockEnabledForBucketTBucketAlreadyOwnedByYouFr   )r   r
   )r0   r   r   r   r%   configurations         r6   r   r   ?  sy    K(..2:M./2?F./*3FF/0	)&)89  s   /2 AANoSuchBucketOperationAbortedr  catch_extra_error_codesc                     | j                  |      }|j                  di       j                  di       j                  di       S )a   
    Get the object lock default retention configuration for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        Object lock default retention configuration dictionary.
    r   ObjectLockConfigurationRuleDefaultRetention)r   r&   r0   r   r   s      r6   r   r   a  sE     44K4HF::/488DHHI[]_``    c                 8    dd|id}| j                  ||       y)aY  
    Set tags for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        object_lock_default_retention (dict): A dictionary containing the object
        lock default retention configuration to be set on the bucket.
    Returns:
        None
    r   r  )ObjectLockEnabledr  )r   r  N)r   )r0   r   r   r   s       r6   r   r   o  s)     "+5GIf4ghD++;X\+]r  c                 .    | j                  |ddi       y)z
    Enable transfer accelerate for the S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        None
    r   r   r   AccelerateConfigurationNr   r   s     r6   r   r     s     11_gir^s1tr  c                 .    | j                  |ddi       y)z
    Disable transfer accelerate for the S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        None
    r   r   r  Nr  r   s     r6   r   r     s     11_git^u1vr  c                 N    | j                  |      }|j                  d      dk(  S )a   
    Get transfer accelerate status of the S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        Transfer accelerate status of the S3 bucket.
    r   r   r   )#get_bucket_accelerate_configurationr&   )r0   r   accelerate_configurations      r6   r   r     s/      )LLT_L`#''1Y>>r  r   c                 J    | j                  |||j                  d             y)a-  
    Set inventory settings for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        tags (dict): A dictionary containing the inventory settings to be set on the bucket.
    Returns:
        None
    r   )r   InventoryConfigurationr   N)"put_bucket_inventory_configurationr&   )r0   r   r   s      r6   r   r     s'     009tAT 1 r  ra   c                 @    | j                  |dt        |      i       y)a  
    Set tags for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        tags (dict): A dictionary containing the tags to be set on the bucket.
    Returns:
        None
    TagSet)r   TaggingN)rm   r   )r0   r   ra   s      r6   rm   rm     s#       hHfgkHl=m nr  r   c                 *    | j                  ||       y)a  
    Delete the inventory settings for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        id (str): The ID used to identify the inventory configuration
    Returns:
        None
    )r   r   N)%delete_bucket_inventory_configuration)r0   r   r   s      r6   r   r     s     33;<3Xr  rT   c                 P    | j                  |t        j                  |             y)a  
    Set the policy for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        policy (dict): A dictionary containing the policy to be set on the bucket.
    Returns:
        None
    )r   PolicyN)r\   rX   dumps)r0   r   rT   s      r6   r\   r\     s      {4::f;MNr  c                 (    | j                  |       y)z
    Delete the policy for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        None
    r   N)rZ   r   s     r6   rZ   rZ     s     ""+"6r  c                     	 | j                  |      j                  d      }|syt        j                  |      }|S # t	        d      $ r Y yw xY w)z
    Get the policy for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        Current bucket policy.
    r   r&  NNoSuchBucketPolicy)rV   r&   rX   rY   r
   )r0   r   current_policy_stringr^   s       r6   rV   rV     s^     ) ; ;; ; O S ST\ ]$$9:  34 s   #= = AArD   c                 .    | j                  |d|i       y)a:  
    Set the request payment configuration for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        payer (str): The entity responsible for charges related to fulfilling the request.
    Returns:
        None
    Payer)r   RequestPaymentConfigurationN)r@   )r0   r   rD   s      r6   r@   r@     s     ((ZachYi(jr  c                 D    | j                  |      j                  d      S )a  
    Get the request payment configuration for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        Payer of the download and request fees.
    r   r-  )r?   r&   r   s     r6   r?   r?     s#     //{/CGGPPr  c                 &    | j                  |      S )z
    Get the versioning configuration for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        Returns the versioning state of a bucket.
    r   )r'   r   s     r6   r'   r'     s     **+*>>r  r3   c                 .    | j                  |d|i       y)aC  
    Set the versioning configuration for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        required_versioning (str): The desired versioning state for the bucket ("Enabled", "Suspended").
    Returns:
        None
    r   )r   VersioningConfigurationN)r(   )r0   r   r3   s      r6   r(   r(   ,  s     ##;QY[nPo#pr  c                 <    | j                  |      }|d   d   dk(  S )a"  
    Retrieve the object lock configuration status for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        True if object lock is enabled for the bucket, False otherwise.
    r   r  r  r   )r   )r0   r   object_lock_configurations      r6   r   r   :  s0     !* G G{ G [$%>?@STXaaar  c                     	 | j                  |      }|j                  di       j                  dg       d   j                  d      S # t        d      $ r Y yt        t        f$ r Y yw xY w)a  
    Retrieve the encryption configuration for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        Encryption configuration of the bucket.
    r   !ServerSideEncryptionConfigurationRulesr   "ApplyServerSideEncryptionByDefault.ServerSideEncryptionConfigurationNotFoundErrorNr|   r&   r
   
IndexErrorr   r  s      r6   r|   r|   H  sv    
000DJJ:B?S"a!S56	

 OP !    AA A)A)(A)c                     	 | j                  |      }|j                  di       j                  dg       d   j                  d      S # t        d      $ r Y yt        t        f$ r Y yw xY w)a&  
    Retrieve the status of server-side encryption for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        Whether or not if server-side encryption is enabled for the bucket.
    r   r6  r7  r   BucketKeyEnabledr9  Nr:  r  s      r6   r   r   _  st    000Dzz=rBFFwPRSTUVZZ[mnnOP ! r<  r   c           	      ^   d}t        d|dz         D ]-  }	 t        |||       t        | |||||k(  d      }||k(  s+|c S  | j                  d|	       y# t        j                  j                  t        j                  j
                  f$ r}| j                  |d       Y d}~d}~ww xY w)
a  
    Set the encryption configuration for an S3 bucket with retry logic.
    Parameters:
        module (AnsibleAWSModule): The Ansible module object.
        s3_client (boto3.client): The Boto3 S3 client object.
        name (str): The name of the S3 bucket.
        expected_encryption (dict): A dictionary containing the expected encryption configuration.
    Returns:
        Updated encryption configuration of the bucket.
          zFailed to set bucket encryptionr   N   r=   retriesz!Failed to apply bucket encryptionr   currentexpectedrD  )	rangeput_bucket_encryptionr)   r*   r+   r,   r-   r~   r   r   r0   r   r   max_retriesrD  r4   r   s           r6   r   r   r  s     KK!O, &	K!)T3FG 8It%8wR]G]hi
 !44%%&" /"$	   --++
 	K   (I JJ		K   A7B,B''B,ru   c                 8    dd|igi}| j                  ||       y)a+  
    Set the encryption configuration for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        encryption (dict): A dictionary containing the encryption configuration.
    Returns:
        None
    r7  r8  r   r6  N)rI  )r0   r   ru   $server_side_encryption_configurations       r6   rI  rI    s2     -47[]g6h5i+j(##>b $ r  c           	      ^   d}t        d|dz         D ]-  }	 t        |||       t        | |||||k(  d      }||k(  s+|c S  | j                  d|	       y# t        j                  j                  t        j                  j
                  f$ r}| j                  |d       Y d}~d}~ww xY w)
a  
    Set the status of server-side encryption for an S3 bucket.
    Parameters:
        module (AnsibleAWSModule): The Ansible module object.
        s3_client (boto3.client): The Boto3 S3 client object.
        name (str): The name of the S3 bucket.
        expected_encryption (bool): The expected status of server-side encryption using AWS KMS.
    Returns:
        The updated status of server-side encryption using AWS KMS for the bucket.
    r@  rA  zFailed to set bucket Keyr   NrB  rC  zFailed to set bucket keyrE  )	rH  put_bucket_keyr)   r*   r+   r,   r-   wait_bucket_key_is_appliedr   rJ  s           r6   r   r     s     KK!O, &	D9d,?@ 8It%8wR]G]hi
 !44%%&" &0BM`jq   --++
 	D   (B CC		DrL  c                 p    | j                  |      }||d   d   d   d<   | j                  ||d          y)a1  
    Set the status of server-side encryption for an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        encryption (bool): The status of server-side encryption using AWS KMS.
    Returns:
        None
    r   r6  r7  r   r>  rN  N)r|   rI  )r0   r   ru   encryption_statuss       r6   rQ  rQ    sS     "77{7K]g9:7CAFGYZ##>OPs>t $ r  c                 (    | j                  |       y)z
    Delete the tagging configuration of an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        None
    r   N)rn   r   s     r6   rn   rn     s     ##;#7r  c                 (    | j                  |       y)z
    Delete the encryption configuration of an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        None
    r   N)r}   r   s     r6   r}   r}     s     &&k&:r     c                 R    	 | j                  |       y# t        d      $ r Y yw xY w)z
    Delete an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        None
    r   r	  N)delete_bucketr
   r   s     r6   rY  rY    s1    {3~.  	r   public_accesc                 *    | j                  ||       y)a  
    Put new public access block to S3 bucket
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        public_access (dict): The public access block configuration.
    Returns:
        None
    )r   PublicAccessBlockConfigurationN)put_public_access_block)r0   r   rZ  s      r6   rL   rL   	  s     %%[Ye%fr  c                 (    | j                  |       y)z
    Delete public access block from S3 bucket
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        None
    r   N)delete_public_access_blockr   s     r6   rM   rM     s     (((<r  c                 (    | j                  |       y)z
    Delete bucket ownership controls from S3 bucket
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        None
    r   N) delete_bucket_ownership_controlsr   s     r6   r   r   $  s     ..k.Br  targetc                 4    | j                  |dd|igi       y)z
    Put bucket ownership controls for S3 bucket
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        None
    r7  ObjectOwnership)r   OwnershipControlsN)put_bucket_ownership_controls)r0   r   rb  s      r6   r   r   1  s*     ++w:KV9T8U.V , r  expected_policyr=   c                 f   t        dd      D ]4  }	 t        ||      }t        |      rt        j                  d       2|c S  |r| j                  d|       yy# t        j                  j                  t        j                  j
                  f$ r}| j                  |d       Y d}~d}~ww xY w)	aB  
    Wait for a bucket policy to be applied to an S3 bucket.
    Parameters:
        module (AnsibleAWSModule): The Ansible module object.
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        expected_policy (dict): The expected bucket policy.
        should_fail (bool): Flag indicating whether to fail if the policy is not applied within the expected time. Default is True.
    Returns:
        The current policy applied to the bucket, or None if the policy failed to apply within the expected time.
    r      rU   r   NrB  z2Bucket policy failed to apply in the expected time)r   requested_policylive_policy)rH  rV   r)   r*   r+   r,   r-   r   timesleepr   )r   r0   r   rg  r=   dummyr^   r4   s           r6   r[   r[   @  s     q" 	"	G.y+FN NO<JJqM!!	" D,& 	 	
  ##1183F3F3R3RS 	G  (E FF	Gs   A7B0B++B0expected_payerc                 X   t        dd      D ]-  }	 t        ||      }|k7  rt        j                  d       +|c S  |r| j                  d|       yy# t        j                  j                  t        j                  j
                  f$ r}| j                  |d       Y d}~d}~ww xY w)	aB  
    Wait for the requester pays setting to be applied to an S3 bucket.
    Parameters:
        module (AnsibleAWSModule): The Ansible module object.
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        expected_payer (bool): The expected status of the requester pays setting.
        should_fail (bool): Flag indicating whether to fail if the setting is not applied within the expected time. Default is True.
    Returns:
        The current status of the requester pays setting applied to the bucket.
    r   ri  r>   r   NrB  z;Bucket request payment failed to apply in the expected time)r   requested_statuslive_status)
rH  r?   r)   r*   r+   r,   r-   rl  rm  r   )r   r0   r   ro  r=   rn  rC   r4   s           r6   rA   rA   b  s     q" )	P$>y+$V! !N2JJqM(() M+- 	 	
  ##1183F3F3R3RS 	P  (N OO	P   A7B)B$$B)c                 X   t        d|      D ]-  }	 t        ||      }|k7  rt        j                  d       +|c S  |r| j                  d|       S # t        j                  j                  t        j                  j
                  f$ r}| j                  |d       Y d}~d}~ww xY w)af  
    Wait for the encryption setting to be applied to an S3 bucket.
    Parameters:
        module (AnsibleAWSModule): The Ansible module object.
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        expected_encryption(dict): The expected encryption setting.
        should_fail (bool): Flag indicating whether to fail if the setting is not applied within the expected time. Default is True.
        retries (int): The number of retries to attempt. Default is 12.
    Returns:
        The current encryption setting applied to the bucket.
    r   +Failed to get updated encryption for bucketr   NrB  z6Bucket encryption failed to apply in the expected timer   requested_encryptionlive_encryption)
rH  r|   r)   r*   r+   r,   r-   rl  rm  r   	r   r0   r   r   r=   rD  rn  ru   r4   s	            r6   r~   r~     s     q'" 	W.y+FJ ,,JJqM H!4& 	 	
  ##1183F3F3R3RS 	W  (U VV	Wrs  c                 X   t        d|      D ]-  }	 t        ||      }|k7  rt        j                  d       +|c S  |r| j                  d|       S # t        j                  j                  t        j                  j
                  f$ r}| j                  |d       Y d}~d}~ww xY w)ag  
    Wait for the bucket key setting to be applied to an S3 bucket.
    Parameters:
        module (AnsibleAWSModule): The Ansible module object.
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        expected_encryption (bool): The expected bucket key setting.
        should_fail (bool): Flag indicating whether to fail if the setting is not applied within the expected time. Default is True.
        retries (int): The number of retries to attempt. Default is 12.
    Returns:
        The current bucket key setting applied to the bucket.
    r   ru  r   NrB  z/Bucket Key failed to apply in the expected timerv  )
rH  r   r)   r*   r+   r,   r-   rl  rm  r   ry  s	            r6   rR  rR    s     q'" 	W'	;?J ,,JJqM A!4& 	 	

  ##1183F3F3R3RS 	W  (U VV	Wrs  c                 p   t        dd      D ]<  }	 t        ||      }j                  d      |k7  rt        j                  d       :|c S  | j                  d|	       y# t        j                  j                  t        j                  j
                  f$ r}| j                  |d       Y d}~d}~ww xY w)
a  
    Wait for the versioning setting to be applied to an S3 bucket.
    Parameters:
        module (AnsibleAWSModule): The Ansible module object.
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        required_versioning (dict): The required versioning status.
    Returns:
        The current versioning status applied to the bucket.
    r      z+Failed to get updated versioning for bucketr   Nr      z6Bucket versioning failed to apply in the expected time)r   requested_versioninglive_versioning)rH  r'   r)   r*   r+   r,   r-   r&   rl  rm  r   )r   r0   r   r3   rn  r2   r4   s          r6   r.   r.     s     q" %	W 5i M   *.AAJJqM$$% D0)   ##1183F3F3R3RS 	W  (U VV	Ws   A!!7B5B00B5expected_tags_dictc                 R   t        dd      D ]-  }	 t        ||      }|k7  rt        j                  d       +|c S  | j                  d|       y# t        j                  j                  t        j                  j
                  f$ r}| j                  |d       Y d}~d}~ww xY w)	a  
    Wait for the tags to be applied to an S3 bucket.
    Parameters:
        module (AnsibleAWSModule): The Ansible module object.
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
        expected_tags_dict (dict): The expected tags dictionary.
    Returns:
        The current tags dictionary applied to the bucket.
    r   ri  rU   r   NrB  z0Bucket tags failed to apply in the expected time)r   requested_tags	live_tags)
rH  rh   r)   r*   r+   r,   r-   rl  rm  r   )r   r0   r   r  rn  rq   r4   s          r6   ro   ro     s     q" %	G <Y T  22JJqM$$% >)#   ##1183F3F3R3RS 	G  (E FF	Gs   A7B&	B!!B&c                     	 | j                  |      j                  d      }t        |      S # t        d      $ r i cY S t        d      $ r i cY S w xY w)a  
    Get the current tags applied to an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        The current tags dictionary applied to the bucket.
    r   r!  NoSuchTagSetNoSuchTagSetError)get_bucket_taggingr&   r
   r   )r0   r   current_tagss      r6   rh   rh      s`     33;3GKKHU *,77 ~. 	23 	s   !. A AAc                 ^    	 | j                  |      }|d   S # t        d      $ r i cY S w xY w)a  
    Get current public access block configuration for a bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
        The current public access block configuration for the bucket.
    r   r\  $NoSuchPublicAccessBlockConfiguration)get_public_access_blockr
   )r0   r   bucket_public_access_blocks      r6   rK   rK     sA    %.%F%Fk%F%Z")*JKKEF 	s    ,,c                 p    	 | j                  |      }|d   d   d   d   S # t        ddg      $ r Y yw xY w)	z
    Get the current bucket ownership controls.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        bucket_name (str): The name of the S3 bucket.
    Returns:
      The object ownership rule
    r   re  r7  r   rd  OwnershipControlsNotFoundErrorNoSuchOwnershipControlsN)get_bucket_ownership_controlsr
   )r0   r   r   s      r6   r   r   $  sU    $BB+BV 34W=a@ARSS @B[\] s   " 55c              +      K   | j                  d      } |j                  di |D ]'  }|j                  dg       D cg c]  }|d   	 c} ) yc c}w w)a  
    Paginate through the list of objects in an S3 bucket.
    This function yields the keys of objects in the S3 bucket, paginating through the results.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        **pagination_params: Additional parameters to pass to the paginator.
    Yields:
        list: A list of keys of objects in the bucket for each page of results.
    list_objects_v2ContentsKeyNr   )get_paginatorpaginater&   r0   pagination_paramspgpagedatas        r6   paginated_listr  4  s\      
	 	 !2	3B0/0 A'+xx
B'?@ttE{@@A@s   ;AA	Ac              +     K   	 | j                  d      } |j                  di |D ]?  }|j                  dg       |j                  dg       z   D cg c]  }|d   |d   f c} A yc c}w # t        d      $ r g  Y yw xY ww)	a  
    Paginate through the list of object versions in an S3 bucket.
    This function yields the keys and version IDs of object versions in the S3 bucket, paginating through the results.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        **pagination_params: Additional parameters to pass to the paginator.
    Yields:
        list: A list of tuples containing keys and version IDs of object versions in the bucket for each page of results.
    list_object_versionsVersionsDeleteMarkersr  	VersionIdr	  Nr   )r  r  r&   r
   r  s        r6   paginated_versions_listr  C  s     $$%;<BKK4"34 	D ?ChhzSU>VY]YaYabqsuYv>v59ed;/0 	 ~. s;   BAA/ A*"A/ )B*A/ /BBBBc                 j   	 t        | |      D ]  }|D cg c]
  \  }}||d }}}|D ]9  }|j                  d      r|j                  d      dk(  s)|j                  d       ; |s[| j                  |d|i      }|j                  d      sdj	                  |d   D 	cg c]  }	|	d	   	 c}	      }
|j                  d
|
 |d   |        yc c}}w c c}	w # t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d}~yd}~ww xY w)a   
    Delete objects from an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        module (AnsibleAWSModule): The Ansible module object.
        name (str): The name of the S3 bucket.
    Returns:
        None
    r   )r  r  r  nullObjects)r   DeleteErrorsz, r  zBCould not empty bucket before deleting. Could not delete objects: )r   errorsresponsezFailed while deleting bucketr   N)r  r&   r   delete_objectsjoinr   r)   r*   r+   r,   r-   )r0   r   r   key_version_pairskeyversionformatted_keysfkrespre   objects_to_deleter4   s               r6   r  r  X  s<   D!84!P 	Ufg\S'c@gNg$ (
 vvk*bff[.AV.KFF;'(  //tYP^D_/`88H%(,		T(^2T1U82T(U%$$absatu#H~!% % 	g 3U --x/B/B/N/NO DQ$BCCDsE   C C-C C *&C C #C
/#C C 7D2D--D2c                 t   |j                   j                  d      }|j                   j                  d      }	 t        | |      }s|j                  d       |r	 t        | ||       	 t        | |       | j                  d
      j                  |t        dd             |j                  d       y# t        j                  j
                  $ r)}|j                  |dt        |              Y d}~d}~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}~9d}~ww xY w# t        j                  j                   $ r}|j                  |d       Y d}~=d}~wt        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d}~d}~ww xY w)z
    This function destroys an S3 bucket.
    Parameters:
        s3_client (boto3.client): The Boto3 S3 client object.
        module (AnsibleAWSModule): The Ansible module object.
    Returns:
        None
    forcer   r   r   Nr   F)r   zFailed while deleting objectsbucket_not_existsrB  <   )DelayMaxAttempts)r   WaiterConfigz7An error occurred waiting for the bucket to be deleted.zFailed to delete bucketT)r%   r&   r   r)   r*   r   r-   r   r+   r,   r   r  rY  r   r   ri   r   )r0   r   r  r   r   r4   s         r6   destroy_bucketr  y  s    MMg&E==V$DG))T: '	I9fd3?i&0166dQU\]kmQn6o T"/ 66 PQ&A'!*$NOO--x/B/B/N/NO GQ$EFFG ##1183F3F3R3RS 	I  (G HH	I ** _Q$]^^--x/B/B/N/NO ?Q$=>>?s_   B5 E )9F) 5EC66:E0E		E7F&F!!F&)H7G:H7H22H7c                  $   t        dPi dt        d      dt        dd      dt        dd	      d
t        dddg      dt        dddg      dt        ddg      dt        dd      dt        dd	      dt        d      dt        d      dt        d      dt        g d      dt               d t        d      d!t        dt        t        dd      t        dd      t        dd      t        dd      "      #      d$t        dd      d%t        d&g d'(      d)t        dd      d*t        d&g d+(      d,t        d      d-t        d      d.t        dt        t        d&d/d0gd1      t        d2      t        d2      3      d4gd4g5      d6t        d7dt        t        dt        t        d&      t        d&d8      t        d&g d9d1      t        d&      :      d;      t        d&      t        d7d&g d<=      t        d&d8      t        d&d>d?gd1      t        d&d@dAgd1      B      C      } t        dDd-E      }d!d$gd)d%gddFgg}dddFggg}t        | |||G      }|j                  j                  d      }|j                  j                  d      }||dHk7  r|j	                  dIJ       t        |j                        }t        j                  dKdLdMgN      } |j                  dQdO|i|}	|j                  j                  d      r,t        |j                  d         }
|
r|j	                  |
J       |j                  j                  d
      }|dk(  rt        |	|       y |dk(  rt        |	|       y y )RNr   T)requiredr   r   )typedefault	dualstackF)r  r  statepresentabsent)r  choicescephrgw)r  r  aliasesra   ri   resource_tags)r  r  rb   r  rT   rX   )r  r   r9   ru   )rz   AES256r{   )r  rv   rw   rG   )block_public_aclsignore_public_aclsblock_public_policyrestrict_public_buckets)r  optionsrH   r   str)BucketOwnerEnforcedBucketOwnerPreferredObjectWriter)r  r  r   r   )privatezpublic-readzpublic-read-writezauthenticated-readr   r   r   
GOVERNANCE
COMPLIANCE)r  r  r  int)modeyearsdays)r  r  )r  r  mutually_exclusiverequired_one_ofr   list)r  r  )CSVORCParquet)
account_idbucketformatprefix)r  r  r  )SizeLastModifiedDateStorageClassETagIsMultipartUploadedReplicationStatusEncryptionStatusObjectLockRetainUntilDateObjectLockModeObjectLockLegalHoldStatusIntelligentTieringAccessTierBucketKeyStatusChecksumAlgorithmObjectAccessControlListObjectOwner)r  elementsr  DailyWeeklyAllCurrent)r   r   r   r   r   r   )r  r  r  )ru   )rv   r   endpoint_url)argument_specrequired_byrequired_ifr  r{   z]Only 'aws:kms' is a valid option for encryption parameter when you specify encryption_key_id.r   r  r	  r
  r  retry_decoratorr   )s3)ri   r   r%   r&   r   r   r   jittered_backoffclientr   r   r  )r  r  r  r  r   ru   rv   extra_paramsr  r0   errr  s               r6   mainr    s    U4 U!vt<U u62U 9y(.CD	U
 %fug>U v'89U VT2U 5v.U  U V$U (U   =>!U" &#U$  V,%U& "&FE"B#'VU#C$(fe$D(,&%(H	
'U8 "vu=9U: 52qr;U< !%&% @=U> e%jk?U@  V,AUB !f-CUD '+u|\.JUYZ&u%
 !22./	'
EUX   #'U#3#>#8Q\`a#/	 "	 ' $"!* UT257H2EPTU)-55)BT_c)dG$(
YUMn )&;K 
01	"$67	n% 
'(K #-	F ""<0J))*=>$y)@o 	 	
 #6==1L//!/1C DO TOT|TI}}/0"6==#89%MMg&E		62	(	y&) 
r  __main__)F)T)Tri  )`DOCUMENTATIONEXAMPLESRETURNrX   rl  typingr   r   r   r)   ImportErroransible.module_utils.basicr   0ansible.module_utils.common.dict_transformationsr   ansible.module_utils.sixr	   <ansible_collections.amazon.aws.plugins.module_utils.botocorer
   ;ansible_collections.amazon.aws.plugins.module_utils.modulesr   :ansible_collections.amazon.aws.plugins.module_utils.policyr   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr   6ansible_collections.amazon.aws.plugins.module_utils.s3r   r   r   r   ;ansible_collections.amazon.aws.plugins.module_utils.taggingr   r   r  r   ri   r7   rE   rR   r_   rs   r   r   r   r   r   r   r   r   r   exponential_backoffr   r   r   r   r   r   r   rm   r   r\   rZ   rV   r@   r?   r'   r(   r   r|   r   r   rI  r   rQ  rn   r}   rY  rL   rM   r   r   r[   rA   r~   rR  r.   ro   rh   rK   r   r  r  r  r  r  __name__r   r  r6   <module>r     s
  KZBJ
X     	 / U 1 \ X W P Y g R W f f410@ 41 41PUVZ\`V`Pa 41n*24D *2C *2TYZ^`dZdTe *2Z67:J 67RU 67Z_`dfj`jZk 67r7*,< 7*C 7*ERVX\R\L] 7*t72*: 72# 72%PTVZPZJ[ 72t=20@ =2 =2PUVZ\`V`Pa =2@4=/? 4=s 4=uUY[_U_O` 4=n#1)9 #1 #1tUYzIZ #1L'1A ' 'QU 'T2A0@ 2A 2APUVZ\`V`Pa 2Aj1W;K 1WSV 1W[`aegkak[l 1WhF#/? F#s F#uUY[_U_O` F#Rf;/? f;R# $   ,#  SW dh  -B nVhEij
a k
a nVhEij^ k^ nVhEij	u k	u nVhEij
w k
w nVhEij
?D 
? k
? nVhEij  $  k nVhEij
os 
o$ 
o k
o nVhEij
YC 
Ys 
Yt 
Y k
Y nVhEij
Oc 
O4 
O k
O nVhEij	7 	7 k	7 nVhEijc c  k( nVhEij
ks 
k3 
k k
k nVhEij	Qs 	Qs 	Q k	Q nVhEij	?# 	?$ 	? k	? nVhEij
q# 
qC 
q k
q nVhEij
b3 
b4 
b k
b nVhEij# $  k, nVhEij3 4  k$"-= "PS "jn "sw "J nVhEij# 4 D  k &6  cg lp D nVhEij3 D T  k$ nVhEij	8# 	8$ 	8 k	8 nVhEij	;S 	;T 	; k	; FXEYZ# $  [" nVhEij
gS 
g 
gQU 
g k
g nVhEij	= 	= 	= k	= nVhEij	CC 	CD 	C k	C nVhEij c d  k gk69LP_c	F ^b69KOD qs  69 PT 	 H qs69PT	D69PT	:"2 C ei nr 68 8 8(S T  s s  AhtCy6I AxUSVX[S[_H]?^ *D&6 Dc Dd DB$#&6 $#4 $#NG*T zF C8  		s   V VV