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

hub.exec.boto3.client.elasticbeanstalk.create_application_version
hub.exec.boto3.client.elasticbeanstalk.delete_application_version
hub.exec.boto3.client.elasticbeanstalk.describe_application_versions
hub.exec.boto3.client.elasticbeanstalk.update_application_version
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, application_name: Text, version_label: Text, description: Text = None, source_build_information: Dict = None, source_bundle: Dict = None, build_configuration: Dict = None, auto_create_application: bool = None, process: bool = None, tags: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates an application version for the specified application. You can create an application version from a
    source bundle in Amazon S3, a commit in AWS CodeCommit, or the output of an AWS CodeBuild build as follows:
    Specify a commit in an AWS CodeCommit repository with SourceBuildInformation. Specify a build in an AWS
    CodeBuild with SourceBuildInformation and BuildConfiguration. Specify a source bundle in S3 with SourceBundle
    Omit both SourceBuildInformation and SourceBundle to use the default sample application.  After you create an
    application version with a specified Amazon S3 bucket and key location, you can't change that Amazon S3
    location. If you change the Amazon S3 location, you receive an exception when you attempt to launch an
    environment from the application version.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        application_name(Text):  The name of the application. If no application is found with this name, and
            AutoCreateApplication is false, returns an InvalidParameterValue error.
        version_label(Text): A label identifying this version. Constraint: Must be unique per application. If an application
            version already exists with this label for the specified application, AWS Elastic Beanstalk
            returns an InvalidParameterValue error.
        description(Text, optional): A description of this application version. Defaults to None.
        source_build_information(Dict, optional): Specify a commit in an AWS CodeCommit Git repository to use as the source code for the
            application version. Defaults to None.
        source_bundle(Dict, optional): The Amazon S3 bucket and key that identify the location of the source bundle for this version.
            The Amazon S3 bucket must be in the same region as the environment.  Specify a source bundle in
            S3 or a commit in an AWS CodeCommit repository (with SourceBuildInformation), but not both. If
            neither SourceBundle nor SourceBuildInformation are provided, Elastic Beanstalk uses a sample
            application. Defaults to None.
        build_configuration(Dict, optional): Settings for an AWS CodeBuild build. Defaults to None.
        auto_create_application(bool, optional): Set to true to create an application with the specified name if it doesn't already exist. Defaults to None.
        process(bool, optional): Pre-processes and validates the environment manifest (env.yaml) and configuration files
            (*.config files in the .ebextensions folder) in the source bundle. Validating configuration
            files can identify issues prior to deploying the application version to an environment. You must
            turn processing on for application versions that you create using AWS CodeBuild or AWS
            CodeCommit. For application versions built from a source bundle in Amazon S3, processing is
            optional.  The Process option validates Elastic Beanstalk configuration files. It doesn't
            validate your application's configuration files, like proxy server or Docker configuration. Defaults to None.
        tags(List, optional): Specifies the tags applied to the application version. Elastic Beanstalk applies these tags only
            to the application version. Environments that use the application version don't inherit the
            tags. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.elasticbeanstalk.create_application_version(
                ctx,
                
                
                **{"ApplicationName": application_name, "VersionLabel": version_label, "Description": description, "SourceBuildInformation": source_build_information, "SourceBundle": source_bundle, "BuildConfiguration": build_configuration, "AutoCreateApplication": auto_create_application, "Process": process, "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.elasticbeanstalk.describe_application_versions(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, application_name: Text, version_label: Text, delete_source_bundle: bool = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the specified version from the specified application.  You cannot delete an application version that is
    associated with a running environment.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        application_name(Text): The name of the application to which the version belongs.
        version_label(Text): The label of the version to delete.
        delete_source_bundle(bool, optional): Set to true to delete the source bundle from your storage bucket. Otherwise, the application
            version is deleted only from Elastic Beanstalk and the source bundle remains in Amazon S3. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.elasticbeanstalk.delete_application_version(
                ctx,
                
                
                **{"ApplicationName": application_name, "VersionLabel": version_label, "DeleteSourceBundle": delete_source_bundle}
            )
            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.elasticbeanstalk.describe_application_versions(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

