use std::fmt;
use std::net::SocketAddr;
use isothrum_runner::ConsoleProtocol;
use serde::{Deserialize, Serialize};
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ReachedConsole {
pub machine: String,
pub protocol: ConsoleProtocol,
pub endpoint: ConsoleEndpoint,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub user: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub password: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub tunnelled: bool,
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "at", rename_all = "snake_case")]
#[non_exhaustive]
pub enum ConsoleEndpoint {
Loopback {
address: SocketAddr,
},
ProxmoxVnc {
websocket: String,
port: u16,
ticket: String,
header: String,
authorization: String,
insecure: bool,
},
}
impl ReachedConsole {
#[must_use]
pub fn of(console: &isothrum_runner::Console) -> Option<ReachedConsole> {
let endpoint = match &console.endpoint {
isothrum_runner::Endpoint::Loopback(address) => ConsoleEndpoint::Loopback { address: *address },
isothrum_runner::Endpoint::ProxmoxVnc(vnc) => ConsoleEndpoint::ProxmoxVnc {
websocket: vnc.websocket.expose().to_owned(),
port: vnc.port,
ticket: vnc.ticket.expose().to_owned(),
header: vnc.authorization.0.clone(),
authorization: vnc.authorization.1.expose().to_owned(),
insecure: vnc.insecure,
},
_ => return None,
};
Some(ReachedConsole {
machine: console.machine.clone(),
protocol: console.protocol,
endpoint,
user: console.user.clone(),
password: console.password.as_ref().map(|p| p.expose().to_owned()),
note: console.note.clone(),
tunnelled: console.tunnel.is_some(),
})
}
}
impl fmt::Debug for ReachedConsole {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ReachedConsole")
.field("machine", &self.machine)
.field("protocol", &self.protocol)
.field("endpoint", &self.endpoint)
.field("user", &self.user)
.field("password", &self.password.as_ref().map(|_| "***"))
.field("note", &self.note)
.field("tunnelled", &self.tunnelled)
.finish()
}
}
impl fmt::Debug for ConsoleEndpoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ConsoleEndpoint::Loopback { address } => f.debug_struct("Loopback").field("address", address).finish(),
ConsoleEndpoint::ProxmoxVnc { port, header, insecure, .. } => f
.debug_struct("ProxmoxVnc")
.field("port", port)
.field("header", header)
.field("insecure", insecure)
.finish_non_exhaustive(),
}
}
}