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

hub.exec.boto3.client.ec2.create_security_group
hub.exec.boto3.client.ec2.delete_security_group
hub.exec.boto3.client.ec2.describe_security_groups
resource = hub.tool.boto3.resource.create(ctx, "ec2", "SecurityGroup", name)
hub.tool.boto3.resource.exec(resource, authorize_egress, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, authorize_ingress, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, create_tags, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, delete, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, revoke_egress, *args, **kwargs)
hub.tool.boto3.resource.exec(resource, revoke_ingress, *args, **kwargs)
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, description: Text, group_name: Text, vpc_id: Text = None, tag_specifications: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a security group. A security group acts as a virtual firewall for your instance to control inbound and
    outbound traffic. For more information, see Amazon EC2 security groups in the Amazon Elastic Compute Cloud User
    Guide and Security groups for your VPC in the Amazon Virtual Private Cloud User Guide. When you create a
    security group, you specify a friendly name of your choice. You can have a security group for use in EC2-Classic
    with the same name as a security group for use in a VPC. However, you can't have two security groups for use in
    EC2-Classic with the same name or two security groups for use in a VPC with the same name. You have a default
    security group for use in EC2-Classic and a default security group for use in your VPC. If you don't specify a
    security group when you launch an instance, the instance is launched into the appropriate default security
    group. A default security group includes a default rule that grants instances unrestricted network access to
    each other. You can add or remove rules from your security groups using AuthorizeSecurityGroupIngress,
    AuthorizeSecurityGroupEgress, RevokeSecurityGroupIngress, and RevokeSecurityGroupEgress. For more information
    about VPC security group limits, see Amazon VPC Limits.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        description(Text): A description for the security group. This is informational only. Constraints: Up to 255
            characters in length Constraints for EC2-Classic: ASCII characters Constraints for EC2-VPC: a-z,
            A-Z, 0-9, spaces, and ._-:/()#,@[]+=&;{}!$*.
        group_name(Text): The name of the security group. Constraints: Up to 255 characters in length. Cannot start with
            sg-. Constraints for EC2-Classic: ASCII characters Constraints for EC2-VPC: a-z, A-Z, 0-9,
            spaces, and ._-:/()#,@[]+=&;{}!$*.
        vpc_id(Text, optional): [EC2-VPC] The ID of the VPC. Required for EC2-VPC. Defaults to None.
        tag_specifications(List, optional): The tags to assign to the security group. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.ec2.security_group.present:
                - name: value
                - description: value
                - group_name: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.ec2.security_group.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, "ec2", "SecurityGroup", 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.ec2.create_security_group(
                ctx,
                DryRun=ctx.test,
                
                **{"Description": description, "GroupName": group_name, "VpcId": vpc_id, "TagSpecifications": tag_specifications}
            )
            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, group_id: Text = None, group_name: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes a security group. If you attempt to delete a security group that is associated with an instance, or is
    referenced by another security group, the operation fails with InvalidGroup.InUse in EC2-Classic or
    DependencyViolation in EC2-VPC.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        group_id(Text, optional): The ID of the security group. Required for a nondefault VPC. Defaults to None.
        group_name(Text, optional): [EC2-Classic, default VPC] The name of the security group. You can specify either the security
            group name or the security group ID. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.ec2.security_group.absent:
                - name: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.ec2.security_group.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, "ec2", "SecurityGroup", 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.ec2.delete_security_group(
                ctx,
                DryRun=ctx.test,
                
                **{"GroupId": group_id, "GroupName": group_name}
            )
            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

