diff --git a/tembo-cli/src/cmd/apply.rs b/tembo-cli/src/cmd/apply.rs index f04db3c6b..1a498b2b0 100644 --- a/tembo-cli/src/cmd/apply.rs +++ b/tembo-cli/src/cmd/apply.rs @@ -109,7 +109,7 @@ pub fn execute_tembo_cloud(env: Environment) -> Result<()> { Ok(()) } -fn get_instance_id_from_state(instance_name: String) -> Result> { +pub fn get_instance_id_from_state(instance_name: String) -> Result> { let contents = match fs::read_to_string(tembo_state_file_path()) { Ok(c) => c, Err(e) => { diff --git a/tembo-cli/src/cmd/delete.rs b/tembo-cli/src/cmd/delete.rs index 2df243101..4cb6d853a 100644 --- a/tembo-cli/src/cmd/delete.rs +++ b/tembo-cli/src/cmd/delete.rs @@ -1,5 +1,19 @@ -use crate::{cli::docker::Docker, Result}; +use std::collections::HashMap; + +use crate::{ + cli::{ + context::{get_current_context, Environment, Target}, + docker::Docker, + tembo_config::InstanceSettings, + }, + Result, +}; use clap::{ArgMatches, Command}; +use core::result::Result::Ok; +use temboclient::apis::{configuration::Configuration, instance_api::delete_instance}; +use tokio::runtime::Runtime; + +use super::apply::{get_instance_id_from_state, get_instance_settings}; // Create init subcommand arguments pub fn make_subcommand() -> Command { @@ -7,7 +21,47 @@ pub fn make_subcommand() -> Command { } pub fn execute(_args: &ArgMatches) -> Result<()> { - Docker::stop_remove("tembo-pg")?; + let env = get_current_context()?; + + if env.target == Target::Docker.to_string() { + Docker::stop_remove("tembo-pg")?; + } else if env.target == Target::TemboCloud.to_string() { + return execute_tembo_cloud(env); + } + + Ok(()) +} + +fn execute_tembo_cloud(env: Environment) -> Result<()> { + let instance_settings: HashMap = get_instance_settings()?; + + let profile = env.clone().selected_profile.unwrap(); + let config = Configuration { + base_path: profile.tembo_host, + bearer_access_token: Some(profile.tembo_access_token), + ..Default::default() + }; + + for (_key, value) in instance_settings.iter() { + let instance_id = get_instance_id_from_state(value.instance_name.clone())?; + if let Some(env_instance_id) = instance_id { + let v = Runtime::new().unwrap().block_on(delete_instance( + &config, + env.org_id.clone().unwrap().as_str(), + &env_instance_id, + )); + + match v { + Ok(result) => { + println!( + "Instance delete started for Instance Id: {}", + result.instance_id + ) + } + Err(error) => eprintln!("Error deleting instance: {}", error), + }; + } + } Ok(()) }