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
+49
View File
@@ -26,6 +26,10 @@ pub fn routes(r: Router<crate::State>) -> Router<crate::State> {
post(revoke_device),
)
.route("/api/v1/tenants/{tenant}/devices/{asset}", delete(remove_device))
.route(
"/api/v1/tenants/{tenant}/devices/{asset}/hypervisor",
post(assign_hypervisor),
)
.route(
"/api/v1/tenants/{tenant}/devices/{asset}/archive",
post(archive_device),
@@ -134,6 +138,51 @@ struct Pairing {
agent_id: Uuid,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct HypervisorAssignment {
// null clears the relation. A missing field is a malformed update.
#[serde(deserialize_with = "required_hypervisor_value")]
hypervisor_asset_id: Option<Uuid>,
}
fn required_hypervisor_value<'de, D: serde::Deserializer<'de>>(value: D) -> Result<Option<Uuid>, D::Error> {
Option::deserialize(value)
}
async fn assign_hypervisor(
State(s): State<crate::State>,
h: HeaderMap,
Path((tenant, asset)): Path<(Uuid, Uuid)>,
Query(pair): Query<Pairing>,
Json(input): Json<HypervisorAssignment>,
) -> Result<Json<Value>, Error> {
const EVENT: &str = "device_hypervisor_assigned";
let (p, mut tx) = context(&s, &h, tenant, "devices:revoke", EVENT).await?;
crate::permissions::require_principal(&mut tx, &p, "inventory:read").await?;
// Match the asset lock order for simultaneous edits of the same two devices.
let ids = [asset, input.hypervisor_asset_id.unwrap_or(asset)];
let rows: Vec<(Uuid, Option<Uuid>)> = sqlx::query_as(
"SELECT asset_id,hypervisor_asset_id FROM assets
WHERE tenant_id=$1 AND asset_id=ANY($2) ORDER BY asset_id FOR UPDATE",
)
.bind(tenant).bind(ids.as_slice()).fetch_all(&mut *tx).await?;
let previous = rows.into_iter().find(|(id, _)| *id == asset)
.ok_or(Error(StatusCode::NOT_FOUND, "NOT_FOUND"))?.1;
crate::devices::validate_vm_host_assignment(
&mut tx, tenant, asset, pair.agent_id, input.hypervisor_asset_id,
).await?;
if previous != input.hypervisor_asset_id {
sqlx::query("UPDATE assets SET hypervisor_asset_id=$3 WHERE tenant_id=$1 AND asset_id=$2")
.bind(tenant).bind(asset).bind(input.hypervisor_asset_id).execute(&mut *tx).await?;
audit(&mut tx, &p, EVENT, json!({"asset_id":asset,"agent_id":pair.agent_id,
"previous_hypervisor_asset_id":previous,"hypervisor_asset_id":input.hypervisor_asset_id})).await?;
}
tx.commit().await?;
Ok(Json(json!({"asset_id":asset,"hypervisor_asset_id":input.hypervisor_asset_id})))
}
async fn remove_device(
State(s): State<crate::State>,
h: HeaderMap,