diff --git a/cli-mock/src/main.rs b/cli-mock/src/main.rs index 7d2b8e2..0e51c7f 100644 --- a/cli-mock/src/main.rs +++ b/cli-mock/src/main.rs @@ -5,7 +5,8 @@ pub mod brain { use anyhow::Result; use brain::{ - brain_cli_service_client::BrainCliServiceClient, NewVmRequest, NodeFilters, NodeListResp, + brain_cli_service_client::BrainCliServiceClient, DeletedVmUpdate, ListVmContractsReq, + NewVmRequest, NodeFilters, NodeListResp, VmContract, }; use lazy_static::lazy_static; use log::{debug, info, warn}; @@ -15,7 +16,11 @@ use tokio_stream::StreamExt; use tonic::transport::Channel; lazy_static! { - static ref SECURE_PUBLIC_KEY: String = generate_random_string(); + static ref SECURE_PUBLIC_KEY: String = use_default_string(); +} + +fn use_default_string() -> String { + "ThisIsMyEternalClient".to_string() } fn generate_random_string() -> String { @@ -92,6 +97,44 @@ async fn submit_vm_request( Ok(()) } +async fn list_contracts(mut client: BrainCliServiceClient) -> Result> { + info!("Getting contracts from brain..."); + let mut contracts = Vec::new(); + let mut grpc_stream = client + .list_vm_contracts(ListVmContractsReq { + admin_pubkey: SECURE_PUBLIC_KEY.to_string(), + node_pubkey: String::new(), + }) + .await? + .into_inner(); + while let Some(stream_update) = grpc_stream.next().await { + match stream_update { + Ok(node) => { + debug!("Received contract from brain: {node:?}"); + contracts.push(node); + } + Err(e) => { + warn!("Received error instead of contracts: {e:?}"); + } + } + } + info!("Brain terminated list_contracts stream."); + Ok(contracts) +} + +async fn delete_vm(mut client: BrainCliServiceClient, uuid: &str) -> Result<()> { + let req = DeletedVmUpdate { + uuid: uuid.to_string(), + }; + info!("Creating VM {req:?}"); + let result = client.delete_vm(req).await; + match result { + Ok(confirmation) => info!("VM deleted: {confirmation:?}"), + Err(e) => log::error!("Could not delete vm: {e:?}"), + }; + Ok(()) +} + #[tokio::main] async fn main() -> Result<()> { env_logger::builder() @@ -110,5 +153,16 @@ async fn main() -> Result<()> { ); } } + + if std::env::var("DELETE_VMS").is_err() { + return Ok(()); + } + + let contracts = list_contracts(client.clone()).await?; + for contract in contracts { + if let Err(e) = delete_vm(client.clone(), &contract.uuid).await { + log::error!("Received error when deleting VM {}: {e:?}", &contract.uuid); + } + } Ok(()) }