def status(config, group, accounts=(), region=None):
    """report current export state status"""
    config = validate.callback(config)
    destination = config.get('destination')
    client = boto3.Session().client('s3')

    for account in config.get('accounts', ()):
        if accounts and account['name'] not in accounts:
            continue

        session = get_session(account['role'], region)
        account_id = session.client('sts').get_caller_identity()['Account']
        prefix = destination.get('prefix', '').rstrip('/') + '/%s' % account_id
        prefix = "%s/flow-log" % prefix

        role = account.pop('role')
        if isinstance(role, six.string_types):
            account['account_id'] = role.split(':')[4]
        else:
            account['account_id'] = role[-1].split(':')[4]

        account.pop('groups')

        try:
            tag_set = client.get_object_tagging(
                Bucket=destination['bucket'], Key=prefix).get('TagSet', [])
        except ClientError:
            account['export'] = 'missing'
            continue
        tags = {t['Key']: t['Value'] for t in tag_set}

        if 'LastExport' not in tags:
            account['export'] = 'empty'
        else:
            last_export = parse(tags['LastExport'])
            account['export'] = last_export.strftime('%Y/%m/%d')

    accounts = [a for a in config.get('accounts') if a in accounts or not accounts]
    accounts.sort(key=operator.itemgetter('export'), reverse=True)
    print(tabulate(accounts, headers='keys'))
def status ( config , group , accounts = ( ) , region = None ) : config = validate . callback ( config ) destination = config . get ( 'destination' ) client = boto3 . Session ( ) . client ( 's3' ) for account in config . get ( 'accounts' , ( ) ) : if accounts and account [ 'name' ] not in accounts : continue session = get_session ( account [ 'role' ] , region ) account_id = session . client ( 'sts' ) . get_caller_identity ( ) [ 'Account' ] prefix = destination . get ( 'prefix' , '' ) . rstrip ( '/' ) + '/%s' % account_id prefix = "%s/flow-log" % prefix role = account . pop ( 'role' ) if isinstance ( role , six . string_types ) : account [ 'account_id' ] = role . split ( ':' ) [ 4 ] else : account [ 'account_id' ] = role [ - 1 ] . split ( ':' ) [ 4 ] account . pop ( 'groups' ) try : tag_set = client . get_object_tagging ( Bucket = destination [ 'bucket' ] , Key = prefix ) . get ( 'TagSet' , [ ] ) except ClientError : account [ 'export' ] = 'missing' continue tags = { t [ 'Key' ] : t [ 'Value' ] for t in tag_set } if 'LastExport' not in tags : account [ 'export' ] = 'empty' else : last_export = parse ( tags [ 'LastExport' ] ) account [ 'export' ] = last_export . strftime ( '%Y/%m/%d' ) accounts = [ a for a in config . get ( 'accounts' ) if a in accounts or not accounts ] accounts . sort ( key = operator . itemgetter ( 'export' ) , reverse = True ) print ( tabulate ( accounts , headers = 'keys' ) )
report current export state status