Skip to content

Commit 615ac0f

Browse files
authored
Check for default subnet (not just VPC) when creating an instance (#3425)
1 parent 294d706 commit 615ac0f

4 files changed

Lines changed: 44 additions & 20 deletions

File tree

‎app/api/util.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import type {
2525
Sled,
2626
SnapshotState,
2727
SupportBundleState,
28-
Vpc,
2928
VpcFirewallRule,
3029
VpcFirewallRuleUpdate,
3130
} from './__generated__/Api'
@@ -132,9 +131,6 @@ export const utf8ByteLength = (s: string) => new TextEncoder().encode(s).length
132131
*/
133132
export const DEFAULT_VPC_NAME = 'default'
134133

135-
export const hasDefaultVpc = (vpcs: Vpc[]) =>
136-
vpcs.some((vpc) => vpc.name === DEFAULT_VPC_NAME)
137-
138134
type PortRange = [number, number]
139135

140136
/** Parse '1234' into [1234, 1234] and '80-100' into [80, 100] */

‎app/components/form/fields/NetworkInterfaceField.tsx‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@
88
import { useState } from 'react'
99
import { useController, type Control } from 'react-hook-form'
1010

11-
import {
12-
DEFAULT_VPC_NAME,
13-
hasDefaultVpc,
14-
type InstanceNetworkInterfaceCreate,
15-
type Vpc,
16-
} from '@oxide/api'
11+
import { DEFAULT_VPC_NAME, type InstanceNetworkInterfaceCreate, type Vpc } from '@oxide/api'
1712

1813
import { HL } from '~/components/HL'
1914
import type { InstanceCreateInput } from '~/forms/instance-create'
@@ -39,13 +34,14 @@ export function NetworkInterfaceField({
3934
control,
4035
disabled,
4136
vpcs,
37+
defaultNetworkingAvailable,
4238
}: {
4339
control: Control<InstanceCreateInput>
4440
disabled: boolean
4541
vpcs: Vpc[]
42+
defaultNetworkingAvailable: boolean
4643
}) {
4744
const [showForm, setShowForm] = useState(false)
48-
const defaultVpcExists = hasDefaultVpc(vpcs)
4945

5046
/**
5147
* Used to preserve previous user choices in case they accidentally
@@ -91,7 +87,7 @@ export function NetworkInterfaceField({
9187
<Radio
9288
name="networkInterfaceType"
9389
value="default"
94-
disabled={!defaultVpcExists || disabled}
90+
disabled={!defaultNetworkingAvailable || disabled}
9591
checked={currentMode === 'default'}
9692
onChange={(e) => handleModeChange(e.target.value)}
9793
>
@@ -100,9 +96,10 @@ export function NetworkInterfaceField({
10096
{
10197
// the no VPCs case is covered by a separate yellow banner message
10298
// saying you can't have any network interfaces
103-
vpcs.length > 0 && !defaultVpcExists && (
99+
vpcs.length > 0 && !defaultNetworkingAvailable && (
104100
<TipIcon>
105-
Default networking requires a VPC named <HL>{DEFAULT_VPC_NAME}</HL>
101+
Default networking requires a VPC and subnet named{' '}
102+
<HL>{DEFAULT_VPC_NAME}</HL>
106103
</TipIcon>
107104
)
108105
}

‎app/forms/instance-create.tsx‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ import type { SetRequired } from 'type-fest'
1515

1616
import {
1717
api,
18+
DEFAULT_VPC_NAME,
1819
diskCan,
1920
genName,
20-
hasDefaultVpc,
2121
INSTANCE_MAX_CPU,
2222
INSTANCE_MAX_RAM_GiB,
2323
isUnicastPool,
2424
MAX_DISK_SIZE_GiB,
2525
poolHasIpVersion,
2626
q,
27+
qErrorsAllowed,
2728
queryClient,
2829
useApiMutation,
2930
usePrefetchedQuery,
@@ -234,6 +235,21 @@ const baseDefaultValues: InstanceCreateInput = {
234235
floatingIps: [],
235236
}
236237

238+
// We disable the default_* NIC types in the form if either
239+
// the default VPC or default subnet is missing.
240+
// (Both use the name `default`).
241+
const defaultSubnetView = (project: string) =>
242+
qErrorsAllowed(
243+
api.vpcSubnetView,
244+
{ path: { subnet: DEFAULT_VPC_NAME }, query: { project, vpc: DEFAULT_VPC_NAME } },
245+
{
246+
errorsExpected: {
247+
explanation: 'the project may not have a default VPC and subnet.',
248+
statusCode: 404,
249+
},
250+
}
251+
)
252+
237253
export async function clientLoader({ params }: Route.ClientLoaderArgs) {
238254
const { project } = params
239255
await Promise.all([
@@ -247,6 +263,7 @@ export async function clientLoader({ params }: Route.ClientLoaderArgs) {
247263
q(api.floatingIpList, { query: { project, limit: ALL_ISH } })
248264
),
249265
queryClient.prefetchQuery(q(api.vpcList, { query: { project, limit: ALL_ISH } })),
266+
queryClient.prefetchQuery(defaultSubnetView(project)),
250267
])
251268
return null
252269
}
@@ -421,13 +438,15 @@ export default function CreateInstanceForm() {
421438
const { data: vpcs } = usePrefetchedQuery(
422439
q(api.vpcList, { query: { project, limit: ALL_ISH } })
423440
)
441+
const { data: defaultSubnet } = usePrefetchedQuery(defaultSubnetView(project))
442+
const defaultNetworkingAvailable = defaultSubnet.type === 'success'
424443

425444
// Determine default network interface type:
426-
// - If a default VPC exists: default to dual-stack (API default, works with both IPv4 and IPv6 subnets)
445+
// - If a default VPC and subnet exist: default to dual-stack (API default, works with both IPv4 and IPv6 subnets)
427446
// - Otherwise: default to 'none' (user must create a VPC first or use custom NICs)
428447
// Note: Decoupled from external IP pool configuration, as NIC IP stack and external IPs are separate concerns
429448
const defaultNetworkInterfaceType: InstanceNetworkInterfaceAttachment['type'] =
430-
hasDefaultVpc(vpcs.items) ? 'default_dual_stack' : 'none'
449+
defaultNetworkingAvailable ? 'default_dual_stack' : 'none'
431450

432451
const defaultSource =
433452
siloImages.length > 0 ? 'siloImage' : projectImages.length > 0 ? 'projectImage' : 'disk'
@@ -840,6 +859,7 @@ export default function CreateInstanceForm() {
840859
isSubmitting={isSubmitting}
841860
unicastPools={unicastPools}
842861
vpcs={vpcs.items}
862+
defaultNetworkingAvailable={defaultNetworkingAvailable}
843863
/>
844864
<FormDivider />
845865
<Form.Heading id="advanced">Advanced</Form.Heading>
@@ -879,11 +899,13 @@ const NetworkingSection = ({
879899
isSubmitting,
880900
unicastPools,
881901
vpcs,
902+
defaultNetworkingAvailable,
882903
}: {
883904
control: Control<InstanceCreateInput>
884905
isSubmitting: boolean
885906
unicastPools: UnicastIpPool[]
886907
vpcs: Vpc[]
908+
defaultNetworkingAvailable: boolean
887909
}) => {
888910
const networkInterfaces = useWatch({ control, name: 'networkInterfaces' })
889911
const [floatingIpModalOpen, setFloatingIpModalOpen] = useState(false)
@@ -966,7 +988,12 @@ const NetworkingSection = ({
966988
{vpcMessage && (
967989
<Message className="mb-4 max-w-lg" variant="notice" content={vpcMessage} />
968990
)}
969-
<NetworkInterfaceField control={control} disabled={isSubmitting} vpcs={vpcs} />
991+
<NetworkInterfaceField
992+
control={control}
993+
disabled={isSubmitting}
994+
vpcs={vpcs}
995+
defaultNetworkingAvailable={defaultNetworkingAvailable}
996+
/>
970997

971998
<div className="flex flex-1 flex-col gap-4">
972999
<h2 className="text-sans-md flex items-center">

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,12 +1217,16 @@ test('custom network interface works without a default VPC', async ({ page }) =>
12171217
const tooltip = page.getByRole('tooltip')
12181218

12191219
await defaultTip.hover()
1220-
await expect(tooltip).toHaveText('Default networking requires a VPC named default')
1220+
await expect(tooltip).toHaveText(
1221+
'Default networking requires a VPC and subnet named default'
1222+
)
12211223

12221224
await page.mouse.move(0, 0)
12231225
await expect(tooltip).toBeHidden()
12241226
await defaultTip.focus()
1225-
await expect(tooltip).toHaveText('Default networking requires a VPC named default')
1227+
await expect(tooltip).toHaveText(
1228+
'Default networking requires a VPC and subnet named default'
1229+
)
12261230

12271231
await expect(noneRadio).toBeEnabled()
12281232
await expect(noneRadio).toBeChecked()

0 commit comments

Comments
 (0)