wasmer_deploy_schema/
instance.rs

1use std::sync::Arc;
2
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6use crate::schema::{AppMeta, DeployWorkloadToken, WorkloadV2};
7
8// TODO: Figure out token type.
9pub type RawToken = String;
10
11/// Id of the node - aka server.
12pub type NodeId = Uuid;
13
14/// Id of a single Webassembly instance.
15#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)]
16pub struct InstanceId(Uuid);
17
18impl InstanceId {
19    pub fn new_random() -> Self {
20        Self(Uuid::new_v4())
21    }
22
23    pub fn to_uuid(&self) -> Uuid {
24        self.0
25    }
26
27    pub fn from_uuid(uuid: Uuid) -> Self {
28        Self(uuid)
29    }
30}
31
32impl std::fmt::Display for InstanceId {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        write!(f, "{}", self.0)
35    }
36}
37
38impl std::str::FromStr for InstanceId {
39    type Err = uuid::Error;
40
41    fn from_str(s: &str) -> Result<Self, Self::Err> {
42        Ok(Self(Uuid::parse_str(s)?))
43    }
44}
45
46impl From<uuid::Uuid> for InstanceId {
47    fn from(uuid: uuid::Uuid) -> Self {
48        Self(uuid)
49    }
50}
51
52impl From<InstanceId> for uuid::Uuid {
53    fn from(id: InstanceId) -> Self {
54        id.0
55    }
56}
57
58#[derive(Clone, Debug, PartialEq, Eq)]
59pub struct WorkloadMeta {
60    /// Token used to create the instance.
61    ///
62    /// Allows associating the instance with an agent.
63    pub token: Option<DeployWorkloadToken>,
64
65    pub workload: WorkloadV2,
66
67    // TODO(theduke): store whole AppVersionV1 entity intstead.
68    pub app_meta: Option<AppMeta>,
69}
70
71impl WorkloadMeta {
72    pub fn agent(&self) -> Option<&str> {
73        if let Some(tok) = &self.token {
74            Some(tok.data.subject())
75        } else {
76            None
77        }
78    }
79}
80
81/// Metadata for a running instance.
82#[derive(Debug, Clone)]
83pub struct InstanceMeta {
84    /// Unique, randomly generated UUID for the instance.
85    pub id: InstanceId,
86    /// Workload related metadata.
87    pub workload: Arc<WorkloadMeta>,
88}
89
90impl InstanceMeta {
91    pub fn new(workload: Arc<WorkloadMeta>) -> Self {
92        Self {
93            id: InstanceId::new_random(),
94            workload,
95        }
96    }
97}