Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

origin api #682

Merged
merged 1 commit into from
Jun 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions components/builder-api/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

136 changes: 134 additions & 2 deletions components/builder-api/src/http/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use iron::headers::{Authorization, Bearer};
use protobuf;
use protocol::jobsrv::{Job, JobCreate, JobGet};
use protocol::sessionsrv::{OAuthProvider, Session, SessionCreate, SessionGet};
use protocol::vault::{Origin, OriginCreate, OriginGet};
use protocol::vault::*;
use protocol::net::{self, NetError, ErrCode};
use router::Router;
use rustc_serialize::json::{self, ToJson};
Expand Down Expand Up @@ -47,6 +47,7 @@ pub fn authenticate(req: &mut Request,
let err: NetError = protobuf::parse_from_bytes(rep.get_body()).unwrap();
Err(render_net_error(&err))
}

_ => unreachable!("unexpected msg: {:?}", rep),
}
}
Expand Down Expand Up @@ -78,7 +79,7 @@ pub fn session_create(req: &mut Request,
request.set_token(token);
request.set_extern_id(user.id);
request.set_email(user.email);
request.set_name(user.name);
request.set_name(user.login);
request.set_provider(OAuthProvider::GitHub);
conn.route(&request).unwrap();
match conn.recv() {
Expand Down Expand Up @@ -296,3 +297,134 @@ fn render_net_error(err: &NetError) -> Response {
};
Response::with((status, encoded))
}

pub fn list_account_invitations(req: &mut Request,
ctx: &Arc<Mutex<zmq::Context>>)
-> IronResult<Response> {
debug!("list_account_invitations");
let session = match authenticate(req, ctx) {
Ok(session) => session,
Err(response) => return Ok(response),
};

let mut conn = Broker::connect(&ctx).unwrap();
let mut request = AccountInvitationListRequest::new();
request.set_account_id(session.get_id());
conn.route(&request).unwrap();
match conn.recv() {
Ok(rep) => {
match rep.get_message_id() {
"AccountInvitationListResponse" => {
let invites: AccountInvitationListResponse =
protobuf::parse_from_bytes(rep.get_body()).unwrap();
let encoded = json::encode(&invites.to_json()).unwrap();
Ok(Response::with((status::Ok, encoded)))
}
"NetError" => {
let err: NetError = protobuf::parse_from_bytes(rep.get_body()).unwrap();
Ok(render_net_error(&err))
}
_ => unreachable!("unexpected msg: {:?}", rep),
}
}
Err(e) => {
error!("{:?}", e);
Ok(Response::with(status::ServiceUnavailable))
}
}
}

pub fn list_user_origins(req: &mut Request,
ctx: &Arc<Mutex<zmq::Context>>)
-> IronResult<Response> {
debug!("list_user_origins");
let session = match authenticate(req, ctx) {
Ok(session) => session,
Err(response) => return Ok(response),
};

let mut conn = Broker::connect(&ctx).unwrap();


let mut request = AccountOriginListRequest::new();
request.set_account_id(session.get_id());
conn.route(&request).unwrap();
match conn.recv() {
Ok(rep) => {
match rep.get_message_id() {
"AccountOriginListResponse" => {
let invites: AccountOriginListResponse =
protobuf::parse_from_bytes(rep.get_body()).unwrap();
let encoded = json::encode(&invites.to_json()).unwrap();
Ok(Response::with((status::Ok, encoded)))
}
"NetError" => {
let err: NetError = protobuf::parse_from_bytes(rep.get_body()).unwrap();
Ok(render_net_error(&err))
}
_ => unreachable!("unexpected msg: {:?}", rep),
}
}
Err(e) => {
error!("{:?}", e);
Ok(Response::with(status::ServiceUnavailable))
}
}
}



pub fn accept_invitation(req: &mut Request,
ctx: &Arc<Mutex<zmq::Context>>)
-> IronResult<Response> {
debug!("accept_invitation");
let session = match authenticate(req, ctx) {
Ok(session) => session,
Err(response) => return Ok(response),
};
let params = &req.extensions.get::<Router>().unwrap();

let invitation_id = match params.find("invitation_id") {
Some(ref invitation_id) => {
match invitation_id.parse::<u64>() {
Ok(v) => v,
Err(_) => return Ok(Response::with(status::BadRequest)),
}
}
None => return Ok(Response::with(status::BadRequest)),
};

// TODO: read the body to determine "ignore"
let ignore_val = false;

let mut conn = Broker::connect(&ctx).unwrap();
let mut request = OriginInvitationAcceptRequest::new();

// make sure we're not trying to accept someone else's request
request.set_account_accepting_request(session.get_id());
request.set_invite_id(invitation_id);
request.set_ignore(ignore_val);

conn.route(&request).unwrap();
match conn.recv() {
Ok(rep) => {
match rep.get_message_id() {
"OriginInvitationAcceptResponse" => {
let _invites: OriginInvitationAcceptResponse =
protobuf::parse_from_bytes(rep.get_body()).unwrap();
// empty response
Ok(Response::with(status::Ok))
}
"NetError" => {
let err: NetError = protobuf::parse_from_bytes(rep.get_body()).unwrap();
Ok(render_net_error(&err))
}
_ => unreachable!("unexpected msg: {:?}", rep),
}
}
Err(e) => {
error!("{:?}", e);
Ok(Response::with(status::ServiceUnavailable))
}
}
}
20 changes: 14 additions & 6 deletions components/builder-api/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ pub fn router(config: Arc<Config>, context: Arc<Mutex<zmq::Context>>) -> Result<
let ctx3 = context.clone();
let ctx4 = context.clone();
let ctx5 = context.clone();
let ctx6 = context.clone();

let router = router!(
get "/authenticate/:code" => move |r: &mut Request| session_create(r, &github, &ctx1),

post "/origins" => move |r: &mut Request| origin_create(r, &ctx2),
get "/origins/:origin" => move |r: &mut Request| origin_show(r, &ctx3),
post "/jobs" => move |r: &mut Request| job_create(r, &ctx2),
get "/jobs/:id" => move |r: &mut Request| job_show(r, &ctx3),

get "/user/invitations" => move |r: &mut Request| list_account_invitations(r, &ctx4),
put "/user/invitations/:invitation_id" => move |r: &mut Request| accept_invitation(r, &ctx5),
get "/user/origins" => move |r: &mut Request| list_user_origins(r, &ctx6),

post "/jobs" => move |r: &mut Request| job_create(r, &ctx4),
get "/jobs/:id" => move |r: &mut Request| job_show(r, &ctx5),
);
let mut chain = Chain::new(router);
chain.link_after(Cors);
Expand All @@ -60,11 +64,15 @@ pub fn router(config: Arc<Config>, context: Arc<Mutex<zmq::Context>>) -> Result<
/// * Listener crashed during startup
pub fn run(config: Arc<Config>, context: Arc<Mutex<zmq::Context>>) -> Result<JoinHandle<()>> {
let (tx, rx) = mpsc::sync_channel(1);

let addr = config.http_addr.clone();
let depot = try!(depot::server::router(config.depot.clone()));
let ctx = context.clone();
let depot = try!(depot::Depot::new(config.depot.clone(), ctx));
let depot_chain = try!(depot::server::router(depot));
let chain = try!(router(config, context));

let mut mount = Mount::new();
mount.mount("/v1", chain).mount("/v1/depot", depot);
mount.mount("/v1", chain).mount("/v1/depot", depot_chain);
let handle = thread::Builder::new()
.name("http-srv".to_string())
.spawn(move || {
Expand Down
4 changes: 4 additions & 0 deletions components/builder-protocol/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ fn protocol_files() -> Vec<PathBuf> {
let mut files = vec![];
for entry in fs::read_dir("protocols").unwrap() {
let file = entry.unwrap();
// skip vim temp files
if file.file_name().to_str().unwrap().starts_with(".") {
Copy link
Collaborator

@reset reset Jun 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any other things we should ignore here? I didn't even think about this! Maybe we should just process things that explicitly end in .proto?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably stick with .proto, but this will work for now.

continue;
}
if file.metadata().unwrap().is_file() {
files.push(file.path());
}
Expand Down
1 change: 1 addition & 0 deletions components/builder-protocol/protocols/net.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum ErrCode {
ENTITY_NOT_FOUND = 4;
INTERNAL = 5;
NO_SHARD = 6;
ACCESS_DENIED = 7;
}

message NetError {
Expand Down
7 changes: 7 additions & 0 deletions components/builder-protocol/protocols/sessionsrv.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ message Account {
required string name = 3;
}

// get an account by GH username
message AccountGet {
required string name = 1;
}

message Session {
required uint64 id = 1;
required string email = 2;
Expand All @@ -33,3 +38,5 @@ message SessionCreate {
message SessionGet {
required string token = 1;
}


Loading