docker_api/api/
task.rs

1//! A task is a container running on a swarm. It is the atomic scheduling unit of swarm.
2//! Swarm mode must be enabled for these endpoints to work.
3
4use crate::{models, Result};
5
6impl_api_ty!(Task => id);
7
8impl Task {
9    impl_api_ep! { task: Task, resp
10        Inspect -> &format!("/tasks/{}", task.id), models::Task
11        Logs -> &format!("/tasks/{}/logs", task.id), ()
12    }
13}
14
15impl Tasks {
16    impl_api_ep! { task: Task, resp
17        List -> "/tasks", models::Task
18    }
19}
20
21pub mod opts {
22    use containers_api::opts::{Filter, FilterItem};
23    use containers_api::{impl_filter_func, impl_opts_builder};
24
25    impl_opts_builder!(url => TaskList);
26
27    #[derive(Clone, Copy, Debug)]
28    pub enum TaskStateFilter {
29        Running,
30        Shutdown,
31        Accepted,
32    }
33
34    impl AsRef<str> for TaskStateFilter {
35        fn as_ref(&self) -> &str {
36            match &self {
37                Self::Running => "running",
38                Self::Shutdown => "shutdown",
39                Self::Accepted => "accepted",
40            }
41        }
42    }
43
44    pub enum TaskFilter {
45        /// The state that the task should be in.
46        DesiredState(TaskStateFilter),
47        /// The ID of the config.
48        Id(String),
49        /// Label in the form of `label=key`
50        LabelKey(String),
51        /// Label in the form of `label=key=val`
52        Label(String, String),
53        /// The name of the config.
54        Name(String),
55        /// Name of the node.
56        Node(String),
57        /// Name of the service.
58        Service(String),
59    }
60
61    impl Filter for TaskFilter {
62        fn query_item(&self) -> FilterItem {
63            use TaskFilter::*;
64            match &self {
65                DesiredState(state) => FilterItem::new("desired-state", state.as_ref().to_string()),
66                Id(id) => FilterItem::new("id", id.to_owned()),
67                LabelKey(key) => FilterItem::new("label", key.to_owned()),
68                Label(key, val) => FilterItem::new("label", format!("{key}={val}")),
69                Name(name) => FilterItem::new("name", name.to_owned()),
70                Node(node) => FilterItem::new("node", node.to_owned()),
71                Service(service) => FilterItem::new("service", service.to_owned()),
72            }
73        }
74    }
75
76    impl TaskListOptsBuilder {
77        impl_filter_func!(
78            /// Filter listed tasks by variants of the enum.
79            TaskFilter
80        );
81    }
82}
83
84pub use opts::*;