From 56413213f9287a6d9ab82de0e63d16a13e85e96e Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Thu, 21 Nov 2024 16:37:05 +0100 Subject: [PATCH] feat: delete canister snapshot (#766) # Motivation Support for the last feature of snapshot: [delete_canister_snapshot](https://internetcomputer.org/docs/current/references/ic-interface-spec#ic-delete_canister_snapshot) # Changes - Add `deleteCanisterSnapshot` to ic-mgmt --------- Signed-off-by: David Dal Busco Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> --- packages/ic-management/README.md | 17 ++++++ .../src/ic-management.canister.spec.ts | 56 +++++++++++++++++++ .../src/ic-management.canister.ts | 28 ++++++++++ 3 files changed, 101 insertions(+) diff --git a/packages/ic-management/README.md b/packages/ic-management/README.md index 3184e1e1..6670b214 100644 --- a/packages/ic-management/README.md +++ b/packages/ic-management/README.md @@ -76,6 +76,7 @@ const { status, memory_size, ...rest } = await canisterStatus(YOUR_CANISTER_ID); - [takeCanisterSnapshot](#gear-takecanistersnapshot) - [listCanisterSnapshots](#gear-listcanistersnapshots) - [loadCanisterSnapshot](#gear-loadcanistersnapshot) +- [deleteCanisterSnapshot](#gear-deletecanistersnapshot) ##### :gear: create @@ -298,6 +299,22 @@ Parameters: [:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L431) +##### :gear: deleteCanisterSnapshot + +Deletes a specific snapshot of a canister. + +| Method | Type | +| ------------------------ | --------------------------------------------------------------------------------------------------------------- | +| `deleteCanisterSnapshot` | `({ canisterId, snapshotId, }: { canisterId: Principal; snapshotId: string or snapshot_id; }) => Promise` | + +Parameters: + +- `params`: - Parameters for the deletion operation. +- `params.canisterId`: - The ID of the canister for which the snapshot will be deleted. +- `params.snapshotId`: - The ID of the snapshot to delete. + +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L462) + ## Resources diff --git a/packages/ic-management/src/ic-management.canister.spec.ts b/packages/ic-management/src/ic-management.canister.spec.ts index 0b9f6b44..40db9cf6 100644 --- a/packages/ic-management/src/ic-management.canister.spec.ts +++ b/packages/ic-management/src/ic-management.canister.spec.ts @@ -951,4 +951,60 @@ describe("ICManagementCanister", () => { await expect(call).rejects.toThrow(error); }); }); + + describe("deleteCanisterSnapshot", () => { + it("should call delete_canister_snapshot with Uint8Array snapshotId", async () => { + const service = mock(); + service.delete_canister_snapshot.mockResolvedValue(undefined); + + const icManagement = await createICManagement(service); + + const params = { + canisterId: mockCanisterId, + snapshotId: Uint8Array.from([1, 2, 3, 4]), + }; + + await icManagement.deleteCanisterSnapshot(params); + + expect(service.delete_canister_snapshot).toHaveBeenCalledWith({ + canister_id: params.canisterId, + snapshot_id: params.snapshotId, + }); + }); + + it("should call delete_canister_snapshot with string snapshotId", async () => { + const service = mock(); + service.delete_canister_snapshot.mockResolvedValue(undefined); + + const icManagement = await createICManagement(service); + + const params = { + canisterId: mockCanisterId, + snapshotId: "000000000000000201010000000000000001", + }; + + await icManagement.deleteCanisterSnapshot(params); + + expect(service.delete_canister_snapshot).toHaveBeenCalledWith({ + canister_id: params.canisterId, + snapshot_id: decodeSnapshotId(params.snapshotId), + }); + }); + + it("should throw an error if delete_canister_snapshot fails", async () => { + const error = new Error("Test error"); + const service = mock(); + service.delete_canister_snapshot.mockRejectedValue(error); + + const icManagement = await createICManagement(service); + + const call = () => + icManagement.deleteCanisterSnapshot({ + canisterId: mockCanisterId, + snapshotId: Uint8Array.from([1, 2, 3, 4]), + }); + + await expect(call).rejects.toThrow(error); + }); + }); }); diff --git a/packages/ic-management/src/ic-management.canister.ts b/packages/ic-management/src/ic-management.canister.ts index 78aeb4bf..aa974752 100644 --- a/packages/ic-management/src/ic-management.canister.ts +++ b/packages/ic-management/src/ic-management.canister.ts @@ -445,4 +445,32 @@ export class ICManagementCanister { sender_canister_version: toNullable(senderCanisterVersion), }); }; + + /** + * Deletes a specific snapshot of a canister. + * + * @link https://internetcomputer.org/docs/current/references/ic-interface-spec#ic-delete_canister_snapshot + * + * @param {Object} params - Parameters for the deletion operation. + * @param {Principal} params.canisterId - The ID of the canister for which the snapshot will be deleted. + * @param {snapshot_id} params.snapshotId - The ID of the snapshot to delete. + * + * @returns {Promise} A promise that resolves when the snapshot is successfully deleted. + * + * @throws {Error} If the deletion operation fails. + */ + deleteCanisterSnapshot = async ({ + canisterId, + snapshotId, + }: { + canisterId: Principal; + snapshotId: SnapshotIdText | snapshot_id; + }): Promise => { + const { delete_canister_snapshot } = this.service; + + await delete_canister_snapshot({ + canister_id: canisterId, + snapshot_id: mapSnapshotId(snapshotId), + }); + }; }