Show known hypervisors and the Hetzner placeholder for virtual machines

This commit is contained in:
2026-11-18 22:39:29 +01:00
parent 08177d8843
commit bcd3bd86ad
13 changed files with 530 additions and 11 deletions
+119
View File
@@ -0,0 +1,119 @@
import { useEffect, useState } from 'react';
import { ApiError, record, request, tenantPath, type RecordValue, type Session } from './api';
import { NavIcon } from './navigation';
import { ErrorBox, errorText, useResource } from './resource';
type Host = { asset_id: string; agent_id: string; hostname: string };
function host(value: unknown): Host | null {
if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
const row = record(value);
const { asset_id, agent_id, hostname } = row;
return typeof asset_id === 'string' && typeof agent_id === 'string' && typeof hostname === 'string'
? { asset_id, agent_id, hostname }
: null;
}
export function HypervisorLabel({ device }: { device: RecordValue }) {
if (device.device_type !== 'virtual' || !device.hypervisor) return null;
const info = record(device.hypervisor);
if (info.kind === 'device') {
const target = host(info);
if (!target || typeof device.tenant_id !== 'string') return null;
const path = ['geraete', device.tenant_id, target.asset_id, target.agent_id].map(encodeURIComponent).join('/');
return <span className="vm-hypervisor">
<NavIcon name="geraete" /><span>Hypervisor <a href={'#' + path} onClick={event => event.stopPropagation()}>
{target.hostname}
</a></span>
</span>;
}
return info.kind === 'placeholder' && info.provider === 'hetzner' ? (
<span className="vm-hypervisor">
<NavIcon name="geraete" /><span>Hypervisor <strong>Hetzner Cloud</strong> <span className="muted">· Platzhalter</span></span>
</span>
) : null;
}
function assignmentError(e: unknown) {
if (e instanceof ApiError) {
if (e.code === 'HYPERVISOR_UNAVAILABLE') return 'Dieser Hypervisor ist im Kundenbestand derzeit nicht verfügbar.';
if (e.code === 'HYPERVISOR_REQUIRES_VM') return 'Die Installation meldet sich derzeit als anderer Gerätetyp.';
if (e.code === 'NOT_FOUND') return 'Diese VM-Kopplung ist nicht mehr im Bestand.';
}
return errorText(e);
}
export function HypervisorAssignment({ device, customer, asset, agent, auth }: {
device: RecordValue; customer: string; asset: string; agent: string; auth: Session;
}) {
const [open, setOpen] = useState(false);
const [search, setSearch] = useState('');
const [query, setQuery] = useState('');
const [draft, setDraft] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
const [notice, setNotice] = useState('');
const [error, setError] = useState<string | null>(null);
const assigned = typeof device.assigned_hypervisor_asset_id === 'string' ? device.assigned_hypervisor_asset_id : '';
const value = draft ?? assigned;
useEffect(() => {
const timer = setTimeout(() => setQuery(search.trim()), 220);
return () => clearTimeout(timer);
}, [search]);
const options = useResource(async signal => {
if (!open) return null;
const data = record(await request(tenantPath(customer, '/hypervisors?q=' + encodeURIComponent(query)),
{ tenantId: customer, signal }));
if (!Array.isArray(data.items)) throw new Error('Die Hypervisor-Liste konnte nicht gelesen werden.');
return { hosts: data.items.map(host).filter((item): item is Host => item !== null), truncated: data.truncated === true };
}, [customer, open, query]);
const assignedHost = host(device.hypervisor);
const hosts = options.data?.hosts ?? [];
const selectedMissing = !!value && !hosts.some(item => item.asset_id === value);
const canSave = value !== assigned && (!value || !selectedMissing);
return <details onToggle={e => {
setOpen(e.currentTarget.open);
if (!e.currentTarget.open) {
setDraft(null);
setSearch('');
setNotice('');
setError(null);
}
}}>
<summary>Hypervisor zuordnen</summary>
{open && <form onSubmit={async e => {
e.preventDefault();
if (!canSave || busy) return;
setBusy(true);
setError(null);
setNotice('');
try {
await request(tenantPath(customer, '/devices/' + encodeURIComponent(asset) + '/hypervisor?agent_id=' + encodeURIComponent(agent)), {
tenantId: customer, csrf: auth.csrf_token, body: { hypervisor_asset_id: value || null }
});
setNotice(value ? 'Hypervisor zugeordnet.' : 'Hostzuordnung entfernt.');
} catch (e) {
setError(assignmentError(e));
} finally {
setBusy(false);
}
}}>
<label>Hypervisor suchen<input type="search" maxLength={120} value={search}
onChange={e => setSearch(e.target.value)} /></label>
<ErrorBox error={error ?? options.error} />
{options.data?.truncated && <p className="muted">Weitere Hypervisoren lassen sich über die Suche finden.</p>}
<label>Hypervisor auswählen<select aria-label="Hypervisor auswählen" value={value}
onChange={e => { setDraft(e.target.value); setNotice(''); setError(null); }} disabled={busy}>
<option value="">Kein konkreter Hypervisor</option>
{selectedMissing && <option value={value} disabled>
{assignedHost?.asset_id === value ? assignedHost.hostname : 'Zugeordneter Host außerhalb der Treffer'}
</option>}
{hosts.map(item => <option key={item.asset_id} value={item.asset_id}>{item.hostname}</option>)}
</select></label>
<button className="primary" disabled={busy || !canSave}>Zuordnung speichern</button>
{notice && <p role="status">{notice}</p>}
</form>}
</details>;
}
+5
View File
@@ -18,6 +18,7 @@ import { CatalogCheckNotice } from './catalog-check';
import { devicePlatform } from './script-catalog';
import { DeviceInventoryPanels, systemDetails } from './device-inventory';
import { DeviceCustomerTransfer } from './device-customer-transfer';
import { HypervisorAssignment, HypervisorLabel } from './device-hypervisor';
import { ErrorBox, errorText, useResource } from './resource';
import { NavIcon, orderDetailRoute } from './navigation';
import { orderTitle, Status } from './order-presentation';
@@ -206,6 +207,7 @@ function DeviceDetail({
<div>
<h1>{label(d.hostname)}</h1>
<Presence d={d} />
<HypervisorLabel device={d} />
</div>
</div>
<section>
@@ -280,6 +282,8 @@ function DeviceDetail({
<details open={managing} onToggle={e => setManaging(e.currentTarget.open)}>
<summary>Gerät verwalten</summary>
<ErrorBox error={error} />
{d.device_type === 'virtual' && d.lifecycle === 'current' &&
<HypervisorAssignment device={d} customer={customer} asset={asset} agent={agent} auth={scoped} />}
<DeviceCustomerTransfer auth={scoped} customer={customer} asset={asset} agent={agent}
customers={customers} active={d.lifecycle === 'current'}
supported={Array.isArray(d.capabilities) && d.capabilities.includes('device.customer_transfer/1')} />
@@ -601,6 +605,7 @@ export function Devices({
>
{label(d.hostname)}
</button>
<HypervisorLabel device={d} />
</td>
<td data-label="Type">{label(d.device_type)}</td>
<td data-label="OS">{label(d.os)}</td>
+2
View File
@@ -94,6 +94,8 @@ section.fleet { padding:0; overflow:hidden; }
.fleet-table th:last-child,.fleet-table td:last-child { padding-right:18px; }
.fleet-table tbody tr { cursor:pointer; } .fleet-table tbody tr:hover,.fleet-table tr:focus-within { background:#f2f7f4; }
.release-table tbody tr { cursor:auto; }
.vm-hypervisor { display:flex; align-items:baseline; gap:6px; margin-top:5px; color:var(--subtle); font-size:.78rem; overflow-wrap:anywhere; }
.vm-hypervisor .nav-icon { width:13px; height:13px; flex:none; align-self:flex-start; margin-top:2px; }
.fleet-table .target-col { width:35px; }
.fleet-table td[data-label="Type"] { overflow-wrap:anywhere; }
.fleet-table input[type=checkbox] { margin:0; }