1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use bytesize::ByteSize;
use serde::{Deserialize, Serialize};

use crate::NetworkId;

use super::Merge;

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct NetworkTokenV1 {
    pub network_id: NetworkId,
}

/// Instance network configuration.
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct CapabilityNetworkV1 {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enabled: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub network_id: Option<NetworkId>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub ingress: Option<NetworkIngressV1>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub egress: Option<NetworkEgressV1>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub vpn: Option<NetworkVpnV1>,
}

impl Merge for CapabilityNetworkV1 {
    fn merge_extend(self, other: &Self) -> Self {
        Self {
            enabled: self.enabled.merge_extend(&other.enabled),
            network_id: self.network_id,
            ingress: self.ingress.merge_extend(&other.ingress),
            egress: self.egress.merge_extend(&other.egress),
            vpn: self.vpn.merge_extend(&other.vpn),
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct NetworkIngressV1 {
    pub enabled: Option<bool>,
    #[schemars(with = "String")]
    pub maximum_bandwidth_per_second: Option<ByteSize>,
}

impl Merge for NetworkIngressV1 {
    fn merge_extend(self, other: &Self) -> Self {
        Self {
            enabled: self.enabled.merge_extend(&other.enabled),
            maximum_bandwidth_per_second: self
                .maximum_bandwidth_per_second
                .merge_extend(&other.maximum_bandwidth_per_second),
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct NetworkFilterV1 {
    /// List of domain names that the VPN connection is allowed to invoke
    #[serde(skip_serializing_if = "Option::is_none")]
    pub domains: Option<Vec<String>>,
}

impl Merge for NetworkFilterV1 {
    fn merge_extend(self, other: &Self) -> Self {
        Self {
            domains: self.domains.merge_extend(&other.domains),
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct NetworkEgressV1 {
    pub enabled: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filter: Option<NetworkFilterV1>,
    #[schemars(with = "String")]
    pub maximum_bandwidth_per_second: Option<ByteSize>,
}

impl Merge for NetworkEgressV1 {
    fn merge_extend(self, other: &Self) -> Self {
        Self {
            enabled: self.enabled.merge_extend(&other.enabled),
            filter: self.filter.merge_extend(&other.filter),
            maximum_bandwidth_per_second: self
                .maximum_bandwidth_per_second
                .merge_extend(&other.maximum_bandwidth_per_second),
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
pub struct NetworkVpnV1 {
    pub enabled: Option<bool>,
    pub public: Option<bool>,
    pub can_egress: Option<bool>,
    #[schemars(with = "String")]
    pub maximum_bandwidth_per_second: Option<ByteSize>,
}

impl NetworkVpnV1 {
    pub fn is_enabled(&self) -> bool {
        self.enabled.unwrap_or(true)
    }

    pub fn is_public(&self) -> bool {
        self.public.unwrap_or(true)
    }

    pub fn can_egress(&self) -> bool {
        self.can_egress.unwrap_or(true)
    }
}

impl Merge for NetworkVpnV1 {
    fn merge_extend(self, other: &Self) -> Self {
        Self {
            enabled: self.enabled.merge_extend(&other.enabled),
            public: self.public.merge_extend(&other.public),
            can_egress: self.can_egress.merge_extend(&other.public),
            maximum_bandwidth_per_second: self
                .maximum_bandwidth_per_second
                .merge_extend(&other.maximum_bandwidth_per_second),
        }
    }
}