oxide_api/
snapshots.rs

1use anyhow::Result;
2
3use crate::Client;
4
5pub struct Snapshots {
6    pub client: Client,
7}
8
9impl Snapshots {
10    #[doc(hidden)]
11    pub fn new(client: Client) -> Self {
12        Snapshots { client }
13    }
14
15    /**
16     * Fetch a snapshot by id.
17     *
18     * This function performs a `GET` to the `/by-id/snapshots/{id}` endpoint.
19     *
20     * **Parameters:**
21     *
22     * * `id: &str`
23     */
24    pub async fn view(&self, id: &str) -> Result<crate::types::Snapshot> {
25        let url = format!(
26            "/by-id/snapshots/{}",
27            crate::progenitor_support::encode_path(id),
28        );
29
30        self.client.get(&url, None).await
31    }
32
33    /**
34     * List snapshots.
35     *
36     * This function performs a `GET` to the `/organizations/{organization_name}/projects/{project_name}/snapshots` 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::Snapshot>> {
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/{}/snapshots?{}",
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::SnapshotResultsPage = self.client.get(&url, None).await?;
75
76        // Return our response data.
77        Ok(resp.items)
78    }
79
80    /**
81     * List snapshots.
82     *
83     * This function performs a `GET` to the `/organizations/{organization_name}/projects/{project_name}/snapshots` 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::Snapshot>> {
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/{}/snapshots?{}",
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::SnapshotResultsPage = 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 snapshot.
139     *
140     * This function performs a `POST` to the `/organizations/{organization_name}/projects/{project_name}/snapshots` endpoint.
141     *
142     * Creates a point-in-time snapshot from a disk.
143     *
144     * **Parameters:**
145     *
146     * * `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.
147     * * `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.
148     */
149    pub async fn post(
150        &self,
151        organization_name: &str,
152        project_name: &str,
153        body: &crate::types::SnapshotCreate,
154    ) -> Result<crate::types::Snapshot> {
155        let url = format!(
156            "/organizations/{}/projects/{}/snapshots",
157            crate::progenitor_support::encode_path(organization_name),
158            crate::progenitor_support::encode_path(project_name),
159        );
160
161        self.client
162            .post(&url, Some(reqwest::Body::from(serde_json::to_vec(body)?)))
163            .await
164    }
165
166    /**
167     * Fetch a snapshot.
168     *
169     * This function performs a `GET` to the `/organizations/{organization_name}/projects/{project_name}/snapshots/{snapshot_name}` endpoint.
170     *
171     * **Parameters:**
172     *
173     * * `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.
174     * * `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.
175     * * `snapshot_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.
176     */
177    pub async fn get(
178        &self,
179        organization_name: &str,
180        project_name: &str,
181        snapshot_name: &str,
182    ) -> Result<crate::types::Snapshot> {
183        let url = format!(
184            "/organizations/{}/projects/{}/snapshots/{}",
185            crate::progenitor_support::encode_path(organization_name),
186            crate::progenitor_support::encode_path(project_name),
187            crate::progenitor_support::encode_path(snapshot_name),
188        );
189
190        self.client.get(&url, None).await
191    }
192
193    /**
194     * Delete a snapshot.
195     *
196     * This function performs a `DELETE` to the `/organizations/{organization_name}/projects/{project_name}/snapshots/{snapshot_name}` endpoint.
197     *
198     * **Parameters:**
199     *
200     * * `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.
201     * * `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.
202     * * `snapshot_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.
203     */
204    pub async fn delete(
205        &self,
206        organization_name: &str,
207        project_name: &str,
208        snapshot_name: &str,
209    ) -> Result<()> {
210        let url = format!(
211            "/organizations/{}/projects/{}/snapshots/{}",
212            crate::progenitor_support::encode_path(organization_name),
213            crate::progenitor_support::encode_path(project_name),
214            crate::progenitor_support::encode_path(snapshot_name),
215        );
216
217        self.client.delete(&url, None).await
218    }
219}