use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::{schema::DeployWorkloadToken, AppId};
pub type RawToken = String;
pub type NodeId = Uuid;
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InstanceId(Uuid);
impl InstanceId {
pub fn new_random() -> Self {
Self(Uuid::new_v4())
}
pub fn to_uuid(&self) -> Uuid {
self.0
}
pub fn from_uuid(uuid: Uuid) -> Self {
Self(uuid)
}
}
impl std::fmt::Display for InstanceId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for InstanceId {
type Err = uuid::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(Uuid::parse_str(s)?))
}
}
impl From<uuid::Uuid> for InstanceId {
fn from(uuid: uuid::Uuid) -> Self {
Self(uuid)
}
}
impl From<InstanceId> for uuid::Uuid {
fn from(id: InstanceId) -> Self {
id.0
}
}
#[derive(Debug, Clone, Serialize)]
pub struct InstanceMeta {
pub token: DeployWorkloadToken,
pub id: InstanceId,
pub app_id: Option<AppId>,
pub agent_id: String,
}
impl InstanceMeta {
pub fn new_for_token(token: DeployWorkloadToken, app_id: Option<AppId>) -> Self {
Self {
agent_id: token.data.subject().to_string(),
token,
id: InstanceId::new_random(),
app_id,
}
}
}