Skip to content

Commit

Permalink
Add /v1/roles_data route
Browse files Browse the repository at this point in the history
  • Loading branch information
tailhook committed Jul 5, 2018
1 parent b1f1474 commit 09af3d8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
3 changes: 2 additions & 1 deletion frontend/pages/api_links.khufu
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ view main():
["schedule", "Schedule"],
["scheduler_input", "Scheduler Input"],
["election", "Election"],
["pending actions", "Pending Actions"],
["pending_actions", "Pending Actions"],
["roles_data", "Roles Data"],
]
for [uri, title, pretty] of items key uri:
<li.api-line>
Expand Down
20 changes: 19 additions & 1 deletion src/daemon/frontend/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use serde_json::{Value, to_writer, to_writer_pretty, to_value};
use serde_millis;
use tk_easyloop::timeout;
use tk_http::Status::{self, NotFound, PermanentRedirect};
use tk_http::Status::{TooManyRequests, ServiceUnavailable};
use tk_http::Status::{TooManyRequests, ServiceUnavailable, InternalServerError};
use tk_http::server::{Codec as CodecTrait};
use tk_http::server::{Encoder, EncoderDone, Error};

Expand Down Expand Up @@ -494,5 +494,23 @@ pub fn serve<S: 'static>(state: &SharedState, config: &Arc<Config>,
}))
}))
}
RolesData => {
Ok(reply(move |e| {
Box::new(state.get_responder().get_roles_data()
.then(move |res| match res {
Ok(Ok(v)) => {
respond(e, format, v)
}
Ok(Err(err)) => {
error!("Error receiving roles_data: {}", err);
error_page(InternalServerError, e)
}
Err(err) => {
error!("Error receiving roles_data: {}", err);
error_page(ServiceUnavailable, e)
}
}))
}))
}
}
}
2 changes: 2 additions & 0 deletions src/daemon/frontend/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum ApiRoute {
WaitAction,
ActionIsPending(u64),
PendingActions,
RolesData,
ForceRenderAll,
RedirectByNodeName,
}
Expand Down Expand Up @@ -139,6 +140,7 @@ fn parse_api(path: &str) -> Option<Route> {
}).ok()
}
("pending_actions", "") => Some(Api(PendingActions, api_suffix(path))),
("roles_data", "") => Some(Api(RolesData, api_suffix(path))),
("log", tail) => parse_log_route(tail).map(Log),
_ => None,
}
Expand Down
27 changes: 25 additions & 2 deletions src/daemon/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use std::path::PathBuf;
use async_slot as slot;
use failure::{Error, err_msg};
use futures::sync::mpsc::{unbounded, UnboundedSender, UnboundedReceiver};
use futures::Stream;
use futures::sync::oneshot;
use futures::{Future, Stream};
use rand::{thread_rng, Rng};
use rand::distributions::Alphanumeric;
use serde_json::Value as Json;
use void::Void;

use id::Id;
use apply::ApplyData;
Expand All @@ -26,14 +28,15 @@ pub enum Request {
// this one isn't actually send over channel, but we're using it for
// simplifying interface
NewSchedule(Arc<Schedule>),
RolesData(oneshot::Sender<Result<RolesResult, Error>>),
ForceRerender,
}

#[derive(Debug, Clone)]
pub struct Responder(Arc<Internal>);


#[derive(Debug, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct RolesResult {
to_render: BTreeMap<String, Json>,
all_roles: HashSet<String>,
Expand Down Expand Up @@ -94,6 +97,17 @@ impl Responder {
self.0.tx.unbounded_send(Request::ForceRerender)
.expect("responder channel works");
}
pub fn get_roles_data(&self)
-> impl Future<Item=Result<RolesResult, Error>, Error=Void>+Send
{
let (tx, rx) = oneshot::channel();
self.0.tx.unbounded_send(Request::RolesData(tx))
.expect("responder channel works");
return rx.map_err(|e| {
error!("responder lost the channel: {}", e);
unreachable!("responder lost the channel");
});
}
}

pub fn run(init: ResponderInit, shared: SharedState) {
Expand Down Expand Up @@ -178,6 +192,15 @@ pub fn run(init: ResponderInit, shared: SharedState) {
}
}
}
Request::RolesData(chan) => {
debug!("Incoming request RolesData");
// maybe store previous id?
let id: String = thread_rng().sample_iter(&Alphanumeric)
.take(24).collect();
chan.send(responder.render_roles(&id, None))
.map_err(|_| debug!("no receiver for RolesData"))
.ok();
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/daemon/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ impl SharedState {
return false;
}
}
pub fn get_responder(&self) -> Responder {
self.0.responder.clone()
}
pub fn force_render(&self) {
self.0.responder.force_rerender();
}
Expand Down

0 comments on commit 09af3d8

Please sign in to comment.