oxide_api/
images.rs

1use anyhow::Result;
2
3use crate::Client;
4
5pub struct Images {
6    pub client: Client,
7}
8
9impl Images {
10    #[doc(hidden)]
11    pub fn new(client: Client) -> Self {
12        Images { client }
13    }
14
15    /**
16     * Fetch an image by id.
17     *
18     * This function performs a `GET` to the `/by-id/images/{id}` endpoint.
19     *
20     * **Parameters:**
21     *
22     * * `id: &str`
23     */
24    pub async fn view(&self, id: &str) -> Result<crate::types::Image> {
25        let url = format!(
26            "/by-id/images/{}",
27            crate::progenitor_support::encode_path(id),
28        );
29
30        self.client.get(&url, None).await
31    }
32
33    /**
34     * List images.
35     *
36     * This function performs a `GET` to the `/organizations/{organization_name}/projects/{project_name}/images` endpoint.
37     *
38     * List images in a project. The images are returned sorted by creation date, with the most recent images appearing first.
39     *
40     * **Parameters:**
41     *
42     * * `limit: u32` -- Maximum number of items returned by a single call.
43     * * `page_token: &str` -- Token returned by previous call to retrieve the subsequent page.
44     * * `sort_by: crate::types::NameSortMode` -- Supported set of sort modes for scanning by name only
45     *  
46     *  Currently, we only support scanning in ascending order.
47     * * `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.
48     * * `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.
49     */
50    pub async fn get_page(
51        &self,
52        limit: u32,
53        organization_name: &str,
54        page_token: &str,
55        project_name: &str,
56        sort_by: crate::types::NameSortMode,
57    ) -> Result<Vec<crate::types::Image>> {
58        let mut query_args: Vec<(String, String)> = Default::default();
59        if !limit.to_string().is_empty() {
60            query_args.push(("limit".to_string(), limit.to_string()));
61        }
62        if !page_token.is_empty() {
63            query_args.push(("page_token".to_string(), page_token.to_string()));
64        }
65        if !sort_by.to_string().is_empty() {
66            query_args.push(("sort_by".to_string(), sort_by.to_string()));
67        }
68        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
69        let url = format!(
70            "/organizations/{}/projects/{}/images?{}",
71            crate::progenitor_support::encode_path(organization_name),
72            crate::progenitor_support::encode_path(project_name),
73            query_
74        );
75
76        let resp: crate::types::ImageResultsPage = self.client.get(&url, None).await?;
77
78        // Return our response data.
79        Ok(resp.items)
80    }
81
82    /**
83     * List images.
84     *
85     * This function performs a `GET` to the `/organizations/{organization_name}/projects/{project_name}/images` endpoint.
86     *
87     * As opposed to `get`, this function returns all the pages of the request at once.
88     *
89     * List images in a project. The images are returned sorted by creation date, with the most recent images appearing first.
90     */
91    pub async fn get_all(
92        &self,
93        organization_name: &str,
94        project_name: &str,
95        sort_by: crate::types::NameSortMode,
96    ) -> Result<Vec<crate::types::Image>> {
97        let mut query_args: Vec<(String, String)> = Default::default();
98        if !sort_by.to_string().is_empty() {
99            query_args.push(("sort_by".to_string(), sort_by.to_string()));
100        }
101        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
102        let url = format!(
103            "/organizations/{}/projects/{}/images?{}",
104            crate::progenitor_support::encode_path(organization_name),
105            crate::progenitor_support::encode_path(project_name),
106            query_
107        );
108
109        let mut resp: crate::types::ImageResultsPage = self.client.get(&url, None).await?;
110
111        let mut items = resp.items;
112        let mut page = resp.next_page;
113
114        // Paginate if we should.
115        while !page.is_empty() {
116            if !url.contains('?') {
117                resp = self
118                    .client
119                    .get(&format!("{}?page={}", url, page), None)
120                    .await?;
121            } else {
122                resp = self
123                    .client
124                    .get(&format!("{}&page={}", url, page), None)
125                    .await?;
126            }
127
128            items.append(&mut resp.items);
129
130            if !resp.next_page.is_empty() && resp.next_page != page {
131                page = resp.next_page.to_string();
132            } else {
133                page = "".to_string();
134            }
135        }
136
137        // Return our response data.
138        Ok(items)
139    }
140
141    /**
142     * Create an image.
143     *
144     * This function performs a `POST` to the `/organizations/{organization_name}/projects/{project_name}/images` endpoint.
145     *
146     * Create a new image in a project.
147     *
148     * **Parameters:**
149     *
150     * * `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.
151     * * `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.
152     */
153    pub async fn post(
154        &self,
155        organization_name: &str,
156        project_name: &str,
157        body: &crate::types::ImageCreate,
158    ) -> Result<crate::types::Image> {
159        let url = format!(
160            "/organizations/{}/projects/{}/images",
161            crate::progenitor_support::encode_path(organization_name),
162            crate::progenitor_support::encode_path(project_name),
163        );
164
165        self.client
166            .post(&url, Some(reqwest::Body::from(serde_json::to_vec(body)?)))
167            .await
168    }
169
170    /**
171     * Fetch an image.
172     *
173     * This function performs a `GET` to the `/organizations/{organization_name}/projects/{project_name}/images/{image_name}` endpoint.
174     *
175     * Fetch the details for a specific image in a project.
176     *
177     * **Parameters:**
178     *
179     * * `image_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.
180     * * `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.
181     * * `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.
182     */
183    pub async fn get(
184        &self,
185        image_name: &str,
186        organization_name: &str,
187        project_name: &str,
188    ) -> Result<crate::types::Image> {
189        let url = format!(
190            "/organizations/{}/projects/{}/images/{}",
191            crate::progenitor_support::encode_path(organization_name),
192            crate::progenitor_support::encode_path(project_name),
193            crate::progenitor_support::encode_path(image_name),
194        );
195
196        self.client.get(&url, None).await
197    }
198
199    /**
200     * Delete an image.
201     *
202     * This function performs a `DELETE` to the `/organizations/{organization_name}/projects/{project_name}/images/{image_name}` endpoint.
203     *
204     * Permanently delete an image from a project. This operation cannot be undone. Any instances in the project using the image will continue to run, however new instances can not be created with this image.
205     *
206     * **Parameters:**
207     *
208     * * `image_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.
209     * * `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.
210     * * `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.
211     */
212    pub async fn delete(
213        &self,
214        image_name: &str,
215        organization_name: &str,
216        project_name: &str,
217    ) -> Result<()> {
218        let url = format!(
219            "/organizations/{}/projects/{}/images/{}",
220            crate::progenitor_support::encode_path(organization_name),
221            crate::progenitor_support::encode_path(project_name),
222            crate::progenitor_support::encode_path(image_name),
223        );
224
225        self.client.delete(&url, None).await
226    }
227}