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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//! Options used for configuring the behavior of certain API endpoints

mod containers;
mod exec;
mod images;
mod manifests;
mod networks;
mod pods;
mod volumes;

pub use containers::*;
pub use exec::*;
pub use images::*;
pub use manifests::*;
pub use networks::*;
pub use pods::*;
pub use volumes::*;

pub type EventsConstraint = (String, Vec<String>);

use std::fmt;

impl_opts_builder!(
    url =>
    /// Used to filter events returned by [Podman::events](crate::Podman::events).
    Events
);

impl EventsOptsBuilder {
    impl_url_str_field!(
        /// Start streaming events from this time
        since => "since"
    );

    impl_url_str_field!(
        /// Stop streaming events later than this
        until => "until"
    );

    impl_url_bool_field!(
        /// when false, do not follow events
        stream => "stream"
    );

    /// A list of constraints for events
    pub fn filters<F>(mut self, filters: F) -> Self
    where
        F: IntoIterator<Item = EventsConstraint>,
    {
        let filters: std::collections::HashMap<_, _> = filters.into_iter().collect();
        self.params.insert(
            "filters",
            serde_json::to_string(&filters).unwrap_or_default(),
        );
        self
    }
}

impl_opts_builder!(url =>
    /// Adjust how filesystem changes inside a container or image are returned.
    Changes
);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// Used with [`ChangesOptsBuilder::diff_type`](ChangesOptsBuilder::diff_type).
pub enum DiffType {
    All,
    Container,
    Image,
}

impl AsRef<str> for DiffType {
    fn as_ref(&self) -> &str {
        match self {
            DiffType::All => "all",
            DiffType::Container => "container",
            DiffType::Image => "image",
        }
    }
}

impl fmt::Display for DiffType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_ref())
    }
}

impl ChangesOptsBuilder {
    impl_url_enum_field!(
        /// Select what you want to match.
        diff_type: DiffType => "diffType"
    );

    impl_url_str_field!(
        /// Specify a second layer which is used to compare against it instead of the parent layer.
        parent => "parent"
    );
}

impl_opts_builder!(url =>
    /// Adjust how systemd units are generated from a container or pod.
    SystemdUnits
);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// Used with [`SystemdUnitsOptsBuilder::restart_policy`](SystemdUnitsOptsBuilder::restart_policy).
pub enum RestartPolicy {
    No,
    OnSuccess,
    OnFailure,
    OnAbnormal,
    OnWatchdog,
    OnAbort,
    Always,
}

impl AsRef<str> for RestartPolicy {
    fn as_ref(&self) -> &str {
        use RestartPolicy::*;
        match self {
            No => "no",
            OnSuccess => "on-success",
            OnFailure => "on-failure",
            OnAbnormal => "on-abnormal",
            OnWatchdog => "on-watchdog",
            OnAbort => "on-abort",
            Always => "always",
        }
    }
}

impl fmt::Display for RestartPolicy {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_ref())
    }
}

impl SystemdUnitsOptsBuilder {
    impl_url_str_field!(
        /// Systemd unit name prefix for containers.
        container_prefix => "containerPrefix"
    );

    impl_url_bool_field!(
        /// Create a new container instead of starting an existing one.
        new => "new"
    );

    impl_url_bool_field!(
        /// Do not generate the header including the Podman version and the timestamp.
        no_header => "noHeader"
    );

    impl_url_str_field!(
        /// Systemd unit name prefix for pods.
        pod_prefix => "podPrefix"
    );

    impl_url_enum_field!(
        /// Systemd restart-policy.
        restart_policy: RestartPolicy => "restartPolicy"
    );

    impl_url_field!(
        /// Configures the time to sleep before restarting a service.
        restart_sec: usize => "restartSec"
    );

    impl_url_str_field!(
        /// Systemd unit name separator between name/id and prefix.
        separator => "separator"
    );

    impl_url_field!(
        /// Start timeout in seconds.
        start_timeout: usize => "startTimeout"
    );

    impl_url_field!(
        /// Stop timeout in seconds.
        stop_timeout: usize => "stopTimeout"
    );

    impl_url_bool_field!(
        /// Use container/pod names instead of IDs.
        use_name => "useName"
    );
}

impl_opts_builder!(url =>
    /// Adjust how a kubernetes YAML will create pods and containers.
    PlayKubernetesYaml
);

impl PlayKubernetesYamlOptsBuilder {
    impl_url_str_field!(
        /// Logging driver for the containers in the pod.
        log_driver => "logDriver"
    );

    impl_url_vec_field!(
        /// Use the network mode or specify an array of networks.
        network => "network"
    );

    impl_url_bool_field!(
        /// Start the pod after creating it.
        start => "start"
    );

    impl_url_vec_field!(
        /// Static IPs used for the pods.
        static_ips => "staticIPs"
    );

    impl_url_vec_field!(
        /// Static MACs used for the pods.
        static_macs => "static_macs"
    );

    impl_url_bool_field!(
        /// Require HTTPS and verify signatures when contacting registries.
        tls_verify => "tlsVerify"
    );
}

//####################################################################################################
//
// Secrets
//
//####################################################################################################

#[derive(serde::Serialize, Debug, Clone)]
pub struct SecretCreateOpts {
    name: String,
    driver: Option<String>,
}

impl SecretCreateOpts {
    /// Returns a new instance of a buildetr for `SecretCreateOpts`.
    ///
    /// Parameters:
    /// * name - User-defined name of the secret.
    ///
    pub fn builder(name: impl Into<String>) -> SecretCreateOptsBuilder {
        SecretCreateOptsBuilder {
            name: name.into(),
            driver: None,
        }
    }

    pub fn serialize(&self) -> Option<String> {
        if self.name.is_empty() {
            return None;
        }

        let mut params = vec![("name", self.name.clone())];

        if let Some(driver) = &self.driver {
            params.push(("driver", driver.clone()));
        }

        Some(crate::util::url::encoded_pairs(params))
    }
}

#[derive(Debug, Clone)]
pub struct SecretCreateOptsBuilder {
    name: String,
    driver: Option<String>,
}

impl SecretCreateOptsBuilder {
    /// Secret driver. Default is `file`.
    pub fn driver(mut self, driver: impl Into<String>) -> Self {
        self.driver = Some(driver.into());
        self
    }

    /// Finish building `SecretCreateOpts`.
    pub fn build(self) -> SecretCreateOpts {
        SecretCreateOpts {
            name: self.name,
            driver: self.driver,
        }
    }
}