From cca5bddbb8dd5cb74809760ae5b25005626b9b87 Mon Sep 17 00:00:00 2001 From: Gregor Reitzenstein Date: Wed, 16 Dec 2020 11:32:31 +0100 Subject: [PATCH] Implement parts of the Machine API --- src/api/machine.rs | 8 ++++---- src/api/machines.rs | 23 ++++++++++++++++++++++- src/network.rs | 2 +- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/api/machine.rs b/src/api/machine.rs index 1cdeb10..db689ac 100644 --- a/src/api/machine.rs +++ b/src/api/machine.rs @@ -5,21 +5,21 @@ use capnp::Error; use crate::schema::api_capnp::State; use crate::schema::api_capnp::machine::*; -use crate::db::machine::MachineIdentifier; use crate::connection::Session; use crate::db::Databases; use crate::db::machine::Status; +use crate::machine::Machine as NwMachine; #[derive(Clone)] pub struct Machine { session: Arc, - id: MachineIdentifier, + machine: NwMachine, db: Databases, } impl Machine { - pub fn new(session: Arc, id: MachineIdentifier, db: Databases) -> Self { - Machine { session, id, db } + pub fn new(session: Arc, machine: NwMachine, db: Databases) -> Self { + Machine { session, machine, db } } pub fn fill(self: Arc, builder: &mut Builder) { diff --git a/src/api/machines.rs b/src/api/machines.rs index 76220c2..15ddf28 100644 --- a/src/api/machines.rs +++ b/src/api/machines.rs @@ -36,6 +36,19 @@ impl machines::Server for Machines { mut results: machines::ListMachinesResults) -> Promise<(), Error> { + let v: Vec<(String, crate::machine::Machine)> = self.network.machines.iter() + .map(|(n, m)| (n.clone(), m.clone())) + .collect(); + + let mut res = results.get(); + let mut machines = res.init_machines(v.len() as u32); + + for (i, (name, machine)) in v.into_iter().enumerate() { + let machine = Arc::new(Machine::new(self.session.clone(), machine, self.db.clone())); + let mut builder = machines.reborrow().get(i as u32); + Machine::fill(machine, &mut builder); + } + Promise::ok(()) } @@ -44,6 +57,14 @@ impl machines::Server for Machines { mut results: machines::GetMachineResults) -> Promise<(), Error> { - unimplemented!() + if let Ok(uid) = params.get().and_then(|x| x.get_uid()) { + if let Some(machine_inner) = self.network.machines.get(uid) { + let machine = Arc::new(Machine::new(self.session.clone(), machine_inner.clone(), self.db.clone())); + let mut builder = results.get().init_machine(); + Machine::fill(machine, &mut builder); + } + } + + Promise::ok(()) } } diff --git a/src/network.rs b/src/network.rs index 65f8403..a962e20 100644 --- a/src/network.rs +++ b/src/network.rs @@ -46,7 +46,7 @@ pub struct Network { // Store connections //miconn: Vec<(String, String)>, - machines: MachineMap, + pub machines: MachineMap, // Store connections //maconn: Vec<(String, String)>,