@@ -305,6 +305,20 @@ fn parse_machine_config_req<'a>(
305
305
Error :: Generic ( StatusCode :: BadRequest , s)
306
306
} ) ?)
307
307
}
308
+
309
+ 0 if method == Method :: Patch => {
310
+ METRICS . patch_api_requests . machine_cfg_count . inc ( ) ;
311
+ Ok ( serde_json:: from_slice :: < VmConfig > ( body)
312
+ . map_err ( |e| {
313
+ METRICS . patch_api_requests . machine_cfg_fails . inc ( ) ;
314
+ Error :: SerdeJson ( e)
315
+ } ) ?
316
+ . into_parsed_request ( None , method)
317
+ . map_err ( |s| {
318
+ METRICS . patch_api_requests . machine_cfg_fails . inc ( ) ;
319
+ Error :: Generic ( StatusCode :: BadRequest , s)
320
+ } ) ?)
321
+ }
308
322
_ => Err ( Error :: InvalidPathMethod ( path, method) ) ,
309
323
}
310
324
}
@@ -1158,6 +1172,26 @@ mod tests {
1158
1172
_ => assert ! ( false ) ,
1159
1173
}
1160
1174
1175
+ // PATCH
1176
+ let vm_config = VmConfig {
1177
+ vcpu_count : Some ( 32 ) ,
1178
+ mem_size_mib : None ,
1179
+ ht_enabled : None ,
1180
+ cpu_template : None ,
1181
+ } ;
1182
+ let body = r#"{
1183
+ "vcpu_count": 32
1184
+ }"# ;
1185
+ match vm_config. into_parsed_request ( None , Method :: Patch ) {
1186
+ Ok ( parsed_req) => {
1187
+ match parse_machine_config_req ( & path, Method :: Patch , & Chunk :: from ( body) ) {
1188
+ Ok ( other_parsed_req) => assert ! ( parsed_req. eq( & other_parsed_req) ) ,
1189
+ _ => assert ! ( false ) ,
1190
+ }
1191
+ }
1192
+ _ => assert ! ( false ) ,
1193
+ }
1194
+
1161
1195
// Error cases
1162
1196
// Error Case: Invalid payload (cannot deserialize the body into a VmConfig object).
1163
1197
assert ! (
@@ -1168,9 +1202,9 @@ mod tests {
1168
1202
// Error Case: Invalid payload (payload is empty).
1169
1203
let expected_err = Err ( Error :: Generic (
1170
1204
StatusCode :: BadRequest ,
1171
- String :: from ( "Empty request." ) ,
1205
+ String :: from ( "Empty PATCH request." ) ,
1172
1206
) ) ;
1173
- assert ! ( parse_machine_config_req( path, Method :: Put , & Chunk :: from( "{}" ) ) == expected_err) ;
1207
+ assert ! ( parse_machine_config_req( path, Method :: Patch , & Chunk :: from( "{}" ) ) == expected_err) ;
1174
1208
1175
1209
// Error Case: cpu count exceeds limitation
1176
1210
let json = "{
@@ -1185,6 +1219,19 @@ mod tests {
1185
1219
} else {
1186
1220
assert ! ( false ) ;
1187
1221
}
1222
+
1223
+ // Error Case: PUT request with missing parameter
1224
+ let json = "{
1225
+ \" mem_size_mib\" : 1025,
1226
+ \" ht_enabled\" : true,
1227
+ \" cpu_template\" : \" T2\"
1228
+ }" ;
1229
+ let body: Chunk = Chunk :: from ( json) ;
1230
+ let expected_err = Err ( Error :: Generic (
1231
+ StatusCode :: BadRequest ,
1232
+ String :: from ( "Missing mandatory fields." ) ,
1233
+ ) ) ;
1234
+ assert ! ( parse_machine_config_req( path, Method :: Put , & body) == expected_err) ;
1188
1235
}
1189
1236
1190
1237
#[ test]
0 commit comments