|
| 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 { useForm } from 'react-hook-form' |
| 9 | +import { useNavigate } from 'react-router' |
| 10 | +import { match } from 'ts-pattern' |
| 11 | + |
| 12 | +import { api, q, queryClient, useApiMutation, usePrefetchedQuery } from '@oxide/api' |
| 13 | + |
| 14 | +import { DescriptionField } from '~/components/form/fields/DescriptionField' |
| 15 | +import { ListboxField } from '~/components/form/fields/ListboxField' |
| 16 | +import { NameField } from '~/components/form/fields/NameField' |
| 17 | +import { NumberField } from '~/components/form/fields/NumberField' |
| 18 | +import { RadioField } from '~/components/form/fields/RadioField' |
| 19 | +import { TextField } from '~/components/form/fields/TextField' |
| 20 | +import { SideModalForm } from '~/components/form/SideModalForm' |
| 21 | +import { HL } from '~/components/HL' |
| 22 | +import { toPoolItem } from '~/components/PoolListboxItem' |
| 23 | +import { titleCrumb } from '~/hooks/use-crumbs' |
| 24 | +import { useProjectSelector } from '~/hooks/use-params' |
| 25 | +import { addToast } from '~/stores/toast' |
| 26 | +import { SideModalFormDocs } from '~/ui/lib/ModalLinks' |
| 27 | +import { ALL_ISH } from '~/util/consts' |
| 28 | +import { validateIpNet } from '~/util/ip' |
| 29 | +import { docLinks } from '~/util/links' |
| 30 | +import { pb } from '~/util/path-builder' |
| 31 | + |
| 32 | +const poolList = q(api.subnetPoolList, { query: { limit: ALL_ISH } }) |
| 33 | + |
| 34 | +export async function clientLoader() { |
| 35 | + await queryClient.prefetchQuery(poolList) |
| 36 | + return null |
| 37 | +} |
| 38 | + |
| 39 | +export const handle = titleCrumb('New External Subnet') |
| 40 | + |
| 41 | +type FormValues = { |
| 42 | + name: string |
| 43 | + description: string |
| 44 | + allocationType: 'auto' | 'explicit' |
| 45 | + prefixLen: number |
| 46 | + pool: string |
| 47 | + subnet: string |
| 48 | +} |
| 49 | + |
| 50 | +const defaultFormValues: Omit<FormValues, 'pool'> = { |
| 51 | + name: '', |
| 52 | + description: '', |
| 53 | + allocationType: 'auto', |
| 54 | + prefixLen: 24, |
| 55 | + subnet: '', |
| 56 | +} |
| 57 | + |
| 58 | +export default function CreateExternalSubnetSideModalForm() { |
| 59 | + const { data: pools } = usePrefetchedQuery(poolList) |
| 60 | + |
| 61 | + const defaultPool = pools.items.find((p) => p.isDefault) |
| 62 | + |
| 63 | + const projectSelector = useProjectSelector() |
| 64 | + const navigate = useNavigate() |
| 65 | + |
| 66 | + const createExternalSubnet = useApiMutation(api.externalSubnetCreate, { |
| 67 | + onSuccess(subnet) { |
| 68 | + queryClient.invalidateEndpoint('externalSubnetList') |
| 69 | + // prettier-ignore |
| 70 | + addToast(<>External subnet <HL>{subnet.name}</HL> created</>) |
| 71 | + navigate(pb.externalSubnets(projectSelector)) |
| 72 | + }, |
| 73 | + }) |
| 74 | + |
| 75 | + const form = useForm({ |
| 76 | + defaultValues: { ...defaultFormValues, pool: defaultPool?.name ?? '' }, |
| 77 | + }) |
| 78 | + |
| 79 | + const allocationType = form.watch('allocationType') |
| 80 | + const selectedPoolName = form.watch('pool') |
| 81 | + const selectedPool = pools.items.find((p) => p.name === selectedPoolName) |
| 82 | + // In auto allocation, the requested prefix length is matched against pool |
| 83 | + // members whose min/max prefix range includes it, then a subnet is carved |
| 84 | + // out of the first member with a large enough gap. Reproducing this member |
| 85 | + // resolution logic is more or less impossible, so we just enforce a max by |
| 86 | + // IP version. |
| 87 | + // https://github.com/oxidecomputer/omicron/blob/e7d260a/nexus/db-queries/src/db/queries/external_subnet.rs#L906-L908 |
| 88 | + const prefixLenMax = !selectedPool || selectedPool.ipVersion === 'v6' ? 128 : 32 |
| 89 | + |
| 90 | + return ( |
| 91 | + <SideModalForm |
| 92 | + form={form} |
| 93 | + formType="create" |
| 94 | + resourceName="external subnet" |
| 95 | + onDismiss={() => navigate(pb.externalSubnets(projectSelector))} |
| 96 | + onSubmit={({ name, description, allocationType, prefixLen, pool, subnet }) => { |
| 97 | + const allocator = match(allocationType) |
| 98 | + .with('explicit', () => ({ type: 'explicit' as const, subnet })) |
| 99 | + .with('auto', () => ({ |
| 100 | + type: 'auto' as const, |
| 101 | + prefixLen, |
| 102 | + poolSelector: { type: 'explicit' as const, pool }, |
| 103 | + })) |
| 104 | + .exhaustive() |
| 105 | + createExternalSubnet.mutate({ |
| 106 | + query: projectSelector, |
| 107 | + body: { name, description, allocator }, |
| 108 | + }) |
| 109 | + }} |
| 110 | + loading={createExternalSubnet.isPending} |
| 111 | + submitError={createExternalSubnet.error} |
| 112 | + > |
| 113 | + <NameField name="name" control={form.control} /> |
| 114 | + <DescriptionField name="description" control={form.control} /> |
| 115 | + <RadioField |
| 116 | + name="allocationType" |
| 117 | + label="Allocation method" |
| 118 | + control={form.control} |
| 119 | + items={[ |
| 120 | + { value: 'auto', label: 'Auto' }, |
| 121 | + { value: 'explicit', label: 'Explicit' }, |
| 122 | + ]} |
| 123 | + /> |
| 124 | + {allocationType === 'auto' ? ( |
| 125 | + <> |
| 126 | + <ListboxField |
| 127 | + name="pool" |
| 128 | + label="Subnet pool" |
| 129 | + control={form.control} |
| 130 | + placeholder="Select a pool" |
| 131 | + noItemsPlaceholder="No pools linked to silo" |
| 132 | + items={pools.items.map(toPoolItem)} |
| 133 | + required |
| 134 | + description="Subnet pool to allocate from" |
| 135 | + /> |
| 136 | + <NumberField |
| 137 | + name="prefixLen" |
| 138 | + label="Prefix length" |
| 139 | + required |
| 140 | + control={form.control} |
| 141 | + min={1} |
| 142 | + max={prefixLenMax} |
| 143 | + description="Prefix length for the allocated subnet (e.g., 24 for a /24). Max is 32 for IPv4 pools, 128 for IPv6." |
| 144 | + /> |
| 145 | + </> |
| 146 | + ) : ( |
| 147 | + <TextField |
| 148 | + name="subnet" |
| 149 | + label="Subnet CIDR" |
| 150 | + required |
| 151 | + control={form.control} |
| 152 | + validate={validateIpNet} |
| 153 | + description="The subnet to reserve, e.g., 10.128.1.0/24" |
| 154 | + /> |
| 155 | + )} |
| 156 | + <SideModalFormDocs docs={[docLinks.externalSubnets]} /> |
| 157 | + </SideModalForm> |
| 158 | + ) |
| 159 | +} |
0 commit comments