diff --git a/src/api/machines.rs b/src/api/machines.rs index 95ebaf3..d251ba5 100644 --- a/src/api/machines.rs +++ b/src/api/machines.rs @@ -199,4 +199,90 @@ impl machines::Server for Machines { Promise::ok(()) } } + + fn get_machine_u_r_n( + &mut self, + params: machines::GetMachineURNParams, + mut results: machines::GetMachineURNResults + ) -> Promise<(), capnp::Error> { + let rc = Rc::clone(&self.session); + if self.session.borrow().is_some() { + let urn = { + let params = pry!(params.get()); + pry!(params.get_urn()) + }; + + let mut parts = urn.split_terminator(':'); + let part_urn = parts.next().map(|u| u == "urn").unwrap_or(false); + let part_fabaccess = parts.next().map(|f| f == "fabaccess").unwrap_or(false); + let part_resource = parts.next().map(|r| r == "resource").unwrap_or(false); + if !(part_urn && part_fabaccess && part_resource) { + return Promise::ok(()) + } + + if let Some(name) = parts.next() { + let name = name.to_string(); + let network = self.network.clone(); + let f = async move { + let session = rc.borrow(); + let user = &session.as_ref().unwrap().authzid; + let permissions = &session.as_ref().unwrap().perms; + + if let Some(machine) = network.machines.get(&name) { + let mut builder = results.get().init_machine(); + let perms = Perms::get_for(&machine.desc.privs, permissions.iter()); + builder.set_name(&name); + if let Some(ref desc) = machine.desc.description { + builder.set_description(desc); + } + if let Some(ref wiki) = machine.desc.wiki { + builder.set_wiki(wiki); + } + builder.set_urn(&format!("urn:fabaccess:resource:{}", &name)); + + let machineapi = Machine::new(user.clone(), perms, machine.clone()); + let state = machine.get_status().await; + if perms.write && state == Status::Free { + builder.set_use(capnp_rpc::new_client(machineapi.clone())); + } + if perms.manage { + //builder.set_transfer(capnp_rpc::new_client(machineapi.clone())); + //builder.set_check(capnp_rpc::new_client(machineapi.clone())); + builder.set_manage(capnp_rpc::new_client(machineapi.clone())); + } + if permissions.iter().any(|r| r.match_perm(&admin_perm())) { + builder.set_admin(capnp_rpc::new_client(machineapi.clone())); + } + + + let s = match machine.get_status().await { + Status::Free => MachineState::Free, + Status::Disabled => MachineState::Disabled, + Status::Blocked(_) => MachineState::Blocked, + Status::InUse(u) => { + if let Some(owner) = u.as_ref() { + if owner == user { + builder.set_inuse(capnp_rpc::new_client(machineapi.clone())); + } + } + MachineState::InUse + }, + Status::Reserved(_) => MachineState::Reserved, + Status::ToCheck(_) => MachineState::ToCheck, + }; + builder.set_state(s); + + builder.set_info(capnp_rpc::new_client(machineapi)); + }; + + Ok(()) + }; + Promise::from_future(f) + } else { + Promise::ok(()) + } + } else { + Promise::ok(()) + } + } }