@@ -20,11 +20,25 @@ pub(crate) fn parse_put_machine_config(body: &Body) -> Result<ParsedRequest, Err
20
20
err
21
21
} ) ?;
22
22
23
+ // Check for the presence of deprecated `cpu_template` field.
24
+ let mut deprecation_message = None ;
25
+ if config. cpu_template . is_some ( ) {
26
+ // `cpu_template` field in request is deprecated.
27
+ METRICS . deprecated_api . deprecated_http_api_calls . inc ( ) ;
28
+ deprecation_message = Some ( "PUT /machine-config: cpu_template field is deprecated." ) ;
29
+ }
30
+
31
+ // Convert `MachineConfig` to `MachineConfigUpdate`.
23
32
let config_update = MachineConfigUpdate :: from ( config) ;
24
33
25
- Ok ( ParsedRequest :: new_sync ( VmmAction :: UpdateVmConfiguration (
26
- config_update,
27
- ) ) )
34
+ // Construct the `ParsedRequest` object.
35
+ let mut parsed_req = ParsedRequest :: new_sync ( VmmAction :: UpdateVmConfiguration ( config_update) ) ;
36
+ // If `cpu_template` was present, set the deprecation message in `parsing_info`.
37
+ if let Some ( msg) = deprecation_message {
38
+ parsed_req. parsing_info ( ) . append_deprecation_message ( msg) ;
39
+ }
40
+
41
+ Ok ( parsed_req)
28
42
}
29
43
30
44
pub ( crate ) fn parse_patch_machine_config ( body : & Body ) -> Result < ParsedRequest , Error > {
@@ -39,17 +53,30 @@ pub(crate) fn parse_patch_machine_config(body: &Body) -> Result<ParsedRequest, E
39
53
return method_to_error ( Method :: Patch ) ;
40
54
}
41
55
42
- Ok ( ParsedRequest :: new_sync ( VmmAction :: UpdateVmConfiguration (
43
- config_update,
44
- ) ) )
56
+ // Check for the presence of deprecated `cpu_template` field.
57
+ let mut deprecation_message = None ;
58
+ if config_update. cpu_template . is_some ( ) {
59
+ // `cpu_template` field in request is deprecated.
60
+ METRICS . deprecated_api . deprecated_http_api_calls . inc ( ) ;
61
+ deprecation_message = Some ( "PATCH /machine-config: cpu_template is deprecated." ) ;
62
+ }
63
+
64
+ // Construct the `ParsedRequest` object.
65
+ let mut parsed_req = ParsedRequest :: new_sync ( VmmAction :: UpdateVmConfiguration ( config_update) ) ;
66
+ // If `cpu_template` was present, set the deprecation message in `parsing_info`.
67
+ if let Some ( msg) = deprecation_message {
68
+ parsed_req. parsing_info ( ) . append_deprecation_message ( msg) ;
69
+ }
70
+
71
+ Ok ( parsed_req)
45
72
}
46
73
47
74
#[ cfg( test) ]
48
75
mod tests {
49
76
use vmm:: cpu_config:: templates:: StaticCpuTemplate ;
50
77
51
78
use super :: * ;
52
- use crate :: parsed_request:: tests:: vmm_action_from_request;
79
+ use crate :: parsed_request:: tests:: { depr_action_from_req , vmm_action_from_request} ;
53
80
54
81
#[ test]
55
82
fn test_parse_get_machine_config_request ( ) {
@@ -79,6 +106,24 @@ mod tests {
79
106
"vcpu_count": 8,
80
107
"mem_size_mib": 1024
81
108
}"# ;
109
+ let expected_config = MachineConfigUpdate {
110
+ vcpu_count : Some ( 8 ) ,
111
+ mem_size_mib : Some ( 1024 ) ,
112
+ smt : Some ( false ) ,
113
+ cpu_template : None ,
114
+ track_dirty_pages : Some ( false ) ,
115
+ } ;
116
+
117
+ match vmm_action_from_request ( parse_put_machine_config ( & Body :: new ( body) ) . unwrap ( ) ) {
118
+ VmmAction :: UpdateVmConfiguration ( config) => assert_eq ! ( config, expected_config) ,
119
+ _ => panic ! ( "Test failed." ) ,
120
+ }
121
+
122
+ let body = r#"{
123
+ "vcpu_count": 8,
124
+ "mem_size_mib": 1024,
125
+ "cpu_template": "None"
126
+ }"# ;
82
127
let expected_config = MachineConfigUpdate {
83
128
vcpu_count : Some ( 8 ) ,
84
129
mem_size_mib : Some ( 1024 ) ,
@@ -102,7 +147,7 @@ mod tests {
102
147
vcpu_count : Some ( 8 ) ,
103
148
mem_size_mib : Some ( 1024 ) ,
104
149
smt : Some ( false ) ,
105
- cpu_template : Some ( StaticCpuTemplate :: None ) ,
150
+ cpu_template : None ,
106
151
track_dirty_pages : Some ( true ) ,
107
152
} ;
108
153
@@ -155,7 +200,7 @@ mod tests {
155
200
vcpu_count : Some ( 8 ) ,
156
201
mem_size_mib : Some ( 1024 ) ,
157
202
smt : Some ( true ) ,
158
- cpu_template : Some ( StaticCpuTemplate :: None ) ,
203
+ cpu_template : None ,
159
204
track_dirty_pages : Some ( true ) ,
160
205
} ;
161
206
@@ -209,4 +254,50 @@ mod tests {
209
254
let body = r#"{}"# ;
210
255
assert ! ( parse_patch_machine_config( & Body :: new( body) ) . is_err( ) ) ;
211
256
}
257
+
258
+ #[ test]
259
+ fn test_depr_cpu_template_in_put_req ( ) {
260
+ // Test that the deprecation message is shown when `cpu_template` is specified.
261
+ let body = r#"{
262
+ "vcpu_count": 8,
263
+ "mem_size_mib": 1024,
264
+ "cpu_template": "None"
265
+ }"# ;
266
+ depr_action_from_req (
267
+ parse_put_machine_config ( & Body :: new ( body) ) . unwrap ( ) ,
268
+ Some ( "PUT /machine-config: cpu_template field is deprecated." . to_string ( ) ) ,
269
+ ) ;
270
+
271
+ // Test that the deprecation message is not shown when `cpu_template` is not specified.
272
+ let body = r#"{
273
+ "vcpu_count": 8,
274
+ "mem_size_mib": 1024
275
+ }"# ;
276
+ let ( _, mut parsing_info) = parse_put_machine_config ( & Body :: new ( body) )
277
+ . unwrap ( )
278
+ . into_parts ( ) ;
279
+ assert ! ( parsing_info. take_deprecation_message( ) . is_none( ) ) ;
280
+ }
281
+
282
+ #[ test]
283
+ fn test_depr_cpu_template_in_patch_req ( ) {
284
+ // Test that the deprecation message is shown when `cpu_template` is specified.
285
+ let body = r#"{
286
+ "vcpu_count": 8,
287
+ "cpu_template": "None"
288
+ }"# ;
289
+ depr_action_from_req (
290
+ parse_patch_machine_config ( & Body :: new ( body) ) . unwrap ( ) ,
291
+ Some ( "PATCH /machine-config: cpu_template is deprecated." . to_string ( ) ) ,
292
+ ) ;
293
+
294
+ // Test that the deprecation message is not shown when `cpu_template` is not specified.
295
+ let body = r#"{
296
+ "vcpu_count": 8
297
+ }"# ;
298
+ let ( _, mut parsing_info) = parse_patch_machine_config ( & Body :: new ( body) )
299
+ . unwrap ( )
300
+ . into_parts ( ) ;
301
+ assert ! ( parsing_info. take_deprecation_message( ) . is_none( ) ) ;
302
+ }
212
303
}
0 commit comments