oxide_api/
images_global.rs

1use anyhow::Result;
2
3use crate::Client;
4
5pub struct ImagesGlobal {
6    pub client: Client,
7}
8
9impl ImagesGlobal {
10    #[doc(hidden)]
11    pub fn new(client: Client) -> Self {
12        ImagesGlobal { client }
13    }
14
15    /**
16     * Fetch a global image by id.
17     *
18     * This function performs a `GET` to the `/by-id/global-images/{id}` endpoint.
19     *
20     * **Parameters:**
21     *
22     * * `id: &str`
23     */
24    pub async fn image_global_view(&self, id: &str) -> Result<crate::types::GlobalImage> {
25        let url = format!(
26            "/by-id/global-images/{}",
27            crate::progenitor_support::encode_path(id),
28        );
29
30        self.client.get(&url, None).await
31    }
32
33    /**
34     * List global images.
35     *
36     * This function performs a `GET` to the `/images` endpoint.
37     *
38     * Returns a list of all the global images. Global 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     */
48    pub async fn images_get(
49        &self,
50        limit: u32,
51        page_token: &str,
52        sort_by: crate::types::NameSortMode,
53    ) -> Result<Vec<crate::types::GlobalImage>> {
54        let mut query_args: Vec<(String, String)> = Default::default();
55        if !limit.to_string().is_empty() {
56            query_args.push(("limit".to_string(), limit.to_string()));
57        }
58        if !page_token.is_empty() {
59            query_args.push(("page_token".to_string(), page_token.to_string()));
60        }
61        if !sort_by.to_string().is_empty() {
62            query_args.push(("sort_by".to_string(), sort_by.to_string()));
63        }
64        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
65        let url = format!("/images?{}", query_);
66
67        let resp: crate::types::GlobalImageResultsPage = self.client.get(&url, None).await?;
68
69        // Return our response data.
70        Ok(resp.items)
71    }
72
73    /**
74     * List global images.
75     *
76     * This function performs a `GET` to the `/images` endpoint.
77     *
78     * As opposed to `images_get`, this function returns all the pages of the request at once.
79     *
80     * Returns a list of all the global images. Global images are returned sorted by creation date, with the most recent images appearing first.
81     */
82    pub async fn images_get_all(
83        &self,
84        sort_by: crate::types::NameSortMode,
85    ) -> Result<Vec<crate::types::GlobalImage>> {
86        let mut query_args: Vec<(String, String)> = Default::default();
87        if !sort_by.to_string().is_empty() {
88            query_args.push(("sort_by".to_string(), sort_by.to_string()));
89        }
90        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
91        let url = format!("/images?{}", query_);
92
93        let mut resp: crate::types::GlobalImageResultsPage = self.client.get(&url, None).await?;
94
95        let mut items = resp.items;
96        let mut page = resp.next_page;
97
98        // Paginate if we should.
99        while !page.is_empty() {
100            if !url.contains('?') {
101                resp = self
102                    .client
103                    .get(&format!("{}?page={}", url, page), None)
104                    .await?;
105            } else {
106                resp = self
107                    .client
108                    .get(&format!("{}&page={}", url, page), None)
109                    .await?;
110            }
111
112            items.append(&mut resp.items);
113
114            if !resp.next_page.is_empty() && resp.next_page != page {
115                page = resp.next_page.to_string();
116            } else {
117                page = "".to_string();
118            }
119        }
120
121        // Return our response data.
122        Ok(items)
123    }
124
125    /**
126     * Create a global image.
127     *
128     * This function performs a `POST` to the `/images` endpoint.
129     *
130     * Create a new global image. This image can then be used by any user as a base for instances.
131     */
132    pub async fn images_post(
133        &self,
134        body: &crate::types::GlobalImageCreate,
135    ) -> Result<crate::types::GlobalImage> {
136        let url = "/images".to_string();
137        self.client
138            .post(&url, Some(reqwest::Body::from(serde_json::to_vec(body)?)))
139            .await
140    }
141
142    /**
143     * Fetch a global image.
144     *
145     * This function performs a `GET` to the `/images/{image_name}` endpoint.
146     *
147     * Returns the details of a specific global image.
148     *
149     * **Parameters:**
150     *
151     * * `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.
152     */
153    pub async fn images_get_image(&self, image_name: &str) -> Result<crate::types::GlobalImage> {
154        let url = format!(
155            "/images/{}",
156            crate::progenitor_support::encode_path(image_name),
157        );
158
159        self.client.get(&url, None).await
160    }
161
162    /**
163     * Delete a global image.
164     *
165     * This function performs a `DELETE` to the `/images/{image_name}` endpoint.
166     *
167     * Permanently delete a global image. This operation cannot be undone. Any instances using the global image will continue to run, however new instances can not be created with this image.
168     *
169     * **Parameters:**
170     *
171     * * `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.
172     */
173    pub async fn images_delete_image(&self, image_name: &str) -> Result<()> {
174        let url = format!(
175            "/images/{}",
176            crate::progenitor_support::encode_path(image_name),
177        );
178
179        self.client.delete(&url, None).await
180    }
181}