Skip to content
This repository has been archived by the owner on Jan 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #26 from synackray/24-prune-traceback-keyerror
Browse files Browse the repository at this point in the history
Added exception handling for missing last_updated field to fix #24
  • Loading branch information
synackray authored Jan 5, 2020
2 parents c046761 + 39ec6aa commit d7683f5
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,31 +1042,44 @@ def prune_objects(self, vc_objects, vc_obj_type):
# needs to be deleted
# Dates are in YY, MM, DD format
current_date = date.today()
modified_date = date(
int(orphan["last_updated"][:4]), # Year
int(orphan["last_updated"][5:7]), # Month
int(orphan["last_updated"][8:10]) # Day
)
# Calculated timedelta then converts it to the days integer
days_orphaned = (current_date - modified_date).days
if days_orphaned >= settings.NB_PRUNE_DELAY_DAYS:
log.info(
"The %s '%s' object has exceeded the %s day max for "
"orphaned objects. Sending it for deletion.",
nb_obj_type, orphan[query_key],
settings.NB_PRUNE_DELAY_DAYS
# Some objects do not have a last_updated field so we must
# handle that gracefully and send for deletion
del_obj = False
try:
modified_date = date(
int(orphan["last_updated"][:4]), # Year
int(orphan["last_updated"][5:7]), # Month
int(orphan["last_updated"][8:10]) # Day
)
# Calculated timedelta then converts it to the days integer
days_orphaned = (current_date - modified_date).days
if days_orphaned >= settings.NB_PRUNE_DELAY_DAYS:
log.info(
"The %s '%s' object has exceeded the %s day max "
"for orphaned objects. Sending it for deletion.",
nb_obj_type, orphan[query_key],
settings.NB_PRUNE_DELAY_DAYS
)
del_obj = True
else:
log.info(
"The %s '%s' object has been orphaned for %s of %s "
"max days. Proceeding to next object.",
nb_obj_type, orphan[query_key], days_orphaned,
settings.NB_PRUNE_DELAY_DAYS
)
except KeyError as err:
log.debug(
"The %s '%s' object does not have a %s "
"field. Sending it for deletion.",
nb_obj_type, orphan[query_key], err
)
del_obj = True
if del_obj:
self.request(
req_type="delete", nb_obj_type=nb_obj_type,
nb_id=orphan["id"],
)
else:
log.info(
"The %s '%s' object has been orphaned for %s of %s max "
"days. Proceeding to next object.",
nb_obj_type, orphan[query_key], days_orphaned,
settings.NB_PRUNE_DELAY_DAYS
)

def search_prefix(self, ip_addr):
"""
Expand Down

0 comments on commit d7683f5

Please sign in to comment.