Restructure ARNavi contour UX and expose project-scoped VPS inventory
Device Core qualification / qualify (push) Successful in 7m42s

This commit is contained in:
DCCONSTRUCTIONS
2026-09-30 19:35:18 +03:00
parent 1bf3ee6d41
commit c0487ca17b
17 changed files with 508 additions and 388 deletions
@@ -32,7 +32,46 @@ export class B2Infrastructure {
this.clients.set(source.secret_ref, client);
return client;
}
async inventory(actor, input) {
fields(input, []);
const projectId = ref(input.projectRef, 'project');
const db = await this.pool.connect();
let sources;
try {
await db.query('begin');
await findProjectWithCapability(db, actor, projectId, 'inventory.read');
sources = (await db.query(`select s.*, v.host_id, h.display_name,
coalesce((select json_agg(l.contour_id order by l.contour_id) from device_b2_contour_links l
where l.source_id=s.id and l.project_id=v.project_id), '[]'::json) as contour_ids
from device_b2_hosts s join device_b2_host_views v on v.source_id=s.id
join device_infrastructure_hosts h on h.id=v.host_id and h.project_id=v.project_id
where v.project_id=$1 and h.lifecycle_state='active'`, [projectId])).rows;
await db.query('commit');
} catch (error) { await db.query('rollback'); throw error; } finally { db.release(); }
// Release the database transaction before waiting for any external host.
const results = await Promise.all(sources.map(async (source) => {
const host = { hostRef: `host:${source.host_id}`, name: source.display_name,
endpoint: source.endpoint, contours: source.contour_ids.length };
try {
const remote = await this.clientFor(source);
const identity = await remote.call('/v2/identity');
if (identity.host_id !== source.remote_host_id) throw b2Error('b2_host_identity_mismatch');
const items = [];
for (const contourId of source.contour_ids) {
const snapshot = await remote.call(`/v2/contours/${opaque(contourId)}/devices`);
items.push(...snapshot.items.map((device) => ({ id: device.id, name: device.name, number: device.number,
active: device.active, identifier: { masked: '…' + String(device.imei).slice(-5) },
contourId, hostRef: host.hostRef, hostName: host.name })));
}
return { host: { ...host, reachable: true, checkedAt: new Date().toISOString(), devices: items.length }, items };
} catch {
return { host: { ...host, reachable: false, checkedAt: new Date().toISOString(), devices: null }, items: [] };
}
}));
return { items: results.flatMap((result) => result.items), hosts: results.map((result) => result.host) };
}
async execute(actor, input, requestId) {
if (input.action === 'inventory') return this.inventory(actor, input);
const projectId = ref(input.projectRef, 'project');
const hostId = ref(input.hostRef, 'host');
const action = input.action;
@@ -44,6 +44,16 @@ export function normalizeLifecycleManagementCommand(kind, input) {
});
}
if (kind === "device.update" && Object.prototype.hasOwnProperty.call(input, "archived")) {
assertAllowedKeys(input, ["projectRef", "deviceRef", "archived"]);
if (typeof input.archived !== "boolean") throw new TypeError("device_archive_state_invalid");
return Object.freeze({
projectId: normalizeEntityRef(input.projectRef, "project"),
deviceId: normalizeEntityRef(input.deviceRef, "device"),
archived: input.archived,
});
}
if (kind === "device.update") {
assertAllowedKeys(input, [
"projectRef",
@@ -91,7 +91,24 @@ export async function authorizeLifecycleManagementReplay(
}
}
async function archiveDevice(client, actor, command) {
const project = await findProjectWithCapability(client, actor, command.projectId, "project.manage");
const current = await findDeviceForUpdate(client, command.deviceId);
if (current.project_id !== project.id) throw domainError("device_update_project_mismatch", 409);
// Archive is a reversible registry operation. It never closes a live channel.
if (!["claimed", "suspended"].includes(current.lifecycle_state)) throw domainError("device_archive_lifecycle_blocked", 409);
const sessions = await client.query(`select exists(select 1 from device_sessions where device_id=$1
and lifecycle_state in ('connecting','online','closing')) as active`, [current.id]);
if (sessions.rows[0]?.active) throw domainError("device_archive_active_session", 409);
const state = command.archived ? "suspended" : "claimed";
await client.query('update device_instances set lifecycle_state=$3, updated_at=now() where id=$1 and project_id=$2', [current.id, project.id, state]);
await addAudit(client, { eventType: command.archived ? "device.archived" : "device.restored", actorRef: actor.userRef,
projectId: project.id, deviceId: current.id, payload: { deviceRef: `device:${current.id}`, lifecycleState: state } });
return { archived: command.archived };
}
async function updateDevice(client, actor, command) {
if (typeof command.archived === "boolean") return archiveDevice(client, actor, command);
const project = await findProjectWithCapability(
client,
actor,
@@ -81,6 +81,16 @@ export function normalizeOntologyManagementCommand(kind, input) {
});
}
if (kind === "infrastructure_host.ensure" && Object.prototype.hasOwnProperty.call(input, "archived")) {
assertAllowedKeys(input, ["projectRef", "hostRef", "archived"]);
if (typeof input.archived !== "boolean") throw new TypeError("device_archive_state_invalid");
return Object.freeze({
projectId: normalizeEntityRef(input.projectRef, "project"),
hostId: normalizeEntityRef(input.hostRef, "host"),
archived: input.archived,
});
}
if (kind === "infrastructure_host.ensure") {
assertAllowedKeys(input, [
"projectRef", "hostKey", "displayName", "providerRef", "externalRef",
@@ -201,7 +201,18 @@ async function closeAssetBinding(client, actor, project, command) {
return { closed: true, assetBinding: assetBindingView(row) };
}
async function archiveHost(client, actor, project, command) {
const result = await client.query(`update device_infrastructure_hosts set lifecycle_state=$3, updated_at=now()
where id=$1 and project_id=$2 and lifecycle_state in ('active','suspended') returning *`,
[command.hostId, project.id, command.archived ? 'suspended' : 'active']);
const row = requireRow(result, "device_host_not_archivable", 409);
await addAudit(client, { actor, project, eventType: command.archived ? 'infrastructure_host.archived' : 'infrastructure_host.restored',
payload: { hostRef: `host:${row.id}`, lifecycleState: row.lifecycle_state } });
return { archived: command.archived };
}
async function ensureHost(client, actor, project, command) {
if (typeof command.archived === "boolean") return archiveHost(client, actor, project, command);
const result = await client.query(
`insert into device_infrastructure_hosts (
id, owner_scope_id, project_id, host_key, display_name, provider_ref,