The ssh popover of an opened agent card gets a folded connections section: a table of the agent's named connections (name, user@host:port) with edit and remove, and a form that saves one. The list is read on the first open; a save is a PUT that replaces a connection of the same name, so the button reads replace and a line names the connection when the name exists; remove asks first. The controller's refusal shows under the form as it comes, and the section says the connections are inactive while ssh is off. The request building is in ssh-connections.ts with a test; swarm-ui.md describes the section.
276 lines
8.6 KiB
TypeScript
276 lines
8.6 KiB
TypeScript
// <AgentSshConnections> — the named SSH connections of one agent, a folded
|
|
// section of its SSH popover: a table of them with a remove button each, and a
|
|
// form to add one.
|
|
//
|
|
// A connection is `name; user; host; port`. It becomes an `ssh <name>` entry in
|
|
// the agent's ssh config, and the agent's hive lets it reach that host and port.
|
|
// It only takes effect while the agent's ssh switch is on, so the section says
|
|
// so when it is off, and stays usable then: the list is what the operator set.
|
|
//
|
|
// Reads `GET /api/agents/{name}/ssh-connections` when the section is first
|
|
// opened. A save is a `PUT` of one connection, which replaces one of the same
|
|
// name, so the form says "replace" and names it when the name is taken. A save
|
|
// answers the whole list; a remove is a `DELETE`. The controller does all the
|
|
// checking, and the form shows its refusal as it arrives.
|
|
//
|
|
// Its state belongs to one agent: callers key it by agent name.
|
|
import { useEffect, useState } from "preact/hooks";
|
|
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
|
|
import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js";
|
|
import { Badge } from "@hive/shared/badge.js";
|
|
import { ActionRow } from "../../ui/action-row/ActionRow.js";
|
|
import { Button } from "../../ui/button/Button.js";
|
|
import { useConfirm } from "../../ui/confirm-dialog/useConfirm.js";
|
|
import { FormStack } from "../../ui/form-stack/FormStack.js";
|
|
import { Hint } from "../../ui/hint/Hint.js";
|
|
import { TextField } from "../../ui/text-field/TextField.js";
|
|
import {
|
|
connectionBody,
|
|
connectionUrl,
|
|
connectionsUrl,
|
|
replaces,
|
|
sortedConnections,
|
|
type SshConnection,
|
|
type SshConnections,
|
|
} from "./ssh-connections.js";
|
|
import "./AgentSshConnections.css";
|
|
|
|
export function AgentSshConnections({
|
|
agent,
|
|
on,
|
|
}: {
|
|
agent: string;
|
|
on: boolean;
|
|
}) {
|
|
// Read on the first open, so a card nobody asks about reads nothing.
|
|
const [opened, setOpened] = useState(false);
|
|
return (
|
|
<details
|
|
class="ui-agent-ssh-target"
|
|
onToggle={(e) => {
|
|
if ((e.currentTarget as HTMLDetailsElement).open) setOpened(true);
|
|
}}
|
|
>
|
|
<summary>connections</summary>
|
|
{opened ? <Connections agent={agent} on={on} /> : null}
|
|
</details>
|
|
);
|
|
}
|
|
|
|
function Connections({ agent, on }: { agent: string; on: boolean }) {
|
|
const [list, setList] = useState<SshConnection[] | null>(null);
|
|
const [loadError, setLoadError] = useState<ProblemDetails | null>(null);
|
|
const [name, setName] = useState("");
|
|
const [user, setUser] = useState("");
|
|
const [host, setHost] = useState("");
|
|
const [port, setPort] = useState("22");
|
|
const [busy, setBusy] = useState(false);
|
|
const [problem, setProblem] = useState<ProblemDetails | null>(null);
|
|
const [confirm, confirmDialog] = useConfirm();
|
|
|
|
async function load() {
|
|
setLoadError(null);
|
|
try {
|
|
const r = await fetch(connectionsUrl(agent));
|
|
if (!r.ok) {
|
|
setLoadError(await readApiError(r));
|
|
return;
|
|
}
|
|
setList(((await r.json()) as SshConnections).connections);
|
|
} catch (e: unknown) {
|
|
setLoadError({ detail: String(e) });
|
|
}
|
|
}
|
|
useEffect(() => {
|
|
void load();
|
|
}, [agent]);
|
|
|
|
async function save(e: Event) {
|
|
e.preventDefault();
|
|
const body = connectionBody(user, host, port);
|
|
if (body === null || name.trim() === "") return;
|
|
setBusy(true);
|
|
setProblem(null);
|
|
try {
|
|
const r = await fetch(connectionUrl(agent, name.trim()), {
|
|
method: "PUT",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
});
|
|
if (!r.ok) {
|
|
// The form stays as typed, so the operator can fix it and save again.
|
|
setProblem(await readApiError(r));
|
|
return;
|
|
}
|
|
setList(((await r.json()) as SshConnections).connections);
|
|
setName("");
|
|
setUser("");
|
|
setHost("");
|
|
setPort("22");
|
|
} catch (err: unknown) {
|
|
setProblem({ detail: String(err) });
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
async function remove(c: SshConnection) {
|
|
if (
|
|
!(await confirm({
|
|
label: `remove the connection ${c.name}`,
|
|
confirmLabel: "remove",
|
|
danger: true,
|
|
body: (
|
|
<p>
|
|
Remove <code>{c.name}</code> ({c.user}@{c.host}:{c.port})? {agent}{" "}
|
|
can no longer <code>ssh {c.name}</code>, and its hive stops letting
|
|
it reach that host.
|
|
</p>
|
|
),
|
|
}))
|
|
) {
|
|
return;
|
|
}
|
|
setBusy(true);
|
|
setProblem(null);
|
|
try {
|
|
const r = await fetch(connectionUrl(agent, c.name), { method: "DELETE" });
|
|
if (!r.ok) {
|
|
setProblem(await readApiError(r));
|
|
return;
|
|
}
|
|
setList((l) => (l ?? []).filter((x) => x.name !== c.name));
|
|
} catch (err: unknown) {
|
|
setProblem({ detail: String(err) });
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
function edit(c: SshConnection) {
|
|
setName(c.name);
|
|
setUser(c.user);
|
|
setHost(c.host);
|
|
setPort(String(c.port));
|
|
setProblem(null);
|
|
}
|
|
|
|
const replacing = list !== null && replaces(list, name);
|
|
const complete =
|
|
name.trim() !== "" && connectionBody(user, host, port) !== null;
|
|
return (
|
|
<div class="ui-agent-ssh-connections">
|
|
<p class="ui-badge-popover-help">
|
|
A named connection is an <code>ssh <name></code> entry in {agent}
|
|
's ssh config, and its hive lets {agent} reach that host and port.
|
|
{on ? null : " Inactive while ssh is off."}
|
|
</p>
|
|
{loadError ? (
|
|
<>
|
|
<ApiErrorPanel
|
|
context="the connections were not read"
|
|
problem={loadError}
|
|
/>
|
|
<Button onClick={() => void load()}>retry</Button>
|
|
</>
|
|
) : list === null ? (
|
|
<Hint>reading the connections…</Hint>
|
|
) : list.length === 0 ? (
|
|
<Hint>no connections</Hint>
|
|
) : (
|
|
<div class="ui-agent-ssh-connections-scroll">
|
|
<table class="ui-agent-ssh-connections-table">
|
|
<thead>
|
|
<tr>
|
|
<th scope="col">name</th>
|
|
<th scope="col">connects to</th>
|
|
<th scope="col">
|
|
<span class="ui-visually-hidden">actions</span>
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{sortedConnections(list).map((c) => (
|
|
<tr key={c.name}>
|
|
<th scope="row">{c.name}</th>
|
|
<td>
|
|
{c.user}@{c.host}:{c.port}
|
|
</td>
|
|
<td class="ui-agent-ssh-connections-actions">
|
|
<Badge
|
|
variant="quiet"
|
|
value="edit"
|
|
ariaLabel={`edit ${c.name}`}
|
|
title={`fill the form with ${c.name}`}
|
|
disabled={busy}
|
|
onClick={() => edit(c)}
|
|
/>{" "}
|
|
<Badge
|
|
variant="quiet"
|
|
value="remove"
|
|
ariaLabel={`remove ${c.name}`}
|
|
title={`remove the connection ${c.name}`}
|
|
disabled={busy}
|
|
onClick={() => void remove(c)}
|
|
/>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
<FormStack onSubmit={(e) => void save(e)}>
|
|
<TextField
|
|
id={`ssh-conn-name-${agent}`}
|
|
label="name"
|
|
value={name}
|
|
placeholder="e.g. k8s-lab-vm"
|
|
onInput={setName}
|
|
/>
|
|
<TextField
|
|
id={`ssh-conn-user-${agent}`}
|
|
label="user"
|
|
value={user}
|
|
placeholder="e.g. root"
|
|
onInput={setUser}
|
|
/>
|
|
<TextField
|
|
id={`ssh-conn-host-${agent}`}
|
|
label="host"
|
|
value={host}
|
|
placeholder="e.g. muede-pc2"
|
|
onInput={setHost}
|
|
/>
|
|
<TextField
|
|
id={`ssh-conn-port-${agent}`}
|
|
label="port"
|
|
value={port}
|
|
onInput={setPort}
|
|
/>
|
|
{replacing ? (
|
|
<Hint>
|
|
There is already a connection named <code>{name.trim()}</code>;
|
|
saving replaces it.
|
|
</Hint>
|
|
) : null}
|
|
{problem ? (
|
|
<ApiErrorPanel
|
|
context="the connections were not changed"
|
|
problem={problem}
|
|
/>
|
|
) : null}
|
|
<ActionRow>
|
|
<Button
|
|
variant="primary"
|
|
type="submit"
|
|
disabled={busy || list === null || !complete}
|
|
>
|
|
{busy ? "saving…" : replacing ? "replace" : "add"}
|
|
</Button>
|
|
</ActionRow>
|
|
</FormStack>
|
|
{confirmDialog}
|
|
</div>
|
|
);
|
|
}
|