Skip to content

Commit 48a08bc

Browse files
api_server: added resource_id to IntoParsedRequest
All API resources should implement the trait IntoParsedRequest instead of having a method into_parsed_request in their implementation. Some resource also have an ID in the path and this needs to be validate. Added a parameter resource_id to into_parsed_request. Signed-off-by: Andreea Florescu <[email protected]>
1 parent b1d1a56 commit 48a08bc

File tree

5 files changed

+75
-32
lines changed

5 files changed

+75
-32
lines changed

api_server/src/http_service.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ fn parse_actions_req<'a>(
118118
.map_err(|e| {
119119
METRICS.put_api_requests.actions_fails.inc();
120120
Error::SerdeJson(e)
121-
})?.into_parsed_request(method)
121+
})?.into_parsed_request(None, method)
122122
.map_err(|msg| {
123123
METRICS.put_api_requests.actions_fails.inc();
124124
Error::Generic(StatusCode::BadRequest, msg)
@@ -132,10 +132,13 @@ fn parse_actions_req<'a>(
132132
METRICS.put_api_requests.actions_fails.inc();
133133
Error::SerdeJson(e)
134134
})?;
135-
let parsed_req = body.clone().into_parsed_request(method).map_err(|msg| {
136-
METRICS.put_api_requests.actions_fails.inc();
137-
Error::Generic(StatusCode::BadRequest, msg)
138-
})?;
135+
let parsed_req = body
136+
.clone()
137+
.into_parsed_request(None, method)
138+
.map_err(|msg| {
139+
METRICS.put_api_requests.actions_fails.inc();
140+
Error::Generic(StatusCode::BadRequest, msg)
141+
})?;
139142
Ok(parsed_req)
140143
}
141144
_ => Err(Error::InvalidPathMethod(path, method)),
@@ -217,7 +220,7 @@ fn parse_drives_req<'a>(
217220
METRICS.put_api_requests.drive_fails.inc();
218221
Error::Generic(StatusCode::BadRequest, format!("{:?}", s))
219222
})?;
220-
Ok(device_cfg.into_parsed_request(method).map_err(|s| {
223+
Ok(device_cfg.into_parsed_request(None, method).map_err(|s| {
221224
METRICS.put_api_requests.drive_fails.inc();
222225
Error::Generic(StatusCode::BadRequest, s)
223226
})?)
@@ -231,7 +234,7 @@ fn parse_drives_req<'a>(
231234
METRICS.patch_api_requests.drive_fails.inc();
232235
Error::SerdeJson(e)
233236
})?,
234-
}.into_parsed_request(method)
237+
}.into_parsed_request(None, method)
235238
.map_err(|s| {
236239
METRICS.patch_api_requests.drive_fails.inc();
237240
Error::Generic(StatusCode::BadRequest, s)
@@ -285,7 +288,7 @@ fn parse_machine_config_req<'a>(
285288
cpu_template: None,
286289
};
287290
Ok(empty_machine_config
288-
.into_parsed_request(method)
291+
.into_parsed_request(None, method)
289292
.map_err(|s| {
290293
METRICS.get_api_requests.machine_cfg_fails.inc();
291294
Error::Generic(StatusCode::BadRequest, s)
@@ -298,7 +301,7 @@ fn parse_machine_config_req<'a>(
298301
.map_err(|e| {
299302
METRICS.put_api_requests.machine_cfg_fails.inc();
300303
Error::SerdeJson(e)
301-
})?.into_parsed_request(method)
304+
})?.into_parsed_request(None, method)
302305
.map_err(|s| {
303306
METRICS.put_api_requests.machine_cfg_fails.inc();
304307
Error::Generic(StatusCode::BadRequest, s)
@@ -870,7 +873,7 @@ mod tests {
870873
rate_limiter: None,
871874
};
872875

873-
match drive_desc.into_parsed_request(Method::Put) {
876+
match drive_desc.into_parsed_request(None, Method::Put) {
874877
Ok(pr) => match parse_drives_req(
875878
&"/foo/bar"[1..].split_terminator('/').collect(),
876879
&"/foo/bar",
@@ -910,7 +913,7 @@ mod tests {
910913
fields: Value::Object(payload_map),
911914
};
912915

913-
match patch_payload.into_parsed_request(Method::Patch) {
916+
match patch_payload.into_parsed_request(None, Method::Patch) {
914917
Ok(pr) => {
915918
match parse_drives_req(&path_tokens, &path, Method::Patch, &id_from_path, &body) {
916919
Ok(pr_drive) => assert!(pr.eq(&pr_drive)),
@@ -1061,7 +1064,7 @@ mod tests {
10611064
cpu_template: Some(CpuFeaturesTemplate::T2),
10621065
};
10631066

1064-
match mcb.into_parsed_request(Method::Put) {
1067+
match mcb.into_parsed_request(None, Method::Put) {
10651068
Ok(pr) => match parse_machine_config_req(&path_tokens, &path, Method::Put, &body) {
10661069
Ok(pr_mcb) => assert!(pr.eq(&pr_mcb)),
10671070
_ => assert!(false),

api_server/src/request/actions.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ fn validate_payload(action_body: &ActionBody) -> Result<(), String> {
7070
}
7171

7272
impl IntoParsedRequest for ActionBody {
73-
fn into_parsed_request(self, _: Method) -> result::Result<ParsedRequest, String> {
73+
fn into_parsed_request(
74+
self,
75+
_: Option<String>,
76+
_: Method,
77+
) -> result::Result<ParsedRequest, String> {
7478
validate_payload(&self)?;
7579
match self.action_type {
7680
ActionType::BlockDeviceRescan => {
@@ -156,7 +160,7 @@ mod tests {
156160
assert!(
157161
result
158162
.unwrap()
159-
.into_parsed_request(Method::Put)
163+
.into_parsed_request(None, Method::Put)
160164
.unwrap()
161165
.eq(&req)
162166
);
@@ -177,7 +181,7 @@ mod tests {
177181
assert!(
178182
result
179183
.unwrap()
180-
.into_parsed_request(Method::Put)
184+
.into_parsed_request(None, Method::Put)
181185
.unwrap()
182186
.eq(&req)
183187
);

api_server/src/request/drive.rs

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ impl PatchDrivePayload {
7373
}
7474

7575
impl IntoParsedRequest for PatchDrivePayload {
76-
fn into_parsed_request(self, method: Method) -> result::Result<ParsedRequest, String> {
76+
fn into_parsed_request(
77+
self,
78+
_: Option<String>,
79+
method: Method,
80+
) -> result::Result<ParsedRequest, String> {
7781
match method {
7882
Method::Patch => {
7983
self.validate()?;
@@ -92,7 +96,11 @@ impl IntoParsedRequest for PatchDrivePayload {
9296
}
9397

9498
impl IntoParsedRequest for BlockDeviceConfig {
95-
fn into_parsed_request(self, method: Method) -> result::Result<ParsedRequest, String> {
99+
fn into_parsed_request(
100+
self,
101+
_: Option<String>,
102+
method: Method,
103+
) -> result::Result<ParsedRequest, String> {
96104
let (sender, receiver) = oneshot::channel();
97105
match method {
98106
Method::Put => Ok(ParsedRequest::Sync(
@@ -172,7 +180,11 @@ mod tests {
172180
let patch_payload = PatchDrivePayload {
173181
fields: Value::Object(payload_map),
174182
};
175-
assert!(patch_payload.into_parsed_request(Method::Patch).is_err());
183+
assert!(
184+
patch_payload
185+
.into_parsed_request(None, Method::Patch)
186+
.is_err()
187+
);
176188

177189
// PATCH with invalid types on fields. Adding a drive_id as number instead of string.
178190
let mut payload_map = Map::<String, Value>::new();
@@ -184,7 +196,11 @@ mod tests {
184196
let patch_payload = PatchDrivePayload {
185197
fields: Value::Object(payload_map),
186198
};
187-
assert!(patch_payload.into_parsed_request(Method::Patch).is_err());
199+
assert!(
200+
patch_payload
201+
.into_parsed_request(None, Method::Patch)
202+
.is_err()
203+
);
188204

189205
// PATCH with invalid types on fields. Adding a path_on_host as bool instead of string.
190206
let mut payload_map = Map::<String, Value>::new();
@@ -196,7 +212,11 @@ mod tests {
196212
let patch_payload = PatchDrivePayload {
197213
fields: Value::Object(payload_map),
198214
};
199-
assert!(patch_payload.into_parsed_request(Method::Patch).is_err());
215+
assert!(
216+
patch_payload
217+
.into_parsed_request(None, Method::Patch)
218+
.is_err()
219+
);
200220

201221
// PATCH with missing path_on_host field.
202222
let mut payload_map = Map::<String, Value>::new();
@@ -207,15 +227,23 @@ mod tests {
207227
let patch_payload = PatchDrivePayload {
208228
fields: Value::Object(payload_map),
209229
};
210-
assert!(patch_payload.into_parsed_request(Method::Patch).is_err());
230+
assert!(
231+
patch_payload
232+
.into_parsed_request(None, Method::Patch)
233+
.is_err()
234+
);
211235

212236
// PATCH with missing drive_id field.
213237
let mut payload_map = Map::<String, Value>::new();
214238
payload_map.insert(String::from("path_on_host"), Value::Bool(true));
215239
let patch_payload = PatchDrivePayload {
216240
fields: Value::Object(payload_map),
217241
};
218-
assert!(patch_payload.into_parsed_request(Method::Patch).is_err());
242+
assert!(
243+
patch_payload
244+
.into_parsed_request(None, Method::Patch)
245+
.is_err()
246+
);
219247

220248
let mut payload_map = Map::<String, Value>::new();
221249
payload_map.insert(String::from("drive_id"), Value::String(String::from("foo")));
@@ -230,14 +258,14 @@ mod tests {
230258

231259
assert!(
232260
pdp.clone()
233-
.into_parsed_request(Method::Patch)
261+
.into_parsed_request(None, Method::Patch)
234262
.eq(&Ok(ParsedRequest::Sync(
235263
VmmAction::UpdateDrivePath("foo".to_string(), "dummy".to_string(), sender),
236264
receiver
237265
)))
238266
);
239267

240-
assert!(pdp.into_parsed_request(Method::Put).is_err());
268+
assert!(pdp.into_parsed_request(None, Method::Put).is_err());
241269
}
242270

243271
#[test]
@@ -308,14 +336,14 @@ mod tests {
308336
assert!(
309337
&desc
310338
.clone()
311-
.into_parsed_request(Method::Options)
339+
.into_parsed_request(None, Method::Options)
312340
.eq(&Ok(ParsedRequest::Dummy))
313341
);
314342
let (sender, receiver) = oneshot::channel();
315343
assert!(
316344
&desc
317345
.clone()
318-
.into_parsed_request(Method::Put)
346+
.into_parsed_request(None, Method::Put)
319347
.eq(&Ok(ParsedRequest::Sync(
320348
VmmAction::InsertBlockDevice(desc, sender),
321349
receiver

api_server/src/request/machine_configuration.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ impl GenerateResponse for MachineConfiguration {
5858
}
5959

6060
impl IntoParsedRequest for MachineConfiguration {
61-
fn into_parsed_request(self, method: Method) -> result::Result<ParsedRequest, String> {
61+
fn into_parsed_request(
62+
self,
63+
_: Option<String>,
64+
method: Method,
65+
) -> result::Result<ParsedRequest, String> {
6266
let (sender, receiver) = oneshot::channel();
6367
match method {
6468
Method::Get => Ok(ParsedRequest::Sync(
@@ -121,7 +125,7 @@ mod tests {
121125
let (sender, receiver) = oneshot::channel();
122126
assert!(
123127
body.clone()
124-
.into_parsed_request(Method::Put)
128+
.into_parsed_request(None, Method::Put)
125129
.eq(&Ok(ParsedRequest::Sync(
126130
VmmAction::SetVmConfiguration(body, sender),
127131
receiver
@@ -136,17 +140,17 @@ mod tests {
136140
assert!(
137141
uninitialized
138142
.clone()
139-
.into_parsed_request(Method::Get)
143+
.into_parsed_request(None, Method::Get)
140144
.is_ok()
141145
);
142146
assert!(
143147
uninitialized
144148
.clone()
145-
.into_parsed_request(Method::Patch)
149+
.into_parsed_request(None, Method::Patch)
146150
.eq(&Ok(ParsedRequest::Dummy))
147151
);
148152

149-
match uninitialized.into_parsed_request(Method::Put) {
153+
match uninitialized.into_parsed_request(None, Method::Put) {
150154
Ok(_) => assert!(false),
151155
Err(e) => assert_eq!(e, String::from("Empty request.")),
152156
};

api_server/src/request/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ pub enum ParsedRequest {
3333
}
3434

3535
pub trait IntoParsedRequest {
36-
fn into_parsed_request(self, method: Method) -> result::Result<ParsedRequest, String>;
36+
fn into_parsed_request(
37+
self,
38+
resource_id: Option<String>,
39+
method: Method,
40+
) -> result::Result<ParsedRequest, String>;
3741
}
3842

3943
// Sync requests have outcomes which implement this trait. The idea is for each outcome to be a

0 commit comments

Comments
 (0)