podman_client/pods/
list.rs

1use std::collections::HashMap;
2
3use http_body_util::Empty;
4use hyper::body::Bytes;
5use url::form_urlencoded::byte_serialize;
6
7use crate::{
8    client::Client,
9    models::{
10        connection::SendRequestOptions,
11        lib::Error,
12        podman::pods::list::{PodList, PodListOptions},
13    },
14};
15
16impl Client {
17    pub async fn pod_list(&self, options: Option<PodListOptions<'_>>) -> Result<PodList, Error> {
18        let mut path = "/libpod/pods/json".to_owned();
19
20        if let Some(options) = options {
21            if let Some(opt_filters) = options.filters {
22                let mut filters = HashMap::new();
23                if let Some(id) = opt_filters.id {
24                    filters.insert("id", id);
25                }
26                if let Some(label) = opt_filters.label {
27                    filters.insert("label", label);
28                }
29                if let Some(name) = opt_filters.name {
30                    filters.insert("name", name);
31                }
32                if let Some(until) = opt_filters.until {
33                    filters.insert("until", until);
34                }
35                if let Some(status) = opt_filters.status {
36                    filters.insert("status", status.iter().map(|s| s.as_str()).collect());
37                }
38                if let Some(network) = opt_filters.network {
39                    filters.insert("network", network);
40                }
41                if let Some(ctr_names) = opt_filters.ctr_names {
42                    filters.insert("ctr-names", ctr_names);
43                }
44                if let Some(ctr_ids) = opt_filters.ctr_ids {
45                    filters.insert("ctr-ids", ctr_ids);
46                }
47                if let Some(ctr_status) = opt_filters.ctr_status {
48                    filters.insert("ctr-status", ctr_status);
49                }
50                if let Some(ctr_number) = opt_filters.ctr_number {
51                    filters.insert("ctr-number", ctr_number);
52                }
53                if !filters.is_empty() {
54                    let filters_json = serde_json::to_string(&filters)?;
55                    let filters_encoded: String = byte_serialize(filters_json.as_bytes()).collect();
56                    path += &["?filters=", &filters_encoded].concat();
57                }
58            }
59        }
60
61        let (_, data) = self
62            .send_request::<_, (), _>(SendRequestOptions {
63                method: "GET",
64                path: &path,
65                header: None,
66                body: Empty::<Bytes>::new(),
67            })
68            .await?;
69
70        Ok(data)
71    }
72}