Skip to content

Commit bc4acfb

Browse files
sudomateoampagentfakemonster
authored
Allow pasting cloud-init user data as text on instance create (#3419)
Closes #3118 Adds a textarea for cloud-init user data on the instance create form, so users can paste user data instead of creating and uploading a file. A **Paste text / Upload file** radio above the input picks the source, and file uploading is the default. - Only the selected source is submitted, so content left in the hidden input is ignored. - Both inputs stay mounted and only the inactive one is hidden, so switching back and forth keeps what was entered. - Pasted text is UTF-8 encoded and then base64-encoded, the same as an uploaded file. Empty or whitespace-only text sends no `user_data`. ### Before ![Before: file upload only](https://ampcode.com/user-content/artifacts/be1dec93f8c5e882d90472a99d8d79f4a8b14a48d77a5fe6c963fc28778ebc3a-file.png) ### After | Paste text (default) | Upload file | | --- | --- | | ![After: paste text](https://ampcode.com/user-content/artifacts/cda660542ad2481b66a1c183579275b94c77aa9b50608c27f9eed8380e3bdd60-file.png) | ![After: upload file](https://ampcode.com/user-content/artifacts/b923e0816d8edd0b7a3057ee5c6443a67b99bf629a52569171b78c10807c208b-file.png) | ### Testing - `npm run tsc` and `npm run lint` pass. - Extended the `can create an instance` e2e test to type user data, switch to file upload and back, and check the text is preserved. - Checked the create request body manually with throwaway Playwright tests (not committed): non-ASCII text round-trips through base64, whitespace-only text omits `user_data`, file mode sends the file bytes and ignores typed text, and switching back to text after choosing a file sends the text. --------- Co-authored-by: Amp <[email protected]> Co-authored-by: Joe Thel <[email protected]>
1 parent 595b391 commit bc4acfb

2 files changed

Lines changed: 102 additions & 16 deletions

File tree

‎app/forms/instance-create.tsx‎

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ import {
5555
DisksTableField,
5656
type DiskTableItem,
5757
} from '~/components/form/fields/DisksTableField'
58-
import { FileField } from '~/components/form/fields/FileField'
5958
import { BootDiskImageSelectField as ImageSelectField } from '~/components/form/fields/ImageSelectField'
6059
import { ListboxField } from '~/components/form/fields/ListboxField'
6160
import { NameField } from '~/components/form/fields/NameField'
6261
import { NetworkInterfaceField } from '~/components/form/fields/NetworkInterfaceField'
6362
import { NumberField } from '~/components/form/fields/NumberField'
6463
import { RadioFieldDyn } from '~/components/form/fields/RadioField'
6564
import { SshKeysField } from '~/components/form/fields/SshKeysField'
65+
import { TextFieldInner } from '~/components/form/fields/TextField'
6666
import { Form } from '~/components/form/Form'
6767
import { FullPageForm } from '~/components/form/FullPageForm'
6868
import { HL } from '~/components/HL'
@@ -73,13 +73,16 @@ import { Button } from '~/ui/lib/Button'
7373
import { toComboboxItems } from '~/ui/lib/Combobox'
7474
import { FormDivider } from '~/ui/lib/Divider'
7575
import { EmptyMessage } from '~/ui/lib/EmptyMessage'
76+
import { FieldLabel } from '~/ui/lib/FieldLabel'
77+
import { FileInput } from '~/ui/lib/FileInput'
7678
import { ItemLabel } from '~/ui/lib/ItemLabel'
7779
import { Listbox } from '~/ui/lib/Listbox'
7880
import { Message } from '~/ui/lib/Message'
7981
import { MiniTable } from '~/ui/lib/MiniTable'
8082
import { Modal } from '~/ui/lib/Modal'
8183
import { PageHeader, PageTitle } from '~/ui/lib/PageHeader'
82-
import { RadioCard } from '~/ui/lib/Radio'
84+
import { Radio, RadioCard } from '~/ui/lib/Radio'
85+
import { RadioGroup } from '~/ui/lib/RadioGroup'
8386
import { Slash } from '~/ui/lib/Slash'
8487
import { Tabs } from '~/ui/lib/Tabs'
8588
import { HintLink, TextInputHint } from '~/ui/lib/TextInput'
@@ -150,7 +153,10 @@ export type InstanceCreateInput = Assign<
150153
diskSource: string
151154
bootDiskReadOnly: boolean
152155

153-
userData: File | null
156+
// userDataSource picks which of the two fields below gets submitted
157+
userDataSource: 'text' | 'file'
158+
userDataText: string
159+
userDataFile: File | null
154160
// ssh keys are always specified. we do not need the undefined case
155161
sshPublicKeys: NonNullable<InstanceCreate['sshPublicKeys']>
156162
// Ephemeral IP fields (dual stack support)
@@ -227,7 +233,9 @@ const baseDefaultValues: InstanceCreateInput = {
227233

228234
start: true,
229235

230-
userData: null,
236+
userDataSource: 'file',
237+
userDataText: '',
238+
userDataFile: null,
231239
ephemeralIpv4: false,
232240
ephemeralIpv4Pool: '',
233241
ephemeralIpv6: false,
@@ -599,9 +607,16 @@ export default function CreateInstanceForm() {
599607
externalIps.push({ type: 'floating', floatingIp })
600608
}
601609

602-
const userData = values.userData
603-
? await readBlobAsBase64(values.userData)
604-
: undefined
610+
const userData = await match(values.userDataSource)
611+
.with('text', () =>
612+
values.userDataText.trim()
613+
? readBlobAsBase64(new Blob([values.userDataText]))
614+
: undefined
615+
)
616+
.with('file', () =>
617+
values.userDataFile ? readBlobAsBase64(values.userDataFile) : undefined
618+
)
619+
.exhaustive()
605620

606621
await createInstance.mutateAsync({
607622
query: { project },
@@ -863,14 +878,7 @@ export default function CreateInstanceForm() {
863878
/>
864879
<FormDivider />
865880
<Form.Heading id="advanced">Advanced</Form.Heading>
866-
<FileField
867-
id="user-data-input"
868-
description={<UserDataDescription />}
869-
name="userData"
870-
label="User Data"
871-
control={control}
872-
disabled={isSubmitting}
873-
/>
881+
<UserDataField control={control} disabled={isSubmitting} />
874882
<Form.Actions>
875883
<Form.Submit loading={createInstance.isPending || createInstance.isSuccess}>
876884
Create instance
@@ -1143,3 +1151,71 @@ const UserDataDescription = () => (
11431151
image supports it. Maximum size 32 KiB.
11441152
</>
11451153
)
1154+
1155+
/**
1156+
* User data can be pasted as text or uploaded as a file. A file is still useful
1157+
* for content that isn't plain text, like gzipped cloud-init config. Both inputs
1158+
* stay mounted (just hidden) so switching back and forth doesn't lose anything,
1159+
* which matters for the file input because it tracks its own selected file.
1160+
*/
1161+
const UserDataField = ({
1162+
control,
1163+
disabled,
1164+
}: {
1165+
control: Control<InstanceCreateInput>
1166+
disabled: boolean
1167+
}) => {
1168+
const sourceField = useController({ control, name: 'userDataSource' })
1169+
const {
1170+
field: { value: _, ...fileField },
1171+
} = useController({ control, name: 'userDataFile' })
1172+
const source = sourceField.field.value
1173+
return (
1174+
<div className="max-w-lg">
1175+
<div className="mb-4">
1176+
<FieldLabel id="user-data-label" htmlFor={`user-data-${source}`} optional>
1177+
User data
1178+
</FieldLabel>
1179+
<TextInputHint id="user-data-help-text">
1180+
<UserDataDescription />
1181+
</TextInputHint>
1182+
</div>
1183+
<RadioGroup
1184+
name="userDataSource"
1185+
className="mb-4"
1186+
// oxlint-disable-next-line jsx-a11y/aria-proptypes -- https://github.com/oxc-project/oxc/issues/17465
1187+
aria-labelledby="user-data-label"
1188+
defaultChecked={source}
1189+
onChange={(e) => sourceField.field.onChange(e.target.value)}
1190+
disabled={disabled}
1191+
>
1192+
<Radio value="file">Upload file</Radio>
1193+
<Radio value="text">Paste text</Radio>
1194+
</RadioGroup>
1195+
<div hidden={source !== 'text'}>
1196+
<TextFieldInner
1197+
id="user-data-text"
1198+
as="textarea"
1199+
rows={8}
1200+
name="userDataText"
1201+
label="User data"
1202+
// oxlint-disable-next-line jsx-a11y/aria-proptypes -- https://github.com/oxc-project/oxc/issues/17465
1203+
aria-labelledby="user-data-label user-data-help-text"
1204+
// user data is usually YAML or a script, where indentation matters
1205+
fieldClassName="text-mono-code!"
1206+
control={control}
1207+
disabled={disabled}
1208+
/>
1209+
</div>
1210+
<div hidden={source !== 'file'}>
1211+
<FileInput
1212+
id="user-data-file"
1213+
// oxlint-disable-next-line jsx-a11y/aria-proptypes -- https://github.com/oxc-project/oxc/issues/17465
1214+
aria-labelledby="user-data-label user-data-help-text"
1215+
disabled={disabled}
1216+
{...fileField}
1217+
/>
1218+
</div>
1219+
</div>
1220+
)
1221+
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,17 @@ test('can create an instance', async ({ page }) => {
8989
await expect(v6PoolDropdown).toContainText('ip-pool-2')
9090

9191
await expect(page.getByRole('radiogroup', { name: 'Network interface' })).toBeVisible()
92-
await expect(page.getByLabel('User data')).toBeVisible()
92+
93+
await expect(page.getByText('Drop a file or click to browse')).toBeVisible()
94+
await page.getByRole('radio', { name: 'Paste text' }).click()
95+
const userData = page.getByRole('textbox', { name: 'User data' })
96+
await userData.fill('#cloud-config\npackages:\n - nginx\n')
97+
// switching to file upload and back keeps what was typed
98+
await page.getByRole('radio', { name: 'Upload file' }).click()
99+
await expect(userData).toBeHidden()
100+
await expect(page.getByText('Drop a file or click to browse')).toBeVisible()
101+
await page.getByRole('radio', { name: 'Paste text' }).click()
102+
await expect(userData).toHaveValue('#cloud-config\npackages:\n - nginx\n')
93103

94104
await page.getByRole('button', { name: 'Create instance' }).click()
95105

0 commit comments

Comments
 (0)