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

hub.exec.boto3.client.ec2.cancel_reserved_instances_listing
hub.exec.boto3.client.ec2.create_reserved_instances_listing
hub.exec.boto3.client.ec2.describe_reserved_instances_listings
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, instance_count: int, price_schedules: List, reserved_instances_id: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a listing for Amazon EC2 Standard Reserved Instances to be sold in the Reserved Instance Marketplace.
    You can submit one Standard Reserved Instance listing at a time. To get a list of your Standard Reserved
    Instances, you can use the DescribeReservedInstances operation.  Only Standard Reserved Instances can be sold in
    the Reserved Instance Marketplace. Convertible Reserved Instances cannot be sold.  The Reserved Instance
    Marketplace matches sellers who want to resell Standard Reserved Instance capacity that they no longer need with
    buyers who want to purchase additional capacity. Reserved Instances bought and sold through the Reserved
    Instance Marketplace work like any other Reserved Instances. To sell your Standard Reserved Instances, you must
    first register as a seller in the Reserved Instance Marketplace. After completing the registration process, you
    can create a Reserved Instance Marketplace listing of some or all of your Standard Reserved Instances, and
    specify the upfront price to receive for them. Your Standard Reserved Instance listings then become available
    for purchase. To view the details of your Standard Reserved Instance listing, you can use the
    DescribeReservedInstancesListings operation. For more information, see Reserved Instance Marketplace in the
    Amazon EC2 User Guide.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        instance_count(int): The number of instances that are a part of a Reserved Instance account to be listed in the
            Reserved Instance Marketplace. This number should be less than or equal to the instance count
            associated with the Reserved Instance ID specified in this call.
        price_schedules(List): A list specifying the price of the Standard Reserved Instance for each month remaining in the
            Reserved Instance term.
        reserved_instances_id(Text): The ID of the active Standard Reserved Instance.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.ec2.reserved_instances_listing.present:
                - name: value
                - instance_count: value
                - price_schedules: value
                - reserved_instances_id: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.ec2.reserved_instances_listing.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.ec2.describe_reserved_instances_listings(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.ec2.create_reserved_instances_listing(
                ctx,
                
                ClientToken=name,
                **{"InstanceCount": instance_count, "PriceSchedules": price_schedules, "ReservedInstancesId": reserved_instances_id}
            )
            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.ec2.describe_reserved_instances_listings(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, reserved_instances_listing_id: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Cancels the specified Reserved Instance listing in the Reserved Instance Marketplace. For more information, see
    Reserved Instance Marketplace in the Amazon EC2 User Guide.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        reserved_instances_listing_id(Text): The ID of the Reserved Instance listing.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

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

