diff --git a/AGENTS.md b/AGENTS.md index 92ca0e08b..d5b4ae004 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -62,6 +62,7 @@ # Mutations & UI flow - Wrap writes in `useApiMutation` and surface results with `addToast` (`app/stores/toast.ts`). Guard destructive flows with the zustand confirm helpers `confirmDelete`/`confirmAction` (`app/stores/confirm-delete.tsx`, `app/stores/confirm-action.ts`), passing a `mutateAsync` lambda so the modal can catch failures and toast them. +- In toasts and confirm modal content, wrap resource names in `` (e.g., `

Are you sure you want to detach {disk.name}?

`). - When a form's `onSuccess` always navigates away, pass `loading={mutation.isPending || mutation.isSuccess}` to the form shell. `isPending` alone flips false before the navigation unmounts the modal, so the button's spinner animates back out right before close. Skip `isSuccess` if the form can stay open and be reused after success, or if the mutation lives in a component that survives the modal (e.g., a tab page with `{open && }`) — there success closes the modal synchronously so `isPending` alone is glitch-free, and a sticky `isSuccess` would strand a spinner on next open. - Mutation error display depends on context. In forms, errors display inline via `submitError={mutation.error}` — do not add `onError` with a toast to the `useApiMutation` call. In `confirmAction`/`confirmDelete` flows, the confirm modal catches the error and shows a toast using `errorTitle` — do not also add `onError` on the mutation, or the user will see two toasts. For standalone actions (fire-and-forget `mutate` calls not wrapped in a confirm modal or form), use `onError` on the mutation to show an error toast. - Keep page scaffolding consistent: `PageHeader`, `PageTitle`, `DocsPopover`, `RefreshButton`, `PropertiesTable`, and `CardBlock` provide the expected layout for new system pages. diff --git a/app/api/util.ts b/app/api/util.ts index 6eb4739a9..23228b0e0 100644 --- a/app/api/util.ts +++ b/app/api/util.ts @@ -299,6 +299,10 @@ const diskStateActions = { detach: ['attached'], // https://github.com/oxidecomputer/omicron/blob/3093818/nexus/db-queries/src/db/datastore/instance.rs#L1077-L1081 setAsBootDisk: ['attached'], + // https://github.com/oxidecomputer/omicron/blob/9971869/nexus/src/app/sagas/finalize_disk.rs#L155-L186 + finalizeImport: ['import_ready'], + // https://github.com/oxidecomputer/omicron/blob/9971869/nexus/src/app/disk.rs#L684-L697 + cancelBulkWrites: ['importing_from_bulk_writes'], } satisfies Record // snapshot has a type check in addition to state check diff --git a/app/forms/image-upload.tsx b/app/forms/image-upload.tsx index bade80684..3945c5604 100644 --- a/app/forms/image-upload.tsx +++ b/app/forms/image-upload.tsx @@ -153,6 +153,7 @@ function getTmpDiskName(imageName: string) { 'import-start-500', 'import-stop-500', 'disk-finalize-500', + 'cancel-upload', ]) if (specialNames.has(imageName)) return imageName } diff --git a/app/pages/project/disks/DisksPage.tsx b/app/pages/project/disks/DisksPage.tsx index 0c0c23999..581f5df0d 100644 --- a/app/pages/project/disks/DisksPage.tsx +++ b/app/pages/project/disks/DisksPage.tsx @@ -29,6 +29,7 @@ import { DiskStateBadge, DiskTypeBadge, ReadOnlyBadge } from '~/components/State import { makeCrumb } from '~/hooks/use-crumbs' import { getProjectSelector, useProjectSelector } from '~/hooks/use-params' import { useQuickActions } from '~/hooks/use-quick-actions' +import { confirmAction } from '~/stores/confirm-action' import { confirmDelete } from '~/stores/confirm-delete' import { addToast } from '~/stores/toast' import { DiskSourceName } from '~/table/cells/DiskSourceCell' @@ -87,6 +88,11 @@ export async function clientLoader({ params }: Route.ClientLoaderArgs) { const colHelper = createColumnHelper() +const invalidateDiskListAndView = () => { + queryClient.invalidateEndpoint('diskList') + queryClient.invalidateEndpoint('diskView') +} + export default function DisksPage() { const { project } = useProjectSelector() @@ -115,41 +121,99 @@ export default function DisksPage() { }, }) + // the states that allow these actions are both temporary, so a failure is + // most likely to be due to a state change from afar. for that reason it's + // worth invalidating on failures as well as successes + const { mutateAsync: finalize } = useApiMutation(api.diskFinalizeImport, { + onSuccess: invalidateDiskListAndView, + onError: invalidateDiskListAndView, + }) + const { mutateAsync: stopBulkWriteImport } = useApiMutation(api.diskBulkWriteImportStop, { + onSuccess: invalidateDiskListAndView, + onError: invalidateDiskListAndView, + }) + const makeActions = useCallback( - (disk: Disk): MenuAction[] => [ - { - label: 'Snapshot', - onActivate() { - // prettier-ignore - addToast(<>Creating snapshot of disk {disk.name}) - createSnapshot({ - query: { project }, - body: { - name: genName(disk.name), - disk: disk.name, - description: '', - }, - }) + (disk: Disk): MenuAction[] => { + const canStopImport = diskCan.finalizeImport(disk) || diskCan.cancelBulkWrites(disk) + + return [ + ...(canStopImport + ? [ + { + label: 'Stop import', + onActivate() { + confirmAction({ + doAction: async () => { + if (diskCan.cancelBulkWrites(disk)) { + await stopBulkWriteImport({ + path: { disk: disk.name }, + query: { project }, + }) + } + + await finalize({ + path: { disk: disk.name }, + query: { project }, + body: {}, + }) + + addToast( + <> + Import stopped for {disk.name} + + ) + }, + modalTitle: 'Stop import', + modalContent: ( +

+ Are you sure you want to stop import for {disk.name}? This + will leave it in the detached state. +

+ ), + errorTitle: 'Failed to stop import', + actionType: 'danger', + }) + }, + }, + ] + : []), + { + label: 'Snapshot', + onActivate() { + // prettier-ignore + addToast(<>Creating snapshot of disk {disk.name}) + createSnapshot({ + query: { project }, + body: { + name: genName(disk.name), + disk: disk.name, + description: '', + }, + }) + }, + disabled: snapshotDisabledReason(disk), }, - disabled: snapshotDisabledReason(disk), - }, - { - label: 'Delete', - onActivate: confirmDelete({ - doDelete: () => deleteDisk({ path: { disk: disk.name }, query: { project } }), - label: disk.name, - resourceKind: 'disk', - }), - disabled: - !diskCan.delete(disk) && - (disk.state.state === 'attached' ? ( - 'Disk must be detached before it can be deleted' - ) : ( - <>Only disks in state {fancifyStates(diskCan.delete.states)} can be deleted - )), - }, - ], - [createSnapshot, deleteDisk, project] + { + label: 'Delete', + onActivate: confirmDelete({ + doDelete: () => deleteDisk({ path: { disk: disk.name }, query: { project } }), + label: disk.name, + resourceKind: 'disk', + }), + disabled: + !diskCan.delete(disk) && + (disk.state.state === 'attached' ? ( + 'Disk must be detached before it can be deleted' + ) : canStopImport ? ( + 'Import must be stopped before the disk can be deleted' + ) : ( + <>Only disks in state {fancifyStates(diskCan.delete.states)} can be deleted + )), + }, + ] + }, + [createSnapshot, deleteDisk, stopBulkWriteImport, finalize, project] ) const columns = useColsWithActions( diff --git a/mock-api/disk.ts b/mock-api/disk.ts index 577cbddce..23a3887fb 100644 --- a/mock-api/disk.ts +++ b/mock-api/disk.ts @@ -239,6 +239,49 @@ export const disks: Json[] = [ disk_type: 'distributed', read_only: false, }, + { + id: '7b898827-35a1-4459-a4e3-34db90640b74', + name: 'tmp-for-image-29884739', + description: 'stuck in import_ready after bailing on an image upload early', + project_id: project.id, + time_created: new Date().toISOString(), + time_modified: new Date().toISOString(), + state: { state: 'import_ready' }, + device_path: '/import', + size: 8 * GiB, + block_size: 2048, + disk_type: 'distributed', + read_only: false, + }, + { + id: 'f874c0b9-72ad-4eac-8e55-e6090e10366a', + name: 'tmp-for-image-59986861', + description: 'stuck in bulk-write after bailing on an image upload early', + project_id: project.id, + time_created: new Date().toISOString(), + time_modified: new Date().toISOString(), + state: { state: 'importing_from_bulk_writes' }, + device_path: '/import', + size: 8 * GiB, + block_size: 2048, + disk_type: 'distributed', + read_only: false, + }, + { + id: '0f60c28e-ead0-48f0-aab9-e74b917dc8e4', + name: 'disk-finalize-fail', + description: + "stuck in bulk-write after bailing on an image upload early, but can't be finalized", + project_id: project.id, + time_created: new Date().toISOString(), + time_modified: new Date().toISOString(), + state: { state: 'importing_from_bulk_writes' }, + device_path: '/import', + size: 8 * GiB, + block_size: 2048, + disk_type: 'distributed', + read_only: false, + }, { id: '3f23c80f-c523-4d86-8292-2ca3f807bb12', name: 'disk-snapshot-error', diff --git a/mock-api/msw/handlers.ts b/mock-api/msw/handlers.ts index 0c38ed297..4279a3cfc 100644 --- a/mock-api/msw/handlers.ts +++ b/mock-api/msw/handlers.ts @@ -294,7 +294,9 @@ export const handlers = makeHandlers({ diskFinalizeImport: ({ path, query, body }) => { const disk = lookup.disk({ ...path, ...query }) - if (disk.name === 'disk-finalize-500') throw internalError('disk finalize failed') + if (disk.name === 'disk-finalize-500' || disk.name === 'disk-finalize-fail') { + throw internalError('disk finalize failed') + } if (disk.state.state !== 'import_ready') { throw `Cannot finalize disk in state ${disk.state.state}. Must be import_ready.` diff --git a/test/e2e/disks.e2e.ts b/test/e2e/disks.e2e.ts index 1e921206e..ed8203f73 100644 --- a/test/e2e/disks.e2e.ts +++ b/test/e2e/disks.e2e.ts @@ -7,6 +7,7 @@ */ import { clickRowAction, + clickRowActions, expect, expectNoToast, expectRowVisible, @@ -93,7 +94,7 @@ test('List disks and snapshot', async ({ page }) => { await page.goto('/projects/mock-project/disks') const table = page.getByRole('table') - await expect(table.getByRole('row')).toHaveCount(16) // 15 + header + await expect(table.getByRole('row')).toHaveCount(19) // 18 + header // check one attached and one not attached await expectRowVisible(table, { @@ -166,6 +167,68 @@ test('Read-only disk snapshot disabled', async ({ page }) => { ) }) +test('Stop import from import_ready', async ({ page }) => { + const diskImportReadyName = 'tmp-for-image-29884739' + await page.goto('/projects/mock-project/disks') + const table = page.getByRole('table') + await expectRowVisible(table, { name: diskImportReadyName, state: 'import ready' }) + + await clickRowActions(page, diskImportReadyName) + await expect(page.getByRole('menuitem', { name: 'Delete' })).toBeDisabled() + await page.getByRole('menuitem', { name: 'Stop import' }).click() + + const modal = page.getByRole('dialog', { name: 'Stop import' }) + await expect(modal).toBeVisible() + await modal.getByRole('button', { name: 'Confirm' }).click() + + await expectToast(page, `Import stopped for ${diskImportReadyName}`) + await expectRowVisible(table, { name: diskImportReadyName, state: 'detached' }) + await clickRowActions(page, diskImportReadyName) + await expect(page.getByRole('menuitem', { name: 'Delete' })).toBeEnabled() +}) + +test('Stop import from importing_from_bulk_writes', async ({ page }) => { + const diskImportingName = 'tmp-for-image-59986861' + await page.goto('/projects/mock-project/disks') + const table = page.getByRole('table') + await expectRowVisible(table, { + name: diskImportingName, + state: 'importing from bulk writes', + }) + + await clickRowActions(page, diskImportingName) + await expect(page.getByRole('menuitem', { name: 'Delete' })).toBeDisabled() + await page.getByRole('menuitem', { name: 'Stop import' }).click() + + const modal = page.getByRole('dialog', { name: 'Stop import' }) + await expect(modal).toBeVisible() + await modal.getByRole('button', { name: 'Confirm' }).click() + + await expectToast(page, `Import stopped for ${diskImportingName}`) + await expectRowVisible(table, { name: diskImportingName, state: 'detached' }) + await clickRowActions(page, diskImportingName) + await expect(page.getByRole('menuitem', { name: 'Delete' })).toBeEnabled() +}) + +test('Stop import from importing_from_bulk_writes shows error and refreshes state when finalize fails', async ({ + page, +}) => { + const diskName = 'disk-finalize-fail' + await page.goto('/projects/mock-project/disks') + const table = page.getByRole('table') + await expectRowVisible(table, { name: diskName, state: 'importing from bulk writes' }) + + await clickRowActions(page, diskName) + await page.getByRole('menuitem', { name: 'Stop import' }).click() + await page + .getByRole('dialog', { name: 'Stop import' }) + .getByRole('button', { name: 'Confirm' }) + .click() + + await expectToast(page, 'Failed to stop import') + await expectRowVisible(table, { name: diskName, state: 'import ready' }) +}) + test.describe('Disk create', () => { test.beforeEach(async ({ page }) => { await page.goto('/projects/mock-project/disks-new') diff --git a/test/e2e/image-upload.e2e.ts b/test/e2e/image-upload.e2e.ts index 34df27799..314e6acb1 100644 --- a/test/e2e/image-upload.e2e.ts +++ b/test/e2e/image-upload.e2e.ts @@ -192,7 +192,7 @@ test.describe('Image upload', () => { for (const state of cancelStates) { test(`cancel in state '${state}'`, async ({ page }) => { - await fillForm(page, 'new-image') + await fillForm(page, 'cancel-upload') await page.getByRole('button', { name: 'Upload image' }).click() @@ -219,7 +219,9 @@ test.describe('Image upload', () => { await page.getByRole('button', { name: 'Cancel' }).click() await page.getByRole('link', { name: 'Disks' }).click() await expect(page.getByRole('cell', { name: 'disk-1', exact: true })).toBeVisible() - await expect(page.getByRole('cell', { name: 'tmp' })).toBeHidden() + await expect( + page.getByRole('cell', { name: 'cancel-upload', exact: true }) + ).toBeHidden() }) }