- Notifications
You must be signed in to change notification settings - Fork1k
feat: add provisioner tags field on template creation#16656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
ae6a28e
Create ProvisionerTagsField
BrunoQuaresma73180f6
Add ProvisionerTagsField into CreateTemplateForm
BrunoQuaresma51e6c68
Merge branch 'main' of https://github.com/coder/coder into bq/add-pro…
BrunoQuaresmafdb4d83
Run fmt
BrunoQuaresma8a35d29
Only display provisioner tags when having provisioners
BrunoQuaresmaFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletionssite/src/modules/provisioners/ProvisionerTagsField.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { expect, userEvent, within } from "@storybook/test"; | ||
import type { ProvisionerDaemon } from "api/typesGenerated"; | ||
import { type FC, useState } from "react"; | ||
import { ProvisionerTagsField } from "./ProvisionerTagsField"; | ||
const meta: Meta<typeof ProvisionerTagsField> = { | ||
title: "modules/provisioners/ProvisionerTagsField", | ||
component: ProvisionerTagsField, | ||
args: { | ||
value: {}, | ||
}, | ||
}; | ||
export default meta; | ||
type Story = StoryObj<typeof ProvisionerTagsField>; | ||
export const Empty: Story = { | ||
args: { | ||
value: {}, | ||
}, | ||
}; | ||
export const WithInitialValue: Story = { | ||
args: { | ||
value: { | ||
cluster: "dogfood-2", | ||
env: "gke", | ||
scope: "organization", | ||
}, | ||
}, | ||
}; | ||
type StatefulProvisionerTagsFieldProps = { | ||
initialValue?: ProvisionerDaemon["tags"]; | ||
}; | ||
const StatefulProvisionerTagsField: FC<StatefulProvisionerTagsFieldProps> = ({ | ||
initialValue = {}, | ||
}) => { | ||
const [value, setValue] = useState<ProvisionerDaemon["tags"]>(initialValue); | ||
return <ProvisionerTagsField value={value} onChange={setValue} />; | ||
}; | ||
export const OnOverwriteOwner: Story = { | ||
play: async ({ canvasElement }) => { | ||
const user = userEvent.setup(); | ||
const canvas = within(canvasElement); | ||
const keyInput = canvas.getByLabelText("Tag key"); | ||
const valueInput = canvas.getByLabelText("Tag value"); | ||
const addButton = canvas.getByRole("button", { name: "Add tag" }); | ||
await user.type(keyInput, "owner"); | ||
await user.type(valueInput, "dogfood-2"); | ||
await user.click(addButton); | ||
await canvas.findByText("Cannot override owner tag"); | ||
}, | ||
}; | ||
export const OnInvalidScope: Story = { | ||
play: async ({ canvasElement }) => { | ||
const user = userEvent.setup(); | ||
const canvas = within(canvasElement); | ||
const keyInput = canvas.getByLabelText("Tag key"); | ||
const valueInput = canvas.getByLabelText("Tag value"); | ||
const addButton = canvas.getByRole("button", { name: "Add tag" }); | ||
await user.type(keyInput, "scope"); | ||
await user.type(valueInput, "invalid"); | ||
await user.click(addButton); | ||
await canvas.findByText("Scope value must be 'organization' or 'user'"); | ||
}, | ||
}; | ||
export const OnAddTag: Story = { | ||
render: () => <StatefulProvisionerTagsField />, | ||
play: async ({ canvasElement }) => { | ||
const user = userEvent.setup(); | ||
const canvas = within(canvasElement); | ||
const keyInput = canvas.getByLabelText("Tag key"); | ||
const valueInput = canvas.getByLabelText("Tag value"); | ||
const addButton = canvas.getByRole("button", { name: "Add tag" }); | ||
await user.type(keyInput, "cluster"); | ||
await user.type(valueInput, "dogfood-2"); | ||
await user.click(addButton); | ||
const addedTag = await canvas.findByTestId("tag-cluster"); | ||
await expect(addedTag).toHaveTextContent("cluster dogfood-2"); | ||
}, | ||
}; | ||
export const OnRemoveTag: Story = { | ||
render: () => ( | ||
<StatefulProvisionerTagsField initialValue={{ cluster: "dogfood-2" }} /> | ||
), | ||
play: async ({ canvasElement }) => { | ||
const user = userEvent.setup(); | ||
const canvas = within(canvasElement); | ||
const removeButton = canvas.getByRole("button", { name: "Delete cluster" }); | ||
await user.click(removeButton); | ||
await expect(canvas.queryByTestId("tag-cluster")).toBeNull(); | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import TextField from "@mui/material/TextField"; | ||
import type { ProvisionerDaemon } from "api/typesGenerated"; | ||
import { Button } from "components/Button/Button"; | ||
import { Input } from "components/Input/Input"; | ||
import { PlusIcon } from "lucide-react"; | ||
import { ProvisionerTag } from "modules/provisioners/ProvisionerTag"; | ||
import { type FC, useRef, useState } from "react"; | ||
import * as Yup from "yup"; | ||
// Users can't delete these tags | ||
const REQUIRED_TAGS = ["scope", "organization", "user"]; | ||
// Users can't override these tags | ||
const IMMUTABLE_TAGS = ["owner"]; | ||
type ProvisionerTagsFieldProps = { | ||
value: ProvisionerDaemon["tags"]; | ||
onChange: (value: ProvisionerDaemon["tags"]) => void; | ||
}; | ||
export const ProvisionerTagsField: FC<ProvisionerTagsFieldProps> = ({ | ||
value: fieldValue, | ||
onChange, | ||
}) => { | ||
return ( | ||
<div className="flex flex-col gap-3"> | ||
<div className="flex items-center gap-2 flex-wrap"> | ||
{Object.entries(fieldValue) | ||
// Filter out since users cannot override it | ||
.filter(([key]) => !IMMUTABLE_TAGS.includes(key)) | ||
.map(([key, value]) => { | ||
const onDelete = (key: string) => { | ||
const { [key]: _, ...newFieldValue } = fieldValue; | ||
onChange(newFieldValue); | ||
}; | ||
return ( | ||
<ProvisionerTag | ||
key={key} | ||
tagName={key} | ||
tagValue={value} | ||
// Required tags can't be deleted | ||
onDelete={REQUIRED_TAGS.includes(key) ? undefined : onDelete} | ||
/> | ||
); | ||
})} | ||
</div> | ||
<NewTagControl | ||
onAdd={(tag) => { | ||
onChange({ ...fieldValue, [tag.key]: tag.value }); | ||
}} | ||
/> | ||
</div> | ||
); | ||
}; | ||
const newTagSchema = Yup.object({ | ||
key: Yup.string() | ||
.required("Key is required") | ||
.notOneOf(["owner"], "Cannot override owner tag"), | ||
value: Yup.string() | ||
.required("Value is required") | ||
.when("key", ([key], schema) => { | ||
if (key === "scope") { | ||
return schema.oneOf( | ||
["organization", "scope"], | ||
"Scope value must be 'organization' or 'user'", | ||
); | ||
} | ||
return schema; | ||
}), | ||
}); | ||
type Tag = { key: string; value: string }; | ||
type NewTagControlProps = { | ||
onAdd: (tag: Tag) => void; | ||
}; | ||
const NewTagControl: FC<NewTagControlProps> = ({ onAdd }) => { | ||
const keyInputRef = useRef<HTMLInputElement>(null); | ||
const [error, setError] = useState<string>(); | ||
const [newTag, setNewTag] = useState<Tag>({ | ||
key: "", | ||
value: "", | ||
}); | ||
const addNewTag = async () => { | ||
try { | ||
await newTagSchema.validate(newTag); | ||
onAdd(newTag); | ||
setNewTag({ key: "", value: "" }); | ||
keyInputRef.current?.focus(); | ||
} catch (e) { | ||
const isValidationError = e instanceof Yup.ValidationError; | ||
if (!isValidationError) { | ||
throw e; | ||
} | ||
if (e instanceof Yup.ValidationError) { | ||
setError(e.errors[0]); | ||
} | ||
} | ||
}; | ||
const addNewTagOnEnter = (e: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (e.key === "Enter") { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
addNewTag(); | ||
} | ||
}; | ||
return ( | ||
<div className="flex flex-col gap-1 max-w-72"> | ||
<div className="flex items-center gap-2"> | ||
<label className="sr-only" htmlFor="tag-key-input"> | ||
Tag key | ||
</label> | ||
<TextField | ||
inputRef={keyInputRef} | ||
size="small" | ||
id="tag-key-input" | ||
name="key" | ||
placeholder="Key" | ||
value={newTag.key} | ||
onChange={(e) => setNewTag({ ...newTag, key: e.target.value.trim() })} | ||
onKeyDown={addNewTagOnEnter} | ||
/> | ||
<label className="sr-only" htmlFor="tag-value-input"> | ||
Tag value | ||
</label> | ||
<TextField | ||
size="small" | ||
id="tag-value-input" | ||
name="value" | ||
placeholder="Value" | ||
value={newTag.value} | ||
onChange={(e) => | ||
setNewTag({ ...newTag, value: e.target.value.trim() }) | ||
} | ||
onKeyDown={addNewTagOnEnter} | ||
/> | ||
<Button | ||
className="flex-shrink-0" | ||
size="icon" | ||
type="button" | ||
onClick={addNewTag} | ||
> | ||
<PlusIcon /> | ||
<span className="sr-only">Add tag</span> | ||
</Button> | ||
</div> | ||
{error && ( | ||
<span className="text-xs text-content-destructive">{error}</span> | ||
)} | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.