Skip to content

Commit

Permalink
removes state file logic and uses get_all method to check if the inst…
Browse files Browse the repository at this point in the history
…ance already exists (#421)
  • Loading branch information
shahadarsh authored Dec 14, 2023
1 parent a25b3e4 commit f78529d
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 65 deletions.
6 changes: 0 additions & 6 deletions tembo-cli/src/cli/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@ pub fn tembo_credentials_file_path() -> String {
tembo_home_dir() + "/credentials"
}

pub const DOT_TEMBO_FOLDER: &str = ".tembo";

pub fn tembo_state_file_path() -> String {
DOT_TEMBO_FOLDER.to_string() + "/tembo.state"
}

pub fn list_context() -> Result<Context> {
let filename = tembo_context_file_path();

Expand Down
50 changes: 23 additions & 27 deletions tembo-cli/src/cmd/apply.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
use crate::{
cli::{
context::{get_current_context, tembo_state_file_path, Environment, Target},
context::{get_current_context, Environment, Target},
tembo_config,
},
Result,
};
use clap::{ArgMatches, Command};
use controller::stacks::get_stack;
use controller::stacks::types::StackType as ControllerStackType;
use std::io::Write;
use std::{
collections::HashMap,
fs::{self, OpenOptions},
fs::{self},
str::FromStr,
};
use temboclient::{
apis::{
configuration::Configuration,
instance_api::{create_instance, put_instance},
instance_api::{create_instance, get_all, put_instance},
},
models::{
Cpu, CreateInstance, Extension, ExtensionInstallLocation, Memory, PgConfig, StackType,
Expand Down Expand Up @@ -99,7 +98,7 @@ pub fn execute_tembo_cloud(env: Environment) -> Result<()> {
};

for (_key, value) in instance_settings.iter() {
let instance_id = get_instance_id_from_state(value.instance_name.clone())?;
let instance_id = get_instance_id(value.instance_name.clone(), &config, env.clone())?;
if let Some(env_instance_id) = instance_id {
update_existing_instance(env_instance_id, value, &config, env.clone());
} else {
Expand All @@ -110,18 +109,26 @@ pub fn execute_tembo_cloud(env: Environment) -> Result<()> {
Ok(())
}

pub fn get_instance_id_from_state(instance_name: String) -> Result<Option<String>> {
let contents = fs::read_to_string(tembo_state_file_path())?;

let tembo_state_map: HashMap<String, String> = toml::from_str(&contents)?;
pub fn get_instance_id(
instance_name: String,
config: &Configuration,
env: Environment,
) -> Result<Option<String>> {
let v = Runtime::new()
.unwrap()
.block_on(get_all(config, env.org_id.clone().unwrap().as_str()));

let tembo_state = tembo_state_map.get(&instance_name);
if tembo_state.is_none() {
Ok(None)
} else {
let instance_id = tembo_state.unwrap().clone();
Ok(Some(instance_id))
}
match v {
Ok(result) => {
for instance in result.iter() {
if instance.instance_name == instance_name {
return Ok(Some(instance.clone().instance_id));
}
}
}
Err(error) => eprintln!("Error getting instance: {}", error),
};
Ok(None)
}

fn update_existing_instance(
Expand Down Expand Up @@ -165,17 +172,6 @@ fn create_new_instance(value: &InstanceSettings, config: &Configuration, env: En
"Instance creation started for instance_name: {} with instance_id: {}",
result.instance_name, result.instance_id
);

let mut state_file = OpenOptions::new()
.append(true)
.open(tembo_state_file_path())
.expect("cannot open file");

let state = format!("{} = \"{}\"\n", result.instance_name, result.instance_id);

state_file
.write_all(state.as_bytes())
.expect("write failed");
}
Err(error) => eprintln!("Error creating instance: {}", error),
};
Expand Down
6 changes: 3 additions & 3 deletions tembo-cli/src/cmd/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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};
use super::apply::{get_instance_id, get_instance_settings};

// Create init subcommand arguments
pub fn make_subcommand() -> Command {
Expand Down Expand Up @@ -43,11 +43,11 @@ fn execute_tembo_cloud(env: Environment) -> Result<()> {
};

for (_key, value) in instance_settings.iter() {
let instance_id = get_instance_id_from_state(value.instance_name.clone())?;
let instance_id = get_instance_id(value.instance_name.clone(), &config, env.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.clone().org_id.unwrap().as_str(),
&env_instance_id,
));

Expand Down
24 changes: 1 addition & 23 deletions tembo-cli/src/cmd/init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
cli::context::{tembo_state_file_path, DOT_TEMBO_FOLDER},
Result,
};
use crate::Result;
use clap::{ArgMatches, Command};

use crate::cli::{
Expand Down Expand Up @@ -69,24 +66,5 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
}
}

match FileUtils::create_dir(".tembo directory".to_string(), DOT_TEMBO_FOLDER.to_string()) {
Ok(t) => t,
Err(e) => {
return Err(e);
}
}

match FileUtils::create_file(
tembo_state_file_path(),
tembo_state_file_path(),
"".to_string(),
false,
) {
Ok(t) => t,
Err(e) => {
return Err(e);
}
}

Ok(())
}
3 changes: 2 additions & 1 deletion tembo-cli/temboclient/.openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.gitignore
.openapi-generator-ignore
.travis.yml
Cargo.toml
README.md
Expand All @@ -25,6 +24,7 @@ docs/ExtensionStatus.md
docs/HeaderConfig.md
docs/Infrastructure.md
docs/Ingress.md
docs/IngressType.md
docs/Instance.md
docs/InstanceApi.md
docs/InstanceEvent.md
Expand Down Expand Up @@ -89,6 +89,7 @@ src/models/extension_status.rs
src/models/header_config.rs
src/models/infrastructure.rs
src/models/ingress.rs
src/models/ingress_type.rs
src/models/instance.rs
src/models/instance_event.rs
src/models/int_or_string.rs
Expand Down
1 change: 1 addition & 0 deletions tembo-cli/temboclient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Class | Method | HTTP request | Description
- [HeaderConfig](docs/HeaderConfig.md)
- [Infrastructure](docs/Infrastructure.md)
- [Ingress](docs/Ingress.md)
- [IngressType](docs/IngressType.md)
- [Instance](docs/Instance.md)
- [InstanceEvent](docs/InstanceEvent.md)
- [IntOrString](docs/IntOrString.md)
Expand Down
10 changes: 10 additions & 0 deletions tembo-cli/temboclient/docs/IngressType.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# IngressType

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


2 changes: 2 additions & 0 deletions tembo-cli/temboclient/docs/Routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**entry_points** | Option<**Vec<String>**> | | [optional]
**ingress_path** | Option<**String**> | | [optional]
**ingress_type** | Option<[**crate::models::IngressType**](IngressType.md)> | | [optional]
**middlewares** | Option<**Vec<String>**> | | [optional]
**port** | **i32** | |

Expand Down
3 changes: 2 additions & 1 deletion tembo-cli/temboclient/src/apis/instance_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

use reqwest;

use crate::{apis::ResponseContent, models::InstanceEvent};
use crate::apis::ResponseContent;
use super::{Error, configuration};
use crate::models::InstanceEvent;


/// struct for typed errors of method [`create_instance`]
Expand Down
1 change: 1 addition & 0 deletions tembo-cli/temboclient/src/apis/stack_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::apis::ResponseContent;
use super::{Error, configuration};
use crate::models::StackType;


/// struct for typed errors of method [`get_all_entities`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
Expand Down
18 changes: 17 additions & 1 deletion tembo-cli/temboclient/src/models/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@
///
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum Cpu {
#[serde(rename = "0.25")]
Variant0Period25,
#[serde(rename = "0.5")]
Variant0Period5,
#[serde(rename = "1")]
Variant1,
#[serde(rename = "2")]
Variant2,
#[serde(rename = "4")]
Variant4,
#[serde(rename = "6")]
Variant6,
#[serde(rename = "8")]
Variant8,
#[serde(rename = "12")]
Variant12,
#[serde(rename = "16")]
Variant16,
#[serde(rename = "32")]
Expand All @@ -30,10 +38,14 @@ pub enum Cpu {
impl ToString for Cpu {
fn to_string(&self) -> String {
match self {
Self::Variant0Period25 => String::from("0.25"),
Self::Variant0Period5 => String::from("0.5"),
Self::Variant1 => String::from("1"),
Self::Variant2 => String::from("2"),
Self::Variant4 => String::from("4"),
Self::Variant6 => String::from("6"),
Self::Variant8 => String::from("8"),
Self::Variant12 => String::from("12"),
Self::Variant16 => String::from("16"),
Self::Variant32 => String::from("32"),
}
Expand All @@ -42,6 +54,10 @@ impl ToString for Cpu {

impl Default for Cpu {
fn default() -> Cpu {
Self::Variant1
Self::Variant0Period25
}
}




39 changes: 39 additions & 0 deletions tembo-cli/temboclient/src/models/ingress_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Tembo Cloud
*
* Platform API for Tembo Cloud </br> </br> To find a Tembo Data API, please find it here: </br> </br> [AWS US East 1](https://api.data-1.use1.tembo.io/swagger-ui/)
*
* The version of the OpenAPI document: v1.0.0
*
* Generated by: https://openapi-generator.tech
*/


///
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum IngressType {
#[serde(rename = "http")]
Http,
#[serde(rename = "tcp")]
Tcp,

}

impl ToString for IngressType {
fn to_string(&self) -> String {
match self {
Self::Http => String::from("http"),
Self::Tcp => String::from("tcp"),
}
}
}

impl Default for IngressType {
fn default() -> IngressType {
Self::Http
}
}




6 changes: 3 additions & 3 deletions tembo-cli/temboclient/src/models/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Instance {
#[serde(rename = "app_services", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub app_services: Option<Option<Vec<crate::models::AppType>>>,
//#[serde(rename = "app_services", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
//pub app_services: Option<Option<Vec<crate::models::AppType>>>,
#[serde(rename = "connection_info", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub connection_info: Option<Option<Box<crate::models::ConnectionInfo>>>,
#[serde(rename = "connection_pooler", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -64,7 +64,7 @@ pub struct Instance {
impl Instance {
pub fn new(cpu: crate::models::Cpu, environment: crate::models::Environment, instance_id: String, instance_name: String, memory: crate::models::Memory, organization_id: String, organization_name: String, replicas: i32, stack_type: crate::models::StackType, state: crate::models::State, storage: crate::models::Storage) -> Instance {
Instance {
app_services: None,
//app_services: None,
connection_info: None,
connection_pooler: None,
cpu,
Expand Down
2 changes: 2 additions & 0 deletions tembo-cli/temboclient/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub mod infrastructure;
pub use self::infrastructure::Infrastructure;
pub mod ingress;
pub use self::ingress::Ingress;
pub mod ingress_type;
pub use self::ingress_type::IngressType;
pub mod instance;
pub use self::instance::Instance;
pub mod instance_event;
Expand Down
6 changes: 6 additions & 0 deletions tembo-cli/temboclient/src/models/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Routing {
#[serde(rename = "entryPoints", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub entry_points: Option<Option<Vec<String>>>,
#[serde(rename = "ingressPath", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub ingress_path: Option<Option<String>>,
#[serde(rename = "ingressType", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub ingress_type: Option<Option<crate::models::IngressType>>,
#[serde(rename = "middlewares", default, with = "::serde_with::rust::double_option", skip_serializing_if = "Option::is_none")]
pub middlewares: Option<Option<Vec<String>>>,
#[serde(rename = "port")]
Expand All @@ -24,7 +28,9 @@ pub struct Routing {
impl Routing {
pub fn new(port: i32) -> Routing {
Routing {
entry_points: None,
ingress_path: None,
ingress_type: None,
middlewares: None,
port,
}
Expand Down
Loading

0 comments on commit f78529d

Please sign in to comment.