Skip to content

Commit

Permalink
Merge pull request #364 from stakwork/fix/validate-instance-type
Browse files Browse the repository at this point in the history
Fix/validate instance type
  • Loading branch information
Evanfeenstra authored Oct 23, 2024
2 parents 177ef92 + 45aed48 commit 3b721e3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/bin/super/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion src/bin/super/superapp/src/Remotes.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
31 changes: 29 additions & 2 deletions src/bin/super/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,21 +577,35 @@ pub async fn create_swarm_ec2(
info: &CreateEc2InstanceInfo,
state: &mut Super,
) -> Result<(), Error> {
let mut actual_vanity_address: Option<String> = 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?;
Expand All @@ -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();
Expand Down Expand Up @@ -832,3 +846,16 @@ pub fn get_swarm_instance_type(
data: Some(value),
});
}

fn get_instance(instance_type: &str) -> Option<AwsInstanceType> {
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());
}

0 comments on commit 3b721e3

Please sign in to comment.