From 64a84c48befc680eb2f4b77d4b258304168b6fcd Mon Sep 17 00:00:00 2001 From: ghe0 Date: Wed, 11 Dec 2024 20:56:03 +0200 Subject: [PATCH] added new vm requests --- src/main.rs | 29 +++++++++++++++++++---------- src/state.rs | 10 ++++++++++ test_data/new_vm_req1.yaml | 15 +++++++++++++++ test_data/new_vm_req2.yaml | 14 ++++++++++++++ test_data/new_vm_req3.yaml | 19 +++++++++++++++++++ test_data/new_vm_req4.yaml | 14 ++++++++++++++ 6 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 test_data/new_vm_req1.yaml create mode 100644 test_data/new_vm_req2.yaml create mode 100644 test_data/new_vm_req3.yaml create mode 100644 test_data/new_vm_req4.yaml diff --git a/src/main.rs b/src/main.rs index b769b19..19d9eec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,17 +4,26 @@ mod constants; mod tcontract; use crate::config::Config; +use crate::state::NewVMRequest; fn main() -> Result<(), Box> { - let config = Config::from_file("test_data/config1.yaml")?; - println!("{:#?}", config); - let config = Config::from_file("test_data/config2.yaml")?; - println!("{:#?}", config); - let config = Config::from_file("test_data/config3.yaml")?; - println!("{:#?}", config); - let config = Config::from_file("test_data/config4.yaml")?; - println!("{:#?}", config); - let config = Config::from_file("test_data/config5.yaml")?; - println!("{:#?}", config); + // let config = Config::from_file("test_data/config1.yaml")?; + // println!("{:#?}", config); + // let config = Config::from_file("test_data/config2.yaml")?; + // println!("{:#?}", config); + // let config = Config::from_file("test_data/config3.yaml")?; + // println!("{:#?}", config); + // let config = Config::from_file("test_data/config4.yaml")?; + // println!("{:#?}", config); + // let config = Config::from_file("test_data/config5.yaml")?; + // println!("{:#?}", config); + let new_vm_req = NewVMRequest::from_file("test_data/new_vm_req1.yaml")?; + println!("{:#?}", new_vm_req); + let new_vm_req = NewVMRequest::from_file("test_data/new_vm_req2.yaml")?; + println!("{:#?}", new_vm_req); + let new_vm_req = NewVMRequest::from_file("test_data/new_vm_req3.yaml")?; + println!("{:#?}", new_vm_req); + let new_vm_req = NewVMRequest::from_file("test_data/new_vm_req4.yaml")?; + println!("{:#?}", new_vm_req); Ok(()) } diff --git a/src/state.rs b/src/state.rs index 2cbe1c9..ba87053 100644 --- a/src/state.rs +++ b/src/state.rs @@ -12,6 +12,7 @@ use std::io::Read; use std::io::Write; use std::path::Path; use std::process::Command; +use serde::Deserialize; pub struct Resources { // QEMU does not support MHz limiation @@ -263,6 +264,7 @@ pub struct VM { dtrfs_sha: String, } +#[derive(Deserialize, Debug)] pub struct NewVMRequest { uuid: String, hostname: String, @@ -279,6 +281,14 @@ pub struct NewVMRequest { dtrfs_sha: String, } +impl NewVMRequest { + pub fn from_file(path: &str) -> Result> { + let content = std::fs::read_to_string(path)?; + let request: NewVMRequest = serde_yaml::from_str(&content)?; + Ok(request) + } +} + pub enum VMCreationErrors { NATandIPv4Conflict, TooManyCores, diff --git a/test_data/new_vm_req1.yaml b/test_data/new_vm_req1.yaml new file mode 100644 index 0000000..0494514 --- /dev/null +++ b/test_data/new_vm_req1.yaml @@ -0,0 +1,15 @@ +uuid: "123e4567-e89b-12d3-a456-426614174000" +hostname: "test-vm-01" +admin_key: "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArandomkeyexample" +extra_ports: + - 8080 + - 8443 +public_ipv4: true +public_ipv6: false +disk_size: 50 +vcpus: 4 +memory: 8192 +kernel_url: "http://example.com/kernel" +kernel_sha: "abc123def4567890ghij" +dtrfs_url: "http://example.com/dtrfs" +dtrfs_sha: "xyz9876543210mnop" diff --git a/test_data/new_vm_req2.yaml b/test_data/new_vm_req2.yaml new file mode 100644 index 0000000..6d34574 --- /dev/null +++ b/test_data/new_vm_req2.yaml @@ -0,0 +1,14 @@ +uuid: "987e6543-e21b-43d3-c321-654987210000" +hostname: "minimal-vm" +admin_key: "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQAnotherExampleKey" +extra_ports: [] +public_ipv4: false +public_ipv6: false +disk_size: 10 +vcpus: 1 +memory: 2048 +kernel_url: "http://minimal.com/kernel" +kernel_sha: "minimalsha123" +dtrfs_url: "http://minimal.com/dtrfs" +dtrfs_sha: "dtrfssha456" + diff --git a/test_data/new_vm_req3.yaml b/test_data/new_vm_req3.yaml new file mode 100644 index 0000000..706cc9d --- /dev/null +++ b/test_data/new_vm_req3.yaml @@ -0,0 +1,19 @@ +uuid: "246e1357-e98b-76d3-f345-129874650000" +hostname: "extensive-vm" +admin_key: "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEExtendedKeyExample" +extra_ports: + - 80 + - 443 + - 3000 + - 5000 + - 6000 +public_ipv4: true +public_ipv6: true +disk_size: 200 +vcpus: 16 +memory: 65536 +kernel_url: "http://large.com/kernel" +kernel_sha: "largekernelsha" +dtrfs_url: "http://large.com/dtrfs" +dtrfs_sha: "largedtrfssha" + diff --git a/test_data/new_vm_req4.yaml b/test_data/new_vm_req4.yaml new file mode 100644 index 0000000..0ea6697 --- /dev/null +++ b/test_data/new_vm_req4.yaml @@ -0,0 +1,14 @@ +uuid: "default-uuid" +hostname: "testing-vm" +admin_key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAAKeyExampleForTesting" +extra_ports: [1234, 5678] +public_ipv4: false +public_ipv6: true +disk_size: 25 +vcpus: 2 +memory: 4096 +kernel_url: "http://testing.com/kernel" +kernel_sha: "testkernelsha" +dtrfs_url: "http://testing.com/dtrfs" +dtrfs_sha: "testdtrfssha" +