Skip to content

Commit b23a56e

Browse files
api_server: implemented IntoParsedRequest...
... for BootSourceConfig, NetworkInterfaceBody and APILoggerDescription. Signed-off-by: Andreea Florescu <[email protected]>
1 parent 48a08bc commit b23a56e

File tree

7 files changed

+63
-47
lines changed

7 files changed

+63
-47
lines changed

api_server/src/http_service.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ fn parse_boot_source_req<'a>(
161161
.map_err(|e| {
162162
METRICS.put_api_requests.boot_source_fails.inc();
163163
Error::SerdeJson(e)
164-
})?.into_parsed_request()
164+
})?.into_parsed_request(None, method)
165165
.map_err(|s| {
166166
METRICS.put_api_requests.boot_source_fails.inc();
167167
Error::Generic(StatusCode::BadRequest, s)
@@ -210,20 +210,19 @@ fn parse_drives_req<'a>(
210210
1 if method == Method::Get => Ok(ParsedRequest::Dummy),
211211

212212
1 if method == Method::Put => {
213+
let unwrapped_id = id_from_path.ok_or(Error::InvalidID)?;
213214
METRICS.put_api_requests.drive_count.inc();
214215

215216
let device_cfg = serde_json::from_slice::<BlockDeviceConfig>(body).map_err(|e| {
216217
METRICS.put_api_requests.drive_fails.inc();
217218
Error::SerdeJson(e)
218219
})?;
219-
device_cfg.check_id(id_from_path.unwrap()).map_err(|s| {
220-
METRICS.put_api_requests.drive_fails.inc();
221-
Error::Generic(StatusCode::BadRequest, format!("{:?}", s))
222-
})?;
223-
Ok(device_cfg.into_parsed_request(None, method).map_err(|s| {
224-
METRICS.put_api_requests.drive_fails.inc();
225-
Error::Generic(StatusCode::BadRequest, s)
226-
})?)
220+
Ok(device_cfg
221+
.into_parsed_request(Some(unwrapped_id.to_string()), method)
222+
.map_err(|s| {
223+
METRICS.put_api_requests.drive_fails.inc();
224+
Error::Generic(StatusCode::BadRequest, s)
225+
})?)
227226
}
228227

229228
1 if method == Method::Patch => {
@@ -261,7 +260,7 @@ fn parse_logger_req<'a>(
261260
.map_err(|e| {
262261
METRICS.put_api_requests.logger_fails.inc();
263262
Error::SerdeJson(e)
264-
})?.into_parsed_request()
263+
})?.into_parsed_request(None, method)
265264
.map_err(|s| {
266265
METRICS.put_api_requests.logger_fails.inc();
267266
Error::Generic(StatusCode::BadRequest, s)
@@ -332,7 +331,7 @@ fn parse_netif_req<'a>(
332331
.map_err(|e| {
333332
METRICS.put_api_requests.network_fails.inc();
334333
Error::SerdeJson(e)
335-
})?.into_parsed_request(unwrapped_id)
334+
})?.into_parsed_request(Some(unwrapped_id.to_string()), method)
336335
.map_err(|s| {
337336
METRICS.put_api_requests.network_fails.inc();
338337
Error::Generic(StatusCode::BadRequest, s)
@@ -873,7 +872,7 @@ mod tests {
873872
rate_limiter: None,
874873
};
875874

876-
match drive_desc.into_parsed_request(None, Method::Put) {
875+
match drive_desc.into_parsed_request(Some(String::from("bar")), Method::Put) {
877876
Ok(pr) => match parse_drives_req(
878877
&"/foo/bar"[1..].split_terminator('/').collect(),
879878
&"/foo/bar",
@@ -1142,7 +1141,7 @@ mod tests {
11421141
allow_mmds_requests: false,
11431142
};
11441143

1145-
match netif.into_parsed_request("bar") {
1144+
match netif.into_parsed_request(Some(String::from("bar")), Method::Put) {
11461145
Ok(pr) => match parse_netif_req(
11471146
&"/foo/bar"[1..].split_terminator('/').collect(),
11481147
&"/foo/bar",

api_server/src/request/boot_source.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::result;
22

33
use futures::sync::oneshot;
4-
use hyper::{Response, StatusCode};
4+
use hyper::{Method, Response, StatusCode};
55

66
use http_service::{json_fault_message, json_response};
7-
use request::{GenerateResponse, ParsedRequest, VmmAction};
7+
use request::{GenerateResponse, IntoParsedRequest, ParsedRequest, VmmAction};
88

99
#[derive(Debug, Deserialize, PartialEq, Serialize)]
1010
#[serde(deny_unknown_fields)]
@@ -44,8 +44,12 @@ impl GenerateResponse for BootSourceConfigError {
4444
}
4545
}
4646

47-
impl BootSourceConfig {
48-
pub fn into_parsed_request(self) -> result::Result<ParsedRequest, String> {
47+
impl IntoParsedRequest for BootSourceConfig {
48+
fn into_parsed_request(
49+
self,
50+
_: Option<String>,
51+
_: Method,
52+
) -> result::Result<ParsedRequest, String> {
4953
let (sender, receiver) = oneshot::channel();
5054
Ok(ParsedRequest::Sync(
5155
VmmAction::ConfigureBootSource(self, sender),
@@ -85,9 +89,12 @@ mod tests {
8589
boot_args: Some(String::from("foobar")),
8690
};
8791
let (sender, receiver) = oneshot::channel();
88-
assert!(body.into_parsed_request().eq(&Ok(ParsedRequest::Sync(
89-
VmmAction::ConfigureBootSource(same_body, sender),
90-
receiver
91-
))))
92+
assert!(
93+
body.into_parsed_request(None, Method::Put)
94+
.eq(&Ok(ParsedRequest::Sync(
95+
VmmAction::ConfigureBootSource(same_body, sender),
96+
receiver
97+
)))
98+
)
9299
}
93100
}

api_server/src/request/drive.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,15 @@ impl IntoParsedRequest for PatchDrivePayload {
9898
impl IntoParsedRequest for BlockDeviceConfig {
9999
fn into_parsed_request(
100100
self,
101-
_: Option<String>,
101+
id_from_path: Option<String>,
102102
method: Method,
103103
) -> result::Result<ParsedRequest, String> {
104+
let id_from_path = id_from_path.unwrap_or(String::new());
105+
if id_from_path != self.drive_id {
106+
return Err(String::from(
107+
"The id from the path does not match the id from the body!",
108+
));
109+
}
104110
let (sender, receiver) = oneshot::channel();
105111
match method {
106112
Method::Put => Ok(ParsedRequest::Sync(
@@ -336,14 +342,14 @@ mod tests {
336342
assert!(
337343
&desc
338344
.clone()
339-
.into_parsed_request(None, Method::Options)
345+
.into_parsed_request(Some(String::from("foo")), Method::Options)
340346
.eq(&Ok(ParsedRequest::Dummy))
341347
);
342348
let (sender, receiver) = oneshot::channel();
343349
assert!(
344350
&desc
345351
.clone()
346-
.into_parsed_request(None, Method::Put)
352+
.into_parsed_request(Some(String::from("foo")), Method::Put)
347353
.eq(&Ok(ParsedRequest::Sync(
348354
VmmAction::InsertBlockDevice(desc, sender),
349355
receiver

api_server/src/request/logger.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::result;
22

33
use futures::sync::oneshot;
4-
use hyper::{Response, StatusCode};
4+
use hyper::{Method, Response, StatusCode};
55

66
use http_service::{json_fault_message, json_response};
7-
use request::{GenerateResponse, ParsedRequest, VmmAction};
7+
use request::{GenerateResponse, IntoParsedRequest, ParsedRequest, VmmAction};
88

99
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
1010
pub enum APILoggerLevel {
@@ -44,8 +44,12 @@ impl GenerateResponse for APILoggerError {
4444
}
4545
}
4646

47-
impl APILoggerDescription {
48-
pub fn into_parsed_request(self) -> result::Result<ParsedRequest, String> {
47+
impl IntoParsedRequest for APILoggerDescription {
48+
fn into_parsed_request(
49+
self,
50+
_: Option<String>,
51+
_: Method,
52+
) -> result::Result<ParsedRequest, String> {
4953
let (sender, receiver) = oneshot::channel();
5054
Ok(ParsedRequest::Sync(
5155
VmmAction::ConfigureLogger(self, sender),
@@ -86,12 +90,12 @@ mod tests {
8690
show_log_origin: None,
8791
};
8892
format!("{:?}", desc);
89-
assert!(&desc.clone().into_parsed_request().is_ok());
93+
assert!(&desc.clone().into_parsed_request(None, Method::Put).is_ok());
9094
let (sender, receiver) = oneshot::channel();
9195
assert!(
9296
&desc
9397
.clone()
94-
.into_parsed_request()
98+
.into_parsed_request(None, Method::Put)
9599
.eq(&Ok(ParsedRequest::Sync(
96100
VmmAction::ConfigureLogger(desc, sender),
97101
receiver

api_server/src/request/net.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use std::result;
22

33
use futures::sync::oneshot;
4-
use hyper::{Response, StatusCode};
4+
use hyper::{Method, Response, StatusCode};
55

66
use super::VmmAction;
77

88
use data_model::vm::{DeviceState, RateLimiterDescription};
99
use http_service::{json_fault_message, json_response};
1010
use net_util::{MacAddr, TapError};
11-
use request::{GenerateResponse, ParsedRequest};
11+
use request::{GenerateResponse, IntoParsedRequest, ParsedRequest};
1212

1313
// This struct represents the strongly typed equivalent of the json body from net iface
1414
// related requests.
@@ -75,8 +75,13 @@ impl GenerateResponse for NetworkInterfaceError {
7575
}
7676
}
7777

78-
impl NetworkInterfaceBody {
79-
pub fn into_parsed_request(self, id_from_path: &str) -> result::Result<ParsedRequest, String> {
78+
impl IntoParsedRequest for NetworkInterfaceBody {
79+
fn into_parsed_request(
80+
self,
81+
id_from_path: Option<String>,
82+
_: Method,
83+
) -> result::Result<ParsedRequest, String> {
84+
let id_from_path = id_from_path.unwrap_or(String::new());
8085
if id_from_path != self.iface_id {
8186
return Err(String::from(
8287
"The id from the path does not match the id from the body!",
@@ -112,12 +117,17 @@ mod tests {
112117
allow_mmds_requests: false,
113118
};
114119

115-
assert!(netif.clone().into_parsed_request("bar").is_err());
120+
assert!(
121+
netif
122+
.clone()
123+
.into_parsed_request(Some(String::from("bar")), Method::Put)
124+
.is_err()
125+
);
116126
let (sender, receiver) = oneshot::channel();
117127
assert!(
118128
netif
119129
.clone()
120-
.into_parsed_request("foo")
130+
.into_parsed_request(Some(String::from("foo")), Method::Put)
121131
.eq(&Ok(ParsedRequest::Sync(
122132
VmmAction::InsertNetworkDevice(netif, sender),
123133
receiver

data_model/src/vm/devices/drive.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,6 @@ pub struct BlockDeviceConfig {
3737
}
3838

3939
impl BlockDeviceConfig {
40-
pub fn check_id(&self, id: &str) -> result::Result<(), String> {
41-
if self.drive_id != String::from(id) {
42-
Err(String::from(
43-
"The id from the path does not match the id from the body!",
44-
))
45-
} else {
46-
Ok(())
47-
}
48-
}
49-
5040
pub fn get_partuuid(&self) -> Option<&String> {
5141
self.partuuid.as_ref()
5242
}

vmm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,7 @@ impl Vmm {
13631363
VmmAction::ConfigureBootSource(boot_source_body, sender) => {
13641364
let boxed_response = Box::new(self.configure_boot_source(
13651365
boot_source_body.kernel_image_path,
1366-
boot_source_body.boot_args
1366+
boot_source_body.boot_args,
13671367
));
13681368
Vmm::send_response(boxed_response, sender);
13691369
}

0 commit comments

Comments
 (0)