patagia-control/hostd/src/machine.rs

43 lines
1.1 KiB
Rust

use dropshot::{endpoint, HttpError, HttpResponseOk, RequestContext};
use schemars::JsonSchema;
use serde::Serialize;
use std::sync::Arc;
use trace_request::trace_request;
use crate::context::ControllerContext;
/// Machine information
#[derive(Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
struct MachineInfo {
machine_id: String,
}
/// Fetch machine info
#[endpoint {
method = GET,
path = "/machine_info",
}]
#[trace_request]
pub async fn describe(
rqctx: RequestContext<Arc<ControllerContext>>,
) -> Result<HttpResponseOk<MachineInfo>, HttpError> {
let hostnamed = zbus_systemd::hostname1::HostnamedProxy::new(&rqctx.context().dbus)
.await
.unwrap();
let machine_id = hostnamed
.machine_id()
.await
.map_err(|e| match e {
err => HttpError::for_internal_error(format!("Error: {}", err)),
})?
// convert bytes to hex string
.iter()
.map(|&b| format!("{:02x}", b))
.collect();
let machine_info = MachineInfo { machine_id };
Ok(HttpResponseOk(machine_info))
}