Skip to content

Commit 5867621

Browse files
api_server: add get for machine-config
Signed-off-by: Andreea Florescu <fandree@amazon.com>
1 parent 5820a32 commit 5867621

4 files changed

Lines changed: 65 additions & 10 deletions

File tree

api_server/src/http_service.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,15 @@ fn parse_machine_config_req<'a>(
183183
body: &Chunk,
184184
) -> Result<'a, ParsedRequest> {
185185
match path_tokens[1..].len() {
186-
0 if method == Method::Get => Ok(ParsedRequest::Dummy),
186+
0 if method == Method::Get => {
187+
let empty_machine_config = MachineConfiguration {
188+
vcpu_count: None,
189+
mem_size_mib: None,
190+
};
191+
Ok(empty_machine_config
192+
.into_parsed_request(method)
193+
.map_err(|s| Error::Generic(StatusCode::BadRequest, s))?)
194+
}
187195

188196
0 if method == Method::Put => Ok(serde_json::from_slice::<MachineConfiguration>(body)
189197
.map_err(Error::SerdeJson)?
@@ -860,10 +868,7 @@ mod tests {
860868
let body: Chunk = Chunk::from(json);
861869

862870
// GET
863-
match parse_machine_config_req(&path_tokens, &path, Method::Get, &body) {
864-
Ok(pr_dummy) => assert!(pr_dummy.eq(&ParsedRequest::Dummy)),
865-
_ => assert!(false),
866-
}
871+
assert!(parse_machine_config_req(&path_tokens, &path, Method::Get, &body).is_ok());
867872

868873
assert!(
869874
parse_machine_config_req(

api_server/src/request/sync/machine_configuration.rs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,41 @@ impl GenerateResponse for PutMachineConfigurationOutcome {
4848
}
4949
}
5050

51+
impl GenerateResponse for MachineConfiguration {
52+
fn generate_response(&self) -> Response {
53+
let vcpu_count = match self.vcpu_count {
54+
Some(v) => v.to_string(),
55+
None => String::from("Uninitialized"),
56+
};
57+
let mem_size = match self.mem_size_mib {
58+
Some(v) => v.to_string(),
59+
None => String::from("Uninitialized"),
60+
};
61+
62+
json_response(
63+
StatusCode::Ok,
64+
format!(
65+
"{{ \"vcpu_count\": {:?}, \"mem_size_mib\": {:?} }}",
66+
vcpu_count, mem_size
67+
),
68+
)
69+
}
70+
}
71+
5172
impl IntoParsedRequest for MachineConfiguration {
52-
fn into_parsed_request(self, _method: Method) -> result::Result<ParsedRequest, String> {
73+
fn into_parsed_request(self, method: Method) -> result::Result<ParsedRequest, String> {
5374
let (sender, receiver) = oneshot::channel();
54-
Ok(ParsedRequest::Sync(
55-
SyncRequest::PutMachineConfiguration(self, sender),
56-
receiver,
57-
))
75+
match method {
76+
Method::Get => Ok(ParsedRequest::Sync(
77+
SyncRequest::GetMachineConfiguration(sender),
78+
receiver,
79+
)),
80+
Method::Put => Ok(ParsedRequest::Sync(
81+
SyncRequest::PutMachineConfiguration(self, sender),
82+
receiver,
83+
)),
84+
_ => Ok(ParsedRequest::Dummy),
85+
}
5886
}
5987
}
6088

@@ -115,5 +143,20 @@ mod tests {
115143
receiver
116144
)))
117145
);
146+
let uninitialized = MachineConfiguration {
147+
vcpu_count: None,
148+
mem_size_mib: None,
149+
};
150+
assert!(
151+
uninitialized
152+
.clone()
153+
.into_parsed_request(Method::Get)
154+
.is_ok()
155+
);
156+
assert!(
157+
uninitialized
158+
.into_parsed_request(Method::Patch)
159+
.eq(&Ok(ParsedRequest::Dummy))
160+
);
118161
}
119162
}

api_server/src/request/sync/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub enum DeviceState {
5353
// This enum contains messages for the VMM which represent sync requests. They each contain various
5454
// bits of information (ids, paths, etc.), together with an OutcomeSender, which is always present.
5555
pub enum SyncRequest {
56+
GetMachineConfiguration(SyncOutcomeSender),
5657
PutBootSource(BootSourceBody, SyncOutcomeSender),
5758
PutDrive(DriveDescription, SyncOutcomeSender),
5859
PutMachineConfiguration(MachineConfiguration, SyncOutcomeSender),

vmm/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,12 @@ impl Vmm {
900900
}
901901
ApiRequest::Sync(req) => {
902902
match req {
903+
SyncRequest::GetMachineConfiguration(sender) => {
904+
sender
905+
.send(Box::new(self.vm_config.clone()))
906+
.map_err(|_| ())
907+
.expect("one-shot channel closed");
908+
}
903909
SyncRequest::PutDrive(drive_description, sender) => {
904910
match self.put_block_device(BlockDeviceConfig::from(drive_description)) {
905911
Ok(_) =>

0 commit comments

Comments
 (0)