Skip to content

Commit ece52b5

Browse files
authored
Add checkboxes to allow for opt-out of default VPC, subnet (#3411)
This PR adds checkboxes to two places in the Console UI, one during Project create, one during VPC create. These allow a user to opt out of creating the default VPC (during Project creation) or the default subnet (during VPC creation). This is the Console part of some earlier changes, with Omicron issue oxidecomputer/omicron#10973 and relevant PR oxidecomputer/omicron#9750. Closes #3375 Todo: will need to update the screenshot at https://docs.oxide.computer/guides/onboarding-projects once merged
1 parent a67b168 commit ece52b5

6 files changed

Lines changed: 180 additions & 38 deletions

File tree

‎app/forms/project-create.tsx‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,37 @@
77
*/
88
import { useForm } from 'react-hook-form'
99
import { useNavigate } from 'react-router'
10+
import type { RequiredDeep } from 'type-fest'
1011

11-
import { api, q, queryClient, useApiMutation, type ProjectCreate } from '@oxide/api'
12+
import {
13+
api,
14+
q,
15+
queryClient,
16+
useApiMutation,
17+
type ProjectCreate,
18+
type ProjectCreateDefaults,
19+
} from '@oxide/api'
1220

21+
import { CheckboxField } from '~/components/form/fields/CheckboxField'
1322
import { DescriptionField } from '~/components/form/fields/DescriptionField'
1423
import { NameField } from '~/components/form/fields/NameField'
1524
import { SideModalForm } from '~/components/form/SideModalForm'
1625
import { HL } from '~/components/HL'
1726
import { titleCrumb } from '~/hooks/use-crumbs'
1827
import { addToast } from '~/stores/toast'
1928
import { SideModalFormDocs } from '~/ui/lib/ModalLinks'
29+
import { TipIcon } from '~/ui/lib/TipIcon'
2030
import { docLinks } from '~/util/links'
2131
import { pb } from '~/util/path-builder'
2232

23-
const defaultValues: ProjectCreate = {
33+
type ProjectCreateFormValues = Omit<ProjectCreate, 'defaults'> & {
34+
createDefaultVpc: boolean
35+
}
36+
37+
const defaultValues: ProjectCreateFormValues = {
2438
name: '',
2539
description: '',
40+
createDefaultVpc: true,
2641
}
2742

2843
export const handle = titleCrumb('New project')
@@ -53,14 +68,32 @@ export default function ProjectCreateSideModalForm() {
5368
formType="create"
5469
resourceName="project"
5570
onDismiss={onDismiss}
56-
onSubmit={({ name, description }) => {
57-
createProject.mutate({ body: { name, description } })
71+
onSubmit={({ name, description, createDefaultVpc }) => {
72+
createProject.mutate({
73+
body: {
74+
name,
75+
description,
76+
// RequiredDeep will make tsc fail if an API bump adds a new
77+
// default resource, so we can decide how to handle it
78+
defaults: createDefaultVpc
79+
? ({
80+
vpc: { type: 'explicit', defaults: { subnet: {} } },
81+
} satisfies RequiredDeep<ProjectCreateDefaults>)
82+
: {},
83+
},
84+
})
5885
}}
5986
loading={createProject.isPending || createProject.isSuccess}
6087
submitError={createProject.error}
6188
>
6289
<NameField name="name" control={form.control} />
6390
<DescriptionField name="description" control={form.control} />
91+
<span className="inline-flex items-center gap-1.5">
92+
<CheckboxField name="createDefaultVpc" control={form.control}>
93+
Create default VPC and subnet
94+
</CheckboxField>
95+
<TipIcon>An instance can only attach to a VPC with a subnet</TipIcon>
96+
</span>
6497
<SideModalFormDocs docs={[docLinks.projects]} />
6598
</SideModalForm>
6699
)

‎app/forms/vpc-create.tsx‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@
77
*/
88
import { useForm } from 'react-hook-form'
99
import { useNavigate } from 'react-router'
10+
import type { RequiredDeep } from 'type-fest'
1011

11-
import { api, q, queryClient, useApiMutation, type VpcCreate } from '@oxide/api'
12+
import {
13+
api,
14+
q,
15+
queryClient,
16+
useApiMutation,
17+
type VpcCreate,
18+
type VpcCreateDefaults,
19+
} from '@oxide/api'
1220

21+
import { CheckboxField } from '~/components/form/fields/CheckboxField'
1322
import { DescriptionField } from '~/components/form/fields/DescriptionField'
1423
import { NameField } from '~/components/form/fields/NameField'
1524
import { TextField } from '~/components/form/fields/TextField'
@@ -19,6 +28,7 @@ import { titleCrumb } from '~/hooks/use-crumbs'
1928
import { useProjectSelector } from '~/hooks/use-params'
2029
import { addToast } from '~/stores/toast'
2130
import { SideModalFormDocs } from '~/ui/lib/ModalLinks'
31+
import { TipIcon } from '~/ui/lib/TipIcon'
2232
import { validateVpcIpv6Prefix } from '~/util/ip'
2333
import { docLinks } from '~/util/links'
2434
import { pb } from '~/util/path-builder'
@@ -28,15 +38,15 @@ type VpcCreateFormValues = {
2838
description: string
2939
dnsName: string
3040
ipv6Prefix: string
31-
// defaults will go here once we support it in the UI. for now, omitting means
32-
// all defaults
41+
createDefaultSubnet: boolean
3342
}
3443

3544
const defaultValues: VpcCreateFormValues = {
3645
name: '',
3746
description: '',
3847
dnsName: '',
3948
ipv6Prefix: '',
49+
createDefaultSubnet: true,
4050
}
4151

4252
export const handle = titleCrumb('New VPC')
@@ -67,9 +77,16 @@ export default function CreateVpcSideModalForm() {
6777
form={form}
6878
formType="create"
6979
resourceName="VPC"
70-
onSubmit={({ ipv6Prefix, ...rest }) => {
71-
// omitting `defaults` means create all defaults, i.e., the original behavior
72-
const body: VpcCreate = { ...rest, ipv6Prefix: ipv6Prefix.trim() || undefined }
80+
onSubmit={({ ipv6Prefix, createDefaultSubnet, ...rest }) => {
81+
const body: VpcCreate = {
82+
...rest,
83+
ipv6Prefix: ipv6Prefix.trim() || undefined,
84+
// RequiredDeep will make tsc fail if an API bump adds a new
85+
// default resource, so we can decide how to handle it
86+
defaults: createDefaultSubnet
87+
? ({ subnet: {} } satisfies RequiredDeep<VpcCreateDefaults>)
88+
: {},
89+
}
7390
createVpc.mutate({ query: projectSelector, body })
7491
}}
7592
onDismiss={() => navigate(pb.vpcs(projectSelector))}
@@ -90,6 +107,17 @@ export default function CreateVpcSideModalForm() {
90107
return validateVpcIpv6Prefix(prefix)
91108
}}
92109
/>
110+
<span className="inline-flex items-center gap-1.5">
111+
<CheckboxField name="createDefaultSubnet" control={form.control}>
112+
Create default subnet
113+
</CheckboxField>
114+
<TipIcon>
115+
<p>
116+
An instance can only attach to a VPC with a subnet.
117+
<br />A subnet can be added later.
118+
</p>
119+
</TipIcon>
120+
</span>
93121
<SideModalFormDocs docs={[docLinks.vpcs]} />
94122
</SideModalForm>
95123
)

‎mock-api/msw/handlers.ts‎

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ import { defaultSilo, toIdp } from '../silo'
4949
import { SUPPORT_BUNDLE_SIZE } from '../support-bundle'
5050
import { timeseriesSchemas } from '../timeseries-schema'
5151
import { getTimestamps } from '../util'
52-
import { defaultFirewallRules } from '../vpc'
5352
import { resendableAlerts, retryPendingDeliveries, validateSubscription } from './alert'
5453
import {
5554
db,
@@ -62,6 +61,7 @@ import {
6261
} from './db'
6362
import {
6463
alreadyExistsErr,
64+
createVpc,
6565
currentUser,
6666
errIfExists,
6767
errIfInvalidDiskSize,
@@ -119,14 +119,25 @@ export const handlers = makeHandlers({
119119
const user = currentUser(cookies)
120120
errIfExists(db.projects, { name: body.name, silo_id: user.silo_id }, 'project')
121121

122+
const { defaults, ...rest } = body
122123
const newProject = {
123124
id: uuid(),
124-
...body,
125+
...rest,
125126
...getTimestamps(),
126127
silo_id: user.silo_id,
127128
}
128129
db.projects.push(newProject)
129130

131+
// omitting `defaults` creates all of them. otherwise only the ones listed
132+
if (!defaults || defaults.vpc) {
133+
createVpc(newProject.id, {
134+
name: 'default',
135+
dns_name: 'default',
136+
description: 'Default VPC',
137+
defaults: defaults?.vpc?.type === 'explicit' ? defaults.vpc.defaults : undefined,
138+
})
139+
}
140+
130141
return json(R.omit(newProject, ['silo_id']), { status: 201 })
131142
},
132143
projectView: ({ path }) => {
@@ -1584,32 +1595,7 @@ export const handlers = makeHandlers({
15841595
requireRole(cookies, 'project', project.id, 'collaborator')
15851596
errIfExists(db.vpcs, { name: body.name, project_id: project.id })
15861597

1587-
const newVpc: Json<Api.Vpc> = {
1588-
id: uuid(),
1589-
project_id: project.id,
1590-
system_router_id: uuid(),
1591-
...body,
1592-
// API is supposed to generate one if none provided. close enough
1593-
ipv6_prefix: body.ipv6_prefix || 'fd2d:4569:88b2::/64',
1594-
...getTimestamps(),
1595-
}
1596-
db.vpcs.push(newVpc)
1597-
1598-
// Also create a default subnet
1599-
const newSubnet: Json<Api.VpcSubnet> = {
1600-
id: uuid(),
1601-
name: 'default',
1602-
vpc_id: newVpc.id,
1603-
ipv6_block: 'fd2d:4569:88b1::/64',
1604-
description: '',
1605-
ipv4_block: '',
1606-
...getTimestamps(),
1607-
}
1608-
db.vpcSubnets.push(newSubnet)
1609-
1610-
// populate default firewall rules
1611-
db.vpcFirewallRules.push(...defaultFirewallRules(newVpc.id))
1612-
1598+
const newVpc = createVpc(project.id, body)
16131599
return json(newVpc, { status: 201 })
16141600
},
16151601
vpcView: ({ path, query }) => lookup.vpc({ ...path, ...query }),

‎mock-api/msw/util.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { differenceInSeconds, subHours } from 'date-fns'
1111
import { IPv4, IPv6 } from 'ip-num/IPNumber.js'
1212
import * as R from 'remeda'
1313
import { match } from 'ts-pattern'
14+
import { v4 as uuid } from 'uuid'
1415

1516
import {
1617
FLEET_ID,
@@ -31,6 +32,9 @@ import {
3132
type SystemMetricQueryParams,
3233
type TimeseriesQuery,
3334
type User,
35+
type Vpc,
36+
type VpcCreate,
37+
type VpcSubnet,
3438
} from '@oxide/api'
3539

3640
import { json, type Json } from '~/api/__generated__/msw-handlers'
@@ -55,6 +59,8 @@ import {
5559
getMockValues,
5660
getHistoPoints,
5761
} from '../oxql-metrics'
62+
import { getTimestamps } from '../util'
63+
import { defaultFirewallRules } from '../vpc'
5864
import { db, lookupById } from './db'
5965

6066
interface PaginateOptions {
@@ -763,3 +769,37 @@ export function randomHex(length: number) {
763769
.map(() => hexChars.charAt(Math.floor(Math.random() * hexChars.length)))
764770
.join('')
765771
}
772+
773+
/** Shared by `vpcCreate` and `projectCreate`, which can create a default VPC */
774+
export function createVpc(projectId: string, body: Json<VpcCreate>) {
775+
const { defaults, ...rest } = body
776+
const newVpc: Json<Vpc> = {
777+
id: uuid(),
778+
project_id: projectId,
779+
system_router_id: uuid(),
780+
...rest,
781+
// API is supposed to generate one if none provided. close enough
782+
ipv6_prefix: body.ipv6_prefix || 'fd2d:4569:88b2::/64',
783+
...getTimestamps(),
784+
}
785+
db.vpcs.push(newVpc)
786+
787+
// omitting `defaults` creates all of them. otherwise only the ones listed
788+
if (!defaults || defaults.subnet) {
789+
const newSubnet: Json<VpcSubnet> = {
790+
id: uuid(),
791+
name: 'default',
792+
vpc_id: newVpc.id,
793+
ipv6_block: 'fd2d:4569:88b1::/64',
794+
description: '',
795+
ipv4_block: '',
796+
...getTimestamps(),
797+
}
798+
db.vpcSubnets.push(newSubnet)
799+
}
800+
801+
// populate default firewall rules, which are always created
802+
db.vpcFirewallRules.push(...defaultFirewallRules(newVpc.id))
803+
804+
return newVpc
805+
}

‎test/e2e/project-create.e2e.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ test.describe('Project create', () => {
2929
await expect(page).toHaveURL('/projects/mock-project-2/instances')
3030
})
3131

32+
test('creates default VPC by default', async ({ page }) => {
33+
await page.getByRole('textbox', { name: 'Name' }).fill('mock-project-2')
34+
await expect(
35+
page.getByRole('checkbox', { name: 'Create default VPC and subnet' })
36+
).toBeChecked()
37+
await page.getByRole('button', { name: 'Create project' }).click()
38+
await expect(page).toHaveURL('/projects/mock-project-2/instances')
39+
40+
// client-side nav to keep the mock db state
41+
await page.getByRole('link', { name: 'VPCs' }).click()
42+
await expect(page.getByRole('cell', { name: 'default' }).first()).toBeVisible()
43+
})
44+
45+
test('can skip creating the default VPC', async ({ page }) => {
46+
await page.getByRole('textbox', { name: 'Name' }).fill('mock-project-2')
47+
await page.getByRole('checkbox', { name: 'Create default VPC and subnet' }).uncheck()
48+
await page.getByRole('button', { name: 'Create project' }).click()
49+
await expect(page).toHaveURL('/projects/mock-project-2/instances')
50+
51+
await page.getByRole('link', { name: 'VPCs' }).click()
52+
await expect(page.getByText('No VPCs')).toBeVisible()
53+
})
54+
3255
test('clicking the scrim dismisses the side modal', async ({ page }) => {
3356
const dialog = page.getByRole('dialog', { name: /Create project/ })
3457
await expect(dialog).toBeVisible()

‎test/e2e/vpcs.e2e.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,38 @@ test('whitespace-only IPv6 prefix is omitted on VPC create', async ({ page }) =>
129129
await expect(page.getByRole('heading', { name: 'vpc-generated-v6' })).toBeVisible()
130130
})
131131

132+
test('creates default subnet on VPC create by default', async ({ page }) => {
133+
await page.goto('/projects/mock-project/vpcs-new')
134+
135+
const dialog = page.getByRole('dialog', { name: 'Create VPC' })
136+
await dialog.getByRole('textbox', { name: 'Name', exact: true }).fill('vpc-with-subnet')
137+
await dialog.getByRole('textbox', { name: 'DNS name' }).fill('vpc-with-subnet')
138+
await expect(
139+
dialog.getByRole('checkbox', { name: 'Create default subnet' })
140+
).toBeChecked()
141+
await dialog.getByRole('button', { name: 'Create VPC' }).click()
142+
143+
await expect(page.getByRole('heading', { name: 'vpc-with-subnet' })).toBeVisible()
144+
await page.getByRole('tab', { name: 'VPC Subnets' }).click()
145+
await expectRowVisible(page.getByRole('table'), { name: 'default' })
146+
})
147+
148+
test('can skip creating the default subnet', async ({ page }) => {
149+
await page.goto('/projects/mock-project/vpcs-new')
150+
151+
const dialog = page.getByRole('dialog', { name: 'Create VPC' })
152+
await dialog
153+
.getByRole('textbox', { name: 'Name', exact: true })
154+
.fill('vpc-without-subnet')
155+
await dialog.getByRole('textbox', { name: 'DNS name' }).fill('vpc-without-subnet')
156+
await dialog.getByRole('checkbox', { name: 'Create default subnet' }).uncheck()
157+
await dialog.getByRole('button', { name: 'Create VPC' }).click()
158+
159+
await expect(page.getByRole('heading', { name: 'vpc-without-subnet' })).toBeVisible()
160+
await page.getByRole('tab', { name: 'VPC Subnets' }).click()
161+
await expect(page.getByText('No VPC subnets')).toBeVisible()
162+
})
163+
132164
test('can create and delete subnet', async ({ page }) => {
133165
await page.goto('/projects/mock-project/vpcs/default')
134166
await page.getByRole('tab', { name: 'VPC Subnets' }).click()

0 commit comments

Comments
 (0)