docker_api/opts/
system.rs

1use containers_api::impl_opts_builder;
2use std::collections::HashMap;
3
4/// Opts for filtering streams of Docker events
5#[derive(Default, Debug)]
6pub struct EventsOpts {
7    params: HashMap<&'static str, String>,
8}
9
10impl EventsOpts {
11    pub fn builder() -> EventsOptsBuilder {
12        EventsOptsBuilder::default()
13    }
14
15    /// serialize Opts as a string. returns None if no Opts are defined
16    pub fn serialize(&self) -> Option<String> {
17        if self.params.is_empty() {
18            None
19        } else {
20            Some(containers_api::url::encoded_pairs(&self.params))
21        }
22    }
23}
24
25#[derive(Copy, Clone)]
26pub enum EventFilterType {
27    Container,
28    Image,
29    Volume,
30    Network,
31    Daemon,
32}
33
34impl AsRef<str> for EventFilterType {
35    fn as_ref(&self) -> &str {
36        match &self {
37            EventFilterType::Container => "container",
38            EventFilterType::Image => "image",
39            EventFilterType::Volume => "volume",
40            EventFilterType::Network => "network",
41            EventFilterType::Daemon => "daemon",
42        }
43    }
44}
45
46/// An enumartion used to filter system events.
47pub enum EventFilter {
48    // TODO: use the Filter trait for this enum
49    Container(String),
50    Event(String),
51    Image(String),
52    Label(String),
53    Type(EventFilterType),
54    Volume(String),
55    Network(String),
56    Daemon(String),
57}
58
59#[derive(Default)]
60/// Builder interface for [`EventOpts`](EventOpts).
61pub struct EventsOptsBuilder {
62    params: HashMap<&'static str, String>,
63    events: Vec<String>,
64    containers: Vec<String>,
65    images: Vec<String>,
66    labels: Vec<String>,
67    volumes: Vec<String>,
68    networks: Vec<String>,
69    daemons: Vec<String>,
70    types: Vec<String>,
71}
72
73impl EventsOptsBuilder {
74    #[cfg(feature = "chrono")]
75    /// Only return events since this time.
76    pub fn since<Tz>(mut self, timestamp: &chrono::DateTime<Tz>) -> Self
77    where
78        Tz: chrono::TimeZone,
79    {
80        self.params
81            .insert("since", timestamp.timestamp().to_string());
82        self
83    }
84
85    #[cfg(not(feature = "chrono"))]
86    /// Only return events since this time, as a UNIX timestamp.
87    pub fn since(mut self, timestamp: i64) -> Self {
88        self.params.insert("since", timestamp.to_string());
89        self
90    }
91
92    #[cfg(feature = "chrono")]
93    /// Only return events before this time.
94    pub fn until<Tz>(mut self, timestamp: &chrono::DateTime<Tz>) -> Self
95    where
96        Tz: chrono::TimeZone,
97    {
98        self.params
99            .insert("until", timestamp.timestamp().to_string());
100        self
101    }
102
103    #[cfg(not(feature = "chrono"))]
104    /// Only return events before this time, as a UNIX timestamp.
105    pub fn until(mut self, timestamp: i64) -> Self {
106        self.params.insert("until", timestamp.to_string());
107        self
108    }
109
110    /// Filter the events by a list of event filters.
111    pub fn filter(mut self, filters: Vec<EventFilter>) -> Self {
112        let mut params = HashMap::new();
113        for f in filters {
114            match f {
115                EventFilter::Container(n) => {
116                    self.containers.push(n);
117                    params.insert("container", self.containers.clone())
118                }
119                EventFilter::Event(n) => {
120                    self.events.push(n);
121                    params.insert("event", self.events.clone())
122                }
123                EventFilter::Image(n) => {
124                    self.images.push(n);
125                    params.insert("image", self.images.clone())
126                }
127                EventFilter::Label(n) => {
128                    self.labels.push(n);
129                    params.insert("label", self.labels.clone())
130                }
131                EventFilter::Volume(n) => {
132                    self.volumes.push(n);
133                    params.insert("volume", self.volumes.clone())
134                }
135                EventFilter::Network(n) => {
136                    self.networks.push(n);
137                    params.insert("network", self.networks.clone())
138                }
139                EventFilter::Daemon(n) => {
140                    self.daemons.push(n);
141                    params.insert("daemon", self.daemons.clone())
142                }
143                EventFilter::Type(n) => {
144                    self.types.push(n.as_ref().to_string());
145                    params.insert("type", self.types.clone())
146                }
147            };
148        }
149        self.params.insert(
150            "filters",
151            serde_json::to_string(&params).unwrap_or_default(),
152        );
153        self
154    }
155
156    /// Build the final event options.
157    pub fn build(self) -> EventsOpts {
158        EventsOpts {
159            params: self.params,
160        }
161    }
162}
163
164#[derive(Copy, Clone)]
165pub enum DataUsageType {
166    Container,
167    Image,
168    Volume,
169    BuildCache,
170}
171
172impl AsRef<str> for DataUsageType {
173    fn as_ref(&self) -> &str {
174        match self {
175            Self::Container => "container",
176            Self::Image => "image",
177            Self::Volume => "volume",
178            Self::BuildCache => "build-cache",
179        }
180    }
181}
182
183impl_opts_builder!(url => SystemDataUsage);
184impl SystemDataUsageOptsBuilder {
185    pub fn types(mut self, types: impl IntoIterator<Item = DataUsageType>) -> Self {
186        self.vec_params.insert(
187            "type",
188            types.into_iter().map(|s| s.as_ref().into()).collect(),
189        );
190        self
191    }
192}