-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5470907
commit 417692b
Showing
2 changed files
with
57 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,67 @@ | ||
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 { | ||
Command::new("delete").about("Deletes database instance locally & on tembo cloud") | ||
} | ||
|
||
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<String, InstanceSettings> = 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(()) | ||
} |