diff --git a/src/state.rs b/src/state.rs index a487410..c27ea00 100644 --- a/src/state.rs +++ b/src/state.rs @@ -38,6 +38,27 @@ pub struct Resources { } impl Resources { + // TODO: improve this to error out if ports are not available + // be careful with loops + // server must provide number of ports in the contract or fail + fn reserve_ports(&mut self, mut nr: u16, config: &Config) -> Vec { + use rand::Rng; + if nr > config.max_ports_per_vm { + nr = config.max_ports_per_vm; + } + let mut published_ports = Vec::new(); + for _ in 0..nr { + for _ in 0..5 { + let port = rand::thread_rng().gen_range(config.public_port_range.clone()); + if self.used_ports.insert(port) { + published_ports.push(port); + } + break; + } + } + Vec::new() + } + fn reserve_vm_if(&mut self) -> String { use rand::{distributions::Alphanumeric, Rng}; loop { @@ -74,8 +95,8 @@ impl Resources { }, }; let mut ips = Vec::new(); - let mask = range - .subnet + let mask = ip + .network() .to_string() .split('/') .next() @@ -115,8 +136,8 @@ impl Resources { }, }; let mut ips = Vec::new(); - let mask = range - .subnet + let mask = ip + .network() .to_string() .split('/') .next() @@ -183,6 +204,12 @@ pub struct VMNIC { ips: Vec, } +impl VMNIC { + fn new() -> VMNIC { + todo!("implement this here to improve code elegance of resource reservation"); + } +} + pub struct VM { uuid: String, hostname: String, @@ -267,7 +294,7 @@ impl VM { } else { vm_nics.push(vmnic); } - }, + } None => return Err(VMCreationErrors::IPv4NotAvailable), } }