Skip to main content

docker/
model.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5fn serialize_as_json<T, S>(t: &T, s: S) -> Result<S::Ok, S::Error>
6where
7    T: Serialize,
8    S: serde::Serializer,
9{
10    s.serialize_str(&serde_json::to_string(t).map_err(|e| serde::ser::Error::custom(format!("{e}")))?)
11}
12
13#[derive(Debug, Clone, Default, PartialEq, Serialize)]
14pub struct ListContainersOptions {
15    /// Return all containers. By default, only running containers are shown
16    pub all: bool,
17    /// Return this number of most recently created containers, including non-running ones
18    pub limit: Option<isize>,
19    /// Return the size of container as fields `SizeRw` and `SizeRootFs`
20    pub size: bool,
21
22    /// See Docker's documentation to learn how to use filters
23    /// https://docs.docker.com/reference/cli/docker/container/ls/#filter
24    #[serde(serialize_with = "serialize_as_json")]
25    pub filters: HashMap<String, Vec<String>>,
26}
27
28#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
29pub struct ContainerSummary {
30    /// The ID of this container
31    #[serde(rename = "Id")]
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub id: Option<String>,
34
35    /// The names that this container has been given
36    #[serde(rename = "Names")]
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub names: Option<Vec<String>>,
39
40    /// The name of the image used when creating this container
41    #[serde(rename = "Image")]
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub image: Option<String>,
44
45    /// The ID of the image that this container was created from
46    #[serde(rename = "ImageID")]
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub image_id: Option<String>,
49
50    /// Command to run when starting the container
51    #[serde(rename = "Command")]
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub command: Option<String>,
54
55    /// When the container was created
56    #[serde(rename = "Created")]
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub created: Option<i64>,
59
60    /// The ports exposed by this container
61    #[serde(rename = "Ports")]
62    #[serde(skip_serializing_if = "Option::is_none")]
63    pub ports: Option<Vec<Port>>,
64
65    /// The size of files that have been created or changed by this container
66    #[serde(rename = "SizeRw")]
67    #[serde(skip_serializing_if = "Option::is_none")]
68    pub size_rw: Option<i64>,
69
70    /// The total size of all the files in this container
71    #[serde(rename = "SizeRootFs")]
72    #[serde(skip_serializing_if = "Option::is_none")]
73    pub size_root_fs: Option<i64>,
74
75    /// User-defined key/value metadata.
76    #[serde(rename = "Labels")]
77    #[serde(skip_serializing_if = "Option::is_none")]
78    pub labels: Option<HashMap<String, String>>,
79
80    /// The state of this container (e.g. `Exited`)
81    #[serde(rename = "State")]
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub state: Option<String>,
84
85    /// Additional human-readable status of this container (e.g. `Exit 0`)
86    #[serde(rename = "Status")]
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub status: Option<String>,
89
90    #[serde(rename = "HostConfig")]
91    #[serde(skip_serializing_if = "Option::is_none")]
92    pub host_config: Option<ContainerSummaryHostConfig>,
93
94    #[serde(rename = "NetworkSettings")]
95    #[serde(skip_serializing_if = "Option::is_none")]
96    pub network_settings: Option<ContainerSummaryNetworkSettings>,
97
98    #[serde(rename = "Mounts")]
99    #[serde(skip_serializing_if = "Option::is_none")]
100    pub mounts: Option<Vec<MountPoint>>,
101}
102
103#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
104pub struct ContainerSummaryHostConfig {
105    #[serde(rename = "NetworkMode")]
106    #[serde(skip_serializing_if = "Option::is_none")]
107    pub network_mode: Option<String>,
108}
109
110/// A summary of the container's network settings
111#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
112pub struct ContainerSummaryNetworkSettings {
113    #[serde(rename = "Networks")]
114    #[serde(skip_serializing_if = "Option::is_none")]
115    pub networks: Option<HashMap<String, EndpointSettings>>,
116}
117
118/// An open port on a container
119#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
120pub struct Port {
121    /// Host IP address that the container's port is mapped to
122    #[serde(rename = "IP")]
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub ip: Option<String>,
125
126    /// Port on the container
127    #[serde(rename = "PrivatePort")]
128    pub private_port: u16,
129
130    /// Port exposed on the host
131    #[serde(rename = "PublicPort")]
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub public_port: Option<u16>,
134
135    #[serde(rename = "Type")]
136    #[serde(skip_serializing_if = "Option::is_none")]
137    // #[serde(with = "::serde_with::As::<::serde_with::NoneAsEmptyString>")]
138    pub typ: Option<PortTypeEnum>,
139}
140
141#[allow(non_camel_case_types)]
142#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize, Eq, Ord)]
143pub enum PortTypeEnum {
144    #[serde(rename = "")]
145    EMPTY,
146    #[serde(rename = "tcp")]
147    TCP,
148    #[serde(rename = "udp")]
149    UDP,
150    #[serde(rename = "sctp")]
151    SCTP,
152}
153
154impl ::std::fmt::Display for PortTypeEnum {
155    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
156        match *self {
157            PortTypeEnum::EMPTY => write!(f, ""),
158            PortTypeEnum::TCP => write!(f, "{}", "tcp"),
159            PortTypeEnum::UDP => write!(f, "{}", "udp"),
160            PortTypeEnum::SCTP => write!(f, "{}", "sctp"),
161        }
162    }
163}
164
165impl ::std::str::FromStr for PortTypeEnum {
166    type Err = String;
167    fn from_str(s: &str) -> Result<Self, Self::Err> {
168        match s {
169            "" => Ok(PortTypeEnum::EMPTY),
170            "tcp" => Ok(PortTypeEnum::TCP),
171            "udp" => Ok(PortTypeEnum::UDP),
172            "sctp" => Ok(PortTypeEnum::SCTP),
173            x => Err(format!("Invalid enum type: {}", x)),
174        }
175    }
176}
177
178impl ::std::convert::AsRef<str> for PortTypeEnum {
179    fn as_ref(&self) -> &str {
180        match self {
181            PortTypeEnum::EMPTY => "",
182            PortTypeEnum::TCP => "tcp",
183            PortTypeEnum::UDP => "udp",
184            PortTypeEnum::SCTP => "sctp",
185        }
186    }
187}
188
189/// Configuration for a network endpoint.
190#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
191pub struct EndpointSettings {
192    #[serde(rename = "IPAMConfig")]
193    #[serde(skip_serializing_if = "Option::is_none")]
194    pub ipam_config: Option<EndpointIpamConfig>,
195
196    #[serde(rename = "Links")]
197    #[serde(skip_serializing_if = "Option::is_none")]
198    pub links: Option<Vec<String>>,
199
200    /// MAC address for the endpoint on this network. The network driver might ignore this parameter.
201    #[serde(rename = "MacAddress")]
202    #[serde(skip_serializing_if = "Option::is_none")]
203    pub mac_address: Option<String>,
204
205    #[serde(rename = "Aliases")]
206    #[serde(skip_serializing_if = "Option::is_none")]
207    pub aliases: Option<Vec<String>>,
208
209    /// Unique ID of the network.
210    #[serde(rename = "NetworkID")]
211    #[serde(skip_serializing_if = "Option::is_none")]
212    pub network_id: Option<String>,
213
214    /// Unique ID for the service endpoint in a Sandbox.
215    #[serde(rename = "EndpointID")]
216    #[serde(skip_serializing_if = "Option::is_none")]
217    pub endpoint_id: Option<String>,
218
219    /// Gateway address for this network.
220    #[serde(rename = "Gateway")]
221    #[serde(skip_serializing_if = "Option::is_none")]
222    pub gateway: Option<String>,
223
224    /// IPv4 address.
225    #[serde(rename = "IPAddress")]
226    #[serde(skip_serializing_if = "Option::is_none")]
227    pub ip_address: Option<String>,
228
229    /// Mask length of the IPv4 address.
230    #[serde(rename = "IPPrefixLen")]
231    #[serde(skip_serializing_if = "Option::is_none")]
232    pub ip_prefix_len: Option<i64>,
233
234    /// IPv6 gateway address.
235    #[serde(rename = "IPv6Gateway")]
236    #[serde(skip_serializing_if = "Option::is_none")]
237    pub ipv6_gateway: Option<String>,
238
239    /// Global IPv6 address.
240    #[serde(rename = "GlobalIPv6Address")]
241    #[serde(skip_serializing_if = "Option::is_none")]
242    pub global_ipv6_address: Option<String>,
243
244    /// Mask length of the global IPv6 address.
245    #[serde(rename = "GlobalIPv6PrefixLen")]
246    #[serde(skip_serializing_if = "Option::is_none")]
247    pub global_ipv6_prefix_len: Option<i64>,
248
249    /// DriverOpts is a mapping of driver options and values. These options are passed directly to the driver and are driver specific.
250    #[serde(rename = "DriverOpts")]
251    #[serde(skip_serializing_if = "Option::is_none")]
252    pub driver_opts: Option<HashMap<String, String>>,
253
254    /// List of all DNS names an endpoint has on a specific network. This list is based on the container name, network aliases, container short ID, and hostname.  These DNS names are non-fully qualified but can contain several dots. You can get fully qualified DNS names by appending `.<network-name>`. For instance, if container name is `my.ctr` and the network is named `testnet`, `DNSNames` will contain `my.ctr` and the FQDN will be `my.ctr.testnet`.
255    #[serde(rename = "DNSNames")]
256    #[serde(skip_serializing_if = "Option::is_none")]
257    pub dns_names: Option<Vec<String>>,
258}
259
260/// MountPoint represents a mount point configuration inside the container. This is used for reporting the mountpoints in use by a container.
261#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
262pub struct MountPoint {
263    /// The mount type:  - `bind` a mount of a file or directory from the host into the container. - `volume` a docker volume with the given `Name`. - `tmpfs` a `tmpfs`. - `npipe` a named pipe from the host into the container. - `cluster` a Swarm cluster volume
264    #[serde(rename = "Type")]
265    #[serde(skip_serializing_if = "Option::is_none")]
266    pub typ: Option<MountPointTypeEnum>,
267
268    /// Name is the name reference to the underlying data defined by `Source` e.g., the volume name.
269    #[serde(rename = "Name")]
270    #[serde(skip_serializing_if = "Option::is_none")]
271    pub name: Option<String>,
272
273    /// Source location of the mount.  For volumes, this contains the storage location of the volume (within `/var/lib/docker/volumes/`). For bind-mounts, and `npipe`, this contains the source (host) part of the bind-mount. For `tmpfs` mount points, this field is empty.
274    #[serde(rename = "Source")]
275    #[serde(skip_serializing_if = "Option::is_none")]
276    pub source: Option<String>,
277
278    /// Destination is the path relative to the container root (`/`) where the `Source` is mounted inside the container.
279    #[serde(rename = "Destination")]
280    #[serde(skip_serializing_if = "Option::is_none")]
281    pub destination: Option<String>,
282
283    /// Driver is the volume driver used to create the volume (if it is a volume).
284    #[serde(rename = "Driver")]
285    #[serde(skip_serializing_if = "Option::is_none")]
286    pub driver: Option<String>,
287
288    /// Mode is a comma separated list of options supplied by the user when creating the bind/volume mount.  The default is platform-specific (`\"z\"` on Linux, empty on Windows).
289    #[serde(rename = "Mode")]
290    #[serde(skip_serializing_if = "Option::is_none")]
291    pub mode: Option<String>,
292
293    /// Whether the mount is mounted writable (read-write).
294    #[serde(rename = "RW")]
295    #[serde(skip_serializing_if = "Option::is_none")]
296    pub rw: Option<bool>,
297
298    /// Propagation describes how mounts are propagated from the host into the mount point, and vice-versa. Refer to the [Linux kernel documentation](https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt) for details. This field is not used on Windows.
299    #[serde(rename = "Propagation")]
300    #[serde(skip_serializing_if = "Option::is_none")]
301    pub propagation: Option<String>,
302}
303
304#[allow(non_camel_case_types)]
305#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize, Eq, Ord)]
306pub enum MountPointTypeEnum {
307    #[serde(rename = "")]
308    EMPTY,
309    #[serde(rename = "bind")]
310    BIND,
311    #[serde(rename = "volume")]
312    VOLUME,
313    #[serde(rename = "tmpfs")]
314    TMPFS,
315    #[serde(rename = "npipe")]
316    NPIPE,
317    #[serde(rename = "cluster")]
318    CLUSTER,
319}
320
321impl ::std::fmt::Display for MountPointTypeEnum {
322    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
323        match *self {
324            MountPointTypeEnum::EMPTY => write!(f, ""),
325            MountPointTypeEnum::BIND => write!(f, "{}", "bind"),
326            MountPointTypeEnum::VOLUME => write!(f, "{}", "volume"),
327            MountPointTypeEnum::TMPFS => write!(f, "{}", "tmpfs"),
328            MountPointTypeEnum::NPIPE => write!(f, "{}", "npipe"),
329            MountPointTypeEnum::CLUSTER => write!(f, "{}", "cluster"),
330        }
331    }
332}
333
334impl ::std::str::FromStr for MountPointTypeEnum {
335    type Err = String;
336    fn from_str(s: &str) -> Result<Self, Self::Err> {
337        match s {
338            "" => Ok(MountPointTypeEnum::EMPTY),
339            "bind" => Ok(MountPointTypeEnum::BIND),
340            "volume" => Ok(MountPointTypeEnum::VOLUME),
341            "tmpfs" => Ok(MountPointTypeEnum::TMPFS),
342            "npipe" => Ok(MountPointTypeEnum::NPIPE),
343            "cluster" => Ok(MountPointTypeEnum::CLUSTER),
344            x => Err(format!("Invalid enum type: {}", x)),
345        }
346    }
347}
348
349impl ::std::convert::AsRef<str> for MountPointTypeEnum {
350    fn as_ref(&self) -> &str {
351        match self {
352            MountPointTypeEnum::EMPTY => "",
353            MountPointTypeEnum::BIND => "bind",
354            MountPointTypeEnum::VOLUME => "volume",
355            MountPointTypeEnum::TMPFS => "tmpfs",
356            MountPointTypeEnum::NPIPE => "npipe",
357            MountPointTypeEnum::CLUSTER => "cluster",
358        }
359    }
360}
361
362/// EndpointIPAMConfig represents an endpoint's IPAM configuration.
363#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
364pub struct EndpointIpamConfig {
365    #[serde(rename = "IPv4Address")]
366    #[serde(skip_serializing_if = "Option::is_none")]
367    pub ipv4_address: Option<String>,
368
369    #[serde(rename = "IPv6Address")]
370    #[serde(skip_serializing_if = "Option::is_none")]
371    pub ipv6_address: Option<String>,
372
373    #[serde(rename = "LinkLocalIPs")]
374    #[serde(skip_serializing_if = "Option::is_none")]
375    pub link_local_i_ps: Option<Vec<String>>,
376}