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

hub.exec.boto3.client.s3.delete_bucket_website
hub.exec.boto3.client.s3.get_bucket_website
hub.exec.boto3.client.s3.put_bucket_website
resource = hub.tool.boto3.resource.create(ctx, "s3", "BucketWebsite", 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, website_configuration: Dict, content_md5: Text = None, expected_bucket_owner: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Sets the configuration of the website that is specified in the website subresource. To configure a bucket as a
    website, you can add this subresource on the bucket with website configuration information such as the file name
    of the index document and any redirect rules. For more information, see Hosting Websites on Amazon S3. This PUT
    action requires the S3:PutBucketWebsite permission. By default, only the bucket owner can configure the website
    attached to a bucket; however, bucket owners can allow other users to set the website configuration by writing a
    bucket policy that grants them the S3:PutBucketWebsite permission. To redirect all website requests sent to the
    bucket's website endpoint, you add a website configuration with the following elements. Because all requests are
    sent to another website, you don't need to provide index document name for the bucket.    WebsiteConfiguration
    RedirectAllRequestsTo     HostName     Protocol    If you want granular control over redirects, you can use the
    following elements to add routing rules that describe conditions for redirecting requests and information about
    the redirect destination. In this case, the website configuration must provide an index document for the bucket,
    because some requests might not be redirected.     WebsiteConfiguration     IndexDocument     Suffix
    ErrorDocument     Key     RoutingRules     RoutingRule     Condition     HttpErrorCodeReturnedEquals
    KeyPrefixEquals     Redirect     Protocol     HostName     ReplaceKeyPrefixWith     ReplaceKeyWith
    HttpRedirectCode    Amazon S3 has a limitation of 50 routing rules per website configuration. If you require
    more than 50 routing rules, you can use object redirect. For more information, see Configuring an Object
    Redirect in the Amazon S3 User Guide.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        bucket(Text): The bucket name.
        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.
        website_configuration(Dict): Container for the request.
        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_website.present:
                - name: value
                - bucket: value
                - website_configuration: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.s3.bucket_website.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", "BucketWebsite", 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_website(
                ctx,
                
                
                **{"Bucket": bucket, "ContentMD5": content_md5, "WebsiteConfiguration": website_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**
    
    This action removes the website configuration for a bucket. Amazon S3 returns a 200 OK response upon
    successfully deleting a website configuration on the specified bucket. You will get a 200 OK response if the
    website configuration you are trying to delete does not exist on the bucket. Amazon S3 returns a 404 response if
    the bucket specified in the request does not exist. This DELETE action requires the S3:DeleteBucketWebsite
    permission. By default, only the bucket owner can delete the website configuration attached to a bucket.
    However, bucket owners can grant other users permission to delete the website configuration by writing a bucket
    policy granting them the S3:DeleteBucketWebsite permission.  For more information about hosting websites, see
    Hosting Websites on Amazon S3.  The following operations are related to DeleteBucketWebsite:    GetBucketWebsite
    PutBucketWebsite

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        bucket(Text): The bucket name for which you want to remove the website configuration.
        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_website.absent:
                - name: value
                - bucket: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.s3.bucket_website.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", "BucketWebsite", 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_website(
                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

