"""
Autogenerated state module using `pop-create-idem <https://gitlab.com/saltstack/pop/pop-create-idem>`__

hub.exec.boto3.client.s3.delete_bucket_replication
hub.exec.boto3.client.s3.get_bucket_replication
hub.exec.boto3.client.s3.put_bucket_replication
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, bucket: Text, replication_configuration: Dict, content_md5: Text = None, token: Text = None, expected_bucket_owner: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
     Creates a replication configuration or replaces an existing one. For more information, see Replication in the
    Amazon S3 User Guide.  Specify the replication configuration in the request body. In the replication
    configuration, you provide the name of the destination bucket or buckets where you want Amazon S3 to replicate
    objects, the IAM role that Amazon S3 can assume to replicate objects on your behalf, and other relevant
    information. A replication configuration must include at least one rule, and can contain a maximum of 1,000.
    Each rule identifies a subset of objects to replicate by filtering the objects in the source bucket. To choose
    additional subsets of objects to replicate, add a rule for each subset. To specify a subset of the objects in
    the source bucket to apply a replication rule to, add the Filter element as a child of the Rule element. You can
    filter objects based on an object key prefix, one or more object tags, or both. When you add the Filter element
    in the configuration, you must also add the following elements: DeleteMarkerReplication, Status, and Priority.
    If you are using an earlier version of the replication configuration, Amazon S3 handles replication of delete
    markers differently. For more information, see Backward Compatibility.  For information about enabling
    versioning on a bucket, see Using Versioning.  Handling Replication of Encrypted Objects  By default, Amazon S3
    doesn't replicate objects that are stored at rest using server-side encryption with CMKs stored in Amazon Web
    Services KMS. To replicate Amazon Web Services KMS-encrypted objects, add the following:
    SourceSelectionCriteria, SseKmsEncryptedObjects, Status, EncryptionConfiguration, and ReplicaKmsKeyID. For
    information about replication configuration, see Replicating Objects Created with SSE Using CMKs stored in
    Amazon Web Services KMS. For information on PutBucketReplication errors, see List of replication-related error
    codes   Permissions  To create a PutBucketReplication request, you must have s3:PutReplicationConfiguration
    permissions for the bucket.  By default, a resource owner, in this case the Amazon Web Services account that
    created the bucket, can perform this operation. The resource owner can also grant others permissions to perform
    the operation. For more information about permissions, see Specifying Permissions in a Policy and Managing
    Access Permissions to Your Amazon S3 Resources.  To perform this operation, the user or role performing the
    action must have the iam:PassRole permission.  The following operations are related to PutBucketReplication:
    GetBucketReplication     DeleteBucketReplication

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        bucket(Text): The name of the bucket.
        content_md5(Text, optional): The base64-encoded 128-bit MD5 digest of the data. You must use this header as a message
            integrity check to verify that the request body was not corrupted in transit. For more
            information, see RFC 1864. For requests made using the Amazon Web Services Command Line
            Interface (CLI) or Amazon Web Services SDKs, this field is calculated automatically. Defaults to None.
        replication_configuration(Dict): A container for replication rules. You can add up to 1,000 rules. The maximum size of a
            replication configuration is 2 MB.
        token(Text, optional): A token to allow Object Lock to be enabled for an existing bucket. Defaults to None.
        expected_bucket_owner(Text, optional): The account ID of the expected bucket owner. If the bucket is owned by a different account, the
            request will fail with an HTTP 403 (Access Denied) error. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.s3.bucket_replication.present:
                - name: value
                - bucket: value
                - replication_configuration: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.s3.bucket_replication.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    
    before = await hub.exec.boto3.client.s3.get_bucket_replication(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.s3.put_bucket_replication(
                ctx,
                
                
                **{"Bucket": bucket, "ContentMD5": content_md5, "ReplicationConfiguration": replication_configuration, "Token": token, "ExpectedBucketOwner": expected_bucket_owner}
            )
            result["result"] = ret["status"]
            if not result["result"]:
                result["comment"] = ret["comment"]
                return result
            ret["comment"] = f"Created '{name}'"
        except hub.tool.boto3.exception.ClientError as e:
            result["comment"] = f"{e.__class__.__name__}: {e}"

    
    # TODO perform other modifications as needed here
    ...

    after = await hub.exec.boto3.client.s3.get_bucket_replication(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, bucket: Text, expected_bucket_owner: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
     Deletes the replication configuration from the bucket. To use this operation, you must have permissions to
    perform the s3:PutReplicationConfiguration action. The bucket owner has these permissions by default and can
    grant it to others. For more information about permissions, see Permissions Related to Bucket Subresource
    Operations and Managing Access Permissions to Your Amazon S3 Resources.   It can take a while for the deletion
    of a replication configuration to fully propagate.   For information about replication configuration, see
    Replication in the Amazon S3 User Guide. The following operations are related to DeleteBucketReplication:
    PutBucketReplication     GetBucketReplication

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        bucket(Text):  The bucket name.
        expected_bucket_owner(Text, optional): The account ID of the expected bucket owner. If the bucket is owned by a different account, the
            request will fail with an HTTP 403 (Access Denied) error. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.s3.bucket_replication.absent:
                - name: value
                - bucket: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.s3.bucket_replication.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    

    before = await hub.exec.boto3.client.s3.get_bucket_replication(name)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.s3.delete_bucket_replication(
                ctx,
                
                
                **{"Bucket": bucket, "ExpectedBucketOwner": expected_bucket_owner}
            )
            result["result"] = ret["status"]
            if not result["result"]:
                result["comment"] = ret["comment"]
                return result
            result["comment"] = f"Deleted '{name}'"
        except hub.tool.boto3.exception.ClientError as e:
            result["comment"] = f"{e.__class__.__name__}: {e}"

    

    after = await hub.exec.boto3.client.s3.get_bucket_replication(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

