@@ -5,7 +5,7 @@ use serde_json::{Map, Value};
5
5
6
6
use super :: super :: VmmAction ;
7
7
use logger:: { Metric , METRICS } ;
8
- use request:: { Body , checked_id , Error , ParsedRequest , StatusCode } ;
8
+ use request:: { checked_id , Body , Error , ParsedRequest , StatusCode } ;
9
9
use vmm:: vmm_config:: drive:: BlockDeviceConfig ;
10
10
11
11
#[ derive( Clone ) ]
@@ -76,10 +76,7 @@ impl PatchDrivePayload {
76
76
}
77
77
}
78
78
79
- pub fn parse_put_drive (
80
- maybe_body : Option < & Body > ,
81
- id_from_path : Option < & & str > ,
82
- ) -> Result < ParsedRequest , Error > {
79
+ pub fn parse_put_drive ( body : & Body , id_from_path : Option < & & str > ) -> Result < ParsedRequest , Error > {
83
80
METRICS . put_api_requests . drive_count . inc ( ) ;
84
81
let id = match id_from_path {
85
82
Some ( & id) => checked_id ( id) ?,
@@ -88,35 +85,25 @@ pub fn parse_put_drive(
88
85
}
89
86
} ;
90
87
91
- if let Some ( body) = maybe_body {
92
- let device_cfg = serde_json:: from_slice :: < BlockDeviceConfig > ( body. raw ( ) ) . map_err ( |e| {
93
- METRICS . put_api_requests . drive_fails . inc ( ) ;
94
- Error :: SerdeJson ( e)
95
- } ) ?;
88
+ let device_cfg = serde_json:: from_slice :: < BlockDeviceConfig > ( body. raw ( ) ) . map_err ( |e| {
89
+ METRICS . put_api_requests . drive_fails . inc ( ) ;
90
+ Error :: SerdeJson ( e)
91
+ } ) ?;
96
92
97
- if id != device_cfg. drive_id {
98
- METRICS . put_api_requests . drive_fails . inc ( ) ;
99
- Err ( Error :: Generic (
100
- StatusCode :: BadRequest ,
101
- "The id from the path does not match the id from the body!" . to_string ( ) ,
102
- ) )
103
- } else {
104
- Ok ( ParsedRequest :: Sync ( VmmAction :: InsertBlockDevice (
105
- device_cfg,
106
- ) ) )
107
- }
108
- } else {
93
+ if id != device_cfg. drive_id {
94
+ METRICS . put_api_requests . drive_fails . inc ( ) ;
109
95
Err ( Error :: Generic (
110
96
StatusCode :: BadRequest ,
111
- "Empty PUT request. " . to_string ( ) ,
97
+ "The id from the path does not match the id from the body! " . to_string ( ) ,
112
98
) )
99
+ } else {
100
+ Ok ( ParsedRequest :: Sync ( VmmAction :: InsertBlockDevice (
101
+ device_cfg,
102
+ ) ) )
113
103
}
114
104
}
115
105
116
- pub fn parse_patch_drive (
117
- maybe_body : Option < & Body > ,
118
- id_from_path : Option < & & str > ,
119
- ) -> Result < ParsedRequest , Error > {
106
+ pub fn parse_patch_drive ( body : & Body , id_from_path : Option < & & str > ) -> Result < ParsedRequest , Error > {
120
107
METRICS . patch_api_requests . drive_count . inc ( ) ;
121
108
let id = match id_from_path {
122
109
Some ( & id) => checked_id ( id) ?,
@@ -125,37 +112,30 @@ pub fn parse_patch_drive(
125
112
}
126
113
} ;
127
114
128
- if let Some ( body) = maybe_body {
129
- METRICS . patch_api_requests . drive_count . inc ( ) ;
130
- let patch_drive_payload = PatchDrivePayload {
131
- fields : serde_json:: from_slice ( body. raw ( ) ) . map_err ( |e| {
132
- METRICS . patch_api_requests . drive_fails . inc ( ) ;
133
- Error :: SerdeJson ( e)
134
- } ) ?,
135
- } ;
136
-
137
- patch_drive_payload. validate ( ) ?;
138
- let drive_id: String = patch_drive_payload. get_string_field_unchecked ( "drive_id" ) ;
139
- let path_on_host: String = patch_drive_payload. get_string_field_unchecked ( "path_on_host" ) ;
140
-
141
- if id != drive_id. as_str ( ) {
115
+ METRICS . patch_api_requests . drive_count . inc ( ) ;
116
+ let patch_drive_payload = PatchDrivePayload {
117
+ fields : serde_json:: from_slice ( body. raw ( ) ) . map_err ( |e| {
142
118
METRICS . patch_api_requests . drive_fails . inc ( ) ;
143
- return Err ( Error :: Generic (
144
- StatusCode :: BadRequest ,
145
- String :: from ( "The id from the path does not match the id from the body!" ) ,
146
- ) ) ;
147
- }
119
+ Error :: SerdeJson ( e)
120
+ } ) ?,
121
+ } ;
148
122
149
- Ok ( ParsedRequest :: Sync ( VmmAction :: UpdateBlockDevicePath (
150
- drive_id,
151
- path_on_host,
152
- ) ) )
153
- } else {
154
- Err ( Error :: Generic (
123
+ patch_drive_payload. validate ( ) ?;
124
+ let drive_id: String = patch_drive_payload. get_string_field_unchecked ( "drive_id" ) ;
125
+ let path_on_host: String = patch_drive_payload. get_string_field_unchecked ( "path_on_host" ) ;
126
+
127
+ if id != drive_id. as_str ( ) {
128
+ METRICS . patch_api_requests . drive_fails . inc ( ) ;
129
+ return Err ( Error :: Generic (
155
130
StatusCode :: BadRequest ,
156
- "Empty PATCH request." . to_string ( ) ,
157
- ) )
131
+ String :: from ( "The id from the path does not match the id from the body!" ) ,
132
+ ) ) ;
158
133
}
134
+
135
+ Ok ( ParsedRequest :: Sync ( VmmAction :: UpdateBlockDevicePath (
136
+ drive_id,
137
+ path_on_host,
138
+ ) ) )
159
139
}
160
140
161
141
#[ cfg( test) ]
@@ -164,45 +144,43 @@ mod tests {
164
144
165
145
#[ test]
166
146
fn test_parse_patch_request ( ) {
167
- assert ! ( parse_patch_drive( None , None ) . is_err( ) ) ;
168
- assert ! ( parse_patch_drive( None , Some ( & "id" ) ) . is_err( ) ) ;
169
- assert ! ( parse_patch_drive( Some ( & Body :: new( "invalid_payload" ) ) , Some ( & "id" ) ) . is_err( ) ) ;
147
+ assert ! ( parse_patch_drive( & Body :: new( "invalid_payload" ) , Some ( & "id" ) ) . is_err( ) ) ;
170
148
171
149
// PATCH with invalid fields.
172
150
let body = r#"{
173
151
"drive_id": "bar",
174
152
"is_read_only": false
175
153
}"# ;
176
- assert ! ( parse_patch_drive( Some ( & Body :: new( body) ) , Some ( & "2" ) ) . is_err( ) ) ;
154
+ assert ! ( parse_patch_drive( & Body :: new( body) , Some ( & "2" ) ) . is_err( ) ) ;
177
155
178
156
// PATCH with invalid types on fields. Adding a drive_id as number instead of string.
179
157
let body = r#"{
180
158
"drive_id": 1000,
181
159
"path_on_host": "dummy"
182
160
}"# ;
183
- let res = parse_patch_drive ( Some ( & Body :: new ( body) ) , Some ( & "1000" ) ) ;
161
+ let res = parse_patch_drive ( & Body :: new ( body) , Some ( & "1000" ) ) ;
184
162
assert ! ( res. is_err( ) ) ;
185
163
186
164
// PATCH with invalid types on fields. Adding a path_on_host as bool instead of string.
187
165
let body = r#"{
188
166
"drive_id": 1000,
189
167
"path_on_host": true
190
168
}"# ;
191
- let res = parse_patch_drive ( Some ( & Body :: new ( body) ) , Some ( & "1000" ) ) ;
169
+ let res = parse_patch_drive ( & Body :: new ( body) , Some ( & "1000" ) ) ;
192
170
assert ! ( res. is_err( ) ) ;
193
171
194
172
// PATCH with missing path_on_host field.
195
173
let body = r#"{
196
174
"drive_id": "dummy_id"
197
175
}"# ;
198
- let res = parse_patch_drive ( Some ( & Body :: new ( body) ) , Some ( & "dummy_id" ) ) ;
176
+ let res = parse_patch_drive ( & Body :: new ( body) , Some ( & "dummy_id" ) ) ;
199
177
assert ! ( res. is_err( ) ) ;
200
178
201
179
// PATCH with missing drive_id field.
202
180
let body = r#"{
203
181
"path_on_host": true
204
182
}"# ;
205
- let res = parse_patch_drive ( Some ( & Body :: new ( body) ) , Some ( & "1000" ) ) ;
183
+ let res = parse_patch_drive ( & Body :: new ( body) , Some ( & "1000" ) ) ;
206
184
assert ! ( res. is_err( ) ) ;
207
185
208
186
// PATCH that tries to update something else other than path_on_host.
@@ -211,20 +189,20 @@ mod tests {
211
189
"path_on_host": "dummy",
212
190
"is_read_only": false
213
191
}"# ;
214
- let res = parse_patch_drive ( Some ( & Body :: new ( body) ) , Some ( & "1234" ) ) ;
192
+ let res = parse_patch_drive ( & Body :: new ( body) , Some ( & "1234" ) ) ;
215
193
assert ! ( res. is_err( ) ) ;
216
194
217
195
// PATCH with payload that is not a json.
218
196
let body = r#"{
219
197
"fields": "dummy_field"
220
198
}"# ;
221
- assert ! ( parse_patch_drive( Some ( & Body :: new( body) ) , Some ( & "1234" ) ) . is_err( ) ) ;
199
+ assert ! ( parse_patch_drive( & Body :: new( body) , Some ( & "1234" ) ) . is_err( ) ) ;
222
200
223
201
let body = r#"{
224
202
"drive_id": "foo",
225
203
"path_on_host": "dummy"
226
204
}"# ;
227
- match parse_patch_drive ( Some ( & Body :: new ( body) ) , Some ( & "foo" ) ) {
205
+ match parse_patch_drive ( & Body :: new ( body) , Some ( & "foo" ) ) {
228
206
Ok ( ParsedRequest :: Sync ( VmmAction :: UpdateBlockDevicePath ( a, b) ) ) => {
229
207
assert_eq ! ( a, "foo" . to_string( ) ) ;
230
208
assert_eq ! ( b, "dummy" . to_string( ) ) ;
@@ -237,21 +215,19 @@ mod tests {
237
215
"drive_id": "foo",
238
216
"path_on_host": "dummy"
239
217
}"# ;
240
- assert ! ( parse_patch_drive( Some ( & Body :: new( body) ) , Some ( & "bar" ) ) . is_err( ) ) ;
218
+ assert ! ( parse_patch_drive( & Body :: new( body) , Some ( & "bar" ) ) . is_err( ) ) ;
241
219
}
242
220
243
221
#[ test]
244
222
fn test_parse_put_request ( ) {
245
- assert ! ( parse_put_drive( None , None ) . is_err( ) ) ;
246
- assert ! ( parse_put_drive( None , Some ( & "id" ) ) . is_err( ) ) ;
247
- assert ! ( parse_put_drive( Some ( & Body :: new( "invalid_payload" ) ) , Some ( & "id" ) ) . is_err( ) ) ;
223
+ assert ! ( parse_put_drive( & Body :: new( "invalid_payload" ) , Some ( & "id" ) ) . is_err( ) ) ;
248
224
249
225
// PATCH with invalid fields.
250
226
let body = r#"{
251
227
"drive_id": "bar",
252
228
"is_read_only": false
253
229
}"# ;
254
- assert ! ( parse_put_drive( Some ( & Body :: new( body) ) , Some ( & "2" ) ) . is_err( ) ) ;
230
+ assert ! ( parse_put_drive( & Body :: new( body) , Some ( & "2" ) ) . is_err( ) ) ;
255
231
256
232
// PATCH with invalid types on fields. Adding a drive_id as number instead of string.
257
233
let body = r#"{
@@ -273,9 +249,9 @@ mod tests {
273
249
}
274
250
}
275
251
}"# ;
276
- assert ! ( parse_put_drive( Some ( & Body :: new( body) ) , Some ( & "1000" ) ) . is_ok( ) ) ;
252
+ assert ! ( parse_put_drive( & Body :: new( body) , Some ( & "1000" ) ) . is_ok( ) ) ;
277
253
278
- assert ! ( parse_put_drive( Some ( & Body :: new( body) ) , Some ( & "foo" ) ) . is_err( ) ) ;
254
+ assert ! ( parse_put_drive( & Body :: new( body) , Some ( & "foo" ) ) . is_err( ) ) ;
279
255
}
280
256
281
257
#[ test]
0 commit comments