oxide_api/disks.rs
1use anyhow::Result;
2
3use crate::Client;
4
5pub struct Disks {
6 pub client: Client,
7}
8
9impl Disks {
10 #[doc(hidden)]
11 pub fn new(client: Client) -> Self {
12 Disks { client }
13 }
14
15 /**
16 * Fetch a disk by id.
17 *
18 * This function performs a `GET` to the `/by-id/disks/{id}` endpoint.
19 *
20 * **Parameters:**
21 *
22 * * `id: &str`
23 */
24 pub async fn view(&self, id: &str) -> Result<crate::types::Disk> {
25 let url = format!(
26 "/by-id/disks/{}",
27 crate::progenitor_support::encode_path(id),
28 );
29
30 self.client.get(&url, None).await
31 }
32
33 /**
34 * List disks.
35 *
36 * This function performs a `GET` to the `/organizations/{organization_name}/projects/{project_name}/disks` endpoint.
37 *
38 * **Parameters:**
39 *
40 * * `limit: u32` -- Maximum number of items returned by a single call.
41 * * `page_token: &str` -- Token returned by previous call to retrieve the subsequent page.
42 * * `sort_by: crate::types::NameSortMode` -- Supported set of sort modes for scanning by name only
43 *
44 * Currently, we only support scanning in ascending order.
45 * * `organization_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
46 * * `project_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
47 */
48 pub async fn get_page(
49 &self,
50 limit: u32,
51 organization_name: &str,
52 page_token: &str,
53 project_name: &str,
54 sort_by: crate::types::NameSortMode,
55 ) -> Result<Vec<crate::types::Disk>> {
56 let mut query_args: Vec<(String, String)> = Default::default();
57 if !limit.to_string().is_empty() {
58 query_args.push(("limit".to_string(), limit.to_string()));
59 }
60 if !page_token.is_empty() {
61 query_args.push(("page_token".to_string(), page_token.to_string()));
62 }
63 if !sort_by.to_string().is_empty() {
64 query_args.push(("sort_by".to_string(), sort_by.to_string()));
65 }
66 let query_ = serde_urlencoded::to_string(&query_args).unwrap();
67 let url = format!(
68 "/organizations/{}/projects/{}/disks?{}",
69 crate::progenitor_support::encode_path(organization_name),
70 crate::progenitor_support::encode_path(project_name),
71 query_
72 );
73
74 let resp: crate::types::DiskResultsPage = self.client.get(&url, None).await?;
75
76 // Return our response data.
77 Ok(resp.items)
78 }
79
80 /**
81 * List disks.
82 *
83 * This function performs a `GET` to the `/organizations/{organization_name}/projects/{project_name}/disks` endpoint.
84 *
85 * As opposed to `get`, this function returns all the pages of the request at once.
86 */
87 pub async fn get_all(
88 &self,
89 organization_name: &str,
90 project_name: &str,
91 sort_by: crate::types::NameSortMode,
92 ) -> Result<Vec<crate::types::Disk>> {
93 let mut query_args: Vec<(String, String)> = Default::default();
94 if !sort_by.to_string().is_empty() {
95 query_args.push(("sort_by".to_string(), sort_by.to_string()));
96 }
97 let query_ = serde_urlencoded::to_string(&query_args).unwrap();
98 let url = format!(
99 "/organizations/{}/projects/{}/disks?{}",
100 crate::progenitor_support::encode_path(organization_name),
101 crate::progenitor_support::encode_path(project_name),
102 query_
103 );
104
105 let mut resp: crate::types::DiskResultsPage = self.client.get(&url, None).await?;
106
107 let mut items = resp.items;
108 let mut page = resp.next_page;
109
110 // Paginate if we should.
111 while !page.is_empty() {
112 if !url.contains('?') {
113 resp = self
114 .client
115 .get(&format!("{}?page={}", url, page), None)
116 .await?;
117 } else {
118 resp = self
119 .client
120 .get(&format!("{}&page={}", url, page), None)
121 .await?;
122 }
123
124 items.append(&mut resp.items);
125
126 if !resp.next_page.is_empty() && resp.next_page != page {
127 page = resp.next_page.to_string();
128 } else {
129 page = "".to_string();
130 }
131 }
132
133 // Return our response data.
134 Ok(items)
135 }
136
137 /**
138 * Create a disk.
139 *
140 * This function performs a `POST` to the `/organizations/{organization_name}/projects/{project_name}/disks` endpoint.
141 *
142 * **Parameters:**
143 *
144 * * `organization_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
145 * * `project_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
146 */
147 pub async fn post(
148 &self,
149 organization_name: &str,
150 project_name: &str,
151 body: &crate::types::DiskCreate,
152 ) -> Result<crate::types::Disk> {
153 let url = format!(
154 "/organizations/{}/projects/{}/disks",
155 crate::progenitor_support::encode_path(organization_name),
156 crate::progenitor_support::encode_path(project_name),
157 );
158
159 self.client
160 .post(&url, Some(reqwest::Body::from(serde_json::to_vec(body)?)))
161 .await
162 }
163
164 /**
165 * Fetch a disk.
166 *
167 * This function performs a `GET` to the `/organizations/{organization_name}/projects/{project_name}/disks/{disk_name}` endpoint.
168 *
169 * **Parameters:**
170 *
171 * * `disk_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
172 * * `organization_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
173 * * `project_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
174 */
175 pub async fn get(
176 &self,
177 disk_name: &str,
178 organization_name: &str,
179 project_name: &str,
180 ) -> Result<crate::types::Disk> {
181 let url = format!(
182 "/organizations/{}/projects/{}/disks/{}",
183 crate::progenitor_support::encode_path(organization_name),
184 crate::progenitor_support::encode_path(project_name),
185 crate::progenitor_support::encode_path(disk_name),
186 );
187
188 self.client.get(&url, None).await
189 }
190
191 /**
192 * Delete a disk.
193 *
194 * This function performs a `DELETE` to the `/organizations/{organization_name}/projects/{project_name}/disks/{disk_name}` endpoint.
195 *
196 * **Parameters:**
197 *
198 * * `disk_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
199 * * `organization_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
200 * * `project_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
201 */
202 pub async fn delete(
203 &self,
204 disk_name: &str,
205 organization_name: &str,
206 project_name: &str,
207 ) -> Result<()> {
208 let url = format!(
209 "/organizations/{}/projects/{}/disks/{}",
210 crate::progenitor_support::encode_path(organization_name),
211 crate::progenitor_support::encode_path(project_name),
212 crate::progenitor_support::encode_path(disk_name),
213 );
214
215 self.client.delete(&url, None).await
216 }
217
218 /**
219 * Fetch disk metrics.
220 *
221 * This function performs a `GET` to the `/organizations/{organization_name}/projects/{project_name}/disks/{disk_name}/metrics/{metric_name}` endpoint.
222 *
223 * **Parameters:**
224 *
225 * * `disk_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
226 * * `metric_name: crate::types::DiskMetricName`
227 * * `organization_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
228 * * `project_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
229 * * `end_time: chrono::DateTime<chrono::Utc>` -- An exclusive end time of metrics.
230 * * `limit: u32` -- Maximum number of items returned by a single call.
231 * * `page_token: &str` -- Token returned by previous call to retrieve the subsequent page.
232 * * `start_time: chrono::DateTime<chrono::Utc>` -- An inclusive start time of metrics.
233 */
234 pub async fn metrics_list(
235 &self,
236 disk_name: &str,
237 end_time: Option<chrono::DateTime<chrono::Utc>>,
238 limit: u32,
239 metric_name: &str,
240 organization_name: &str,
241 page_token: &str,
242 project_name: &str,
243 start_time: Option<chrono::DateTime<chrono::Utc>>,
244 ) -> Result<Vec<crate::types::Measurement>> {
245 let mut query_args: Vec<(String, String)> = Default::default();
246 if let Some(date) = end_time {
247 query_args.push(("end_time".to_string(), date.to_rfc3339()));
248 }
249 if !limit.to_string().is_empty() {
250 query_args.push(("limit".to_string(), limit.to_string()));
251 }
252 if !page_token.is_empty() {
253 query_args.push(("page_token".to_string(), page_token.to_string()));
254 }
255 if let Some(date) = start_time {
256 query_args.push(("start_time".to_string(), date.to_rfc3339()));
257 }
258 let query_ = serde_urlencoded::to_string(&query_args).unwrap();
259 let url = format!(
260 "/organizations/{}/projects/{}/disks/{}/metrics/{}?{}",
261 crate::progenitor_support::encode_path(organization_name),
262 crate::progenitor_support::encode_path(project_name),
263 crate::progenitor_support::encode_path(disk_name),
264 crate::progenitor_support::encode_path(metric_name),
265 query_
266 );
267
268 let resp: crate::types::MeasurementResultsPage = self.client.get(&url, None).await?;
269
270 // Return our response data.
271 Ok(resp.items)
272 }
273
274 /**
275 * Fetch disk metrics.
276 *
277 * This function performs a `GET` to the `/organizations/{organization_name}/projects/{project_name}/disks/{disk_name}/metrics/{metric_name}` endpoint.
278 *
279 * As opposed to `metrics_list`, this function returns all the pages of the request at once.
280 */
281 pub async fn metrics_list_all(
282 &self,
283 disk_name: &str,
284 end_time: Option<chrono::DateTime<chrono::Utc>>,
285 metric_name: &str,
286 organization_name: &str,
287 project_name: &str,
288 start_time: Option<chrono::DateTime<chrono::Utc>>,
289 ) -> Result<Vec<crate::types::Measurement>> {
290 let mut query_args: Vec<(String, String)> = Default::default();
291 if let Some(date) = end_time {
292 query_args.push(("end_time".to_string(), date.to_rfc3339()));
293 }
294 if let Some(date) = start_time {
295 query_args.push(("start_time".to_string(), date.to_rfc3339()));
296 }
297 let query_ = serde_urlencoded::to_string(&query_args).unwrap();
298 let url = format!(
299 "/organizations/{}/projects/{}/disks/{}/metrics/{}?{}",
300 crate::progenitor_support::encode_path(organization_name),
301 crate::progenitor_support::encode_path(project_name),
302 crate::progenitor_support::encode_path(disk_name),
303 crate::progenitor_support::encode_path(metric_name),
304 query_
305 );
306
307 let mut resp: crate::types::MeasurementResultsPage = self.client.get(&url, None).await?;
308
309 let mut items = resp.items;
310 let mut page = resp.next_page;
311
312 // Paginate if we should.
313 while !page.is_empty() {
314 if !url.contains('?') {
315 resp = self
316 .client
317 .get(&format!("{}?page={}", url, page), None)
318 .await?;
319 } else {
320 resp = self
321 .client
322 .get(&format!("{}&page={}", url, page), None)
323 .await?;
324 }
325
326 items.append(&mut resp.items);
327
328 if !resp.next_page.is_empty() && resp.next_page != page {
329 page = resp.next_page.to_string();
330 } else {
331 page = "".to_string();
332 }
333 }
334
335 // Return our response data.
336 Ok(items)
337 }
338}