From b96a6cf7c23c19fa99bb77949b8aaddf384ba3f6 Mon Sep 17 00:00:00 2001 From: beckerinj Date: Wed, 9 Nov 2022 01:52:21 -0500 Subject: [PATCH] implement container stuff in periphery client --- Cargo.lock | 4 + lib/periphery_client/Cargo.toml | 6 +- lib/periphery_client/src/lib.rs | 136 ++++++++++++++++++++++++++++++++ lib/types/src/lib.rs | 4 +- 4 files changed, 147 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0d8ea4e7f..c8e98b5be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1482,7 +1482,11 @@ checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" name = "periphery_client" version = "0.1.0" dependencies = [ + "anyhow", "reqwest", + "serde", + "serde_json", + "types", ] [[package]] diff --git a/lib/periphery_client/Cargo.toml b/lib/periphery_client/Cargo.toml index f40f9fd27..1c3bb2034 100644 --- a/lib/periphery_client/Cargo.toml +++ b/lib/periphery_client/Cargo.toml @@ -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" \ No newline at end of file +types = { path = "../types" } +reqwest = { version = "0.11", features = ["json"] } +serde = "1.0" +serde_json = "1.0" +anyhow = "1.0" \ No newline at end of file diff --git a/lib/periphery_client/src/lib.rs b/lib/periphery_client/src/lib.rs index 8b1378917..c9a18301c 100644 --- a/lib/periphery_client/src/lib.rs +++ b/lib/periphery_client/src/lib.rs @@ -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> { + self.get_json(server, "/container/list").await + } + + pub async fn container_start( + &self, + server: &Server, + container_name: &str, + ) -> anyhow::Result { + 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 { + 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 { + self.post_json( + server, + &format!("/container/remove"), + &json!({ "name": container_name }), + ) + .await + } + + pub async fn deploy(&self, server: &Server, deployment: &Deployment) -> anyhow::Result { + self.post_json( + server, + &format!("/container/deploy"), + deployment, + ) + .await + } + + async fn get_json( + &self, + server: &Server, + endpoint: &str, + ) -> anyhow::Result { + 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::() + .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( + &self, + server: &Server, + endpoint: &str, + body: &B, + ) -> anyhow::Result { + 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::() + .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 + )) + } + } +} diff --git a/lib/types/src/lib.rs b/lib/types/src/lib.rs index faa1a853d..edcc6c572 100644 --- a/lib/types/src/lib.rs +++ b/lib/types/src/lib.rs @@ -44,7 +44,7 @@ pub struct Server { #[serde(rename = "_id", skip_serializing_if = "Option::is_none")] pub id: Option, pub name: String, - pub host: String, + pub address: String, pub permissions: PermissionsMap, pub to_notify: Vec, 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,