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

hub.exec.boto3.client.s3.delete_bucket_lifecycle
hub.exec.boto3.client.s3.get_bucket_lifecycle
hub.exec.boto3.client.s3.put_bucket_lifecycle
resource = hub.tool.boto3.resource.create(ctx, "s3", "BucketLifecycle", name)
hub.tool.boto3.resource.exec(resource, delete, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, put, *args, **kwargs)
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, bucket: Text, content_md5: Text = None, lifecycle_configuration: Dict = None, expected_bucket_owner: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
     For an updated version of this API, see PutBucketLifecycleConfiguration. This version has been deprecated.
    Existing lifecycle configurations will work. For new lifecycle configurations, use the updated API.   Creates a
    new lifecycle configuration for the bucket or replaces an existing lifecycle configuration. For information
    about lifecycle configuration, see Object Lifecycle Management in the Amazon S3 User Guide.  By default, all
    Amazon S3 resources, including buckets, objects, and related subresources (for example, lifecycle configuration
    and website configuration) are private. Only the resource owner, the Amazon Web Services account that created
    the resource, can access it. The resource owner can optionally grant access permissions to others by writing an
    access policy. For this operation, users must get the s3:PutLifecycleConfiguration permission. You can also
    explicitly deny permissions. Explicit denial also supersedes any other permissions. If you want to prevent users
    or accounts from removing or deleting objects from your bucket, you must deny them permissions for the following
    actions:     s3:DeleteObject     s3:DeleteObjectVersion     s3:PutLifecycleConfiguration    For more information
    about permissions, see Managing Access Permissions to your Amazon S3 Resources in the Amazon S3 User Guide. For
    more examples of transitioning objects to storage classes such as STANDARD_IA or ONEZONE_IA, see Examples of
    Lifecycle Configuration.  Related Resources     GetBucketLifecycle(Deprecated)
    GetBucketLifecycleConfiguration     RestoreObject    By default, a resource owner—in this case, a bucket owner,
    which is the Amazon Web Services account that created the bucket—can perform any of the operations. A resource
    owner can also grant others permission to perform the operation. For more information, see the following topics
    in the Amazon S3 User Guide:     Specifying Permissions in a Policy     Managing Access Permissions to your
    Amazon S3 Resources

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        bucket(Text): .
        content_md5(Text, optional):  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.
        lifecycle_configuration(Dict, optional): . 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_lifecycle.present:
                - name: value
                - bucket: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.s3.bucket_lifecycle.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    resource = hub.tool.boto3.resource.create(ctx, "s3", "BucketLifecycle", name)
    before = await hub.tool.boto3.resource.describe(resource)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.s3.put_bucket_lifecycle(
                ctx,
                
                
                **{"Bucket": bucket, "ContentMD5": content_md5, "LifecycleConfiguration": lifecycle_configuration, "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.tool.boto3.resource.describe(resource)
    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 lifecycle configuration from the specified bucket. Amazon S3 removes all the lifecycle configuration
    rules in the lifecycle subresource associated with the bucket. Your objects never expire, and Amazon S3 no
    longer automatically deletes any objects on the basis of rules contained in the deleted lifecycle configuration.
    To use this operation, you must have permission to perform the s3:PutLifecycleConfiguration action. By default,
    the bucket owner has this permission and the bucket owner can grant this permission to others. There is usually
    some time lag before lifecycle configuration deletion is fully propagated to all the Amazon S3 systems. For more
    information about the object expiration, see Elements to Describe Lifecycle Actions. Related actions include:
    PutBucketLifecycleConfiguration     GetBucketLifecycleConfiguration

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        bucket(Text): The bucket name of the lifecycle to delete.
        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_lifecycle.absent:
                - name: value
                - bucket: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.s3.bucket_lifecycle.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    resource = hub.tool.boto3.resource.create(ctx, "s3", "BucketLifecycle", name)

    before = await hub.tool.boto3.resource.describe(resource)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.s3.delete_bucket_lifecycle(
                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.tool.boto3.resource.describe(resource)
    result["changes"] = differ.deep_diff(before, after)
    return result

