2020-11-17 14:28:04 +01:00
|
|
|
use std::sync::Arc;
|
2020-11-17 14:15:29 +01:00
|
|
|
|
|
|
|
use capnp::capability::Promise;
|
|
|
|
use capnp::Error;
|
|
|
|
|
2020-11-17 14:28:04 +01:00
|
|
|
use crate::schema::api_capnp::machines;
|
|
|
|
use crate::connection::Session;
|
|
|
|
|
2020-11-20 13:06:55 +01:00
|
|
|
use crate::db::Databases;
|
|
|
|
use crate::db::machine::uuid_from_api;
|
|
|
|
use crate::db::machine::MachineDB;
|
|
|
|
|
|
|
|
use super::machine::Machine;
|
|
|
|
|
2020-11-17 14:28:04 +01:00
|
|
|
/// An implementation of the `Machines` API
|
2020-11-17 14:35:16 +01:00
|
|
|
pub struct Machines {
|
2020-11-17 14:28:04 +01:00
|
|
|
/// A reference to the connection — as long as at least one API endpoint is
|
|
|
|
/// still alive the session has to be as well.
|
|
|
|
session: Arc<Session>,
|
2020-11-20 13:06:55 +01:00
|
|
|
|
|
|
|
db: Databases,
|
2020-11-17 14:28:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Machines {
|
2020-11-20 13:06:55 +01:00
|
|
|
pub fn new(session: Arc<Session>, db: Databases) -> Self {
|
2020-11-19 14:53:14 +01:00
|
|
|
info!(session.log, "Machines created");
|
2020-11-20 13:06:55 +01:00
|
|
|
Self { session, db }
|
2020-11-17 14:28:04 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-17 14:15:29 +01:00
|
|
|
|
|
|
|
impl machines::Server for Machines {
|
|
|
|
fn list_machines(&mut self,
|
|
|
|
_params: machines::ListMachinesParams,
|
|
|
|
mut results: machines::ListMachinesResults)
|
|
|
|
-> Promise<(), Error>
|
|
|
|
{
|
|
|
|
Promise::ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_machine(&mut self,
|
2020-11-20 13:06:55 +01:00
|
|
|
params: machines::GetMachineParams,
|
2020-11-17 14:15:29 +01:00
|
|
|
mut results: machines::GetMachineResults)
|
|
|
|
-> Promise<(), Error>
|
|
|
|
{
|
2020-11-20 13:06:55 +01:00
|
|
|
match params.get() {
|
|
|
|
Ok(reader) => {
|
|
|
|
if let Ok(api_id) = reader.get_uuid() {
|
|
|
|
let id = uuid_from_api(api_id);
|
|
|
|
if self.db.machine.exists(id) {
|
|
|
|
// TODO check disclose permission
|
|
|
|
|
|
|
|
let builder = results.get().init_machine();
|
|
|
|
|
|
|
|
let m = Machine::new(self.session.clone(), id, self.db.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Promise::ok(())
|
|
|
|
}
|
|
|
|
Err(e) => Promise::err(e),
|
|
|
|
}
|
2020-11-17 14:15:29 +01:00
|
|
|
}
|
|
|
|
}
|