Get an example to work — barely.

This commit is contained in:
Gregor Reitzenstein 2020-11-20 15:43:03 +01:00
parent e7bbc7e001
commit cbb6242f89
9 changed files with 71 additions and 13 deletions

12
examples/README.md Normal file
View File

@ -0,0 +1,12 @@
# API-Testsetup
wirklich nur um das API zu testen. ATM implementiert: machine::read
1. `cargo run -- --print-default > /tmp/bffh.toml` um eine default config zu generieren
1. in /tmp/bffh.toml den parameter `machines` auf ./examples/machines.toml umbiegen
* Bei mir z.b. `~/Development/FabInfra/Diflouroborane/examples/machines.toml`
1. Ein mosquitto o.ä MQTT Server starten
* Bringt aber leider gerade nicht viel ^^'
1. `cargo run -- -c /tmp/bffh.toml`
1. ???
1. PROFIT!

View File

@ -55,10 +55,8 @@ impl connection_capnp::bootstrap::Server for Bootstrap {
mut res: Results<machines_results::Owned> mut res: Results<machines_results::Owned>
) -> Promise<(), capnp::Error> { ) -> Promise<(), capnp::Error> {
// TODO actual permission check and stuff // TODO actual permission check and stuff
if self.session.user.is_some() { let c = capnp_rpc::new_client(Machines::new(self.session.clone(), self.db.clone()));
let c = capnp_rpc::new_client(Machines::new(self.session.clone(), self.db.clone())); res.get().set_machines(c);
res.get().set_machines(c);
}
Promise::ok(()) Promise::ok(())
} }

View File

