PreventValue=["true","True","TRUE"]

accounts_IDs = config['aws']['accounts']

for accountid in accounts_IDs:
    acct_module = accounts()
    account_info=acct_module.getaccount(accountid)
    print "Account Name : {0} Env: {1}".format(account_info["name"],account_info["env"])

    preserveList=[]
    termiList=[]
    warningList=[]

    session = getsession(accountid)
    ec2 = session.client('ec2')
    describe_instancs=ec2.describe_instances()
    if describe_instancs:
       for instances in describe_instancs["Reservations"]:
           for instance in instances["Instances"]:

               if instance["State"]["Name"]=="stopped":
                  InstanceInfo={}
                  InstanceInfo["InstanceId"]=instance["InstanceId"]
                  InstanceInfo["Name"]=''
                  InstanceInfo["Preserve"]=''
                  InstanceInfo["Time"]=''

                  last_starttime_us=instance["LaunchTime"]
                  if instance.get("Tags"):
def elb_info(accountid):
    prevent_elb = []
    empty_elb = []
    notraffic_elb = []
    warning_notraffic_elb = []

    session = getsession(accountid)
    elb_session = session.client("elb")
    describe_elb = elb_session.describe_load_balancers()
    for elb in describe_elb["LoadBalancerDescriptions"]:
        get_tags = get_preserve_tag(elb["LoadBalancerName"], session)
        if get_tags == "True":
            prevent_elb.append(elb["LoadBalancerName"])
        else:
            if elb["CreatedTime"] < daletedays_ourzone:
                if not elb["Instances"]:
                    empty_elb.append(elb["LoadBalancerName"])
                else:
                    no_traffic = get_notraffic_elb(
                        elb["LoadBalancerName"],
                        session,
                        config["system"]["warningdays"],
                        config["system"]["deletedays"],
                    )
                    if no_traffic == "True":
                        notraffic_elb.append(elb["LoadBalancerName"])
                    elif no_traffic == "Warning":
                        warning_notraffic_elb.append(elb["LoadBalancerName"])

    notraffic_asg = []
    for item in notraffic_elb:
        asg_info = elb_in_asg(item, session)
        if asg_info:
            notraffic_asg.append(asg_info)
            notraffic_elb.remove(item)
    empty_asg = []
    for item in empty_elb:
        asg_info = elb_in_asg(item, session)

        if asg_info:
            empty_asg.append(asg_info)
            empty_elb.remove(item)

    notraffic_instances = []
    for item in notraffic_elb:
        instances = get_elb_instance(item, describe_elb)
        notraffic_instances.append(instances)

    sorted_prevent_elb = sorted(prevent_elb)
    sorted_warning_notraffic_elb = sorted(warning_notraffic_elb)
    sorted_notraffic_asg = sorted(notraffic_asg, key=lambda k: k["ELB"])
    sorted_notraffic_instances = sorted(notraffic_instances, key=lambda k: k["ELB"])

    empty_elb_info = []
    warning_elb_info = []
    empty_asg_info = []
    for eptelb in empty_elb:
        event = get_event_log(accountid, eptelb, "DeregisterInstancesFromLoadBalancer", warning, deld)
        if event["deletion"] == "Warning":
            warning_elb_info.append(event)
        elif event["deletion"] == "True":
            empty_elb_info.append(event)
    for asg in empty_asg:
        event = get_event_log(accountid, asg["asgName"], "UpdateAutoScalingGroup", warning, deld)
        event["asg"] = asg["asgName"]
        event["resource"] = asg["ELB"][0]

        if event["deletion"] == "Warning":
            warning_elb_info.append(event)
        elif event["deletion"] == "True":
            empty_asg_info.append(event)

    sorted_empty_elb_info = sorted(empty_elb_info, key=lambda k: k["resource"])
    sorted_warning_elb_info = sorted(warning_elb_info, key=lambda k: k["resource"])
    sorted_empty_asg_info = sorted(empty_asg_info, key=lambda k: k["resource"])

    if sorted_warning_notraffic_elb or sorted_notraffic_asg or sorted_notraffic_instances:
        print "[   no traffic ELBs   ]"
        if sorted_warning_notraffic_elb:
            print "=============Warming List================"
            for elb in sorted_warning_notraffic_elb:
                print "{0} doesn't have traffic for more than 3 days, deletion less than three days ".format(elb)
        if sorted_notraffic_asg or sorted_notraffic_instances:
            print "=============Deleting List================"

            for elb in sorted_notraffic_instances:
                print "{0} doesn't have traffic for more than 5 days ".format(elb["ELB"])
            for asg in sorted_notraffic_asg:
                print "{0} in autoscaling Group {1} doesn't have traffic for more than 5 days ".format(
                    asg["ELB"][0], asg["asgName"]
                )

    ## update asg to 0
    # asg_client  = session.client('autoscaling')
    # asg.update_auto_scaling_group(
    #  AutoScalingGroupName = asg["asgName"],
    #  MinSize=0,
    #  DesiredCapacity = 0
    #  )

    if sorted_empty_elb_info or sorted_warning_elb_info or sorted_empty_asg_info:
        print "[   no instances ELBs   ]"
        if sorted_warning_elb_info:
            print "=============Warming List================"
            for elb in sorted_warning_elb_info:
                print "WARNING {0} doesn't have a instance for more then 3 days, going to delete less then two days".format(
                    elb["resource"]
                )

        if sorted_empty_elb_info or sorted_empty_asg_info:
            print "=============Deleting List================"
            for elb in sorted_empty_elb_info:
                if not elb["time"]:
                    print "{0} doesn't have a instance for more then 5 days".format(elb["resource"])
                else:
                    print "{0} doesn't have a instance since {1}".format(elb["resource"], elb["time"])
                ## deletion
                elb_session.delete_load_balancer(LoadBalancerName=elb["resource"])

            for asg in sorted_empty_asg_info:
                if not asg["time"]:
                    print "{0} in autoscaling Group {1} doesn't have a instance for more then 5 days".format(
                        asg["resource"], asg["asg"]
                    )
                else:
                    print "{0} in autoscaling Group {1} doesn't have a instance since {2}".format(
                        asg["resource"], asg["asg"], asg["time"]
                    )
    ### deletion
    #              elb_session.delete_load_balancer(LoadBalancerName=asg["resource"]

    if sorted_prevent_elb:
        print "=============Prevent List================"
        for prevent_elb in sorted_prevent_elb:
            print "{0} is prevented from deletion".format(prevent_elb)

    print ""