Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
better action export error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Oreglia committed Sep 27, 2017
1 parent ccd2ee1 commit c60f96a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
20 changes: 11 additions & 9 deletions safetypy/safetypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,26 +388,29 @@ def get_audit_actions(self, date_modified, offset=0, page_length=100):
)
result = self.parse_json(response.content) if response.status_code == requests.codes.ok else None
self.log_http_status(response.status_code, 'GET actions')
if result is None or None in [result.get('count'), result.get('offset'), result.get('total')]:
if result is None or None in [result.get('count'), result.get('offset'), result.get('total'), result.get('actions')]:
return None
return self.get_page_of_actions(logger, date_modified, result, offset, page_length)

def get_page_of_actions(self, logger, date_modified, result, offset=0, page_length=100):
def get_page_of_actions(self, logger, date_modified, previous_page, offset=0, page_length=100):
"""
Returns a page of action search results
:param logger: the logger
:param date_modified: fetch from that date onwards
:param result: a page of action search results
:param previous_page: a page of action search results
:param offset: the index to start retrieving actions from
:param page_length: the number of actions to return on the search page (max 100)
:return: Array of action objects
"""
if result['count'] + result['offset'] < result['total']:
logger.info('Paging Actions. Offset: ' + str(offset + page_length) + '. Total: ' + str(result['total']))
return self.get_audit_actions(date_modified, offset + page_length) + result['actions']
elif result['count'] + result['offset'] == result['total']:
return result['actions']
if previous_page['count'] + previous_page['offset'] < previous_page['total']:
logger.info('Paging Actions. Offset: ' + str(offset + page_length) + '. Total: ' + str(previous_page['total']))
next_page = self.get_audit_actions(date_modified, offset + page_length)
if next_page is None:
return None
return next_page + previous_page['actions']
elif previous_page['count'] + previous_page['offset'] == previous_page['total']:
return previous_page['actions']

def get_audit(self, audit_id):
"""
Expand All @@ -431,7 +434,6 @@ def log_http_status(status_code, message):
:param status_code: http status code to log
:param message: to describe where the status code was obtained
"""

logger = logging.getLogger('sp_logger')
status_description = requests.status_codes._codes[status_code][0]
log_string = str(status_code) + ' [' + status_description + '] status received ' + message
Expand Down
13 changes: 5 additions & 8 deletions tools/exporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,6 @@ def show_export_profiles_and_exit(list_export_profiles, sc_client):
print(row_boundary)
sys.exit(0)


def export_actions(logger, settings, sc_client):
"""
Export all actions created after date specified
Expand All @@ -745,15 +744,13 @@ def sync_exports(logger, settings, sc_client):
:param settings: Settings from command line and configuration file
:param sc_client: Instance of SDK object
"""
# GET list of audits
last_successful = get_last_successful(logger)
list_of_audits = sc_client.discover_audits(modified_after=last_successful)
# exporting actions if specified by user
if 'actions' in settings[EXPORT_FORMATS]:
export_actions(logger, settings, sc_client)
# if there are audits start exporting
if list_of_audits is not None and bool(set(settings[EXPORT_FORMATS]) & {'pdf', 'docx', 'csv', 'media',
'web-report-link'}):
if not bool(set(settings[EXPORT_FORMATS]) & {'pdf', 'docx', 'csv', 'media', 'web-report-link'}):
return
last_successful = get_last_successful(logger)
list_of_audits = sc_client.discover_audits(modified_after=last_successful)
if list_of_audits is not None:
logger.info(str(list_of_audits['total']) + ' audits discovered')
export_count = 1
export_total = list_of_audits['total']
Expand Down

0 comments on commit c60f96a

Please sign in to comment.