Skip to content

Commit

Permalink
adds support for deleting instance
Browse files Browse the repository at this point in the history
  • Loading branch information
shahadarsh committed Dec 10, 2023
1 parent 5470907 commit 417692b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tembo-cli/src/cmd/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub fn execute_tembo_cloud(env: Environment) -> Result<()> {
Ok(())
}

fn get_instance_id_from_state(instance_name: String) -> Result<Option<String>> {
pub fn get_instance_id_from_state(instance_name: String) -> Result<Option<String>> {
let contents = match fs::read_to_string(tembo_state_file_path()) {
Ok(c) => c,
Err(e) => {
Expand Down
58 changes: 56 additions & 2 deletions tembo-cli/src/cmd/delete.rs
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(())
}

0 comments on commit 417692b

Please sign in to comment.