Skip to content

Commit

Permalink
fix: errors in draft modifications commit workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
keithmanville committed Dec 26, 2024
1 parent ace1e7e commit 6c2c72e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/dioptra/restapi/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def handle_draft_already_exists(error: DraftAlreadyExistsError):
log.debug(error.to_message())
return error_result(error, http.HTTPStatus.BAD_REQUEST, {})

@api.errorhandler(DraftAlreadyExistsError)
@api.errorhandler(DraftResourceModificationsCommitError)
def handle_draft_resource_modifications_commit_error(
error: DraftResourceModificationsCommitError,
):
Expand Down
28 changes: 16 additions & 12 deletions src/dioptra/restapi/v1/workflows/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,36 +125,40 @@ def commit_draft(self, draft_id: int, **kwargs) -> dict:
log=log,
)
else: # the draft contains modifications to an existing resource
resource = views.get_latest_resource_snapshot(
draft.resource_type, draft.resource_id
snapshot = views.get_latest_resource_snapshot(
draft.resource_type, draft.payload["resource_id"]
)
if resource is None:
if snapshot is None:
raise EntityDoesNotExistError(
draft.resource_type, resource_id=draft.resource_id
draft.resource_type, resource_id=draft.payload["resource_id"]
)

# if the underlying resource was modified since the draft was created,
# raise an error with the information necessary to reconcile the draft.
if draft.resource_snapshot_id != resource.latest_snapshot_id:
if (
draft.payload["resource_snapshot_id"]
!= snapshot.resource.latest_snapshot_id
):
prev_snapshot = views.get_resource_snapshot(
draft.resource_type, draft.resource_snapshot_id
draft.resource_type, draft.payload["resource_snapshot_id"]
)
if prev_snapshot is None:
raise EntityDoesNotExistError(
draft.resource_type, snapshot_id=draft.resource_snapshot_id
draft.resource_type,
snapshot_id=draft.payload["resource_snapshot_id"],
)

curr_snapshot = views.get_resource_snapshot(
draft.resource_type, resource.latest_snapshot_id
draft.resource_type, snapshot.resource.latest_snapshot_id
)
if curr_snapshot is None:
raise EntityDoesNotExistError(
draft.resource_type, resource_id=draft.resource_id
draft.resource_type, resource_id=draft.payload["resource_id"]
)

raise DraftResourceModificationsCommitError(
resource_type=draft.resource_type,
resource_id=draft.resource_id,
resource_id=draft.payload["resource_id"],
draft=utils.build_resource(
draft.resource_type, {draft.resource_type: draft}
),
Expand All @@ -166,8 +170,8 @@ def commit_draft(self, draft_id: int, **kwargs) -> dict:
),
)

resource_dict = self._resource_service.modify(
resource_id=draft.resource_id,
resource_dict = self._resource_id_service.modify(
resource_id=draft.payload["resource_id"],
resource_type=draft.resource_type,
resource_data=draft.payload["resource_data"],
group_id=draft.group_id,
Expand Down

0 comments on commit 6c2c72e

Please sign in to comment.