@ -139,6 +139,7 @@ impl auth_capnp::authentication::Server for Auth {
}; };
// The step may either return an error, a success or the need for more data // The step may either return an error, a success or the need for more data
// TODO: Set the session user. Needs a lookup though <.>
match step_res { match step_res {
Ok(Step::Done(b)) => { Ok(Step::Done(b)) => {
use auth_capnp::response::Result; use auth_capnp::response::Result;

View File

@ -3,10 +3,12 @@ use std::sync::Arc;
use capnp::capability::Promise; use capnp::capability::Promise;
use capnp::Error; use capnp::Error;
use crate::schema::api_capnp::State;
use crate::schema::api_capnp::machine::*; use crate::schema::api_capnp::machine::*;
use crate::db::machine::MachineIdentifier; use crate::db::machine::MachineIdentifier;
use crate::connection::Session; use crate::connection::Session;
use crate::db::Databases; use crate::db::Databases;
use crate::db::machine::Status;
#[derive(Clone)] #[derive(Clone)]
pub struct Machine { pub struct Machine {
@ -19,6 +21,42 @@ impl Machine {
pub fn new(session: Arc<Session>, id: MachineIdentifier, db: Databases) -> Self { pub fn new(session: Arc<Session>, id: MachineIdentifier, db: Databases) -> Self {
Machine { session, id, db } Machine { session, id, db }
} }
pub fn fill(self: Arc<Self>, builder: &mut Builder) {
// TODO check permissions
builder.set_read(capnp_rpc::new_client(Read(self.clone())));
// TODO set all the others
}
pub fn fill_info(&self, builder: &mut m_info::Builder) {
if let Some(desc) = self.db.machine.get_desc(&self.id) {
builder.set_name(&desc.name);
if let Some(d) = desc.description.as_ref() {
builder.set_description(d);
}
// TODO: Set `responsible`
// TODO: Error Handling
if let Some(state) = self.db.machine.get_state(&self.id) {
match state.state {
Status::Free => builder.set_state(State::Free),
Status::InUse(_u) => {
builder.set_state(State::InUse);
}
Status::ToCheck(_u) => {
builder.set_state(State::ToCheck);
}
Status::Blocked(_u) => {
builder.set_state(State::Blocked);
}
Status::Disabled => builder.set_state(State::Disabled),
Status::Reserved(_u) => {
builder.set_state(State::Reserved);
}
}
}
}
}
} }
struct Read(Arc<Machine>); struct Read(Arc<Machine>);
@ -26,10 +64,12 @@ struct Read(Arc<Machine>);
impl read::Server for Read { impl read::Server for Read {
fn info(&mut self, fn info(&mut self,
_params: read::InfoParams, _params: read::InfoParams,
_results: read::InfoResults) mut results: read::InfoResults)
-> Promise<(), Error> -> Promise<(), Error>
{ {
unimplemented!() let mut b = results.get().init_minfo();
self.0.fill_info(&mut b);
Promise::ok(())
} }
} }

View File

@ -47,11 +47,16 @@ impl machines::Server for Machines {
if let Ok(api_id) = reader.get_uuid() { if let Ok(api_id) = reader.get_uuid() {
let id = uuid_from_api(api_id); let id = uuid_from_api(api_id);
if self.db.machine.exists(id) { if self.db.machine.exists(id) {
debug!(self.session.log, "Accessing machine {}", id);
// TODO check disclose permission // TODO check disclose permission
let builder = results.get().init_machine(); let mut builder = results.get().init_machine();
let m = Machine::new(self.session.clone(), id, self.db.clone()); let m = Machine::new(self.session.clone(), id, self.db.clone());
Machine::fill(Arc::new(m), &mut builder);
} else {
debug!(self.session.log, "Client requested nonexisting machine {}", id);
} }
} }
Promise::ok(()) Promise::ok(())

View File

@ -24,9 +24,9 @@ pub fn read(path: &Path) -> Result<Settings> {
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Settings { pub struct Settings {
pub machines: PathBuf,
pub listens: Box<[Listen]>, pub listens: Box<[Listen]>,
pub shelly: Option<ShellyCfg>, pub shelly: Option<ShellyCfg>,
pub machines: PathBuf,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]

View File

@ -63,7 +63,7 @@ pub fn uuid_from_api(uuid: crate::schema::api_capnp::u_u_i_d::Reader) -> Uuid {
Uuid::from_u128(num) Uuid::from_u128(num)
} }
pub fn api_from_uuid(uuid: Uuid, mut wr: crate::schema::api_capnp::u_u_i_d::Builder) { pub fn api_from_uuid(uuid: Uuid, mut wr: crate::schema::api_capnp::u_u_i_d::Builder) {
let num = uuid.to_u128_le(); let num = uuid.as_u128();
let uuid0 = num as u64; let uuid0 = num as u64;
let uuid1 = (num >> 64) as u64; let uuid1 = (num >> 64) as u64;
wr.set_uuid0(uuid0); wr.set_uuid0(uuid0);
@ -88,6 +88,7 @@ pub fn init(log: Logger, config: &Settings, env: Arc<lmdb::Environment>) -> Resu
type MachMap = HashMap<MachineIdentifier, MachineDescription>; type MachMap = HashMap<MachineIdentifier, MachineDescription>;
#[derive(Debug)]
pub struct MachineDB { pub struct MachineDB {
state_db: Internal, state_db: Internal,
def_db: MachMap, def_db: MachMap,

View File

@ -24,10 +24,10 @@ use crate::db::machine::{MachineIdentifier, Status, MachineState};
/// permissions. /// permissions.
pub struct Machine { pub struct Machine {
/// Globally unique machine readable identifier /// Globally unique machine readable identifier
id: MachineIdentifier, pub id: MachineIdentifier,
/// Descriptor of the machine /// Descriptor of the machine
desc: MachineDescription, pub desc: MachineDescription,
/// The state of the machine as bffh thinks the machine *should* be in. /// The state of the machine as bffh thinks the machine *should* be in.
/// ///
@ -87,9 +87,9 @@ impl Machine {
/// Combining this with the actual state of the system will return a machine /// Combining this with the actual state of the system will return a machine
pub struct MachineDescription { pub struct MachineDescription {
/// The name of the machine. Doesn't need to be unique but is what humans will be presented. /// The name of the machine. Doesn't need to be unique but is what humans will be presented.
name: String, pub name: String,
/// An optional description of the Machine. /// An optional description of the Machine.
description: Option<String>, pub description: Option<String>,
/// The permission required /// The permission required
#[serde(flatten)] #[serde(flatten)]

View File

@ -212,6 +212,7 @@ fn main() -> Result<(), Error> {
let mdb = mdb?; let mdb = mdb?;
let defs = machine::MachineDescription::load_file(&config.machines)?; let defs = machine::MachineDescription::load_file(&config.machines)?;
let machdb = db::machine::MachineDB::new(mdb, defs); let machdb = db::machine::MachineDB::new(mdb, defs);
info!(log, "{:?}", machdb);
let pdb = pdb?; let pdb = pdb?;
let mut ac = db::access::AccessControl::new(); let mut ac = db::access::AccessControl::new();
ac.add_source_unchecked("Internal".to_string(), Box::new(pdb)); ac.add_source_unchecked("Internal".to_string(), Box::new(pdb));