edge_schema/
instance.rs

1use std::sync::Arc;
2
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6use crate::schema::{AppMeta, 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 nil() -> Self {
24        Self(Uuid::nil())
25    }
26
27    pub fn to_uuid(&self) -> Uuid {
28        self.0
29    }
30
31    pub fn from_uuid(uuid: Uuid) -> Self {
32        Self(uuid)
33    }
34}
35
36impl std::fmt::Display for InstanceId {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        write!(f, "{}", self.0)
39    }
40}
41
42impl std::str::FromStr for InstanceId {
43    type Err = uuid::Error;
44
45    fn from_str(s: &str) -> Result<Self, Self::Err> {
46        Ok(Self(Uuid::parse_str(s)?))
47    }
48}
49
50impl From<uuid::Uuid> for InstanceId {
51    fn from(uuid: uuid::Uuid) -> Self {
52        Self(uuid)
53    }
54}
55
56impl From<InstanceId> for uuid::Uuid {
57    fn from(id: InstanceId) -> Self {
58        id.0
59    }
60}
61
62#[derive(Clone, Debug, PartialEq, Eq)]
63pub struct WorkloadMeta {
64    pub workload: WorkloadV2,
65
66    // TODO(theduke): store whole AppVersionV1 entity intstead.
67    pub app_meta: Option<AppMeta>,
68}
69
70impl WorkloadMeta {
71    pub fn agent(&self) -> Option<&str> {
72        // TODO: implement agent passing/determination for metering.
73        None
74    }
75}
76
77/// Metadata for a running instance.
78#[derive(Debug, Clone)]
79pub struct InstanceMeta {
80    /// Unique, randomly generated UUID for the instance.
81    pub id: InstanceId,
82    /// Workload related metadata.
83    pub workload: Arc<WorkloadMeta>,
84}
85
86impl InstanceMeta {
87    pub fn new(workload: Arc<WorkloadMeta>) -> Self {
88        Self {
89            id: InstanceId::new_random(),
90            workload,
91        }
92    }
93}