replace very confusing code with idiomatic rust
This commit is contained in:
parent
abf09e8d26
commit
1e893b49c0
42
src/state.rs
42
src/state.rs
@ -361,14 +361,14 @@ pub struct VM {
|
|||||||
storage_dir: String,
|
storage_dir: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VM {
|
impl From<VM> for snp_proto::MeasurementArgs {
|
||||||
fn to_grpc_response<T>(self, build_resp: impl FnOnce(snp_proto::MeasurementArgs) -> T) -> T {
|
fn from(vm: VM) -> Self {
|
||||||
let mut nic_index: u32 = if self.fw_ports.is_empty() { 0 } else { 1 };
|
let mut nic_index: u32 = if vm.fw_ports.is_empty() { 0 } else { 1 };
|
||||||
let mut ips = Vec::new();
|
let mut ips = Vec::new();
|
||||||
let mut dtrfs_api_endpoint = String::new();
|
let mut dtrfs_api_endpoint = String::new();
|
||||||
|
|
||||||
// TODO: when brain supports multiple IPs per VM, fix this
|
// TODO: when brain supports multiple IPs per VM, fix this
|
||||||
for nic in self.nics {
|
for nic in vm.nics {
|
||||||
for ip in nic.ips {
|
for ip in nic.ips {
|
||||||
if ip.address.parse::<std::net::Ipv4Addr>().is_ok() {
|
if ip.address.parse::<std::net::Ipv4Addr>().is_ok() {
|
||||||
dtrfs_api_endpoint = ip.address.clone();
|
dtrfs_api_endpoint = ip.address.clone();
|
||||||
@ -386,39 +386,37 @@ impl VM {
|
|||||||
if !dtrfs_api_endpoint.is_empty() {
|
if !dtrfs_api_endpoint.is_empty() {
|
||||||
dtrfs_api_endpoint += ":22";
|
dtrfs_api_endpoint += ":22";
|
||||||
} else {
|
} else {
|
||||||
dtrfs_api_endpoint += &format!("{}:{}", IP_INFO.ip, self.fw_ports[0].0);
|
dtrfs_api_endpoint += &format!("{}:{}", IP_INFO.ip, vm.fw_ports[0].0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let args = snp_proto::MeasurementArgs {
|
snp_proto::MeasurementArgs {
|
||||||
dtrfs_api_endpoint,
|
dtrfs_api_endpoint,
|
||||||
exposed_ports: self.fw_ports.iter().map(|(host_port, _)| *host_port as u32).collect(),
|
exposed_ports: vm.fw_ports.iter().map(|(host_port, _)| *host_port as u32).collect(),
|
||||||
ips,
|
ips,
|
||||||
ovmf_hash: OVMF_HASH.to_string(),
|
ovmf_hash: OVMF_HASH.to_string(),
|
||||||
};
|
}
|
||||||
|
|
||||||
build_resp(args)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Into<snp_proto::NewVmResp> for VM {
|
impl From<VM> for snp_proto::NewVmResp {
|
||||||
fn into(self) -> snp_proto::NewVmResp {
|
fn from(vm: VM) -> Self {
|
||||||
let uuid = self.uuid.clone();
|
let uuid = vm.uuid.clone();
|
||||||
self.to_grpc_response(|args| snp_proto::NewVmResp {
|
snp_proto::NewVmResp {
|
||||||
uuid,
|
uuid,
|
||||||
args: Some(args),
|
args: Some(vm.into()),
|
||||||
error: "".to_string(),
|
error: "".to_string(),
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Into<snp_proto::UpdateVmResp> for VM {
|
impl From<VM> for snp_proto::UpdateVmResp {
|
||||||
fn into(self) -> snp_proto::UpdateVmResp {
|
fn from(vm: VM) -> Self {
|
||||||
let uuid = self.uuid.clone();
|
let uuid = vm.uuid.clone();
|
||||||
self.to_grpc_response(|args| snp_proto::UpdateVmResp {
|
snp_proto::UpdateVmResp {
|
||||||
uuid,
|
uuid,
|
||||||
args: Some(args),
|
args: Some(vm.into()),
|
||||||
error: "".to_string(),
|
error: "".to_string(),
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user