Skip to content

Commit f31d533

Browse files
authored
/lookup/instances/:id looks up instance and redirects to project instance route (#1944)
/instances/:id looks up instance and redirects to project instance route
1 parent b4552ee commit f31d533

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

app/pages/lookups.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright Oxide Computer Company
7+
*/
8+
import { redirect, type LoaderFunctionArgs } from 'react-router-dom'
9+
10+
import { apiQueryClient } from '@oxide/api'
11+
12+
import { trigger404 } from 'app/components/ErrorBoundary'
13+
import { pb } from 'app/util/path-builder'
14+
15+
export async function instanceLookupLoader({ params }: LoaderFunctionArgs) {
16+
try {
17+
const instance = await apiQueryClient.fetchQuery('instanceView', {
18+
path: { instance: params.instance! },
19+
})
20+
const project = await apiQueryClient.fetchQuery('projectView', {
21+
path: { project: instance.projectId },
22+
})
23+
return redirect(pb.instance({ project: project.name, instance: instance.name }))
24+
} catch (_e) {
25+
throw trigger404
26+
}
27+
}

app/routes.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import DeviceAuthSuccessPage from './pages/DeviceAuthSuccessPage'
4343
import DeviceAuthVerifyPage from './pages/DeviceAuthVerifyPage'
4444
import { LoginPage } from './pages/LoginPage'
4545
import { LoginPageSaml } from './pages/LoginPageSaml'
46+
import { instanceLookupLoader } from './pages/lookups'
4647
import {
4748
DisksPage,
4849
ImagesPage,
@@ -229,6 +230,15 @@ export const routes = createRoutesFromElements(
229230
loader={SiloUtilizationPage.loader}
230231
handle={{ crumb: 'Utilization' }}
231232
/>
233+
234+
{/* let's do both. what could go wrong*/}
235+
<Route
236+
path="lookup/instances/:instance"
237+
element={null}
238+
loader={instanceLookupLoader}
239+
/>
240+
<Route path="lookup/i/:instance" element={null} loader={instanceLookupLoader} />
241+
232242
<Route loader={ProjectsPage.loader} element={<ProjectsPage />}>
233243
<Route path="projects" handle={{ crumb: 'Projects' }} element={null} />
234244
<Route

app/test/e2e/lookup-routes.e2e.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright Oxide Computer Company
7+
*/
8+
import { expect, test } from '@playwright/test'
9+
10+
test.describe('/lookup/i', () => {
11+
test('404s on existing name', async ({ page }) => {
12+
await page.goto('/lookup/i/db1')
13+
await expect(page.getByText('Page not found')).toBeVisible()
14+
})
15+
16+
test('404s on empty ID', async ({ page }) => {
17+
await page.goto('/lookup/i/')
18+
await expect(page.getByText('Page not found')).toBeVisible()
19+
})
20+
21+
test('looks up instance by ID', async ({ page }) => {
22+
await page.goto('/lookup/i/935499b3-fd96-432a-9c21-83a3dc1eece4')
23+
await expect(page).toHaveURL('/projects/mock-project/instances/db1/storage')
24+
})
25+
})
26+
27+
test.describe('/lookup/instances', () => {
28+
test('404s on existing name', async ({ page }) => {
29+
await page.goto('/lookup/instances/db1')
30+
await expect(page.getByText('Page not found')).toBeVisible()
31+
})
32+
33+
test('404s on empty ID', async ({ page }) => {
34+
await page.goto('/lookup/instances/')
35+
await expect(page.getByText('Page not found')).toBeVisible()
36+
})
37+
38+
test('looks up instance by ID', async ({ page }) => {
39+
await page.goto('/lookup/instances/935499b3-fd96-432a-9c21-83a3dc1eece4')
40+
await expect(page).toHaveURL('/projects/mock-project/instances/db1/storage')
41+
})
42+
})

0 commit comments

Comments
 (0)