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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
use std::future::Future;
#[cfg(feature = "tracing")]
use crate::TRACING_TARGET_SERVICE;
use crate::model::{GetPodQuery, ListPodsQuery, Pod, PodCreateInput, PodUpdateInput, Pods};
use crate::{Result, RunpodClient};
/// Trait for managing pods (V1 API).
///
/// Provides methods for creating, listing, retrieving, updating, and controlling pods.
/// This trait is implemented on [`RunpodClient`](crate::RunpodClient).
pub trait PodsService {
/// Creates a new pod.
///
/// # Arguments
///
/// * `input` - Configuration for the new pod
///
/// # Returns
///
/// Returns the created pod information.
///
/// # Example
///
/// ```no_run
/// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
/// # use runpod_sdk::model::PodCreateInput;
/// # use runpod_sdk::service::PodsService;
/// # async fn example() -> Result<()> {
/// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
/// let client = RunpodClient::new(config)?;
///
/// let input = PodCreateInput {
/// image_name: Some("runpod/pytorch:latest".to_string()),
/// gpu_count: Some(1),
/// ..Default::default()
/// };
///
/// let pod = client.create_pod(input).await?;
/// println!("Created pod: {}", pod.id);
/// # Ok(())
/// # }
/// ```
fn create_pod(&self, input: PodCreateInput) -> impl Future<Output = Result<Pod>>;
/// Lists pods with optional filtering.
///
/// # Arguments
///
/// * `query` - Query parameters for filtering and pagination
///
/// # Returns
///
/// Returns a vector of pods matching the query criteria.
///
/// # Example
///
/// ```no_run
/// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
/// # use runpod_sdk::model::ListPodsQuery;
/// # use runpod_sdk::service::PodsService;
/// # async fn example() -> Result<()> {
/// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
/// let client = RunpodClient::new(config)?;
///
/// let query = ListPodsQuery {
/// include_machine: Some(true),
/// ..Default::default()
/// };
///
/// let pods = client.list_pods(query).await?;
/// println!("Found {} pods", pods.len());
/// # Ok(())
/// # }
/// ```
fn list_pods(&self, query: ListPodsQuery) -> impl Future<Output = Result<Pods>>;
/// Gets a specific pod by ID.
///
/// # Arguments
///
/// * `pod_id` - The unique identifier of the pod
/// * `query` - Query parameters for including additional information
///
/// # Returns
///
/// Returns the pod information.
///
/// # Example
///
/// ```no_run
/// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
/// # use runpod_sdk::model::GetPodQuery;
/// # use runpod_sdk::service::PodsService;
/// # async fn example() -> Result<()> {
/// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
/// let client = RunpodClient::new(config)?;
///
/// let query = GetPodQuery {
/// include_machine: Some(true),
/// ..Default::default()
/// };
///
/// let pod = client.get_pod("pod_id", query).await?;
/// println!("Pod: {:?}", pod);
/// # Ok(())
/// # }
/// ```
fn get_pod(&self, pod_id: &str, query: GetPodQuery) -> impl Future<Output = Result<Pod>>;
/// Updates an existing pod.
///
/// # Arguments
///
/// * `pod_id` - The unique identifier of the pod to update
/// * `input` - Update parameters for the pod
///
/// # Returns
///
/// Returns the updated pod information.
///
/// # Example
///
/// ```no_run
/// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
/// # use runpod_sdk::model::PodUpdateInput;
/// # use runpod_sdk::service::PodsService;
/// # async fn example() -> Result<()> {
/// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
/// let client = RunpodClient::new(config)?;
///
/// let input = PodUpdateInput {
/// name: Some("Updated Pod".to_string()),
/// ..Default::default()
/// };
///
/// let pod = client.update_pod("pod_id", input).await?;
/// println!("Updated pod: {}", pod.id);
/// # Ok(())
/// # }
/// ```
fn update_pod(&self, pod_id: &str, input: PodUpdateInput) -> impl Future<Output = Result<Pod>>;
/// Deletes a pod.
///
/// # Arguments
///
/// * `pod_id` - The unique identifier of the pod to delete
///
/// # Example
///
/// ```no_run
/// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
/// # use runpod_sdk::service::PodsService;
/// # async fn example() -> Result<()> {
/// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
/// let client = RunpodClient::new(config)?;
///
/// client.delete_pod("pod_id").await?;
/// println!("Pod deleted");
/// # Ok(())
/// # }
/// ```
fn delete_pod(&self, pod_id: &str) -> impl Future<Output = Result<()>>;
/// Starts or resumes a pod.
///
/// # Arguments
///
/// * `pod_id` - The unique identifier of the pod to start
///
/// # Example
///
/// ```no_run
/// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
/// # use runpod_sdk::service::PodsService;
/// # async fn example() -> Result<()> {
/// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
/// let client = RunpodClient::new(config)?;
///
/// client.start_pod("pod_id").await?;
/// println!("Pod started");
/// # Ok(())
/// # }
/// ```
fn start_pod(&self, pod_id: &str) -> impl Future<Output = Result<()>>;
/// Stops a pod.
///
/// # Arguments
///
/// * `pod_id` - The unique identifier of the pod to stop
///
/// # Example
///
/// ```no_run
/// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
/// # use runpod_sdk::service::PodsService;
/// # async fn example() -> Result<()> {
/// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
/// let client = RunpodClient::new(config)?;
///
/// client.stop_pod("pod_id").await?;
/// println!("Pod stopped");
/// # Ok(())
/// # }
/// ```
fn stop_pod(&self, pod_id: &str) -> impl Future<Output = Result<()>>;
/// Resets a pod.
///
/// This operation will restart the pod with a fresh filesystem state.
///
/// # Arguments
///
/// * `pod_id` - The unique identifier of the pod to reset
///
/// # Example
///
/// ```no_run
/// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
/// # use runpod_sdk::service::PodsService;
/// # async fn example() -> Result<()> {
/// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
/// let client = RunpodClient::new(config)?;
///
/// client.reset_pod("pod_id").await?;
/// println!("Pod reset");
/// # Ok(())
/// # }
/// ```
fn reset_pod(&self, pod_id: &str) -> impl Future<Output = Result<()>>;
/// Restarts a pod.
///
/// This operation will stop and then start the pod.
///
/// # Arguments
///
/// * `pod_id` - The unique identifier of the pod to restart
///
/// # Example
///
/// ```no_run
/// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
/// # use runpod_sdk::service::PodsService;
/// # async fn example() -> Result<()> {
/// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
/// let client = RunpodClient::new(config)?;
///
/// client.restart_pod("pod_id").await?;
/// println!("Pod restarted");
/// # Ok(())
/// # }
/// ```
fn restart_pod(&self, pod_id: &str) -> impl Future<Output = Result<()>>;
}
impl PodsService for RunpodClient {
async fn create_pod(&self, input: PodCreateInput) -> Result<Pod> {
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, "Creating pod");
let response = self.post("/pods").json(&input).send().await?;
let response = response.error_for_status()?;
let pod: Pod = response.json().await?;
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id = %pod.id, "Pod created successfully");
Ok(pod)
}
async fn list_pods(&self, query: ListPodsQuery) -> Result<Pods> {
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, "Listing pods");
let response = self.get("/pods").query(&query).send().await?;
let response = response.error_for_status()?;
let pods: Pods = response.json().await?;
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, count = pods.len(), "Pods retrieved successfully");
Ok(pods)
}
async fn get_pod(&self, pod_id: &str, query: GetPodQuery) -> Result<Pod> {
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id, "Getting pod");
let path = format!("/pods/{}", pod_id);
let response = self.get(&path).query(&query).send().await?;
let response = response.error_for_status()?;
let pod: Pod = response.json().await?;
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, "Pod retrieved successfully");
Ok(pod)
}
async fn update_pod(&self, pod_id: &str, input: PodUpdateInput) -> Result<Pod> {
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id, "Updating pod");
let path = format!("/pods/{}", pod_id);
let response = self.patch(&path).json(&input).send().await?;
let response = response.error_for_status()?;
let pod: Pod = response.json().await?;
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, "Pod updated successfully");
Ok(pod)
}
async fn delete_pod(&self, pod_id: &str) -> Result<()> {
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id, "Deleting pod");
let path = format!("/pods/{}", pod_id);
let response = self.delete(&path).send().await?;
response.error_for_status()?;
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, "Pod deleted successfully");
Ok(())
}
async fn start_pod(&self, pod_id: &str) -> Result<()> {
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id, "Starting pod");
let path = format!("/pods/{}/start", pod_id);
let response = self.post(&path).send().await?;
response.error_for_status()?;
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, "Pod started successfully");
Ok(())
}
async fn stop_pod(&self, pod_id: &str) -> Result<()> {
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id, "Stopping pod");
let path = format!("/pods/{}/stop", pod_id);
let response = self.post(&path).send().await?;
response.error_for_status()?;
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, "Pod stopped successfully");
Ok(())
}
async fn reset_pod(&self, pod_id: &str) -> Result<()> {
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id, "Resetting pod");
let path = format!("/pods/{}/reset", pod_id);
let response = self.post(&path).send().await?;
response.error_for_status()?;
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, "Pod reset successfully");
Ok(())
}
async fn restart_pod(&self, pod_id: &str) -> Result<()> {
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, pod_id, "Restarting pod");
let path = format!("/pods/{}/restart", pod_id);
let response = self.post(&path).send().await?;
response.error_for_status()?;
#[cfg(feature = "tracing")]
tracing::debug!(target: TRACING_TARGET_SERVICE, "Pod restarted successfully");
Ok(())
}
}