|
| 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 | + |
| 9 | +import { useState } from 'react' |
| 10 | +import { useForm } from 'react-hook-form' |
| 11 | + |
| 12 | +import { |
| 13 | + apiQueryClient, |
| 14 | + useApiMutation, |
| 15 | + usePrefetchedApiQuery, |
| 16 | + type SiloQuotasUpdate, |
| 17 | +} from '~/api' |
| 18 | +import { NumberField } from '~/components/form/fields/NumberField' |
| 19 | +import { SideModalForm } from '~/components/form/SideModalForm' |
| 20 | +import { useSiloSelector } from '~/hooks/use-params' |
| 21 | +import { Button } from '~/ui/lib/Button' |
| 22 | +import { Message } from '~/ui/lib/Message' |
| 23 | +import { Table } from '~/ui/lib/Table' |
| 24 | +import { classed } from '~/util/classed' |
| 25 | +import { links } from '~/util/links' |
| 26 | +import { bytesToGiB, GiB } from '~/util/units' |
| 27 | + |
| 28 | +const Unit = classed.span`ml-1 text-tertiary` |
| 29 | + |
| 30 | +export function SiloQuotasTab() { |
| 31 | + const { silo } = useSiloSelector() |
| 32 | + const { data: utilization } = usePrefetchedApiQuery('siloUtilizationView', { |
| 33 | + path: { silo: silo }, |
| 34 | + }) |
| 35 | + |
| 36 | + const { allocated: quotas, provisioned } = utilization |
| 37 | + |
| 38 | + const [editing, setEditing] = useState(false) |
| 39 | + |
| 40 | + return ( |
| 41 | + <> |
| 42 | + <Table className="max-w-lg"> |
| 43 | + <Table.Header> |
| 44 | + <Table.HeaderRow> |
| 45 | + <Table.HeadCell>Resource</Table.HeadCell> |
| 46 | + <Table.HeadCell>Provisioned</Table.HeadCell> |
| 47 | + <Table.HeadCell>Quota</Table.HeadCell> |
| 48 | + </Table.HeaderRow> |
| 49 | + </Table.Header> |
| 50 | + <Table.Body> |
| 51 | + <Table.Row> |
| 52 | + <Table.Cell>CPU</Table.Cell> |
| 53 | + <Table.Cell> |
| 54 | + {provisioned.cpus} <Unit>vCPUs</Unit> |
| 55 | + </Table.Cell> |
| 56 | + <Table.Cell> |
| 57 | + {quotas.cpus} <Unit>vCPUs</Unit> |
| 58 | + </Table.Cell> |
| 59 | + </Table.Row> |
| 60 | + <Table.Row> |
| 61 | + <Table.Cell>Memory</Table.Cell> |
| 62 | + <Table.Cell> |
| 63 | + {bytesToGiB(provisioned.memory)} <Unit>GiB</Unit> |
| 64 | + </Table.Cell> |
| 65 | + <Table.Cell> |
| 66 | + {bytesToGiB(quotas.memory)} <Unit>GiB</Unit> |
| 67 | + </Table.Cell> |
| 68 | + </Table.Row> |
| 69 | + <Table.Row> |
| 70 | + <Table.Cell>Storage</Table.Cell> |
| 71 | + <Table.Cell> |
| 72 | + {bytesToGiB(provisioned.storage)} <Unit>GiB</Unit> |
| 73 | + </Table.Cell> |
| 74 | + <Table.Cell> |
| 75 | + {bytesToGiB(quotas.storage)} <Unit>GiB</Unit> |
| 76 | + </Table.Cell> |
| 77 | + </Table.Row> |
| 78 | + </Table.Body> |
| 79 | + </Table> |
| 80 | + <div className="mt-4 flex space-x-2"> |
| 81 | + <Button size="sm" onClick={() => setEditing(true)}> |
| 82 | + Edit quotas |
| 83 | + </Button> |
| 84 | + </div> |
| 85 | + {editing && <EditQuotasForm onDismiss={() => setEditing(false)} />} |
| 86 | + </> |
| 87 | + ) |
| 88 | +} |
| 89 | + |
| 90 | +function EditQuotasForm({ onDismiss }: { onDismiss: () => void }) { |
| 91 | + const { silo } = useSiloSelector() |
| 92 | + const { data: utilization } = usePrefetchedApiQuery('siloUtilizationView', { |
| 93 | + path: { silo: silo }, |
| 94 | + }) |
| 95 | + const quotas = utilization.allocated |
| 96 | + |
| 97 | + // required because we need to rule out undefined because NumberField hates that |
| 98 | + const defaultValues: Required<SiloQuotasUpdate> = { |
| 99 | + cpus: quotas.cpus, |
| 100 | + memory: bytesToGiB(quotas.memory), |
| 101 | + storage: bytesToGiB(quotas.storage), |
| 102 | + } |
| 103 | + |
| 104 | + const form = useForm({ defaultValues }) |
| 105 | + |
| 106 | + const updateQuotas = useApiMutation('siloQuotasUpdate', { |
| 107 | + onSuccess() { |
| 108 | + apiQueryClient.invalidateQueries('siloUtilizationView') |
| 109 | + onDismiss() |
| 110 | + }, |
| 111 | + }) |
| 112 | + |
| 113 | + return ( |
| 114 | + <SideModalForm |
| 115 | + form={form} |
| 116 | + formType="edit" |
| 117 | + resourceName="Quotas" |
| 118 | + title="Edit quotas" |
| 119 | + onDismiss={onDismiss} |
| 120 | + onSubmit={({ cpus, memory, storage }) => |
| 121 | + updateQuotas.mutate({ |
| 122 | + body: { |
| 123 | + cpus, |
| 124 | + memory: memory * GiB, |
| 125 | + // TODO: we use GiB on instance create but TiB on utilization. HM |
| 126 | + storage: storage * GiB, |
| 127 | + }, |
| 128 | + path: { silo }, |
| 129 | + }) |
| 130 | + } |
| 131 | + loading={updateQuotas.isPending} |
| 132 | + submitError={updateQuotas.error} |
| 133 | + > |
| 134 | + <Message content={<LearnMore />} variant="info" /> |
| 135 | + |
| 136 | + <NumberField name="cpus" label="CPU" units="vCPUs" required control={form.control} /> |
| 137 | + <NumberField |
| 138 | + name="memory" |
| 139 | + label="Memory" |
| 140 | + units="GiB" |
| 141 | + required |
| 142 | + control={form.control} |
| 143 | + /> |
| 144 | + <NumberField |
| 145 | + name="storage" |
| 146 | + label="Storage" |
| 147 | + units="GiB" |
| 148 | + required |
| 149 | + control={form.control} |
| 150 | + /> |
| 151 | + </SideModalForm> |
| 152 | + ) |
| 153 | +} |
| 154 | + |
| 155 | +function LearnMore() { |
| 156 | + return ( |
| 157 | + <> |
| 158 | + If a quota is set below the amount currently in use, users will not be able to |
| 159 | + provision resources. Learn more about quotas in the{' '} |
| 160 | + <a |
| 161 | + href={links.siloQuotasDocs} |
| 162 | + // don't need color and hover color because message text is already color-info anyway |
| 163 | + className="underline" |
| 164 | + target="_blank" |
| 165 | + rel="noreferrer" |
| 166 | + > |
| 167 | + Silos |
| 168 | + </a>{' '} |
| 169 | + guide. |
| 170 | + </> |
| 171 | + ) |
| 172 | +} |
0 commit comments