Convert actions to Enum by dgarros · Pull Request #90 · networktocode/diffsync · GitHub
Skip to content
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
25 changes: 13 additions & 12 deletions diffsync/diff.py
9 changes: 9 additions & 0 deletions diffsync/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,12 @@ class DiffSyncStatus(enum.Enum):
SUCCESS = "success"
FAILURE = "failure"
ERROR = "error"


class DiffSyncActions(enum.Enum):
"""List of valid Action for DiffSyncModel."""

CREATE = "create"
UPDATE = "update"
DELETE = "delete"
NO_CHANGE = None
20 changes: 10 additions & 10 deletions diffsync/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import structlog # type: ignore

from .diff import Diff, DiffElement
from .enum import DiffSyncModelFlags, DiffSyncFlags, DiffSyncStatus
from .enum import DiffSyncModelFlags, DiffSyncFlags, DiffSyncStatus, DiffSyncActions
from .exceptions import ObjectNotFound, ObjectNotCreated, ObjectNotUpdated, ObjectNotDeleted, ObjectCrudException
from .utils import intersection, symmetric_difference

Expand Down Expand Up @@ -296,7 +296,7 @@ def __init__( # pylint: disable=too-many-arguments
# Local state maintained during synchronization
self.logger: structlog.BoundLogger = self.base_logger
self.model_class: Type["DiffSyncModel"]
self.action: Optional[str] = None
self.action: Optional[DiffSyncActions] = None

def incr_elements_processed(self, delta: int = 1):
"""Increment self.elements_processed, then call self.callback if present."""
Expand Down Expand Up @@ -353,11 +353,11 @@ def sync_diff_element(self, element: DiffElement, parent_model: "DiffSyncModel"
self.logger.warning("No object resulted from sync, will not process child objects.")
return changed

if self.action == "create":
if self.action == DiffSyncActions.CREATE:
if parent_model:
parent_model.add_child(model)
self.dst_diffsync.add(model)
elif self.action == "delete":
elif self.action == DiffSyncActions.DELETE:
if parent_model:
parent_model.remove_child(model)
if model.model_flags & DiffSyncModelFlags.SKIP_CHILDREN_ON_DELETE:
Expand Down Expand Up @@ -390,16 +390,16 @@ def sync_model(
return (False, model)

try:
self.logger.debug(f"Attempting model {self.action}")
if self.action == "create":
self.logger.debug(f"Attempting model {self.action.value}")
if self.action == DiffSyncActions.CREATE:
if model is not None:
raise ObjectNotCreated(f"Failed to create {self.model_class.get_type()} {ids} - it already exists!")
model = self.model_class.create(diffsync=self.dst_diffsync, ids=ids, attrs=attrs)
elif self.action == "update":
elif self.action == DiffSyncActions.UPDATE:
if model is None:
raise ObjectNotUpdated(f"Failed to update {self.model_class.get_type()} {ids} - not found!")
model = model.update(attrs=attrs)
elif self.action == "delete":
elif self.action == DiffSyncActions.DELETE:
if model is None:
raise ObjectNotDeleted(f"Failed to delete {self.model_class.get_type()} {ids} - not found!")
model = model.delete()
Expand All @@ -410,7 +410,7 @@ def sync_model(
status, message = model.get_status()
else:
status = DiffSyncStatus.FAILURE
message = f"{self.model_class.get_type()} {self.action} did not return the model object."
message = f"{self.model_class.get_type()} {self.action.value} did not return the model object."

except ObjectCrudException as exception:
status = DiffSyncStatus.ERROR
Expand All @@ -424,7 +424,7 @@ def sync_model(

return (True, model)

def log_sync_status(self, action: Optional[str], status: DiffSyncStatus, message: str):
def log_sync_status(self, action: Optional[DiffSyncActions], status: DiffSyncStatus, message: str):
"""Log the current sync status at the appropriate verbosity with appropriate context.

Helper method to `sync_diff_element`/`sync_model`.
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_diffsync.py