generated from Patagia/template-nix
This commit is contained in:
parent
8e99ab4555
commit
dbf9498354
12 changed files with 752 additions and 58 deletions
hostd/src
43
hostd/src/machine.rs
Normal file
43
hostd/src/machine.rs
Normal file
|
@ -0,0 +1,43 @@
|
|||
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))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue