mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
vmm: Implement the /api/v1/vm.info endpoint
This, for now, returns the VM config and its state. Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
|
||||
extern crate threadpool;
|
||||
|
||||
use crate::api::http_endpoint::{VmActionHandler, VmCreate};
|
||||
use crate::api::http_endpoint::{VmActionHandler, VmCreate, VmInfo};
|
||||
use crate::api::{ApiRequest, VmAction};
|
||||
use crate::{Error, Result};
|
||||
use micro_http::{HttpConnection, Request, Response, StatusCode, Version};
|
||||
@@ -56,6 +56,7 @@ lazy_static! {
|
||||
|
||||
r.routes.insert(endpoint!("/vm.create"), Box::new(VmCreate {}));
|
||||
r.routes.insert(endpoint!("/vm.boot"), Box::new(VmActionHandler::new(VmAction::Boot)));
|
||||
r.routes.insert(endpoint!("/vm.info"), Box::new(VmInfo {}));
|
||||
r.routes.insert(endpoint!("/vm.shutdown"), Box::new(VmActionHandler::new(VmAction::Shutdown)));
|
||||
r.routes.insert(endpoint!("/vm.reboot"), Box::new(VmActionHandler::new(VmAction::Reboot)));
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
use crate::api::http::EndpointHandler;
|
||||
use crate::api::VmConfig;
|
||||
use crate::api::{vm_boot, vm_create, vm_reboot, vm_shutdown, ApiRequest, VmAction};
|
||||
use crate::api::{vm_boot, vm_create, vm_info, vm_reboot, vm_shutdown, ApiRequest, VmAction};
|
||||
use crate::{Error, Result};
|
||||
use micro_http::{Body, Method, Request, Response, StatusCode, Version};
|
||||
use serde_json::Error as SerdeError;
|
||||
@@ -25,6 +25,9 @@ pub enum HttpError {
|
||||
/// Could not boot a VM
|
||||
VmBoot(Error),
|
||||
|
||||
/// Could not get the VM information
|
||||
VmInfo(Error),
|
||||
|
||||
/// Could not shut a VM down
|
||||
VmShutdown(Error),
|
||||
|
||||
@@ -124,3 +127,29 @@ impl EndpointHandler for VmActionHandler {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// /api/v1/vm.info handler
|
||||
pub struct VmInfo {}
|
||||
|
||||
impl EndpointHandler for VmInfo {
|
||||
fn handle_request(
|
||||
&self,
|
||||
req: &Request,
|
||||
api_notifier: EventFd,
|
||||
api_sender: Sender<ApiRequest>,
|
||||
) -> Response {
|
||||
match req.method() {
|
||||
Method::Get => match vm_info(api_notifier, api_sender).map_err(HttpError::VmInfo) {
|
||||
Ok(info) => {
|
||||
let mut response = Response::new(Version::Http11, StatusCode::OK);
|
||||
let info_serialized = serde_json::to_string(&info).unwrap();
|
||||
|
||||
response.set_body(Body::new(info_serialized));
|
||||
response
|
||||
}
|
||||
Err(e) => error_response(e, StatusCode::InternalServerError),
|
||||
},
|
||||
_ => Response::new(Version::Http11, StatusCode::BadRequest),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ pub mod http;
|
||||
pub mod http_endpoint;
|
||||
|
||||
use crate::config::VmConfig;
|
||||
use crate::vm::Error as VmError;
|
||||
use crate::vm::{Error as VmError, VmState};
|
||||
use crate::{Error, Result};
|
||||
use std::sync::mpsc::{channel, Sender};
|
||||
use std::sync::Arc;
|
||||
@@ -55,6 +55,9 @@ pub enum ApiError {
|
||||
/// The VM is already created.
|
||||
VmAlreadyCreated,
|
||||
|
||||
/// The VM info is not available.
|
||||
VmInfo,
|
||||
|
||||
/// The VM config is missing.
|
||||
VmMissingConfig,
|
||||
|
||||
@@ -71,9 +74,18 @@ pub enum ApiError {
|
||||
VmReboot,
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
pub struct VmInfo {
|
||||
pub config: Arc<VmConfig>,
|
||||
pub state: VmState,
|
||||
}
|
||||
|
||||
pub enum ApiResponsePayload {
|
||||
/// No data is sent on the channel.
|
||||
Empty,
|
||||
|
||||
/// Virtual machine information
|
||||
VmInfo(VmInfo),
|
||||
}
|
||||
|
||||
/// This is the response sent by the VMM API server through the mpsc channel.
|
||||
@@ -92,6 +104,9 @@ pub enum ApiRequest {
|
||||
/// VmBoot error back.
|
||||
VmBoot(Sender<ApiResponse>),
|
||||
|
||||
/// Request the VM information.
|
||||
VmInfo(Sender<ApiResponse>),
|
||||
|
||||
/// Shut the previously booted virtual machine down.
|
||||
/// If the VM was not previously booted or created, the VMM API server
|
||||
/// will send a VmShutdown error back.
|
||||
@@ -188,3 +203,23 @@ pub fn vm_shutdown(api_evt: EventFd, api_sender: Sender<ApiRequest>) -> Result<(
|
||||
pub fn vm_reboot(api_evt: EventFd, api_sender: Sender<ApiRequest>) -> Result<()> {
|
||||
vm_action(api_evt, api_sender, VmAction::Reboot)
|
||||
}
|
||||
|
||||
pub fn vm_info(api_evt: EventFd, api_sender: Sender<ApiRequest>) -> Result<VmInfo> {
|
||||
let (response_sender, response_receiver) = channel();
|
||||
|
||||
// Send the VM request.
|
||||
api_sender
|
||||
.send(ApiRequest::VmInfo(response_sender))
|
||||
.map_err(Error::ApiRequestSend)?;
|
||||
api_evt.write(1).map_err(Error::EventFdWrite)?;
|
||||
|
||||
let vm_info = response_receiver
|
||||
.recv()
|
||||
.map_err(Error::ApiResponseRecv)?
|
||||
.map_err(|_| Error::ApiVmInfo)?;
|
||||
|
||||
match vm_info {
|
||||
ApiResponsePayload::VmInfo(info) => Ok(info),
|
||||
_ => Err(Error::ApiVmInfo),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user