implement container stuff in periphery client

This commit is contained in:
beckerinj
2022-11-09 01:52:21 -05:00
parent 6728507914
commit b96a6cf7c2
4 changed files with 147 additions and 3 deletions
Generated
+4
View File
@@ -1482,7 +1482,11 @@ checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e"
name = "periphery_client"
version = "0.1.0"
dependencies = [
"anyhow",
"reqwest",
"serde",
"serde_json",
"types",
]
[[package]]
+5 -1
View File
@@ -6,4 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = "0.11"
types = { path = "../types" }
reqwest = { version = "0.11", features = ["json"] }
serde = "1.0"
serde_json = "1.0"
anyhow = "1.0"
+136
View File
@@ -1 +1,137 @@
use anyhow::{anyhow, Context};
use reqwest::StatusCode;
use serde::{de::DeserializeOwned, Serialize};
use serde_json::json;
use types::{BasicContainerInfo, Server, Log, Deployment};
pub struct PeripheryClient {
http_client: reqwest::Client,
}
impl PeripheryClient {
pub fn new() -> PeripheryClient {
PeripheryClient {
http_client: reqwest::Client::new(),
}
}
pub async fn container_list(&self, server: &Server) -> anyhow::Result<Vec<BasicContainerInfo>> {
self.get_json(server, "/container/list").await
}
pub async fn container_start(
&self,
server: &Server,
container_name: &str,
) -> anyhow::Result<Log> {
self.post_json(
server,
&format!("/container/start"),
&json!({ "name": container_name }),
)
.await
}
pub async fn container_stop(
&self,
server: &Server,
container_name: &str,
) -> anyhow::Result<Log> {
self.post_json(
server,
&format!("/container/stop"),
&json!({ "name": container_name }),
)
.await
}
pub async fn container_remove(
&self,
server: &Server,
container_name: &str,
) -> anyhow::Result<Log> {
self.post_json(
server,
&format!("/container/remove"),
&json!({ "name": container_name }),
)
.await
}
pub async fn deploy(&self, server: &Server, deployment: &Deployment) -> anyhow::Result<Log> {
self.post_json(
server,
&format!("/container/deploy"),
deployment,
)
.await
}
async fn get_json<R: DeserializeOwned>(
&self,
server: &Server,
endpoint: &str,
) -> anyhow::Result<R> {
let res = self
.http_client
.get(format!("{}{endpoint}", server.address))
.send()
.await
.context(format!(
"failed at request to server {} | no response",
server.name
))?;
let status = res.status();
if status == StatusCode::OK {
let parsed = res
.json::<R>()
.await
.context("failed at parsing response")?;
Ok(parsed)
} else {
let error = res
.text()
.await
.context(format!("failed at getting error text | status: {status}"))?;
Err(anyhow!(
"failed at request to server {} | status: {status} | error: {error:#?}",
server.name
))
}
}
async fn post_json<B: Serialize, R: DeserializeOwned>(
&self,
server: &Server,
endpoint: &str,
body: &B,
) -> anyhow::Result<R> {
let res = self
.http_client
.post(format!("{}{endpoint}", server.address))
.json(body)
.send()
.await
.context(format!(
"failed at request to server {} | no response",
server.name
))?;
let status = res.status();
if status == StatusCode::OK {
let parsed = res
.json::<R>()
.await
.context("failed at parsing response")?;
Ok(parsed)
} else {
let error = res
.text()
.await
.context(format!("failed at getting error text | status: {status}"))?;
Err(anyhow!(
"failed at request to server {} | status: {status} | error: {error:#?}",
server.name
))
}
}
}
+2 -2
View File
@@ -44,7 +44,7 @@ pub struct Server {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
pub id: Option<ObjectId>,
pub name: String,
pub host: String,
pub address: String,
pub permissions: PermissionsMap,
pub to_notify: Vec<String>,
pub cpu_alert: f64,
@@ -67,7 +67,7 @@ impl Default for Server {
Self {
id: None,
name: String::new(),
host: String::new(),
address: String::new(),
permissions: HashMap::new(),
to_notify: Vec::new(),
cpu_alert: 50.0,