Skip to content

Commit

Permalink
feat: delete canister snapshot (#766)
Browse files Browse the repository at this point in the history
# 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 <[email protected]>
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
peterpeterparker and github-actions[bot] authored Nov 21, 2024
1 parent e662b54 commit 5641321
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/ic-management/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<void>` |

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)

<!-- TSDOC_END -->

## Resources
Expand Down
56 changes: 56 additions & 0 deletions packages/ic-management/src/ic-management.canister.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IcManagementService>();
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<IcManagementService>();
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<IcManagementService>();
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);
});
});
});
28 changes: 28 additions & 0 deletions packages/ic-management/src/ic-management.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>} 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<void> => {
const { delete_canister_snapshot } = this.service;

await delete_canister_snapshot({
canister_id: canisterId,
snapshot_id: mapSnapshotId(snapshotId),
});
};
}

0 comments on commit 5641321

Please sign in to comment.