runpod_sdk/service/volumes.rs
1use std::future::Future;
2
3#[cfg(feature = "tracing")]
4use crate::TRACING_TARGET_SERVICE;
5use crate::model::{
6 NetworkVolume, NetworkVolumeCreateInput, NetworkVolumeUpdateInput, NetworkVolumes,
7};
8use crate::{Result, RunpodClient};
9
10/// Trait for managing network volumes.
11///
12/// Provides methods for creating, listing, retrieving, updating, and deleting network volumes.
13/// This trait is implemented on the [`RunpodClient`](crate::client::RunpodClient).
14pub trait VolumesService {
15 /// Creates a new network volume.
16 ///
17 /// # Arguments
18 ///
19 /// * `input` - Configuration for the new network volume
20 ///
21 /// # Returns
22 ///
23 /// Returns the created network volume information.
24 ///
25 /// # Example
26 ///
27 /// ```no_run
28 /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
29 /// # use runpod_sdk::model::NetworkVolumeCreateInput;
30 /// # use runpod_sdk::service::VolumesService;
31 /// # async fn example() -> Result<()> {
32 /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
33 /// let client = RunpodClient::new(config)?;
34 ///
35 /// let input = NetworkVolumeCreateInput {
36 /// name: "My Volume".to_string(),
37 /// size: 50,
38 /// data_center_id: "EU-RO-1".to_string(),
39 /// };
40 ///
41 /// let volume = client.create_volume(input).await?;
42 /// println!("Created volume: {}", volume.id);
43 /// # Ok(())
44 /// # }
45 /// ```
46 fn create_volume(
47 &self,
48 input: NetworkVolumeCreateInput,
49 ) -> impl Future<Output = Result<NetworkVolume>>;
50
51 /// Lists all network volumes.
52 ///
53 /// # Returns
54 ///
55 /// Returns a vector of all network volumes associated with the account.
56 ///
57 /// # Example
58 ///
59 /// ```no_run
60 /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
61 /// # use runpod_sdk::service::VolumesService;
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 volumes = client.list_volumes().await?;
67 /// println!("Found {} volumes", volumes.len());
68 /// # Ok(())
69 /// # }
70 /// ```
71 fn list_volumes(&self) -> impl Future<Output = Result<NetworkVolumes>>;
72
73 /// Gets a specific network volume by ID.
74 ///
75 /// # Arguments
76 ///
77 /// * `volume_id` - The unique identifier of the network volume
78 ///
79 /// # Returns
80 ///
81 /// Returns the network volume information.
82 ///
83 /// # Example
84 ///
85 /// ```no_run
86 /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
87 /// # use runpod_sdk::service::VolumesService;
88 /// # async fn example() -> Result<()> {
89 /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
90 /// let client = RunpodClient::new(config)?;
91 ///
92 /// let volume = client.get_volume("volume_id").await?;
93 /// println!("Volume: {:?}", volume);
94 /// # Ok(())
95 /// # }
96 /// ```
97 fn get_volume(&self, volume_id: &str) -> impl Future<Output = Result<NetworkVolume>>;
98
99 /// Updates an existing network volume.
100 ///
101 /// # Arguments
102 ///
103 /// * `volume_id` - The unique identifier of the volume to update
104 /// * `input` - Update parameters for the network volume
105 ///
106 /// # Returns
107 ///
108 /// Returns the updated network volume information.
109 ///
110 /// # Example
111 ///
112 /// ```no_run
113 /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
114 /// # use runpod_sdk::model::NetworkVolumeUpdateInput;
115 /// # use runpod_sdk::service::VolumesService;
116 /// # async fn example() -> Result<()> {
117 /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
118 /// let client = RunpodClient::new(config)?;
119 ///
120 /// let input = NetworkVolumeUpdateInput {
121 /// name: Some("Updated Volume".to_string()),
122 /// ..Default::default()
123 /// };
124 ///
125 /// let volume = client.update_volume("volume_id", input).await?;
126 /// println!("Updated volume: {}", volume.id);
127 /// # Ok(())
128 /// # }
129 /// ```
130 fn update_volume(
131 &self,
132 volume_id: &str,
133 input: NetworkVolumeUpdateInput,
134 ) -> impl Future<Output = Result<NetworkVolume>>;
135
136 /// Deletes a network volume.
137 ///
138 /// This operation will permanently remove the network volume and all its data.
139 ///
140 /// # Arguments
141 ///
142 /// * `volume_id` - The unique identifier of the volume to delete
143 ///
144 /// # Example
145 ///
146 /// ```no_run
147 /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
148 /// # use runpod_sdk::service::VolumesService;
149 /// # async fn example() -> Result<()> {
150 /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
151 /// let client = RunpodClient::new(config)?;
152 ///
153 /// client.delete_volume("volume_id").await?;
154 /// println!("Volume deleted");
155 /// # Ok(())
156 /// # }
157 /// ```
158 fn delete_volume(&self, volume_id: &str) -> impl Future<Output = Result<()>>;
159}
160
161impl VolumesService for RunpodClient {
162 async fn create_volume(&self, input: NetworkVolumeCreateInput) -> Result<NetworkVolume> {
163 #[cfg(feature = "tracing")]
164 tracing::debug!(target: TRACING_TARGET_SERVICE, "Creating network volume");
165
166 let response = self.post("/networkvolumes").json(&input).send().await?;
167 let response = response.error_for_status()?;
168 let volume: NetworkVolume = response.json().await?;
169
170 #[cfg(feature = "tracing")]
171 tracing::debug!(target: TRACING_TARGET_SERVICE, volume_id = %volume.id, "Network volume created successfully");
172
173 Ok(volume)
174 }
175
176 async fn list_volumes(&self) -> Result<NetworkVolumes> {
177 #[cfg(feature = "tracing")]
178 tracing::debug!(target: TRACING_TARGET_SERVICE, "Listing network volumes");
179
180 let response = self.get("/networkvolumes").send().await?;
181 let response = response.error_for_status()?;
182 let volumes: NetworkVolumes = response.json().await?;
183
184 #[cfg(feature = "tracing")]
185 tracing::debug!(
186 count = volumes.len(),
187 "Network volumes retrieved successfully"
188 );
189
190 Ok(volumes)
191 }
192
193 async fn get_volume(&self, volume_id: &str) -> Result<NetworkVolume> {
194 #[cfg(feature = "tracing")]
195 tracing::debug!(target: TRACING_TARGET_SERVICE, "Getting network volume");
196
197 let path = format!("/networkvolumes/{}", volume_id);
198 let response = self.get(&path).send().await?;
199 let response = response.error_for_status()?;
200 let volume: NetworkVolume = response.json().await?;
201
202 #[cfg(feature = "tracing")]
203 tracing::debug!(target: TRACING_TARGET_SERVICE, "Network volume retrieved successfully");
204
205 Ok(volume)
206 }
207
208 async fn update_volume(
209 &self,
210 volume_id: &str,
211 input: NetworkVolumeUpdateInput,
212 ) -> Result<NetworkVolume> {
213 #[cfg(feature = "tracing")]
214 tracing::debug!(target: TRACING_TARGET_SERVICE, "Updating network volume");
215
216 let path = format!("/networkvolumes/{}", volume_id);
217 let response = self.patch(&path).json(&input).send().await?;
218 let response = response.error_for_status()?;
219 let volume: NetworkVolume = response.json().await?;
220
221 #[cfg(feature = "tracing")]
222 tracing::debug!(target: TRACING_TARGET_SERVICE, "Network volume updated successfully");
223
224 Ok(volume)
225 }
226
227 async fn delete_volume(&self, volume_id: &str) -> Result<()> {
228 #[cfg(feature = "tracing")]
229 tracing::debug!(target: TRACING_TARGET_SERVICE, "Deleting network volume");
230
231 let path = format!("/networkvolumes/{}", volume_id);
232 let response = self.delete(&path).send().await?;
233 response.error_for_status()?;
234
235 #[cfg(feature = "tracing")]
236 tracing::debug!(target: TRACING_TARGET_SERVICE, "Network volume deleted successfully");
237
238 Ok(())
239 }
240}