Refines machines

This commit is contained in:
Gregor Reitzenstein 2020-09-17 10:51:51 +02:00
parent 5bd5cd57df
commit 2686ea112f
3 changed files with 36 additions and 7 deletions

View File

@ -19,9 +19,9 @@ use crate::config::Settings;
use crate::error::Result; use crate::error::Result;
// FIXME: fabinfra/fabaccess/bffh#3 // FIXME: fabinfra/fabaccess/bffh#3
type UserIdentifier = u64; pub type UserIdentifier = u64;
type RoleIdentifier = u64; pub type RoleIdentifier = u64;
type PermIdentifier = u64; pub type PermIdentifier = u64;
pub struct PermissionsProvider { pub struct PermissionsProvider {
log: Logger, log: Logger,

View File

@ -11,6 +11,7 @@ use smol::lock::RwLock;
use crate::error::Result; use crate::error::Result;
use crate::config::Settings; use crate::config::Settings;
use crate::access;
use capnp::Error; use capnp::Error;
@ -26,6 +27,7 @@ pub type ID = Uuid;
/// Status of a Machine /// Status of a Machine
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)] #[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[repr(u8)]
pub enum Status { pub enum Status {
/// Not currently used by anybody /// Not currently used by anybody
Free, Free,
@ -100,11 +102,16 @@ impl MachineManager {
/// machine, checking that the user who wants the machine (de)activated has the required /// machine, checking that the user who wants the machine (de)activated has the required
/// permissions. /// permissions.
pub struct Machine { pub struct Machine {
/// Computer-readable identifier for this machine
// Implicit in database since it's the key.
#[serde(skip)]
id: ID,
/// The human-readable name of the machine. Does not need to be unique /// The human-readable name of the machine. Does not need to be unique
name: String, name: String,
/// The required permission to use this machine. /// The required permission to use this machine.
perm: String, perm: access::PermIdentifier,
/// 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.
/// ///
@ -114,8 +121,9 @@ pub struct Machine {
} }
impl Machine { impl Machine {
pub fn new(name: String, perm: String) -> Machine { pub fn new(id: Uuid, name: String, perm: access::PermIdentifier) -> Machine {
Machine { Machine {
id: id,
name: name, name: name,
perm: perm, perm: perm,
state: Mutable::new(Status::Free), state: Mutable::new(Status::Free),
@ -133,6 +141,24 @@ impl Machine {
pub fn signal(&self) -> impl Signal { pub fn signal(&self) -> impl Signal {
self.state.signal().dedupe() self.state.signal().dedupe()
} }
/// Requests to use a machine. Returns `true` if successful.
///
/// This will update the internal state of the machine, notifying connected actors, if any.
pub fn request_use<T: Transaction>
( &mut self
, txn: &T
, pp: &access::PermissionsProvider
, who: access::UserIdentifier
) -> Result<bool>
{
if pp.check(txn, who, self.perm)? {
self.state.set(Status::Occupied);
return Ok(true);
} else {
return Ok(false);
}
}
} }
pub struct MachineDB { pub struct MachineDB {
@ -149,7 +175,10 @@ impl MachineDB {
{ {
match txn.get(self.db, &uuid.as_bytes()) { match txn.get(self.db, &uuid.as_bytes()) {
Ok(bytes) => { Ok(bytes) => {
Ok(Some(flexbuffers::from_slice(bytes)?)) let mut machine: Machine = flexbuffers::from_slice(bytes)?;
machine.id = uuid;
Ok(Some(machine))
}, },
Err(lmdb::Error::NotFound) => { Ok(None) }, Err(lmdb::Error::NotFound) => { Ok(None) },
Err(e) => { Err(e.into()) }, Err(e) => { Err(e.into()) },

View File

@ -34,5 +34,5 @@ impl Network {
enum Event { enum Event {
/// An user wants to use a machine /// An user wants to use a machine
// TODO: Define /what/ an user wants to do with said machine? // TODO: Define /what/ an user wants to do with said machine?
MachineRequest(machine::ID, access::UserIdentifer), MachineRequest(machine::ID, access::UserIdentifier),
} }