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

hub.exec.boto3.client.iam.create_access_key
hub.exec.boto3.client.iam.delete_access_key
hub.exec.boto3.client.iam.list_access_keys
hub.exec.boto3.client.iam.update_access_key
resource = hub.tool.boto3.resource.create(ctx, "iam", "AccessKey", name)
hub.tool.boto3.resource.exec(resource, activate, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, deactivate, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, delete, *args, **kwargs)
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, user_name: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
     Creates a new Amazon Web Services secret access key and corresponding Amazon Web Services access key ID for the
    specified user. The default status for new keys is Active. If you do not specify a user name, IAM determines the
    user name implicitly based on the Amazon Web Services access key ID signing the request. This operation works
    for access keys under the account. Consequently, you can use this operation to manage account root user
    credentials. This is true even if the account has no associated users.  For information about quotas on the
    number of keys you can create, see IAM and STS quotas in the IAM User Guide.  To ensure the security of your
    account, the secret access key is accessible only during key and user creation. You must save the key (for
    example, in a text file) if you want to be able to access it again. If a secret key is lost, you can delete the
    access keys for the associated user and then create new keys.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        user_name(Text, optional): The name of the IAM user that the new key will belong to. This parameter allows (through its
            regex pattern) a string of characters consisting of upper and lowercase alphanumeric characters
            with no spaces. You can also include any of the following characters: _+=,.@-. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.iam.access_key.present:
                - name: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.iam.access_key.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    resource = hub.tool.boto3.resource.create(ctx, "iam", "AccessKey", name)
    before = await hub.tool.boto3.resource.describe(resource)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.iam.create_access_key(
                ctx,
                
                
                **{"UserName": user_name}
            )
            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.tool.boto3.resource.describe(resource)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, access_key_id: Text, user_name: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the access key pair associated with the specified IAM user. If you do not specify a user name, IAM
    determines the user name implicitly based on the Amazon Web Services access key ID signing the request. This
    operation works for access keys under the account. Consequently, you can use this operation to manage account
    root user credentials even if the account has no associated users.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        user_name(Text, optional): The name of the user whose access key pair you want to delete. This parameter allows (through
            its regex pattern) a string of characters consisting of upper and lowercase alphanumeric
            characters with no spaces. You can also include any of the following characters: _+=,.@-. Defaults to None.
        access_key_id(Text): The access key ID for the access key ID and secret access key you want to delete. This parameter
            allows (through its regex pattern) a string of characters that can consist of any upper or
            lowercased letter or digit.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.iam.access_key.absent:
                - name: value
                - access_key_id: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.iam.access_key.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    resource = hub.tool.boto3.resource.create(ctx, "iam", "AccessKey", name)

    before = await hub.tool.boto3.resource.describe(resource)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.iam.delete_access_key(
                ctx,
                
                
                **{"UserName": user_name, "AccessKeyId": access_key_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.tool.boto3.resource.describe(resource)
    result["changes"] = differ.deep_diff(before, after)
    return result

