Add routes to manipulate VM
This commit is contained in:
@ -76,6 +76,11 @@ pub struct VMInfo {
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize, Debug)]
|
||||
pub struct VMState {
|
||||
pub state: String,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, Debug)]
|
||||
pub struct TokenRight {
|
||||
verb: String,
|
||||
@ -129,7 +134,7 @@ impl TokenInfo {
|
||||
}
|
||||
|
||||
/// Perform a request on the API
|
||||
async fn request<D: Display, E: serde::de::DeserializeOwned>(uri: D) -> anyhow::Result<E> {
|
||||
async fn request<D: Display>(uri: D) -> anyhow::Result<reqwest::Response> {
|
||||
let url = format!("{}{}", AppConfig::get().virtweb_base_url, uri);
|
||||
log::debug!("Will query {uri}...");
|
||||
|
||||
@ -154,20 +159,73 @@ async fn request<D: Display, E: serde::de::DeserializeOwned>(uri: D) -> anyhow::
|
||||
return Err(VirtWebClientError::InvalidStatusCode(res.status().as_u16()).into());
|
||||
}
|
||||
|
||||
Ok(res.json().await?)
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
/// Perform a request on the API
|
||||
async fn json_request<D: Display, E: serde::de::DeserializeOwned>(uri: D) -> anyhow::Result<E> {
|
||||
Ok(request(uri).await?.json().await?)
|
||||
}
|
||||
|
||||
/// Get current token information
|
||||
pub async fn get_token_info() -> anyhow::Result<TokenInfo> {
|
||||
let res: TokenInfo =
|
||||
request(format!("/api/token/{}", AppConfig::get().virtweb_token_id)).await?;
|
||||
json_request(format!("/api/token/{}", AppConfig::get().virtweb_token_id)).await?;
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
/// Get a vm information
|
||||
pub async fn get_vm_info(id: VMUuid) -> anyhow::Result<VMInfo> {
|
||||
let res: VMInfo = request(format!("/api/vm/{}", id.0)).await?;
|
||||
|
||||
Ok(res)
|
||||
pub async fn vm_info(id: VMUuid) -> anyhow::Result<VMInfo> {
|
||||
json_request(id.route_info()).await
|
||||
}
|
||||
|
||||
/// Get a vm information
|
||||
pub async fn vm_state(id: VMUuid) -> anyhow::Result<VMState> {
|
||||
json_request(id.route_state()).await
|
||||
}
|
||||
|
||||
/// Start a vm
|
||||
pub async fn vm_start(id: VMUuid) -> anyhow::Result<()> {
|
||||
request(id.route_start()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Shutdown a vm
|
||||
pub async fn vm_shutdown(id: VMUuid) -> anyhow::Result<()> {
|
||||
request(id.route_shutdown()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Kill a vm
|
||||
pub async fn vm_kill(id: VMUuid) -> anyhow::Result<()> {
|
||||
request(id.route_kill()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reset a vm
|
||||
pub async fn vm_reset(id: VMUuid) -> anyhow::Result<()> {
|
||||
request(id.route_reset()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Suspend a vm
|
||||
pub async fn vm_suspend(id: VMUuid) -> anyhow::Result<()> {
|
||||
request(id.route_suspend()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Resume a vm
|
||||
pub async fn vm_resume(id: VMUuid) -> anyhow::Result<()> {
|
||||
request(id.route_resume()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Resume a vm
|
||||
pub async fn vm_screenshot(id: VMUuid) -> anyhow::Result<Vec<u8>> {
|
||||
Ok(request(id.route_screenshot())
|
||||
.await?
|
||||
.bytes()
|
||||
.await?
|
||||
.to_vec())
|
||||
}
|
||||
|
Reference in New Issue
Block a user