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

hub.exec.boto3.client.ec2.create_fleet
hub.exec.boto3.client.ec2.delete_fleets
hub.exec.boto3.client.ec2.describe_fleets
hub.exec.boto3.client.ec2.modify_fleet
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, launch_template_configs: List, target_capacity_specification: Dict, spot_options: Dict = None, on_demand_options: Dict = None, excess_capacity_termination_policy: Text = None, terminate_instances_with_expiration: bool = None, type_: Text = None, valid_from: Text = None, valid_until: Text = None, replace_unhealthy_instances: bool = None, tag_specifications: List = None, context: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Launches an EC2 Fleet. You can create a single EC2 Fleet that includes multiple launch specifications that vary
    by instance type, AMI, Availability Zone, or subnet. For more information, see Launching an EC2 Fleet in the
    Amazon EC2 User Guide.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        spot_options(Dict, optional): Describes the configuration of Spot Instances in an EC2 Fleet. Defaults to None.
        on_demand_options(Dict, optional): Describes the configuration of On-Demand Instances in an EC2 Fleet. Defaults to None.
        excess_capacity_termination_policy(Text, optional): Indicates whether running instances should be terminated if the total target capacity of the EC2
            Fleet is decreased below the current size of the EC2 Fleet. Defaults to None.
        launch_template_configs(List): The configuration for the EC2 Fleet.
        target_capacity_specification(Dict): The number of units to request.
        terminate_instances_with_expiration(bool, optional): Indicates whether running instances should be terminated when the EC2 Fleet expires. Defaults to None.
        type_(Text, optional): The fleet type. The default value is maintain.    maintain - The EC2 Fleet places an
            asynchronous request for your desired capacity, and continues to maintain your desired Spot
            capacity by replenishing interrupted Spot Instances.    request - The EC2 Fleet places an
            asynchronous one-time request for your desired capacity, but does submit Spot requests in
            alternative capacity pools if Spot capacity is unavailable, and does not maintain Spot capacity
            if Spot Instances are interrupted.    instant - The EC2 Fleet places a synchronous one-time
            request for your desired capacity, and returns errors for any instances that could not be
            launched.   For more information, see EC2 Fleet request types in the Amazon EC2 User Guide. Defaults to None.
        valid_from(Text, optional): The start date and time of the request, in UTC format (for example, YYYY-MM-DDTHH:MM:SSZ). The
            default is to start fulfilling the request immediately. Defaults to None.
        valid_until(Text, optional): The end date and time of the request, in UTC format (for example, YYYY-MM-DDTHH:MM:SSZ). At this
            point, no new EC2 Fleet requests are placed or able to fulfill the request. If no value is
            specified, the request remains until you cancel it. Defaults to None.
        replace_unhealthy_instances(bool, optional): Indicates whether EC2 Fleet should replace unhealthy Spot Instances. Supported only for fleets
            of type maintain. For more information, see EC2 Fleet health checks in the Amazon EC2 User
            Guide. Defaults to None.
        tag_specifications(List, optional): The key-value pair for tagging the EC2 Fleet request on creation. For more information, see
            Tagging your resources. If the fleet type is instant, specify a resource type of fleet to tag
            the fleet or instance to tag the instances at launch. If the fleet type is maintain or request,
            specify a resource type of fleet to tag the fleet. You cannot specify a resource type of
            instance. To tag instances at launch, specify the tags in a launch template. Defaults to None.
        context(Text, optional): Reserved. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.ec2.fleet.present:
                - name: value
                - launch_template_configs: value
                - target_capacity_specification: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.ec2.fleet.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_fleets(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.ec2.create_fleet(
                ctx,
                DryRun=ctx.test,
                ClientToken=name,
                **{"SpotOptions": spot_options, "OnDemandOptions": on_demand_options, "ExcessCapacityTerminationPolicy": excess_capacity_termination_policy, "LaunchTemplateConfigs": launch_template_configs, "TargetCapacitySpecification": target_capacity_specification, "TerminateInstancesWithExpiration": terminate_instances_with_expiration, "Type": type_, "ValidFrom": valid_from, "ValidUntil": valid_until, "ReplaceUnhealthyInstances": replace_unhealthy_instances, "TagSpecifications": tag_specifications, "Context": context}
            )
            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_fleets(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, fleet_ids: List, terminate_instances: bool)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the specified EC2 Fleet. After you delete an EC2 Fleet, it launches no new instances. You must specify
    whether a deleted EC2 Fleet should also terminate its instances. If you choose to terminate the instances, the
    EC2 Fleet enters the deleted_terminating state. Otherwise, the EC2 Fleet enters the deleted_running state, and
    the instances continue to run until they are interrupted or you terminate them manually. For instant fleets, EC2
    Fleet must terminate the instances when the fleet is deleted. A deleted instant fleet with running instances is
    not supported.  Restrictions    You can delete up to 25 instant fleets in a single request. If you exceed this
    number, no instant fleets are deleted and an error is returned. There is no restriction on the number of fleets
    of type maintain or request that can be deleted in a single request.   Up to 1000 instances can be terminated in
    a single request to delete instant fleets.   For more information, see Deleting an EC2 Fleet in the Amazon EC2
    User Guide.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        fleet_ids(List): The IDs of the EC2 Fleets.
        terminate_instances(bool): Indicates whether to terminate the instances when the EC2 Fleet is deleted. The default is to
            terminate the instances. To let the instances continue to run after the EC2 Fleet is deleted,
            specify NoTerminateInstances. Supported only for fleets of type maintain and request. For
            instant fleets, you cannot specify NoTerminateInstances. A deleted instant fleet with running
            instances is not supported.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

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

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.ec2.delete_fleets(
                ctx,
                DryRun=ctx.test,
                
                **{"FleetIds": fleet_ids, "TerminateInstances": terminate_instances}
            )
            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_fleets(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

