Skip to content

Commit

Permalink
add ephemeral-storage commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tzneal committed Aug 13, 2024
1 parent 10a9a2b commit eaf5931
Show file tree
Hide file tree
Showing 9 changed files with 992 additions and 14 deletions.
87 changes: 87 additions & 0 deletions sources/api/apiclient/src/ephemeral_storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use model::ephemeral_storage::{Bind, Filesystem, Init};
use snafu::ResultExt;
use std::path::Path;

/// Requests ephemeral storage initialization through the API
pub async fn initialize<P>(
socket_path: P,
filesystem: Option<Filesystem>,
disks: Option<Vec<String>>,
) -> Result<()>
where
P: AsRef<Path>,
{
let uri = "/actions/ephemeral-storage/init";
let opts =
serde_json::to_string(&Init { filesystem, disks }).context(error::JsonSerializeSnafu {})?;
let method = "POST";
let (_status, _body) = crate::raw_request(&socket_path, &uri, method, Some(opts))
.await
.context(error::RequestSnafu { uri, method })?;
Ok(())
}

/// Requests binding of directories to configured ephemeral storage through the API
pub async fn bind<P>(socket_path: P, targets: Vec<String>) -> Result<()>
where
P: AsRef<Path>,
{
let uri = "/actions/ephemeral-storage/bind";
let opts = serde_json::to_string(&Bind { targets }).context(error::JsonSerializeSnafu {})?;
let method = "POST";
let (_status, _body) = crate::raw_request(&socket_path, &uri, method, Some(opts))
.await
.context(error::RequestSnafu { uri, method })?;
Ok(())
}

/// Lists the ephemeral disks available for configuration
pub async fn list_disks<P>(socket_path: P, format: Option<String>) -> Result<String>
where
P: AsRef<Path>,
{
list(socket_path, "list-disks", format).await
}

/// Lists the ephemeral disks available for configuration
pub async fn list_dirs<P>(socket_path: P, format: Option<String>) -> Result<String>
where
P: AsRef<Path>,
{
list(socket_path, "list-dirs", format).await
}
async fn list<P>(socket_path: P, item: &str, format: Option<String>) -> Result<String>
where
P: AsRef<Path>,
{
let mut query = Vec::new();
if let Some(query_format) = format {
query.push(format!("format={}", query_format));
}

let uri = format!("/actions/ephemeral-storage/{}?{}", item, query.join("&"));
let method = "GET";
let (_status, body) = crate::raw_request(&socket_path, &uri, method, None)
.await
.context(error::RequestSnafu { uri, method })?;
Ok(body)
}
mod error {
use snafu::Snafu;

#[derive(Debug, Snafu)]
#[snafu(visibility(pub(super)))]
pub enum Error {
#[snafu(display("Failed {} request to '{}': {}", method, uri, source))]
Request {
method: String,
uri: String,
#[snafu(source(from(crate::Error, Box::new)))]
source: Box<crate::Error>,
},
#[snafu(display("Failed to serialize parameters"))]
JsonSerialize { source: serde_json::Error },
}
}
pub use error::Error;
pub type Result<T> = std::result::Result<T, error::Error>;
1 change: 1 addition & 0 deletions sources/api/apiclient/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use snafu::{ensure, ResultExt};
use std::{fmt, fmt::Display, path::Path};

pub mod apply;
pub mod ephemeral_storage;
pub mod exec;
pub mod get;
pub mod reboot;
Expand Down
Loading

0 comments on commit eaf5931

Please sign in to comment.