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

hub.exec.boto3.client.iotthingsgraph.create_system_instance
hub.exec.boto3.client.iotthingsgraph.delete_system_instance
hub.exec.boto3.client.iotthingsgraph.deploy_system_instance
hub.exec.boto3.client.iotthingsgraph.get_system_instance
hub.exec.boto3.client.iotthingsgraph.search_system_instances
hub.exec.boto3.client.iotthingsgraph.undeploy_system_instance
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, definition: Dict, target: Text, tags: List = None, greengrass_group_name: Text = None, s3_bucket_name: Text = None, metrics_configuration: Dict = None, flow_actions_role_arn: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a system instance.  This action validates the system instance, prepares the deployment-related
    resources. For Greengrass deployments, it updates the Greengrass group that is specified by the
    greengrassGroupName parameter. It also adds a file to the S3 bucket specified by the s3BucketName parameter. You
    need to call DeploySystemInstance after running this action. For Greengrass deployments, since this action
    modifies and adds resources to a Greengrass group and an S3 bucket on the caller's behalf, the calling identity
    must have write permissions to both the specified Greengrass group and S3 bucket. Otherwise, the call will fail
    with an authorization error. For cloud deployments, this action requires a flowActionsRoleArn value. This is an
    IAM role that has permissions to access AWS services, such as AWS Lambda and AWS IoT, that the flow uses when it
    executes. If the definition document doesn't specify a version of the user's namespace, the latest version will
    be used by default.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        tags(List, optional): Metadata, consisting of key-value pairs, that can be used to categorize your system instances. Defaults to None.
        definition(Dict): A document that defines an entity.
        target(Text): The target type of the deployment. Valid values are GREENGRASS and CLOUD.
        greengrass_group_name(Text, optional): The name of the Greengrass group where the system instance will be deployed. This value is
            required if the value of the target parameter is GREENGRASS. Defaults to None.
        s3_bucket_name(Text, optional): The name of the Amazon Simple Storage Service bucket that will be used to store and deploy the
            system instance's resource file. This value is required if the value of the target parameter is
            GREENGRASS. Defaults to None.
        metrics_configuration(Dict, optional): An object that specifies whether cloud metrics are collected in a deployment and, if so, what
            role is used to collect metrics. Defaults to None.
        flow_actions_role_arn(Text, optional): The ARN of the IAM role that AWS IoT Things Graph will assume when it executes the flow. This
            role must have read and write access to AWS Lambda and AWS IoT and any other AWS services that
            the flow uses when it executes. This value is required if the value of the target parameter is
            CLOUD. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.iotthingsgraph.create_system_instance(
                ctx,
                
                
                **{"tags": tags, "definition": definition, "target": target, "greengrassGroupName": greengrass_group_name, "s3BucketName": s3_bucket_name, "metricsConfiguration": metrics_configuration, "flowActionsRoleArn": flow_actions_role_arn}
            )
            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.iotthingsgraph.get_system_instance(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, id_: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes a system instance. Only system instances that have never been deployed, or that have been undeployed can
    be deleted. Users can create a new system instance that has the same ID as a deleted system instance.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        id_(Text, optional): The ID of the system instance to be deleted. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

