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>
) -> Promise<(), capnp::Error> {
// 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()));
res.get().set_machines(c);
}
let c = capnp_rpc::new_client(Machines::new(self.session.clone(), self.db.clone()));
res.get().set_machines(c);
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
// TODO: Set the session user. Needs a lookup though <.>
match step_res {
Ok(Step::Done(b)) => {
use auth_capnp::response::Result;

View File

@ -3,10 +3,12 @@ use std::sync::Arc;
use capnp::capability::Promise;
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;
#[derive(Clone)]
pub struct Machine {
@ -19,6 +21,42 @@ impl Machine {
pub fn new(session: Arc<Session>, id: MachineIdentifier, db: Databases) -> Self {
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>);
@ -26,10 +64,12 @@ struct Read(Arc<Machine>);
impl read::Server for Read {
fn info(&mut self,
_params: read::InfoParams,
_results: read::InfoResults)
mut results: read::InfoResults)
-> 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() {
let id = uuid_from_api(api_id);
if self.db.machine.exists(id) {
debug!(self.session.log, "Accessing machine {}", id);
// 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());
Machine::fill(Arc::new(m), &mut builder);
} else {
debug!(self.session.log, "Client requested nonexisting machine {}", id);
}
}
Promise::ok(())

View File

@ -24,9 +24,9 @@ pub fn read(path: &Path) -> Result<Settings> {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Settings {
pub machines: PathBuf,
pub listens: Box<[Listen]>,
pub shelly: Option<ShellyCfg>,
pub machines: PathBuf,
}
#[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)
}
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 uuid1 = (num >> 64) as u64;
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>;
#[derive(Debug)]
pub struct MachineDB {
state_db: Internal,
def_db: MachMap,

View File

@ -24,10 +24,10 @@ use crate::db::machine::{MachineIdentifier, Status, MachineState};
/// permissions.
pub struct Machine {
/// Globally unique machine readable identifier
id: MachineIdentifier,
pub id: MachineIdentifier,
/// Descriptor of the machine
desc: MachineDescription,
pub desc: MachineDescription,
/// 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
pub struct MachineDescription {
/// 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.
description: Option<String>,
pub description: Option<String>,
/// The permission required
#[serde(flatten)]

View File

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