jenkins_sdk/api/
computers.rs

1use crate::transport::request::{Request, RequestBody};
2use crate::{ComputerName, Error, ExecutorsInfo};
3use http::HeaderValue;
4use serde_json::Value;
5
6/// Jenkins computers/nodes (core) APIs.
7#[derive(Clone)]
8#[cfg(feature = "async")]
9pub struct ComputersService {
10    client: crate::Client,
11}
12
13#[cfg(feature = "async")]
14impl ComputersService {
15    pub(crate) fn new(client: crate::Client) -> Self {
16        Self { client }
17    }
18}
19
20#[cfg(feature = "async")]
21impl ComputersService {
22    /// `GET /computer/api/json`
23    pub async fn list(&self, tree: Option<&str>) -> Result<Value, Error> {
24        let mut req = Request::get(["computer", "api", "json"]);
25        if let Some(tree) = tree {
26            req = req.query_pair("tree", tree);
27        }
28        self.client.send_json(req).await
29    }
30
31    /// `POST /computer/doCreateItem?name=<name>` with XML body.
32    pub async fn create_from_xml(
33        &self,
34        name: impl Into<ComputerName>,
35        xml: impl Into<Vec<u8>>,
36    ) -> Result<(), Error> {
37        let name = name.into();
38        let req = Request::post(["computer", "doCreateItem"])
39            .query_pair("name", name.as_str())
40            .body(RequestBody::bytes_with_content_type(
41                xml.into(),
42                HeaderValue::from_static("application/xml"),
43            ));
44        self.client.send_unit(req).await
45    }
46
47    /// `POST /computer/doCreateItem?name=<to>&mode=copy&from=<from>`
48    pub async fn copy(
49        &self,
50        from: impl Into<ComputerName>,
51        to: impl Into<ComputerName>,
52    ) -> Result<(), Error> {
53        let from = from.into();
54        let to = to.into();
55        let req = Request::post(["computer", "doCreateItem"])
56            .query_pair("name", to.as_str())
57            .query_pair("mode", "copy")
58            .query_pair("from", from.as_str());
59        self.client.send_unit(req).await
60    }
61
62    /// `GET /computer/api/json` (typed subset).
63    pub async fn executors_info(&self) -> Result<ExecutorsInfo, Error> {
64        self.client
65            .send_json(Request::get(["computer", "api", "json"]))
66            .await
67    }
68
69    /// `GET /computer/<name>/api/json`
70    pub async fn computer(
71        &self,
72        name: impl Into<ComputerName>,
73        tree: Option<&str>,
74    ) -> Result<Value, Error> {
75        let name = name.into();
76        let mut req = Request::get(["computer", name.as_str(), "api", "json"]);
77        if let Some(tree) = tree {
78            req = req.query_pair("tree", tree);
79        }
80        self.client.send_json(req).await
81    }
82
83    /// `POST /computer/<name>/toggleOffline`
84    pub async fn toggle_offline(
85        &self,
86        name: impl Into<ComputerName>,
87        offline_message: Option<&str>,
88    ) -> Result<(), Error> {
89        let name = name.into();
90        let mut req = Request::post(["computer", name.as_str(), "toggleOffline"]);
91        if let Some(message) = offline_message {
92            req = req.query_pair("offlineMessage", message);
93        }
94        self.client.send_unit(req).await
95    }
96
97    /// `POST /computer/<name>/doDelete`
98    pub async fn delete(&self, name: impl Into<ComputerName>) -> Result<(), Error> {
99        let name = name.into();
100        self.client
101            .send_unit(Request::post(["computer", name.as_str(), "doDelete"]))
102            .await
103    }
104
105    /// `GET /computer/<name>/config.xml`
106    pub async fn get_config_xml(&self, name: impl Into<ComputerName>) -> Result<Vec<u8>, Error> {
107        let name = name.into();
108        self.client
109            .send_bytes(Request::get(["computer", name.as_str(), "config.xml"]))
110            .await
111    }
112
113    /// `POST /computer/<name>/config.xml` with XML body.
114    pub async fn update_config_xml(
115        &self,
116        name: impl Into<ComputerName>,
117        xml: impl Into<Vec<u8>>,
118    ) -> Result<(), Error> {
119        let name = name.into();
120        self.client
121            .send_unit(
122                Request::post(["computer", name.as_str(), "config.xml"]).body(
123                    RequestBody::bytes_with_content_type(
124                        xml.into(),
125                        HeaderValue::from_static("application/xml"),
126                    ),
127                ),
128            )
129            .await
130    }
131
132    /// `POST /computer/<name>/connect`
133    pub async fn connect(&self, name: impl Into<ComputerName>) -> Result<(), Error> {
134        let name = name.into();
135        self.client
136            .send_unit(Request::post(["computer", name.as_str(), "connect"]))
137            .await
138    }
139
140    /// `POST /computer/<name>/disconnect`
141    pub async fn disconnect(&self, name: impl Into<ComputerName>) -> Result<(), Error> {
142        let name = name.into();
143        self.client
144            .send_unit(Request::post(["computer", name.as_str(), "disconnect"]))
145            .await
146    }
147
148    /// `POST /computer/<name>/launchSlaveAgent`
149    pub async fn launch_slave_agent(&self, name: impl Into<ComputerName>) -> Result<(), Error> {
150        let name = name.into();
151        self.client
152            .send_unit(Request::post([
153                "computer",
154                name.as_str(),
155                "launchSlaveAgent",
156            ]))
157            .await
158    }
159}
160
161/// Jenkins computers/nodes (core) APIs (blocking).
162#[cfg(feature = "blocking")]
163#[derive(Clone)]
164pub struct BlockingComputersService {
165    client: crate::BlockingClient,
166}
167
168#[cfg(feature = "blocking")]
169impl BlockingComputersService {
170    pub(crate) fn new(client: crate::BlockingClient) -> Self {
171        Self { client }
172    }
173}
174
175#[cfg(feature = "blocking")]
176impl BlockingComputersService {
177    /// `GET /computer/api/json`
178    pub fn list(&self, tree: Option<&str>) -> Result<Value, Error> {
179        let mut req = Request::get(["computer", "api", "json"]);
180        if let Some(tree) = tree {
181            req = req.query_pair("tree", tree);
182        }
183        self.client.send_json(req)
184    }
185
186    /// `POST /computer/doCreateItem?name=<name>` with XML body.
187    pub fn create_from_xml(
188        &self,
189        name: impl Into<ComputerName>,
190        xml: impl Into<Vec<u8>>,
191    ) -> Result<(), Error> {
192        let name = name.into();
193        let req = Request::post(["computer", "doCreateItem"])
194            .query_pair("name", name.as_str())
195            .body(RequestBody::bytes_with_content_type(
196                xml.into(),
197                HeaderValue::from_static("application/xml"),
198            ));
199        self.client.send_unit(req)
200    }
201
202    /// `POST /computer/doCreateItem?name=<to>&mode=copy&from=<from>`
203    pub fn copy(
204        &self,
205        from: impl Into<ComputerName>,
206        to: impl Into<ComputerName>,
207    ) -> Result<(), Error> {
208        let from = from.into();
209        let to = to.into();
210        let req = Request::post(["computer", "doCreateItem"])
211            .query_pair("name", to.as_str())
212            .query_pair("mode", "copy")
213            .query_pair("from", from.as_str());
214        self.client.send_unit(req)
215    }
216
217    /// `GET /computer/api/json` (typed subset).
218    pub fn executors_info(&self) -> Result<ExecutorsInfo, Error> {
219        self.client
220            .send_json(Request::get(["computer", "api", "json"]))
221    }
222
223    /// `GET /computer/<name>/api/json`
224    pub fn computer(
225        &self,
226        name: impl Into<ComputerName>,
227        tree: Option<&str>,
228    ) -> Result<Value, Error> {
229        let name = name.into();
230        let mut req = Request::get(["computer", name.as_str(), "api", "json"]);
231        if let Some(tree) = tree {
232            req = req.query_pair("tree", tree);
233        }
234        self.client.send_json(req)
235    }
236
237    /// `POST /computer/<name>/toggleOffline`
238    pub fn toggle_offline(
239        &self,
240        name: impl Into<ComputerName>,
241        offline_message: Option<&str>,
242    ) -> Result<(), Error> {
243        let name = name.into();
244        let mut req = Request::post(["computer", name.as_str(), "toggleOffline"]);
245        if let Some(message) = offline_message {
246            req = req.query_pair("offlineMessage", message);
247        }
248        self.client.send_unit(req)
249    }
250
251    /// `POST /computer/<name>/doDelete`
252    pub fn delete(&self, name: impl Into<ComputerName>) -> Result<(), Error> {
253        let name = name.into();
254        self.client
255            .send_unit(Request::post(["computer", name.as_str(), "doDelete"]))
256    }
257
258    /// `GET /computer/<name>/config.xml`
259    pub fn get_config_xml(&self, name: impl Into<ComputerName>) -> Result<Vec<u8>, Error> {
260        let name = name.into();
261        self.client
262            .send_bytes(Request::get(["computer", name.as_str(), "config.xml"]))
263    }
264
265    /// `POST /computer/<name>/config.xml` with XML body.
266    pub fn update_config_xml(
267        &self,
268        name: impl Into<ComputerName>,
269        xml: impl Into<Vec<u8>>,
270    ) -> Result<(), Error> {
271        let name = name.into();
272        self.client.send_unit(
273            Request::post(["computer", name.as_str(), "config.xml"]).body(
274                RequestBody::bytes_with_content_type(
275                    xml.into(),
276                    HeaderValue::from_static("application/xml"),
277                ),
278            ),
279        )
280    }
281
282    /// `POST /computer/<name>/connect`
283    pub fn connect(&self, name: impl Into<ComputerName>) -> Result<(), Error> {
284        let name = name.into();
285        self.client
286            .send_unit(Request::post(["computer", name.as_str(), "connect"]))
287    }
288
289    /// `POST /computer/<name>/disconnect`
290    pub fn disconnect(&self, name: impl Into<ComputerName>) -> Result<(), Error> {
291        let name = name.into();
292        self.client
293            .send_unit(Request::post(["computer", name.as_str(), "disconnect"]))
294    }
295
296    /// `POST /computer/<name>/launchSlaveAgent`
297    pub fn launch_slave_agent(&self, name: impl Into<ComputerName>) -> Result<(), Error> {
298        let name = name.into();
299        self.client.send_unit(Request::post([
300            "computer",
301            name.as_str(),
302            "launchSlaveAgent",
303        ]))
304    }
305}