podman_api/opts/
networks.rs

1use crate::{models, Value};
2
3use containers_api::opts::{Filter, FilterItem};
4use containers_api::{
5    impl_field, impl_filter_func, impl_map_field, impl_opts_builder, impl_str_field, impl_vec_field,
6};
7
8impl_opts_builder!(json =>
9    /// Adjust how a network is created.
10    NetworkCreate
11);
12
13impl NetworkCreateOptsBuilder {
14    impl_str_field!(
15        /// The timestamp when this network was created. Example: `"2019-08-24T14:15:22Z"`
16        created => "created"
17    );
18
19    impl_field!(
20        /// Whether name resolution is active for container on this Network.
21        dns_enabled: bool => "dns_enabled"
22    );
23
24    impl_str_field!(
25        /// Driver for this Network, e.g. bridge, macvlan...
26        driver => "driver"
27    );
28
29    impl_str_field!(
30        /// ID of the Network.
31        id => "id"
32    );
33
34    impl_field!(
35        /// Whether the Network should not have external routes to public or other Networks.
36        internal: bool => "internal"
37    );
38
39    impl_map_field!(json
40        /// Contains options used for the ip assignment.
41        ipam_options => "ipam_options"
42    );
43
44    impl_field!(
45        /// If set to true an ipv6 subnet should be created for this net.
46        ipv6_enabled: bool => "ipv6_enabled"
47    );
48
49    impl_map_field!(json
50        /// A set of key-value labels that have been applied to the Network.
51        labels => "labels"
52    );
53
54    impl_str_field!(
55        /// Name of the Network.
56        name => "name"
57    );
58
59    impl_str_field!(
60        /// The network interface name on the host.
61        network_interface => "network_interface"
62    );
63
64    impl_map_field!(json
65        /// A set of key-value options that have been applied to the Network.
66        options => "options"
67    );
68
69    impl_vec_field!(
70        /// Subnets to use for this network.
71        subnets: models::Subnet => "subnets"
72    );
73}
74
75#[derive(Debug)]
76/// Used to filter listed network configurations by one of the variants.
77pub enum NetworkListFilter {
78    /// Matches network name (accepts regex).
79    Name(String),
80    /// Matches for full or partial ID.
81    Id(crate::Id),
82    /// Only bridge is supported.
83    Driver(String),
84    /// Matches networks with key label.
85    LabelKey(String),
86    /// Matches networks with key=value label.
87    LabelKeyVal(String, String),
88    /// Matches networks without key label.
89    NoLabelKey(String),
90    /// Matches networks without key=value label.
91    NoLabelKeyVal(String, String),
92    /// Matches all networks that were create before the given timestamp.
93    // TODO: use DateTime
94    Until(String),
95}
96
97impl Filter for NetworkListFilter {
98    fn query_item(&self) -> FilterItem {
99        use NetworkListFilter::*;
100        match &self {
101            Name(name) => FilterItem::new("name", name.clone()),
102            Id(id) => FilterItem::new("id", id.to_string()),
103            Driver(driver) => FilterItem::new("driver", driver.clone()),
104            LabelKey(key) => FilterItem::new("label", key.clone()),
105            LabelKeyVal(key, val) => FilterItem::new("label", format!("{key}={val}")),
106            NoLabelKey(key) => FilterItem::new("label!", key.clone()),
107            NoLabelKeyVal(key, val) => FilterItem::new("label!", format!("{key}={val}")),
108            Until(until) => FilterItem::new("until", until.clone()),
109        }
110    }
111}
112
113impl_opts_builder!(url =>
114    /// Adjust how networks are listed.
115    NetworkList
116);
117
118impl NetworkListOptsBuilder {
119    impl_filter_func!(NetworkListFilter);
120}
121
122#[derive(Debug)]
123/// Used to filter unused networks to remove.
124pub enum NetworkPruneFilter {
125    /// Matches networks with key label.
126    LabelKey(String),
127    /// Matches networks with key=value label.
128    LabelKeyVal(String, String),
129    /// Matches networks without key label.
130    NoLabelKey(String),
131    /// Matches networks without key=value label.
132    NoLabelKeyVal(String, String),
133    /// Matches all networks that were create before the given timestamp.
134    Until(String),
135}
136
137impl Filter for NetworkPruneFilter {
138    fn query_item(&self) -> FilterItem {
139        use NetworkPruneFilter::*;
140        match &self {
141            LabelKey(key) => FilterItem::new("label", key.clone()),
142            LabelKeyVal(key, val) => FilterItem::new("label", format!("{key}={val}")),
143            NoLabelKey(key) => FilterItem::new("label!", key.clone()),
144            NoLabelKeyVal(key, val) => FilterItem::new("label!", format!("{key}={val}")),
145            Until(until) => FilterItem::new("until", until.clone()),
146        }
147    }
148}
149
150impl_opts_builder!(url =>
151    /// Adjust how unused networks are removed.
152    NetworkPrune
153);
154
155impl NetworkPruneOptsBuilder {
156    impl_filter_func!(NetworkPruneFilter);
157}
158
159impl_opts_builder!(json =>
160    /// Adjust how a container is disconnected from a network.
161    NetworkDisconnect
162);
163
164impl NetworkDisconnectOptsBuilder {
165    impl_str_field!(
166        /// Name or ID of the container to disconnect.
167        container => "Container"
168    );
169
170    impl_field!(
171        /// Force disconnect the container.
172        force: bool => "Force"
173    );
174}
175
176impl_opts_builder!(json =>
177    /// Adjust how a container is connected to a network.
178    NetworkConnect
179);
180
181impl NetworkConnectOptsBuilder {
182    impl_vec_field!(
183        /// Aliases contains a list of names which the dns server should resolve to this container.
184        /// Should only be set when DNSEnabled is true on the Network. If aliases are set but there
185        /// is no dns support for this network the network interface implementation should ignore
186        /// this and NOT error.
187        aliases => "aliases"
188    );
189
190    impl_str_field!(
191        container => "container"
192    );
193
194    impl_str_field!(
195        /// Interface name for this container. Required in the backend. Optional in the frontend.
196        /// Will be filled with ethX (where X is a integer) when empty.
197        interface_name => "interface_name"
198    );
199
200    /// Static IPs for the container.
201    pub fn static_ips<I>(mut self, ips: impl IntoIterator<Item = I>) -> Self
202    where
203        I: IntoIterator<Item = u8>,
204    {
205        let ips: Vec<Vec<_>> = ips.into_iter().map(|a| a.into_iter().collect()).collect();
206        self.params.insert("static_ips", serde_json::json!(ips));
207        self
208    }
209
210    /// Static mac for the container.
211    pub fn static_mac(mut self, mac: impl IntoIterator<Item = u8>) -> Self {
212        let mac: Vec<_> = mac.into_iter().collect();
213        self.params.insert("static_mac", serde_json::json!(mac));
214        self
215    }
216}
217
218impl NetworkConnectOpts {
219    pub(crate) fn for_container(&self, container: &crate::Id) -> Self {
220        let mut new = self.clone();
221        new.params
222            .insert("container", Value::String(container.to_string()));
223        new
224    }
225}