From 41d2db1b63641d7d2cfc9db80e41e8437f6c96b3 Mon Sep 17 00:00:00 2001 From: "Carpenter, Adam (CORP)" Date: Thu, 13 Jan 2022 13:53:26 -0500 Subject: feat: support station busyout/release toggling via extension Introduces bulk station busyout/release toggle endpoints via the `simple_busy` extension. Bumps `angelsharkd` version. Feature is documented in README. Removes TODO in `simple_deprov` README. --- angelsharkd/Cargo.toml | 3 +- angelsharkd/src/routes/extensions/mod.rs | 8 ++ .../src/routes/extensions/simple_busy/README.md | 50 +++++++++++ .../src/routes/extensions/simple_busy/mod.rs | 98 ++++++++++++++++++++++ .../src/routes/extensions/simple_deprov/README.md | 2 +- 5 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 angelsharkd/src/routes/extensions/simple_busy/README.md create mode 100644 angelsharkd/src/routes/extensions/simple_busy/mod.rs (limited to 'angelsharkd') diff --git a/angelsharkd/Cargo.toml b/angelsharkd/Cargo.toml index 0bb1294..62ffebb 100644 --- a/angelsharkd/Cargo.toml +++ b/angelsharkd/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "angelsharkd" -version = "0.2.1" +version = "0.2.2" edition = "2021" authors = ["Adam T. Carpenter "] description = "A HTTP interface into one or more Communication Managers" @@ -8,6 +8,7 @@ description = "A HTTP interface into one or more Communication Managers" [features] simple_search = [] simple_deprov = [] +simple_busy = [] [dependencies.libangelshark] path = "../libangelshark" diff --git a/angelsharkd/src/routes/extensions/mod.rs b/angelsharkd/src/routes/extensions/mod.rs index 54053af..a775880 100644 --- a/angelsharkd/src/routes/extensions/mod.rs +++ b/angelsharkd/src/routes/extensions/mod.rs @@ -1,6 +1,8 @@ use crate::config::Config; use warp::{path, Filter, Rejection, Reply}; +#[cfg(feature = "simple_busy")] +mod simple_busy; #[cfg(feature = "simple_deprov")] mod simple_deprov; #[cfg(feature = "simple_search")] @@ -26,6 +28,12 @@ pub fn filter(config: &Config) -> impl Filter impl Filter + Clone { + post() + .and(path!("service" / "busyout" / ..)) + .and(content_length_limit(SIXTEEN_K)) + .and(json()) + .map(move |entries| queue_and_run(entries, "busyout", runner.to_owned())) +} + +pub fn release_filter( + runner: AcmRunner, +) -> impl Filter + Clone { + post() + .and(path!("service" / "release" / ..)) + .and(content_length_limit(SIXTEEN_K)) + .and(json()) + .map(move |entries| queue_and_run(entries, "release", runner.to_owned())) +} + +pub fn toggle_filter( + runner: AcmRunner, +) -> impl Filter + Clone { + post() + .and(path!("service" / "toggle" / ..)) + .and(content_length_limit(SIXTEEN_K)) + .and(json()) + .map(move |entries| queue_and_run(entries, "toggle", runner.to_owned())) +} + +fn queue_and_run(entries: Entries, command: &str, mut runner: AcmRunner) -> impl Reply { + for entry in entries.into_iter() { + if command == "toggle" { + runner.queue_input( + &entry.acm, + &Message::new(&format!("busyout station {}", entry.ext)), + ); + runner.queue_input( + &entry.acm, + &Message::new(&format!("release station {}", entry.ext)), + ); + } else { + runner.queue_input( + &entry.acm, + &Message::new(&format!("{} station {}", command, entry.ext)), + ); + } + } + + // generate output on runner + let output: Result>, _> = runner + .run() + .map(|(name, output)| -> Result, anyhow::Error> { + Ok(output? + .into_iter() + .filter_map(move |msg| { + (msg.command != "logoff").then(|| Response::from((name.to_owned(), msg))) + }) + .collect()) + }) + .collect(); + + // handle errors and package output as json + match output { + Err(e) => { + error!("busyout-release extension: {}", e); + reply::with_status( + reply::json(&Error { + reason: e.to_string(), + }), + StatusCode::INTERNAL_SERVER_ERROR, + ) + } + Ok(r) => reply::with_status( + reply::json(&r.into_iter().flatten().collect::>()), + StatusCode::OK, + ), + } +} + +type Entries = Vec; +#[derive(Debug, Deserialize)] +struct Entry { + acm: String, + ext: String, +} diff --git a/angelsharkd/src/routes/extensions/simple_deprov/README.md b/angelsharkd/src/routes/extensions/simple_deprov/README.md index 4900075..f7a3fe2 100644 --- a/angelsharkd/src/routes/extensions/simple_deprov/README.md +++ b/angelsharkd/src/routes/extensions/simple_deprov/README.md @@ -16,7 +16,7 @@ cargo build --bin angelsharkd --features simple_deprov ... ## `POST /extensions/deprov` Remove Objects -The request type is TODO: +The request type is as follows: ```json POST /extensions/deprov -- cgit v1.2.3