From 92f4e15412ea56d6d31d2e66be0c25f1f3aea957 Mon Sep 17 00:00:00 2001 From: Noor Date: Wed, 14 May 2025 13:31:12 +0530 Subject: [PATCH] App contract delete --- src/db/app.rs | 68 ++++++++++++++++++++++++++++++++++++------------- src/grpc/app.rs | 24 +++++++++-------- 2 files changed, 64 insertions(+), 28 deletions(-) diff --git a/src/db/app.rs b/src/db/app.rs index 6c30cf8..d7f9601 100644 --- a/src/db/app.rs +++ b/src/db/app.rs @@ -1,5 +1,5 @@ use super::Error; -use crate::constants::{ACCOUNT, ACTIVE_APP, APP_NODE, NEW_APP_REQ}; +use crate::constants::{ACCOUNT, ACTIVE_APP, APP_NODE, DELETED_APP, NEW_APP_REQ}; use crate::db; use crate::db::general::Report; use crate::old_brain; @@ -158,24 +158,47 @@ impl AppNodeWithReports { #[derive(Debug, Serialize, Deserialize)] pub struct ActiveApp { - id: RecordId, + pub id: RecordId, #[serde(rename = "in")] - admin: RecordId, + pub admin: RecordId, #[serde(rename = "out")] - app_node: RecordId, - app_name: String, - mapped_ports: Vec<(u64, u64)>, - host_ipv4: String, - vcpus: u64, - memory_mb: u64, - disk_size_gb: u64, - created_at: Datetime, - price_per_unit: u64, - locked_nano: u64, - collected_at: Datetime, - mr_enclave: String, - package_url: String, - hratls_pubkey: String, + pub app_node: RecordId, + pub app_name: String, + pub mapped_ports: Vec<(u64, u64)>, + pub host_ipv4: String, + pub vcpus: u64, + pub memory_mb: u64, + pub disk_size_gb: u64, + pub created_at: Datetime, + pub price_per_unit: u64, + pub locked_nano: u64, + pub collected_at: Datetime, + pub mr_enclave: String, + pub package_url: String, + pub hratls_pubkey: String, +} + +impl From for DeletedApp { + fn from(value: ActiveApp) -> Self { + Self { + id: value.id, + admin: value.admin, + app_node: value.app_node, + app_name: value.app_name, + mapped_ports: value.mapped_ports, + host_ipv4: value.host_ipv4, + vcpus: value.vcpus, + memory_mb: value.memory_mb, + disk_size_gb: value.disk_size_gb, + created_at: value.created_at, + price_per_unit: value.price_per_unit, + locked_nano: value.locked_nano, + collected_at: value.collected_at, + mr_enclave: value.mr_enclave, + package_url: value.package_url, + hratls_pubkey: value.hratls_pubkey, + } + } } impl ActiveApp { @@ -208,6 +231,17 @@ impl ActiveApp { Ok(()) } + + pub async fn delete(db: &Surreal, id: &str) -> Result { + let deleted_app: Option = db.delete((ACTIVE_APP, id)).await?; + if let Some(deleted_app) = deleted_app { + let deleted_app: DeletedApp = deleted_app.into(); + let _: Vec = db.insert(DELETED_APP).relation(deleted_app).await?; + Ok(true) + } else { + Ok(false) + } + } } #[derive(Debug, Serialize, Deserialize)] diff --git a/src/grpc/app.rs b/src/grpc/app.rs index 10c69d0..d22851c 100644 --- a/src/grpc/app.rs +++ b/src/grpc/app.rs @@ -1,4 +1,5 @@ use crate::constants::{ACCOUNT, APP_NODE}; +use crate::db::app::ActiveApp; use crate::db::prelude as db; use crate::grpc::{check_sig_from_parts, check_sig_from_req}; use detee_shared::app_proto::brain_app_cli_server::BrainAppCli; @@ -37,8 +38,7 @@ impl BrainAppDaemon for AppDaemonServer { async fn register_app_node( &self, req: tonic::Request, - ) -> Result::RegisterAppNodeStream>, tonic::Status> - { + ) -> Result::RegisterAppNodeStream>, Status> { let req = check_sig_from_req(req)?; info!("Starting app_node registration process for {:?}", req); @@ -77,7 +77,7 @@ impl BrainAppDaemon for AppDaemonServer { async fn brain_messages( &self, req: tonic::Request, - ) -> Result::BrainMessagesStream>, tonic::Status> { + ) -> Result::BrainMessagesStream>, Status> { let auth = req.into_inner(); let pubkey = auth.pubkey.clone(); check_sig_from_parts( @@ -114,7 +114,7 @@ impl BrainAppDaemon for AppDaemonServer { async fn daemon_messages( &self, req: tonic::Request>, - ) -> Result, tonic::Status> { + ) -> Result, Status> { let mut req_stream = req.into_inner(); let pubkey: String; if let Some(Ok(msg)) = req_stream.next().await { @@ -186,7 +186,7 @@ impl BrainAppCli for AppCliServer { async fn deploy_app( &self, req: tonic::Request, - ) -> Result, tonic::Status> { + ) -> Result, Status> { let req = check_sig_from_req(req)?; info!("deploy_app process starting for {:?}", req); @@ -196,17 +196,19 @@ impl BrainAppCli for AppCliServer { async fn delete_app( &self, req: tonic::Request, - ) -> Result, tonic::Status> { + ) -> Result, Status> { let req = check_sig_from_req(req)?; info!("delete_app process starting for {:?}", req); - - todo!() + match ActiveApp::delete(&self.db, &req.uuid).await? { + true => Ok(Response::new(Empty {})), + false => Err(Status::not_found(format!("Could not find App contract {}", &req.uuid))), + } } async fn list_app_contracts( &self, req: tonic::Request, - ) -> Result::ListAppContractsStream>, tonic::Status> { + ) -> Result::ListAppContractsStream>, Status> { let req = check_sig_from_req(req)?; info!("list_app_contracts process starting for {:?}", req); @@ -245,7 +247,7 @@ impl BrainAppCli for AppCliServer { async fn list_app_nodes( &self, req: tonic::Request, - ) -> Result::ListAppNodesStream>, tonic::Status> { + ) -> Result::ListAppNodesStream>, Status> { let req = check_sig_from_req(req)?; info!("list_app_nodes process starting for {:?}", req); let app_nodes = db::AppNodeWithReports::find_by_filters(&self.db, req, false).await?; @@ -262,7 +264,7 @@ impl BrainAppCli for AppCliServer { async fn get_one_app_node( &self, req: tonic::Request, - ) -> Result, tonic::Status> { + ) -> Result, Status> { let req = check_sig_from_req(req)?; info!("get_one_app_node process starting for {:?}", req); let app_node = db::AppNodeWithReports::find_by_filters(&self.db, req, true)