Skip to content

Commit 2d5877d

Browse files
alexandruagandreeaflorescu
authored andcommitted
api/http_service: implemented GET /actions/{action_id}
Signed-off-by: Alexandru Agache <[email protected]>
1 parent 0e32ea9 commit 2d5877d

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

api_server/src/http_service.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ fn parse_request<'a>(
108108
path: &'a str,
109109
body: &Chunk,
110110
) -> Result<'a, ParsedRequest> {
111+
// Commenting this out for now.
112+
/*
111113
if cfg!(debug_assertions) {
112114
println!(
113115
"{}",
@@ -119,6 +121,7 @@ fn parse_request<'a>(
119121
)
120122
);
121123
}
124+
*/
122125

123126
if !path.starts_with('/') {
124127
return Err(Error::InvalidPathMethod(path, method));
@@ -308,11 +311,21 @@ impl hyper::server::Service for ApiServerHttpService {
308311
// are in a similar condition right now.
309312
Either::A(future::ok(empty_response(StatusCode::Ok)))
310313
}
311-
GetAction(_id) => {
312-
// todo: what should we do when an action id is not found vs. not processed
313-
// by the vmm yet?
314-
Either::A(future::ok(empty_response(StatusCode::Ok)))
315-
}
314+
GetAction(id) => match action_map.borrow().get(&id) {
315+
Some(value) => match *value {
316+
ActionMapValue::Pending(_) => Either::A(future::ok(json_response(
317+
StatusCode::Conflict,
318+
json_fault_message("Action is still pending."),
319+
))),
320+
ActionMapValue::JsonResponse(ref status, ref body) => {
321+
Either::A(future::ok(json_response(status.clone(), body.clone())))
322+
}
323+
},
324+
None => Either::A(future::ok(json_response(
325+
StatusCode::NotFound,
326+
json_fault_message("Action not found."),
327+
))),
328+
},
316329
Async(id, async_req, outcome_receiver) => {
317330
if send_to_vmm(
318331
ApiRequest::Async(async_req),
@@ -333,7 +346,7 @@ impl hyper::server::Service for ApiServerHttpService {
333346
// Let's see if the action is still in the map (it might have
334347
// been evicted by newer actions, although that's extremely
335348
// unlikely under in normal circumstances).
336-
let hyper_response = match action_map
349+
let (response_status, json_body) = match action_map
337350
.borrow_mut()
338351
.get_mut(id.as_str())
339352
{
@@ -345,12 +358,12 @@ impl hyper::server::Service for ApiServerHttpService {
345358
async_body.set_timestamp(timestamp);
346359
// We use unwrap because the serialize operation
347360
// should not fail in this case.
348-
json_response(
361+
(
349362
StatusCode::Ok,
350363
serde_json::to_string(&async_body).unwrap(),
351364
)
352365
}
353-
AsyncOutcome::Error(msg) => json_response(
366+
AsyncOutcome::Error(msg) => (
354367
StatusCode::BadRequest,
355368
json_fault_message(msg),
356369
),
@@ -359,9 +372,10 @@ impl hyper::server::Service for ApiServerHttpService {
359372
_ => return,
360373
};
361374
// Replace the old value with the already built response.
362-
action_map
363-
.borrow_mut()
364-
.insert(id, ActionMapValue::Response(hyper_response));
375+
action_map.borrow_mut().insert(
376+
id,
377+
ActionMapValue::JsonResponse(response_status, json_body),
378+
);
365379
})
366380
.map_err(|_| ()),
367381
);

api_server/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ use sys_util::EventFd;
3535
// because it's inherently static at this point.
3636
pub enum ActionMapValue {
3737
Pending(AsyncRequestBody),
38-
Response(hyper::Response),
38+
// The response status code, and the response json body.
39+
JsonResponse(hyper::StatusCode, String),
3940
}
4041

4142
// A map that holds information about currently pending, and previous async actions.

0 commit comments

Comments
 (0)