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

hub.exec.boto3.client.codepipeline.delete_webhook
hub.exec.boto3.client.codepipeline.list_webhooks
hub.exec.boto3.client.codepipeline.put_webhook
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, webhook: Dict, tags: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Defines a webhook and returns a unique webhook URL generated by CodePipeline. This URL can be supplied to third
    party source hosting providers to call every time there's a code change. When CodePipeline receives a POST
    request on this URL, the pipeline defined in the webhook is started as long as the POST request satisfied the
    authentication and filtering requirements supplied when defining the webhook. RegisterWebhookWithThirdParty and
    DeregisterWebhookWithThirdParty APIs can be used to automatically configure supported third parties to call the
    generated webhook URL.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        webhook(Dict): The detail provided in an input file to create the webhook, such as the webhook name, the
            pipeline name, and the action name. Give the webhook a unique name that helps you identify it.
            You might name the webhook after the pipeline and action it targets so that you can easily
            recognize what it's used for later.
        tags(List, optional): The tags for the webhook. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.codepipeline.webhook.present:
                - name: value
                - webhook: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.codepipeline.webhook.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.codepipeline.list_webhooks(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.codepipeline.put_webhook(
                ctx,
                
                
                **{"webhook": webhook, "tags": tags}
            )
            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.codepipeline.list_webhooks(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes a previously created webhook by name. Deleting the webhook stops AWS CodePipeline from starting a
    pipeline every time an external event occurs. The API returns successfully when trying to delete a webhook that
    is already deleted. If a deleted webhook is re-created by calling PutWebhook with the same name, it will have a
    different URL.

    Args:
        name(Text): The name of the webhook you want to delete.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.codepipeline.webhook.absent:
                - name: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.codepipeline.webhook.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.codepipeline.list_webhooks(name)

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

