diff --git a/src/bin/super/state.rs b/src/bin/super/state.rs index 61081c7a..60cafa3e 100644 --- a/src/bin/super/state.rs +++ b/src/bin/super/state.rs @@ -25,7 +25,7 @@ pub struct RemoteStack { pub ec2_instance_id: String, } -#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Default)] +#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Default, Clone)] pub struct AwsInstanceType { pub name: String, pub value: String, diff --git a/src/bin/super/superapp/src/Remotes.svelte b/src/bin/super/superapp/src/Remotes.svelte index c05e2432..3713d640 100644 --- a/src/bin/super/superapp/src/Remotes.svelte +++ b/src/bin/super/superapp/src/Remotes.svelte @@ -473,10 +473,15 @@ async function handleSubmitCreateEc2() { isSubmitting = true; + + if (vanity_address) { + `${vanity_address}${domain}`; + } + try { const data = { name: `${name}${swarm_name_suffix}`, - vanity_address: `${vanity_address}${domain}`, + vanity_address, instance_type: selected_instance, }; diff --git a/src/bin/super/util.rs b/src/bin/super/util.rs index 0d6e5b37..07a22598 100644 --- a/src/bin/super/util.rs +++ b/src/bin/super/util.rs @@ -577,21 +577,35 @@ pub async fn create_swarm_ec2( info: &CreateEc2InstanceInfo, state: &mut Super, ) -> Result<(), Error> { + let mut actual_vanity_address: Option = None; + + let instance_type = get_instance(&info.instance_type); + + if instance_type.is_none() { + return Err(anyhow!("Invalid instance type")); + } + if let Some(vanity_address) = &info.vanity_address { if !vanity_address.is_empty() { if let Some(subdomain) = vanity_address.strip_suffix(".sphinx.chat") { + if subdomain.is_empty() { + return Err(anyhow!("Provide a valid vanity address")); + } + let domain_status = is_valid_domain(subdomain.to_string()); if !domain_status.is_empty() { return Err(anyhow!(domain_status)); } + actual_vanity_address = Some(vanity_address.to_string()); } else { return Err(anyhow!("Vanity Address doesn't match the expected format.")); } } } + let ec2_intance = create_ec2_instance( info.name.clone(), - info.vanity_address.clone(), + actual_vanity_address.clone(), info.instance_type.clone(), ) .await?; @@ -606,7 +620,7 @@ pub async fn create_swarm_ec2( let mut host = default_host.clone(); - if let Some(custom_domain) = &info.vanity_address { + if let Some(custom_domain) = &actual_vanity_address { log::info!("vanity address is being set"); if !custom_domain.is_empty() { host = custom_domain.clone(); @@ -832,3 +846,16 @@ pub fn get_swarm_instance_type( data: Some(value), }); } + +fn get_instance(instance_type: &str) -> Option { + let instance_types = instance_types(); + let postion = instance_types + .iter() + .position(|instance| instance.value == instance_type); + + if let None = postion { + return None; + } + + return Some(instance_types[postion.unwrap()].clone()); +}