Skip to content

Fixes #102: Record individual object actions in branch job logs #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions netbox_branching/models/branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def sync(self, user, commit=True):

# Retrieve unsynced changes before we update the Branch's status
if changes := self.get_unsynced_changes().order_by('time'):
logger.debug(f"Found {len(changes)} changes to sync")
logger.info(f"Found {len(changes)} changes to sync")
else:
logger.info(f"No changes found; aborting.")
return
Expand All @@ -256,12 +256,13 @@ def sync(self, user, commit=True):
with transaction.atomic(using=self.connection_name):
# Apply each change from the main schema
for change in changes:
change.apply(using=self.connection_name)
change.apply(using=self.connection_name, logger=logger)
if not commit:
raise AbortTransaction()

except Exception as e:
logger.error(e)
if err_message := str(e):
logger.error(err_message)
# Restore original branch status
Branch.objects.filter(pk=self.pk).update(status=BranchStatusChoices.READY)
raise e
Expand Down Expand Up @@ -296,7 +297,7 @@ def merge(self, user, commit=True):

# Retrieve staged changes before we update the Branch's status
if changes := self.get_unmerged_changes().order_by('time'):
logger.debug(f"Found {len(changes)} changes to merge")
logger.info(f"Found {len(changes)} changes to merge")
else:
logger.info(f"No changes found; aborting.")
return
Expand All @@ -319,12 +320,13 @@ def merge(self, user, commit=True):
with event_tracking(request):
request.id = change.request_id
request.user = change.user
change.apply(using=DEFAULT_DB_ALIAS)
change.apply(using=DEFAULT_DB_ALIAS, logger=logger)
if not commit:
raise AbortTransaction()

except Exception as e:
logger.error(e)
if err_message := str(e):
logger.error(err_message)
# Disconnect signal receiver & restore original branch status
post_save.disconnect(handler, sender=ObjectChange_)
Branch.objects.filter(pk=self.pk).update(status=BranchStatusChoices.READY)
Expand Down Expand Up @@ -364,7 +366,7 @@ def revert(self, user, commit=True):

# Retrieve applied changes before we update the Branch's status
if changes := self.get_changes().order_by('-time'):
logger.debug(f"Found {len(changes)} changes to revert")
logger.info(f"Found {len(changes)} changes to revert")
else:
logger.info(f"No changes found; aborting.")
return
Expand All @@ -387,12 +389,13 @@ def revert(self, user, commit=True):
with event_tracking(request):
request.id = change.request_id
request.user = change.user
change.undo()
change.undo(logger=logger)
if not commit:
raise AbortTransaction()

except Exception as e:
logger.error(e)
if err_message := str(e):
logger.error(err_message)
# Disconnect signal receiver & restore original branch status
post_save.disconnect(handler, sender=ObjectChange_)
Branch.objects.filter(pk=self.pk).update(status=BranchStatusChoices.MERGED)
Expand Down
11 changes: 6 additions & 5 deletions netbox_branching/models/changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class ObjectChange(ObjectChange_):
class Meta:
proxy = True

def apply(self, using=DEFAULT_DB_ALIAS):
def apply(self, using=DEFAULT_DB_ALIAS, logger=None):
"""
Apply the change using the specified database connection.
"""
logger = logging.getLogger('netbox_branching.models.ObjectChange.apply')
logger = logger or logging.getLogger('netbox_branching.models.ObjectChange.apply')
model = self.changed_object_type.model_class()
logger.debug(f'Applying change {self} using {using}')
logger.info(f'Applying change {self} using {using}')

# Creating a new object
if self.action == ObjectChangeActionChoices.ACTION_CREATE:
Expand Down Expand Up @@ -62,12 +62,13 @@ def apply(self, using=DEFAULT_DB_ALIAS):

apply.alters_data = True

def undo(self, using=DEFAULT_DB_ALIAS):
def undo(self, using=DEFAULT_DB_ALIAS, logger=None):
"""
Revert a previously applied change using the specified database connection.
"""
logger = logging.getLogger('netbox_branching.models.ObjectChange.undo')
logger = logger or logging.getLogger('netbox_branching.models.ObjectChange.undo')
model = self.changed_object_type.model_class()
logger.info(f'Undoing change {self} using {using}')

# Deleting a previously created object
if self.action == ObjectChangeActionChoices.ACTION_CREATE:
Expand Down