def main(arg):
    access_key = arg.accessKey
    secret_key = arg.secretKey
    region_list = []
    unused_resource_details = {}
    try:
        print('Connecting to AWS account ')
        session = boto3.session.Session(aws_access_key_id=access_key, aws_secret_access_key=secret_key)
    except:
        print('\x1b[1;31;40m Please do Check for Credentials provided or Internet Connection and Try Again\n')
        quit()
    iam = session.client('sts')
    account_id = iam.get_caller_identity()['Account']
    print('Successfully connected to AWS account', account_id)
    print('Scanning for unused resources across all available regions.')
    print('Wait for few minutes...\n')
    function_list = [ebs_volume, elastic_ip, network_interface, vpc, subnet, security_group, classic_loadbalancer, app_nw_gateway_loadbalancer, iam_user, iam_group]
    print('Collecting list of enabled region')
    available_regions = session.client('ec2', region_name='us-east-1')
    enabled_regions = available_regions.describe_regions()['Regions']
    for region in enabled_regions:
        region_list.append(region['RegionName'])
    manager = multiprocessing.Manager()
    unused_resource_count = manager.dict()
    credentials = manager.dict()
    credentials['access_key'] = access_key
    credentials['secret_key'] = secret_key
    credentials['account_id'] = account_id
    jobs = []
    try:
        os.mkdir('./aws_logs')
    except:
        pass
    for function in function_list:
        try:
            p = multiprocessing.Process(target=function, args=(function, credentials, unused_resource_count, region_list))
            jobs.append(p)
            p.start()
        except:
            print('Exception occurred while creating processes. Please try again later!')
            quit()
    if jobs:
        for process in jobs:
            try:
                process.join()
            except:
                print('Exception occurred while joining processes. Please try again later!')
                quit()
    os.chdir('./aws_logs')
    writer = pd.ExcelWriter('unused_resources.xlsx')
    all_files = glob.glob('*.csv')
    for f in all_files:
        df = pd.read_csv(f)
        df.to_excel(writer, sheet_name=f.split('.')[0], index=False)
    writer.save()
    for f in all_files:
        os.remove(f)
    print('Completed account scan')
    unused_resource_details.update({'AWS::EC2::Volume': unused_resource_count[ebs_volume], 'AWS::EC2::EIP': unused_resource_count[elastic_ip], 'AWS::EC2::NetworkInterface': unused_resource_count[network_interface], 'AWS::EC2::VPC': unused_resource_count[vpc], 'AWS::EC2::Subnet': unused_resource_count[subnet], 'AWS::EC2::SecurityGroup': unused_resource_count[security_group], 'AWS::ElasticLoadBalancing::LoadBalancer': unused_resource_count[classic_loadbalancer], 'AWS::ElasticLoadBalancingV2::LoadBalancer': unused_resource_count[app_nw_gateway_loadbalancer], 'AWS::IAM::User': unused_resource_count[iam_user], 'AWS::IAM::Group': unused_resource_count[iam_group]})
    print('\nUnused Resources in the Account:')
    unused_resource_count = 0
    for (key, value) in sorted(unused_resource_details.items(), key=(lambda x: x[1]), reverse=True):
        if (value != 0):
            print('\t{} : {}'.format(key, value))
            unused_resource_count += value
    print('\n\nSummary:')
    print('\tTotal Unused Resources:', unused_resource_count)
    print('\n\nDetailed unused resource information can be found at: aws_logs/unused_resources.xlsx')
def main(arg):
    access_key = arg.accessKey
    secret_key = arg.secretKey
    region_list = []
    unused_resource_details = {}
    try:
        print('Connecting to AWS account ')
        session = boto3.session.Session(aws_access_key_id=access_key, aws_secret_access_key=secret_key)
    except:
        print('\x1b[1;31;40m Please do Check for Credentials provided or Internet Connection and Try Again\n')
        quit()
    iam = session.client('sts')
    account_id = iam.get_caller_identity()['Account']
    print('Successfully connected to AWS account', account_id)
    print('Scanning for unused resources across all available regions.')
    print('Wait for few minutes...\n')
    function_list = [ebs_volume, elastic_ip, network_interface, vpc, subnet, security_group, classic_loadbalancer, app_nw_gateway_loadbalancer, iam_user, iam_group]
    print('Collecting list of enabled region')
    available_regions = session.client('ec2', region_name='us-east-1')
    enabled_regions = available_regions.describe_regions()['Regions']
    for region in enabled_regions:
        region_list.append(region['RegionName'])
    manager = multiprocessing.Manager()
    unused_resource_count = manager.dict()
    credentials = manager.dict()
    credentials['access_key'] = access_key
    credentials['secret_key'] = secret_key
    credentials['account_id'] = account_id
    jobs = []
    try:
        os.mkdir('./aws_logs')
    except:
        pass
    for function in function_list:
        try:
            p = multiprocessing.Process(target=function, args=(function, credentials, unused_resource_count, region_list))
            jobs.append(p)
            p.start()
        except:
            print('Exception occurred while creating processes. Please try again later!')
            quit()
    if jobs:
        for process in jobs:
            try:
                process.join()
            except:
                print('Exception occurred while joining processes. Please try again later!')
                quit()
    os.chdir('./aws_logs')
    writer = pd.ExcelWriter('unused_resources.xlsx')
    all_files = glob.glob('*.csv')
    for f in all_files:
        df = pd.read_csv(f)
        df.to_excel(writer, sheet_name=f.split('.')[0], index=False)
    writer.save()
    for f in all_files:
        os.remove(f)
    print('Completed account scan')
    unused_resource_details.update({'AWS::EC2::Volume': unused_resource_count[ebs_volume], 'AWS::EC2::EIP': unused_resource_count[elastic_ip], 'AWS::EC2::NetworkInterface': unused_resource_count[network_interface], 'AWS::EC2::VPC': unused_resource_count[vpc], 'AWS::EC2::Subnet': unused_resource_count[subnet], 'AWS::EC2::SecurityGroup': unused_resource_count[security_group], 'AWS::ElasticLoadBalancing::LoadBalancer': unused_resource_count[classic_loadbalancer], 'AWS::ElasticLoadBalancingV2::LoadBalancer': unused_resource_count[app_nw_gateway_loadbalancer], 'AWS::IAM::User': unused_resource_count[iam_user], 'AWS::IAM::Group': unused_resource_count[iam_group]})
    print('\nUnused Resources in the Account:')
    unused_resource_count = 0
    for (key, value) in sorted(unused_resource_details.items(), key=(lambda x: x[1]), reverse=True):
        if (value != 0):
            print('\t{} : {}'.format(key, value))
            unused_resource_count += value
    print('\n\nSummary:')
    print('\tTotal Unused Resources:', unused_resource_count)
    print('\n\nDetailed unused resource information can be found at: aws_logs/unused_resources.xlsx')