Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 11 additions & 10 deletions diffsync/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from .exceptions import ObjectAlreadyExists
from .utils import intersection, OrderedDefaultDict
from .enum import DiffSyncActions


class Diff:
Expand Down Expand Up @@ -105,9 +106,9 @@ def order_children_default(cls, children: Mapping) -> Iterator["DiffElement"]:
def summary(self) -> Mapping[Text, int]:
"""Build a dict summary of this Diff and its child DiffElements."""
summary = {
"create": 0,
"update": 0,
"delete": 0,
DiffSyncActions.CREATE: 0,
DiffSyncActions.UPDATE: 0,
DiffSyncActions.DELETE: 0,
"no-change": 0,
}
for child in self.get_children():
Expand Down Expand Up @@ -224,18 +225,18 @@ def action(self) -> Optional[Text]:
"""Action, if any, that should be taken to remediate the diffs described by this element.

Returns:
str: "create", "update", "delete", or None
str: DiffSyncActions ("create", "update", "delete", or None)
"""
if self.source_attrs is not None and self.dest_attrs is None:
return "create"
return DiffSyncActions.CREATE
if self.source_attrs is None and self.dest_attrs is not None:
return "delete"
return DiffSyncActions.DELETE
if (
self.source_attrs is not None
and self.dest_attrs is not None
and any(self.source_attrs[attr_key] != self.dest_attrs[attr_key] for attr_key in self.get_attrs_keys())
):
return "update"
return DiffSyncActions.UPDATE

return None

Expand Down Expand Up @@ -328,9 +329,9 @@ def has_diffs(self, include_children: bool = True) -> bool:
def summary(self) -> Mapping[Text, int]:
"""Build a summary of this DiffElement and its children."""
summary = {
"create": 0,
"update": 0,
"delete": 0,
DiffSyncActions.CREATE: 0,
DiffSyncActions.UPDATE: 0,
DiffSyncActions.DELETE: 0,
"no-change": 0,
}
if self.action:
Expand Down
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: # pylint: disable=too-few-public-methods
Comment thread
glennmatthews marked this conversation as resolved.
Outdated
"""List of valid Action for DiffSyncModel."""

CREATE = "create"
UPDATE = "update"
DELETE = "delete"
NO_CHANGE = None
12 changes: 6 additions & 6 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 @@ -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 @@ -391,15 +391,15 @@ def sync_model(

try:
self.logger.debug(f"Attempting model {self.action}")
if self.action == "create":
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 Down