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

hub.exec.boto3.client.s3.delete_bucket_cors
hub.exec.boto3.client.s3.get_bucket_cors
hub.exec.boto3.client.s3.put_bucket_cors
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, bucket: Text, cors_configuration: Dict, content_md5: Text = None, expected_bucket_owner: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Sets the cors configuration for your bucket. If the configuration exists, Amazon S3 replaces it. To use this
    operation, you must be allowed to perform the s3:PutBucketCORS action. By default, the bucket owner has this
    permission and can grant it to others. You set this configuration on a bucket so that the bucket can service
    cross-origin requests. For example, you might want to enable a request whose origin is http://www.example.com to
    access your Amazon S3 bucket at my.example.bucket.com by using the browser's XMLHttpRequest capability. To
    enable cross-origin resource sharing (CORS) on a bucket, you add the cors subresource to the bucket. The cors
    subresource is an XML document in which you configure rules that identify origins and the HTTP methods that can
    be executed on your bucket. The document is limited to 64 KB in size.  When Amazon S3 receives a cross-origin
    request (or a pre-flight OPTIONS request) against a bucket, it evaluates the cors configuration on the bucket
    and uses the first CORSRule rule that matches the incoming browser request to enable a cross-origin request. For
    a rule to match, the following conditions must be met:   The request's Origin header must match AllowedOrigin
    elements.   The request method (for example, GET, PUT, HEAD, and so on) or the Access-Control-Request-Method
    header in case of a pre-flight OPTIONS request must be one of the AllowedMethod elements.    Every header
    specified in the Access-Control-Request-Headers request header of a pre-flight request must match an
    AllowedHeader element.     For more information about CORS, go to Enabling Cross-Origin Resource Sharing in the
    Amazon S3 User Guide.  Related Resources     GetBucketCors     DeleteBucketCors     RESTOPTIONSobject

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        bucket(Text): Specifies the bucket impacted by the corsconfiguration.
        cors_configuration(Dict): Describes the cross-origin access configuration for objects in an Amazon S3 bucket. For more
            information, see Enabling Cross-Origin Resource Sharing in the Amazon S3 User Guide.
        content_md5(Text, optional): The base64-encoded 128-bit MD5 digest of the data. This header must be used as a message
            integrity check to verify that the request body was not corrupted in transit. For more
            information, go to 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.
        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_cor.present:
                - name: value
                - bucket: value
                - cors_configuration: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.s3.bucket_cor.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_cors(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.s3.put_bucket_cors(
                ctx,
                
                
                **{"Bucket": bucket, "CORSConfiguration": cors_configuration, "ContentMD5": content_md5, "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_cors(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 cors configuration information set for the bucket. To use this operation, you must have permission
    to perform the s3:PutBucketCORS action. The bucket owner has this permission by default and can grant this
    permission to others.  For information about cors, see Enabling Cross-Origin Resource Sharing in the Amazon S3
    User Guide.  Related Resources:     PutBucketCors     RESTOPTIONSobject

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        bucket(Text): Specifies the bucket whose cors configuration is being deleted.
        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_cor.absent:
                - name: value
                - bucket: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.s3.bucket_cor.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_cors(name)

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

