runpod_sdk/service/
pods.rs

1use std::future::Future;
2
3#[cfg(feature = "tracing")]
4use crate::TRACING_TARGET_SERVICE;
5use crate::model::{GetPodQuery, ListPodsQuery, Pod, PodCreateInput, PodUpdateInput, Pods};
6use crate::{Result, RunpodClient};
7
8/// Trait for managing pods (V1 API).
9///
10/// Provides methods for creating, listing, retrieving, updating, and controlling pods.
11/// This trait is implemented on [`RunpodClient`](crate::RunpodClient).
12pub trait PodsService {
13    /// Creates a new pod.
14    ///
15    /// # Arguments
16    ///
17    /// * `input` - Configuration for the new pod
18    ///
19    /// # Returns
20    ///
21    /// Returns the created pod information.
22    ///
23    /// # Example
24    ///
25    /// ```no_run
26    /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
27    /// # use runpod_sdk::model::PodCreateInput;
28    /// # use runpod_sdk::service::PodsService;
29    /// # async fn example() -> Result<()> {
30    /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
31    /// let client = RunpodClient::new(config)?;
32    ///
33    /// let input = PodCreateInput {
34    ///     image_name: Some("runpod/pytorch:latest".to_string()),
35    ///     gpu_count: Some(1),
36    ///     ..Default::default()
37    /// };
38    ///
39    /// let pod = client.create_pod(input).await?;
40    /// println!("Created pod: {}", pod.id);
41    /// # Ok(())
42    /// # }
43    /// ```
44    fn create_pod(&self, input: PodCreateInput) -> impl Future<Output = Result<Pod>>;
45
46    /// Lists pods with optional filtering.
47    ///
48    /// # Arguments
49    ///
50    /// * `query` - Query parameters for filtering and pagination
51    ///
52    /// # Returns
53    ///
54    /// Returns a vector of pods matching the query criteria.
55    ///
56    /// # Example
57    ///
58    /// ```no_run
59    /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
60    /// # use runpod_sdk::model::ListPodsQuery;
61    /// # use runpod_sdk::service::PodsService;
62    /// # async fn example() -> Result<()> {
63    /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
64    /// let client = RunpodClient::new(config)?;
65    ///
66    /// let query = ListPodsQuery {
67    ///     include_machine: Some(true),
68    ///     ..Default::default()
69    /// };
70    ///
71    /// let pods = client.list_pods(query).await?;
72    /// println!("Found {} pods", pods.len());
73    /// # Ok(())
74    /// # }
75    /// ```
76    fn list_pods(&self, query: ListPodsQuery) -> impl Future<Output = Result<Pods>>;
77
78    /// Gets a specific pod by ID.
79    ///
80    /// # Arguments
81    ///
82    /// * `pod_id` - The unique identifier of the pod
83    /// * `query` - Query parameters for including additional information
84    ///
85    /// # Returns
86    ///
87    /// Returns the pod information.
88    ///
89    /// # Example
90    ///
91    /// ```no_run
92    /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
93    /// # use runpod_sdk::model::GetPodQuery;
94    /// # use runpod_sdk::service::PodsService;
95    /// # async fn example() -> Result<()> {
96    /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
97    /// let client = RunpodClient::new(config)?;
98    ///
99    /// let query = GetPodQuery {
100    ///     include_machine: Some(true),
101    ///     ..Default::default()
102    /// };
103    ///
104    /// let pod = client.get_pod("pod_id", query).await?;
105    /// println!("Pod: {:?}", pod);
106    /// # Ok(())
107    /// # }
108    /// ```
109    fn get_pod(&self, pod_id: &str, query: GetPodQuery) -> impl Future<Output = Result<Pod>>;
110
111    /// Updates an existing pod.
112    ///
113    /// # Arguments
114    ///
115    /// * `pod_id` - The unique identifier of the pod to update
116    /// * `input` - Update parameters for the pod
117    ///
118    /// # Returns
119    ///
120    /// Returns the updated pod information.
121    ///
122    /// # Example
123    ///
124    /// ```no_run
125    /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
126    /// # use runpod_sdk::model::PodUpdateInput;
127    /// # use runpod_sdk::service::PodsService;
128    /// # async fn example() -> Result<()> {
129    /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
130    /// let client = RunpodClient::new(config)?;
131    ///
132    /// let input = PodUpdateInput {
133    ///     name: Some("Updated Pod".to_string()),
134    ///     ..Default::default()
135    /// };
136    ///
137    /// let pod = client.update_pod("pod_id", input).await?;
138    /// println!("Updated pod: {}", pod.id);
139    /// # Ok(())
140    /// # }
141    /// ```
142    fn update_pod(&self, pod_id: &str, input: PodUpdateInput) -> impl Future<Output = Result<Pod>>;
143
144    /// Deletes a pod.
145    ///
146    /// # Arguments
147    ///
148    /// * `pod_id` - The unique identifier of the pod to delete
149    ///
150    /// # Example
151    ///
152    /// ```no_run
153    /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
154    /// # use runpod_sdk::service::PodsService;
155    /// # async fn example() -> Result<()> {
156    /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
157    /// let client = RunpodClient::new(config)?;
158    ///
159    /// client.delete_pod("pod_id").await?;
160    /// println!("Pod deleted");
161    /// # Ok(())
162    /// # }
163    /// ```
164    fn delete_pod(&self, pod_id: &str) -> impl Future<Output = Result<()>>;
165
166    /// Starts or resumes a pod.
167    ///
168    /// # Arguments
169    ///
170    /// * `pod_id` - The unique identifier of the pod to start
171    ///
172    /// # Example
173    ///
174    /// ```no_run
175    /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
176    /// # use runpod_sdk::service::PodsService;
177    /// # async fn example() -> Result<()> {
178    /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
179    /// let client = RunpodClient::new(config)?;
180    ///
181    /// client.start_pod("pod_id").await?;
182    /// println!("Pod started");
183    /// # Ok(())
184    /// # }
185    /// ```
186    fn start_pod(&self, pod_id: &str) -> impl Future<Output = Result<()>>;
187
188    /// Stops a pod.
189    ///
190    /// # Arguments
191    ///
192    /// * `pod_id` - The unique identifier of the pod to stop
193    ///
194    /// # Example
195    ///
196    /// ```no_run
197    /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
198    /// # use runpod_sdk::service::PodsService;
199    /// # async fn example() -> Result<()> {
200    /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
201    /// let client = RunpodClient::new(config)?;
202    ///
203    /// client.stop_pod("pod_id").await?;
204    /// println!("Pod stopped");
205    /// # Ok(())
206    /// # }
207    /// ```
208    fn stop_pod(&self, pod_id: &str) -> impl Future<Output = Result<()>>;
209
210    /// Resets a pod.
211    ///
212    /// This operation will restart the pod with a fresh filesystem state.
213    ///
214    /// # Arguments
215    ///
216    /// * `pod_id` - The unique identifier of the pod to reset
217    ///
218    /// # Example
219    ///
220    /// ```no_run
221    /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
222    /// # use runpod_sdk::service::PodsService;
223    /// # async fn example() -> Result<()> {
224    /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
225    /// let client = RunpodClient::new(config)?;
226    ///
227    /// client.reset_pod("pod_id").await?;
228    /// println!("Pod reset");
229    /// # Ok(())
230    /// # }
231    /// ```
232    fn reset_pod(&self, pod_id: &str) -> impl Future<Output = Result<()>>;
233
234    /// Restarts a pod.
235    ///
236    /// This operation will stop and then start the pod.
237    ///
238    /// # Arguments
239    ///
240    /// * `pod_id` - The unique identifier of the pod to restart
241    ///
242    /// # Example
243    ///
244    /// ```no_run
245    /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
246    /// # use runpod_sdk::service::PodsService;
247    /// # async fn example() -> Result<()> {
248    /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
249    /// let client = RunpodClient::new(config)?;
250    ///
251    /// client.restart_pod("pod_id").await?;
252    /// println!("Pod restarted");
253    /// # Ok(())
254    /// # }
255    /// ```
256    fn restart_pod(&self, pod_id: &str) -> impl Future<Output = Result<()>>;
257}
258
259impl PodsService for RunpodClient {
260    async fn create_pod(&self, input: PodCreateInput) -> Result<Pod> {
261        #[cfg(feature = "tracing")]
262        tracing::debug!(target: TRACING_TARGET_SERVICE, "Creating pod");
263
264        let response = self.post("/pods").json(&input).send().await?;
265        let response = response.error_for_status()?;
266        let pod: Pod = response.json().await?;
267
268        #[cfg(feature = "tracing")]
269        tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id = %pod.id, "Pod created successfully");
270
271        Ok(pod)
272    }
273
274    async fn list_pods(&self, query: ListPodsQuery) -> Result<Pods> {
275        #[cfg(feature = "tracing")]
276        tracing::debug!(target: TRACING_TARGET_SERVICE, "Listing pods");
277
278        let response = self.get("/pods").query(&query).send().await?;
279        let response = response.error_for_status()?;
280        let pods: Pods = response.json().await?;
281
282        #[cfg(feature = "tracing")]
283        tracing::debug!(target: TRACING_TARGET_SERVICE, count = pods.len(), "Pods retrieved successfully");
284
285        Ok(pods)
286    }
287
288    async fn get_pod(&self, pod_id: &str, query: GetPodQuery) -> Result<Pod> {
289        #[cfg(feature = "tracing")]
290        tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id, "Getting pod");
291
292        let path = format!("/pods/{}", pod_id);
293        let response = self.get(&path).query(&query).send().await?;
294        let response = response.error_for_status()?;
295        let pod: Pod = response.json().await?;
296
297        #[cfg(feature = "tracing")]
298        tracing::debug!(target: TRACING_TARGET_SERVICE, "Pod retrieved successfully");
299
300        Ok(pod)
301    }
302
303    async fn update_pod(&self, pod_id: &str, input: PodUpdateInput) -> Result<Pod> {
304        #[cfg(feature = "tracing")]
305        tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id, "Updating pod");
306
307        let path = format!("/pods/{}", pod_id);
308        let response = self.patch(&path).json(&input).send().await?;
309        let response = response.error_for_status()?;
310        let pod: Pod = response.json().await?;
311
312        #[cfg(feature = "tracing")]
313        tracing::debug!(target: TRACING_TARGET_SERVICE, "Pod updated successfully");
314
315        Ok(pod)
316    }
317
318    async fn delete_pod(&self, pod_id: &str) -> Result<()> {
319        #[cfg(feature = "tracing")]
320        tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id, "Deleting pod");
321
322        let path = format!("/pods/{}", pod_id);
323        let response = self.delete(&path).send().await?;
324        response.error_for_status()?;
325
326        #[cfg(feature = "tracing")]
327        tracing::debug!(target: TRACING_TARGET_SERVICE, "Pod deleted successfully");
328
329        Ok(())
330    }
331
332    async fn start_pod(&self, pod_id: &str) -> Result<()> {
333        #[cfg(feature = "tracing")]
334        tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id, "Starting pod");
335
336        let path = format!("/pods/{}/start", pod_id);
337        let response = self.post(&path).send().await?;
338        response.error_for_status()?;
339
340        #[cfg(feature = "tracing")]
341        tracing::debug!(target: TRACING_TARGET_SERVICE, "Pod started successfully");
342
343        Ok(())
344    }
345
346    async fn stop_pod(&self, pod_id: &str) -> Result<()> {
347        #[cfg(feature = "tracing")]
348        tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id, "Stopping pod");
349
350        let path = format!("/pods/{}/stop", pod_id);
351        let response = self.post(&path).send().await?;
352        response.error_for_status()?;
353
354        #[cfg(feature = "tracing")]
355        tracing::debug!(target: TRACING_TARGET_SERVICE, "Pod stopped successfully");
356
357        Ok(())
358    }
359
360    async fn reset_pod(&self, pod_id: &str) -> Result<()> {
361        #[cfg(feature = "tracing")]
362        tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id, "Resetting pod");
363
364        let path = format!("/pods/{}/reset", pod_id);
365        let response = self.post(&path).send().await?;
366        response.error_for_status()?;
367
368        #[cfg(feature = "tracing")]
369        tracing::debug!(target: TRACING_TARGET_SERVICE, "Pod reset successfully");
370
371        Ok(())
372    }
373
374    async fn restart_pod(&self, pod_id: &str) -> Result<()> {
375        #[cfg(feature = "tracing")]
376        tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id, "Restarting pod");
377
378        let path = format!("/pods/{}/restart", pod_id);
379        let response = self.post(&path).send().await?;
380        response.error_for_status()?;
381
382        #[cfg(feature = "tracing")]
383        tracing::debug!(target: TRACING_TARGET_SERVICE, "Pod restarted successfully");
384
385        Ok(())
386    }
387}