@@ -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 field 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,23 @@ 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
+ assert_eq ! (
117
+ vmm_action_from_request( parse_put_machine_config( & Body :: new( body) ) . unwrap( ) ) ,
118
+ VmmAction :: UpdateVmConfiguration ( expected_config)
119
+ ) ;
120
+
121
+ let body = r#"{
122
+ "vcpu_count": 8,
123
+ "mem_size_mib": 1024,
124
+ "cpu_template": "None"
125
+ }"# ;
82
126
let expected_config = MachineConfigUpdate {
83
127
vcpu_count : Some ( 8 ) ,
84
128
mem_size_mib : Some ( 1024 ) ,
@@ -101,7 +145,7 @@ mod tests {
101
145
vcpu_count : Some ( 8 ) ,
102
146
mem_size_mib : Some ( 1024 ) ,
103
147
smt : Some ( false ) ,
104
- cpu_template : Some ( StaticCpuTemplate :: None ) ,
148
+ cpu_template : None ,
105
149
track_dirty_pages : Some ( true ) ,
106
150
} ;
107
151
assert_eq ! (
@@ -149,7 +193,7 @@ mod tests {
149
193
vcpu_count : Some ( 8 ) ,
150
194
mem_size_mib : Some ( 1024 ) ,
151
195
smt : Some ( true ) ,
152
- cpu_template : Some ( StaticCpuTemplate :: None ) ,
196
+ cpu_template : None ,
153
197
track_dirty_pages : Some ( true ) ,
154
198
} ;
155
199
assert_eq ! (
@@ -201,4 +245,50 @@ mod tests {
201
245
let body = r#"{}"# ;
202
246
assert ! ( parse_patch_machine_config( & Body :: new( body) ) . is_err( ) ) ;
203
247
}
248
+
249
+ #[ test]
250
+ fn test_depr_cpu_template_in_put_req ( ) {
251
+ // Test that the deprecation message is shown when `cpu_template` is specified.
252
+ let body = r#"{
253
+ "vcpu_count": 8,
254
+ "mem_size_mib": 1024,
255
+ "cpu_template": "None"
256
+ }"# ;
257
+ depr_action_from_req (
258
+ parse_put_machine_config ( & Body :: new ( body) ) . unwrap ( ) ,
259
+ Some ( "PUT /machine-config: cpu_template field is deprecated." . to_string ( ) ) ,
260
+ ) ;
261
+
262
+ // Test that the deprecation message is not shown when `cpu_template` is not specified.
263
+ let body = r#"{
264
+ "vcpu_count": 8,
265
+ "mem_size_mib": 1024
266
+ }"# ;
267
+ let ( _, mut parsing_info) = parse_put_machine_config ( & Body :: new ( body) )
268
+ . unwrap ( )
269
+ . into_parts ( ) ;
270
+ assert ! ( parsing_info. take_deprecation_message( ) . is_none( ) ) ;
271
+ }
272
+
273
+ #[ test]
274
+ fn test_depr_cpu_template_in_patch_req ( ) {
275
+ // Test that the deprecation message is shown when `cpu_template` is specified.
276
+ let body = r#"{
277
+ "vcpu_count": 8,
278
+ "cpu_template": "None"
279
+ }"# ;
280
+ depr_action_from_req (
281
+ parse_patch_machine_config ( & Body :: new ( body) ) . unwrap ( ) ,
282
+ Some ( "PATCH /machine-config: cpu_template field is deprecated." . to_string ( ) ) ,
283
+ ) ;
284
+
285
+ // Test that the deprecation message is not shown when `cpu_template` is not specified.
286
+ let body = r#"{
287
+ "vcpu_count": 8
288
+ }"# ;
289
+ let ( _, mut parsing_info) = parse_patch_machine_config ( & Body :: new ( body) )
290
+ . unwrap ( )
291
+ . into_parts ( ) ;
292
+ assert ! ( parsing_info. take_deprecation_message( ) . is_none( ) ) ;
293
+ }
204
294
}
0 commit comments