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

hub.exec.boto3.client.iot.delete_certificate
hub.exec.boto3.client.iot.describe_certificate
hub.exec.boto3.client.iot.list_certificates
hub.exec.boto3.client.iot.register_certificate
hub.exec.boto3.client.iot.transfer_certificate
hub.exec.boto3.client.iot.update_certificate
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, certificate_pem: Text, ca_certificate_pem: Text = None, set_as_active: bool = None, status: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Registers a device certificate with AWS IoT. If you have more than one CA certificate that has the same subject
    field, you must specify the CA certificate that was used to sign the device certificate being registered.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        certificate_pem(Text): The certificate data, in PEM format.
        ca_certificate_pem(Text, optional): The CA certificate used to sign the device certificate being registered. Defaults to None.
        set_as_active(bool, optional): A boolean value that specifies if the certificate is set to active. Defaults to None.
        status(Text, optional): The status of the register certificate request. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.iot.register_certificate(
                ctx,
                
                
                **{"certificatePem": certificate_pem, "caCertificatePem": ca_certificate_pem, "setAsActive": set_as_active, "status": status}
            )
            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.iot.describe_certificate(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, certificate_id: Text, force_delete: bool = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the specified certificate. A certificate cannot be deleted if it has a policy or IoT thing attached to
    it or if its status is set to ACTIVE. To delete a certificate, first use the DetachPrincipalPolicy API to detach
    all policies. Next, use the UpdateCertificate API to set the certificate to the INACTIVE status.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        certificate_id(Text): The ID of the certificate. (The last part of the certificate ARN contains the certificate ID.).
        force_delete(bool, optional): Forces the deletion of a certificate if it is inactive and is not attached to an IoT thing. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

