Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: delete canister snapshot #766

Merged
merged 9 commits into from
Nov 21, 2024
17 changes: 17 additions & 0 deletions packages/ic-management/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const { status, memory_size, ...rest } = await canisterStatus(YOUR_CANISTER_ID);
- [fetchCanisterLogs](#gear-fetchcanisterlogs)
- [takeCanisterSnapshot](#gear-takecanistersnapshot)
- [listCanisterSnapshots](#gear-listcanistersnapshots)
- [deleteCanisterSnapshot](#gear-deletecanistersnapshot)

##### :gear: create

Expand Down Expand Up @@ -280,6 +281,22 @@ Parameters:

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/ic-management/src/ic-management.canister.ts#L405)

##### :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#L430)

<!-- 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 @@ -872,4 +872,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 @@ -413,4 +413,32 @@ export class ICManagementCanister {
canister_id: canisterId,
});
};

/**
* 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;
AntonioVentilii marked this conversation as resolved.
Show resolved Hide resolved
}): Promise<void> => {
const { delete_canister_snapshot } = this.service;

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