Skip to content

Commit

Permalink
Add ResultListing as return Obj of acquire_listing
Browse files Browse the repository at this point in the history
  • Loading branch information
kntrain committed Apr 2, 2021
1 parent 7e2cde7 commit 2055571
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
14 changes: 14 additions & 0 deletions aws_list_all/listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,17 @@ def resources(self): # pylint:disable=too-many-branches
response['truncated'] = [True]

return response


class ResultListing(object):
"""Represents a listing result summary acquired from the function acquire_listing"""
def __init__(self, input, result_type, details):
self.input = input
self.result_type = result_type
self.details = details

@property
def to_tuple(self):
"""Return a tiple of strings describing the result of an executed query"""
return (self.result_type, self.input.service, self.input.region,
self.input.operation, self.input.profile, self.details)
18 changes: 9 additions & 9 deletions aws_list_all/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from traceback import print_exc

from .introspection import get_listing_operations, get_regions_for_service
from .listing import RawListing, FilteredListing
from .listing import RawListing, FilteredListing, ResultListing
from os.path import dirname;

RESULT_NOTHING = '---'
Expand Down Expand Up @@ -231,18 +231,18 @@ def do_query(services, selected_regions=(), selected_operations=(), verbose=0, p
print('...done')
for result_type in (RESULT_NOTHING, RESULT_SOMETHING, RESULT_NO_ACCESS, RESULT_ERROR):
for result in sorted(results_by_type[result_type]):
print(*result)
print(*result.to_tuple)


def execute_query(to_run, verbose, parallel, results_by_type):
# the `with` block is a workaround for a bug: https://bugs.python.org/issue35629
with contextlib.closing(ThreadPool(parallel)) as pool:
for result in pool.imap_unordered(partial(acquire_listing, verbose), to_run):
results_by_type[result[0]].append(result)
results_by_type[result.result_type].append(result)
if verbose > 1:
print('ExecutedQueryResult: {}'.format(result))
print('ExecutedQueryResult: {}'.format(result.to_tuple))
else:
print(result[0][-1], end='')
print(result.to_tuple[0][-1], end='')
sys.stdout.flush()
return results_by_type

Expand All @@ -266,11 +266,11 @@ def acquire_listing(verbose, what):

resource_count = listingFile.resource_total_count
if listingFile.input.error == RESULT_ERROR:
return (RESULT_ERROR, service, region, operation, profile, 'Error(Error during processing of resources)')
return ResultListing(listing, RESULT_ERROR, 'Error(Error during processing of resources)')
if resource_count > 0:
return (RESULT_SOMETHING, service, region, operation, profile, ', '.join(listingFile.resource_types))
return ResultListing(listing, RESULT_SOMETHING, ', '.join(listingFile.resource_types))
else:
return (RESULT_NOTHING, service, region, operation, profile, ', '.join(listingFile.resource_types))
return ResultListing(listing, RESULT_NOTHING, ', '.join(listingFile.resource_types))
except Exception as exc: # pylint:disable=broad-except
duration = time() - start_time
if verbose > 1:
Expand All @@ -295,7 +295,7 @@ def acquire_listing(verbose, what):
listing = RawListing(service, region, operation, {}, profile, result_type)
with open('{}_{}_{}_{}.json'.format(service, operation, region, profile), 'w') as jsonfile:
json.dump(listing.to_json(), jsonfile, default=datetime.isoformat)
return (result_type, service, region, operation, profile, repr(exc))
return ResultListing(listing, result_type, repr(exc))


def do_list_files(filenames, verbose=0, not_found=False, errors=False, denied=False, unfilter=()):
Expand Down

0 comments on commit 2055571

Please sign in to comment.