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

hub.exec.boto3.client.elasticbeanstalk.compose_environments
hub.exec.boto3.client.elasticbeanstalk.create_environment
hub.exec.boto3.client.elasticbeanstalk.describe_environments
hub.exec.boto3.client.elasticbeanstalk.rebuild_environment
hub.exec.boto3.client.elasticbeanstalk.terminate_environment
hub.exec.boto3.client.elasticbeanstalk.update_environment
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, application_name: Text, environment_name: Text = None, group_name: Text = None, description: Text = None, cname_prefix: Text = None, tier: Dict = None, tags: List = None, version_label: Text = None, template_name: Text = None, solution_stack_name: Text = None, platform_arn: Text = None, option_settings: List = None, options_to_remove: List = None, operations_role: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Launches an AWS Elastic Beanstalk environment for the specified application using the specified configuration.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        application_name(Text): The name of the application that is associated with this environment.
        environment_name(Text, optional): A unique name for the environment. Constraint: Must be from 4 to 40 characters in length. The
            name can contain only letters, numbers, and hyphens. It can't start or end with a hyphen. This
            name must be unique within a region in your account. If the specified name already exists in the
            region, Elastic Beanstalk returns an InvalidParameterValue error.  If you don't specify the
            CNAMEPrefix parameter, the environment name becomes part of the CNAME, and therefore part of the
            visible URL for your application. Defaults to None.
        group_name(Text, optional): The name of the group to which the target environment belongs. Specify a group name only if the
            environment's name is specified in an environment manifest and not with the environment name
            parameter. See Environment Manifest (env.yaml) for details. Defaults to None.
        description(Text, optional): Your description for this environment. Defaults to None.
        cname_prefix(Text, optional): If specified, the environment attempts to use this value as the prefix for the CNAME in your
            Elastic Beanstalk environment URL. If not specified, the CNAME is generated automatically by
            appending a random alphanumeric string to the environment name. Defaults to None.
        tier(Dict, optional): Specifies the tier to use in creating this environment. The environment tier that you choose
            determines whether Elastic Beanstalk provisions resources to support a web application that
            handles HTTP(S) requests or a web application that handles background-processing tasks. Defaults to None.
        tags(List, optional): Specifies the tags applied to resources in the environment. Defaults to None.
        version_label(Text, optional): The name of the application version to deploy. Default: If not specified, Elastic Beanstalk
            attempts to deploy the sample application. Defaults to None.
        template_name(Text, optional): The name of the Elastic Beanstalk configuration template to use with the environment.  If you
            specify TemplateName, then don't specify SolutionStackName. Defaults to None.
        solution_stack_name(Text, optional): The name of an Elastic Beanstalk solution stack (platform version) to use with the environment.
            If specified, Elastic Beanstalk sets the configuration values to the default values associated
            with the specified solution stack. For a list of current solution stacks, see Elastic Beanstalk
            Supported Platforms in the AWS Elastic Beanstalk Platforms guide.  If you specify
            SolutionStackName, don't specify PlatformArn or TemplateName. Defaults to None.
        platform_arn(Text, optional): The Amazon Resource Name (ARN) of the custom platform to use with the environment. For more
            information, see Custom Platforms in the AWS Elastic Beanstalk Developer Guide.  If you specify
            PlatformArn, don't specify SolutionStackName. Defaults to None.
        option_settings(List, optional): If specified, AWS Elastic Beanstalk sets the specified configuration options to the requested
            value in the configuration set for the new environment. These override the values obtained from
            the solution stack or the configuration template. Defaults to None.
        options_to_remove(List, optional): A list of custom user-defined configuration options to remove from the configuration set for
            this new environment. Defaults to None.
        operations_role(Text, optional): The Amazon Resource Name (ARN) of an existing IAM role to be used as the environment's
            operations role. If specified, Elastic Beanstalk uses the operations role for permissions to
            downstream services during this call and during subsequent calls acting on this environment. To
            specify an operations role, you must have the iam:PassRole permission for the role. For more
            information, see Operations roles in the AWS Elastic Beanstalk Developer Guide. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.elasticbeanstalk.create_environment(
                ctx,
                
                
                **{"ApplicationName": application_name, "EnvironmentName": environment_name, "GroupName": group_name, "Description": description, "CNAMEPrefix": cname_prefix, "Tier": tier, "Tags": tags, "VersionLabel": version_label, "TemplateName": template_name, "SolutionStackName": solution_stack_name, "PlatformArn": platform_arn, "OptionSettings": option_settings, "OptionsToRemove": options_to_remove, "OperationsRole": operations_role}
            )
            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_environments(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, environment_id: Text = None, environment_name: Text = None, terminate_resources: bool = None, force_terminate: bool = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Terminates the specified environment.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        environment_id(Text, optional): The ID of the environment to terminate.  Condition: You must specify either this or an
            EnvironmentName, or both. If you do not specify either, AWS Elastic Beanstalk returns
            MissingRequiredParameter error. Defaults to None.
        environment_name(Text, optional): The name of the environment to terminate.  Condition: You must specify either this or an
            EnvironmentId, or both. If you do not specify either, AWS Elastic Beanstalk returns
            MissingRequiredParameter error. Defaults to None.
        terminate_resources(bool, optional): Indicates whether the associated AWS resources should shut down when the environment is
            terminated:    true: The specified environment as well as the associated AWS resources, such as
            Auto Scaling group and LoadBalancer, are terminated.    false: AWS Elastic Beanstalk resource
            management is removed from the environment, but the AWS resources continue to operate.    For
            more information, see the  AWS Elastic Beanstalk User Guide.    Default: true   Valid Values:
            true | false. Defaults to None.
        force_terminate(bool, optional): Terminates the target environment even if another environment in the same group is dependent on
            it. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.elasticbeanstalk.terminate_environment(
                ctx,
                
                
                **{"EnvironmentId": environment_id, "EnvironmentName": environment_name, "TerminateResources": terminate_resources, "ForceTerminate": force_terminate}
            )
            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_environments(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

