|
| 1 | +import React, { FC, useState } from "react"; |
| 2 | +import { |
| 3 | + Button, |
| 4 | + Col, |
| 5 | + Form, |
| 6 | + Input, |
| 7 | + Row, |
| 8 | + Select, |
| 9 | +} from "@canonical/react-components"; |
| 10 | +import { useFormik } from "formik"; |
| 11 | +import * as Yup from "yup"; |
| 12 | +import Aside from "components/Aside"; |
| 13 | +import NotificationRow from "components/NotificationRow"; |
| 14 | +import PanelHeader from "components/PanelHeader"; |
| 15 | +import { useNotify } from "context/notify"; |
| 16 | +import { useQueryClient } from "@tanstack/react-query"; |
| 17 | +import { queryKeys } from "util/queryKeys"; |
| 18 | +import SubmitButton from "components/SubmitButton"; |
| 19 | +import { checkDuplicateName } from "util/helpers"; |
| 20 | +import usePanelParams from "util/usePanelParams"; |
| 21 | +import { LxdStorage } from "types/storage"; |
| 22 | +import { createStoragePool } from "api/storages"; |
| 23 | +import { getSourceHelpForDriver, storageDrivers } from "util/storageOptions"; |
| 24 | +import ItemName from "components/ItemName"; |
| 25 | + |
| 26 | +const StorageForm: FC = () => { |
| 27 | + const panelParams = usePanelParams(); |
| 28 | + const notify = useNotify(); |
| 29 | + const queryClient = useQueryClient(); |
| 30 | + const controllerState = useState<AbortController | null>(null); |
| 31 | + |
| 32 | + const StorageSchema = Yup.object().shape({ |
| 33 | + name: Yup.string() |
| 34 | + .test( |
| 35 | + "deduplicate", |
| 36 | + "A storage pool with this name already exists", |
| 37 | + (value) => |
| 38 | + checkDuplicateName( |
| 39 | + value, |
| 40 | + panelParams.project, |
| 41 | + controllerState, |
| 42 | + "storage-pools" |
| 43 | + ) |
| 44 | + ) |
| 45 | + .required("This field is required"), |
| 46 | + }); |
| 47 | + |
| 48 | + const formik = useFormik({ |
| 49 | + initialValues: { |
| 50 | + name: "", |
| 51 | + description: "", |
| 52 | + driver: "zfs", |
| 53 | + source: "", |
| 54 | + size: "", |
| 55 | + }, |
| 56 | + validationSchema: StorageSchema, |
| 57 | + onSubmit: ({ name, description, driver, source, size }) => { |
| 58 | + const storagePool: LxdStorage = { |
| 59 | + name, |
| 60 | + description, |
| 61 | + driver, |
| 62 | + source: driver !== "btrfs" ? source : undefined, |
| 63 | + config: { |
| 64 | + size: size ? `${size}GiB` : undefined, |
| 65 | + }, |
| 66 | + }; |
| 67 | + |
| 68 | + createStoragePool(storagePool, panelParams.project) |
| 69 | + .then(() => { |
| 70 | + void queryClient.invalidateQueries({ |
| 71 | + queryKey: [queryKeys.storage], |
| 72 | + }); |
| 73 | + notify.success( |
| 74 | + <> |
| 75 | + Storage <ItemName item={storagePool} bold /> created. |
| 76 | + </> |
| 77 | + ); |
| 78 | + panelParams.clear(); |
| 79 | + }) |
| 80 | + .catch((e) => { |
| 81 | + formik.setSubmitting(false); |
| 82 | + notify.failure("Storage pool creation failed", e); |
| 83 | + }); |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + const submitForm = () => { |
| 88 | + void formik.submitForm(); |
| 89 | + }; |
| 90 | + |
| 91 | + return ( |
| 92 | + <Aside> |
| 93 | + <div className="p-panel l-site"> |
| 94 | + <PanelHeader |
| 95 | + title={<h2 className="p-heading--4">Create storage pool</h2>} |
| 96 | + /> |
| 97 | + <div className="p-panel__content"> |
| 98 | + <NotificationRow /> |
| 99 | + <Row> |
| 100 | + <Form onSubmit={formik.handleSubmit} stacked> |
| 101 | + <Input |
| 102 | + id="name" |
| 103 | + name="name" |
| 104 | + type="text" |
| 105 | + label="Name" |
| 106 | + onBlur={formik.handleBlur} |
| 107 | + onChange={formik.handleChange} |
| 108 | + value={formik.values.name} |
| 109 | + error={formik.touched.name ? formik.errors.name : null} |
| 110 | + required |
| 111 | + stacked |
| 112 | + /> |
| 113 | + <Input |
| 114 | + id="description" |
| 115 | + name="description" |
| 116 | + type="text" |
| 117 | + label="Description" |
| 118 | + onBlur={formik.handleBlur} |
| 119 | + onChange={formik.handleChange} |
| 120 | + value={formik.values.description} |
| 121 | + error={ |
| 122 | + formik.touched.description ? formik.errors.description : null |
| 123 | + } |
| 124 | + stacked |
| 125 | + /> |
| 126 | + <Select |
| 127 | + id="driver" |
| 128 | + name="driver" |
| 129 | + help={ |
| 130 | + formik.values.driver === "zfs" |
| 131 | + ? "ZFS gives best performance and reliability" |
| 132 | + : undefined |
| 133 | + } |
| 134 | + label="Driver" |
| 135 | + options={storageDrivers} |
| 136 | + onChange={formik.handleChange} |
| 137 | + value={formik.values.driver} |
| 138 | + required |
| 139 | + stacked |
| 140 | + /> |
| 141 | + <Input |
| 142 | + id="size" |
| 143 | + name="size" |
| 144 | + type="number" |
| 145 | + help="When left blank, defaults to 20% of free disk space. Default will be between 5GiB and 30GiB" |
| 146 | + label="Size in GiB" |
| 147 | + onBlur={formik.handleBlur} |
| 148 | + onChange={formik.handleChange} |
| 149 | + value={formik.values.size} |
| 150 | + error={formik.touched.size ? formik.errors.size : null} |
| 151 | + stacked |
| 152 | + /> |
| 153 | + <Input |
| 154 | + id="source" |
| 155 | + name="source" |
| 156 | + type="text" |
| 157 | + disabled={formik.values.driver === "btrfs"} |
| 158 | + help={getSourceHelpForDriver(formik.values.driver)} |
| 159 | + label="Source" |
| 160 | + onBlur={formik.handleBlur} |
| 161 | + onChange={formik.handleChange} |
| 162 | + value={formik.values.source} |
| 163 | + error={formik.touched.source ? formik.errors.source : null} |
| 164 | + stacked |
| 165 | + /> |
| 166 | + </Form> |
| 167 | + </Row> |
| 168 | + </div> |
| 169 | + <div className="l-footer--sticky p-bottom-controls"> |
| 170 | + <hr /> |
| 171 | + <Row className="u-align--right"> |
| 172 | + <Col size={12}> |
| 173 | + <Button appearance="base" onClick={panelParams.clear}> |
| 174 | + Cancel |
| 175 | + </Button> |
| 176 | + <SubmitButton |
| 177 | + isSubmitting={formik.isSubmitting} |
| 178 | + isDisabled={!formik.isValid} |
| 179 | + onClick={submitForm} |
| 180 | + buttonLabel="Create" |
| 181 | + /> |
| 182 | + </Col> |
| 183 | + </Row> |
| 184 | + </div> |
| 185 | + </div> |
| 186 | + </Aside> |
| 187 | + ); |
| 188 | +}; |
| 189 | + |
| 190 | +export default StorageForm; |
0 commit comments