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

hub.exec.boto3.client.opsworks.create_app
hub.exec.boto3.client.opsworks.delete_app
hub.exec.boto3.client.opsworks.describe_apps
hub.exec.boto3.client.opsworks.update_app
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, stack_id: Text, type_: Text, shortname: Text = None, description: Text = None, data_sources: List = None, app_source: Dict = None, domains: List = None, enable_ssl: bool = None, ssl_configuration: Dict = None, attributes: Dict = None, environment: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates an app for a specified stack. For more information, see Creating Apps.  Required Permissions: To use
    this action, an IAM user must have a Manage permissions level for the stack, or an attached policy that
    explicitly grants permissions. For more information on user permissions, see Managing User Permissions.

    Args:
        name(Text): The app name.
        stack_id(Text): The stack ID.
        shortname(Text, optional): The app's short name. Defaults to None.
        description(Text, optional): A description of the app. Defaults to None.
        data_sources(List, optional): The app's data source. Defaults to None.
        type_(Text): The app type. Each supported type is associated with a particular layer. For example, PHP
            applications are associated with a PHP layer. AWS OpsWorks Stacks deploys an application to
            those instances that are members of the corresponding layer. If your app isn't one of the
            standard types, or you prefer to implement your own Deploy recipes, specify other.
        app_source(Dict, optional): A Source object that specifies the app repository. Defaults to None.
        domains(List, optional): The app virtual host settings, with multiple domains separated by commas. For example:
            'www.example.com, example.com'. Defaults to None.
        enable_ssl(bool, optional): Whether to enable SSL for the app. Defaults to None.
        ssl_configuration(Dict, optional): An SslConfiguration object with the SSL configuration. Defaults to None.
        attributes(Dict, optional): One or more user-defined key/value pairs to be added to the stack attributes. Defaults to None.
        environment(List, optional): An array of EnvironmentVariable objects that specify environment variables to be associated with
            the app. After you deploy the app, these variables are defined on the associated app server
            instance. For more information, see  Environment Variables. There is no specific limit on the
            number of environment variables. However, the size of the associated data structure - which
            includes the variables' names, values, and protected flag values - cannot exceed 20 KB. This
            limit should accommodate most if not all use cases. Exceeding it will cause an exception with
            the message, "Environment: is too large (maximum is 20KB)."  If you have specified one or more
            environment variables, you cannot modify the stack's Chef version. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.opsworks.create_app(
                ctx,
                
                
                **{"Name": name, "StackId": stack_id, "Shortname": shortname, "Description": description, "DataSources": data_sources, "Type": type_, "AppSource": app_source, "Domains": domains, "EnableSsl": enable_ssl, "SslConfiguration": ssl_configuration, "Attributes": attributes, "Environment": environment}
            )
            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.opsworks.describe_apps(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, app_id: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes a specified app.  Required Permissions: To use this action, an IAM user must have a Manage permissions
    level for the stack, or an attached policy that explicitly grants permissions. For more information on user
    permissions, see Managing User Permissions.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        app_id(Text): The app ID.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

