vmm: api: Allow HTTP PUT request to return a response

Adding the codepath to return a response from a PUT request.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2020-06-11 12:01:04 +02:00 committed by Rob Bradford
parent 1a2b3c8f3e
commit 038180269e
2 changed files with 34 additions and 23 deletions

View File

@ -126,7 +126,15 @@ pub trait EndpointHandler: Sync + Send {
) -> Response {
match req.method() {
Method::Put => match self.put_handler(api_notifier, api_sender, &req.body) {
Ok(_) => Response::new(Version::Http11, StatusCode::NoContent),
Ok(response_body) => {
if let Some(body) = response_body {
let mut response = Response::new(Version::Http11, StatusCode::OK);
response.set_body(body);
response
} else {
Response::new(Version::Http11, StatusCode::NoContent)
}
}
Err(e @ HttpError::BadRequest) => error_response(e, StatusCode::BadRequest),
Err(e @ HttpError::SerdeJsonDeserialize(_)) => {
error_response(e, StatusCode::BadRequest)
@ -142,7 +150,7 @@ pub trait EndpointHandler: Sync + Send {
_api_notifier: EventFd,
_api_sender: Sender<ApiRequest>,
_body: &Option<Body>,
) -> std::result::Result<(), HttpError> {
) -> std::result::Result<Option<Body>, HttpError> {
Err(HttpError::BadRequest)
}
}

View File

@ -71,8 +71,9 @@ impl EndpointHandler for VmActionHandler {
api_notifier: EventFd,
api_sender: Sender<ApiRequest>,
body: &Option<Body>,
) -> std::result::Result<(), HttpError> {
) -> std::result::Result<Option<Body>, HttpError> {
use VmAction::*;
let response_body: Option<Body> = None;
if let Some(body) = body {
match self.action {
AddDevice(_) => vm_add_device(
@ -80,84 +81,86 @@ impl EndpointHandler for VmActionHandler {
api_sender,
Arc::new(serde_json::from_slice(body.raw())?),
)
.map_err(HttpError::VmAddDevice),
.map_err(HttpError::VmAddDevice)?,
AddDisk(_) => vm_add_disk(
api_notifier,
api_sender,
Arc::new(serde_json::from_slice(body.raw())?),
)
.map_err(HttpError::VmAddDisk),
.map_err(HttpError::VmAddDisk)?,
AddFs(_) => vm_add_fs(
api_notifier,
api_sender,
Arc::new(serde_json::from_slice(body.raw())?),
)
.map_err(HttpError::VmAddFs),
.map_err(HttpError::VmAddFs)?,
AddPmem(_) => vm_add_pmem(
api_notifier,
api_sender,
Arc::new(serde_json::from_slice(body.raw())?),
)
.map_err(HttpError::VmAddPmem),
.map_err(HttpError::VmAddPmem)?,
AddNet(_) => vm_add_net(
api_notifier,
api_sender,
Arc::new(serde_json::from_slice(body.raw())?),
)
.map_err(HttpError::VmAddNet),
.map_err(HttpError::VmAddNet)?,
AddVsock(_) => vm_add_vsock(
api_notifier,
api_sender,
Arc::new(serde_json::from_slice(body.raw())?),
)
.map_err(HttpError::VmAddVsock),
.map_err(HttpError::VmAddVsock)?,
RemoveDevice(_) => vm_remove_device(
api_notifier,
api_sender,
Arc::new(serde_json::from_slice(body.raw())?),
)
.map_err(HttpError::VmRemoveDevice),
.map_err(HttpError::VmRemoveDevice)?,
Resize(_) => vm_resize(
api_notifier,
api_sender,
Arc::new(serde_json::from_slice(body.raw())?),
)
.map_err(HttpError::VmResize),
.map_err(HttpError::VmResize)?,
Restore(_) => vm_restore(
api_notifier,
api_sender,
Arc::new(serde_json::from_slice(body.raw())?),
)
.map_err(HttpError::VmRestore),
.map_err(HttpError::VmRestore)?,
Snapshot(_) => vm_snapshot(
api_notifier,
api_sender,
Arc::new(serde_json::from_slice(body.raw())?),
)
.map_err(HttpError::VmSnapshot),
.map_err(HttpError::VmSnapshot)?,
_ => Err(HttpError::BadRequest),
}
_ => return Err(HttpError::BadRequest),
};
} else {
match self.action {
Boot => vm_boot(api_notifier, api_sender).map_err(HttpError::VmBoot),
Delete => vm_delete(api_notifier, api_sender).map_err(HttpError::VmDelete),
Shutdown => vm_shutdown(api_notifier, api_sender).map_err(HttpError::VmShutdown),
Reboot => vm_reboot(api_notifier, api_sender).map_err(HttpError::VmReboot),
Pause => vm_pause(api_notifier, api_sender).map_err(HttpError::VmPause),
Resume => vm_resume(api_notifier, api_sender).map_err(HttpError::VmResume),
_ => Err(HttpError::BadRequest),
}
Boot => vm_boot(api_notifier, api_sender).map_err(HttpError::VmBoot)?,
Delete => vm_delete(api_notifier, api_sender).map_err(HttpError::VmDelete)?,
Shutdown => vm_shutdown(api_notifier, api_sender).map_err(HttpError::VmShutdown)?,
Reboot => vm_reboot(api_notifier, api_sender).map_err(HttpError::VmReboot)?,
Pause => vm_pause(api_notifier, api_sender).map_err(HttpError::VmPause)?,
Resume => vm_resume(api_notifier, api_sender).map_err(HttpError::VmResume)?,
_ => return Err(HttpError::BadRequest),
};
}
Ok(response_body)
}
}