wasmer_deploy_schema/schema/
network.rs

1use bytesize::ByteSize;
2use serde::{Deserialize, Serialize};
3
4use crate::NetworkId;
5
6use super::Merge;
7
8#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
9pub struct NetworkTokenV1 {
10    pub network_id: NetworkId,
11}
12
13/// Instance network configuration.
14#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
15pub struct CapabilityNetworkV1 {
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub enabled: Option<bool>,
18
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub network_id: Option<NetworkId>,
21
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub ingress: Option<NetworkIngressV1>,
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub egress: Option<NetworkEgressV1>,
26
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub vpn: Option<NetworkVpnV1>,
29}
30
31impl Merge for CapabilityNetworkV1 {
32    fn merge_extend(self, other: &Self) -> Self {
33        Self {
34            enabled: self.enabled.merge_extend(&other.enabled),
35            network_id: self.network_id,
36            ingress: self.ingress.merge_extend(&other.ingress),
37            egress: self.egress.merge_extend(&other.egress),
38            vpn: self.vpn.merge_extend(&other.vpn),
39        }
40    }
41}
42
43#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
44pub struct NetworkIngressV1 {
45    pub enabled: Option<bool>,
46    #[schemars(with = "String")]
47    pub maximum_bandwidth_per_second: Option<ByteSize>,
48}
49
50impl Merge for NetworkIngressV1 {
51    fn merge_extend(self, other: &Self) -> Self {
52        Self {
53            enabled: self.enabled.merge_extend(&other.enabled),
54            maximum_bandwidth_per_second: self
55                .maximum_bandwidth_per_second
56                .merge_extend(&other.maximum_bandwidth_per_second),
57        }
58    }
59}
60
61#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
62pub struct NetworkFilterV1 {
63    /// List of domain names that the VPN connection is allowed to invoke
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub domains: Option<Vec<String>>,
66}
67
68impl Merge for NetworkFilterV1 {
69    fn merge_extend(self, other: &Self) -> Self {
70        Self {
71            domains: self.domains.merge_extend(&other.domains),
72        }
73    }
74}
75
76#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
77pub struct NetworkEgressV1 {
78    pub enabled: Option<bool>,
79    #[serde(skip_serializing_if = "Option::is_none")]
80    pub filter: Option<NetworkFilterV1>,
81    #[schemars(with = "String")]
82    pub maximum_bandwidth_per_second: Option<ByteSize>,
83}
84
85impl Merge for NetworkEgressV1 {
86    fn merge_extend(self, other: &Self) -> Self {
87        Self {
88            enabled: self.enabled.merge_extend(&other.enabled),
89            filter: self.filter.merge_extend(&other.filter),
90            maximum_bandwidth_per_second: self
91                .maximum_bandwidth_per_second
92                .merge_extend(&other.maximum_bandwidth_per_second),
93        }
94    }
95}
96
97#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
98pub struct NetworkVpnV1 {
99    pub enabled: Option<bool>,
100    pub public: Option<bool>,
101    pub can_egress: Option<bool>,
102    #[schemars(with = "String")]
103    pub maximum_bandwidth_per_second: Option<ByteSize>,
104}
105
106impl NetworkVpnV1 {
107    pub fn is_enabled(&self) -> bool {
108        self.enabled.unwrap_or(true)
109    }
110
111    pub fn is_public(&self) -> bool {
112        self.public.unwrap_or(true)
113    }
114
115    pub fn can_egress(&self) -> bool {
116        self.can_egress.unwrap_or(true)
117    }
118}
119
120impl Merge for NetworkVpnV1 {
121    fn merge_extend(self, other: &Self) -> Self {
122        Self {
123            enabled: self.enabled.merge_extend(&other.enabled),
124            public: self.public.merge_extend(&other.public),
125            can_egress: self.can_egress.merge_extend(&other.public),
126            maximum_bandwidth_per_second: self
127                .maximum_bandwidth_per_second
128                .merge_extend(&other.maximum_bandwidth_per_second),
129        }
130    }
131}