oxide_api/
ip_pools.rs

1use anyhow::Result;
2
3use crate::Client;
4
5pub struct IpPools {
6    pub client: Client,
7}
8
9impl IpPools {
10    #[doc(hidden)]
11    pub fn new(client: Client) -> Self {
12        IpPools { client }
13    }
14
15    /**
16     * List IP pools.
17     *
18     * This function performs a `GET` to the `/ip-pools` endpoint.
19     *
20     * **Parameters:**
21     *
22     * * `limit: u32` -- Maximum number of items returned by a single call.
23     * * `page_token: &str` -- Token returned by previous call to retrieve the subsequent page.
24     * * `sort_by: crate::types::NameOrIdSortMode` -- Supported set of sort modes for scanning by name or id.
25     */
26    pub async fn get_page(
27        &self,
28        limit: u32,
29        page_token: &str,
30        sort_by: crate::types::NameOrIdSortMode,
31    ) -> Result<Vec<crate::types::IpPool>> {
32        let mut query_args: Vec<(String, String)> = Default::default();
33        if !limit.to_string().is_empty() {
34            query_args.push(("limit".to_string(), limit.to_string()));
35        }
36        if !page_token.is_empty() {
37            query_args.push(("page_token".to_string(), page_token.to_string()));
38        }
39        if !sort_by.to_string().is_empty() {
40            query_args.push(("sort_by".to_string(), sort_by.to_string()));
41        }
42        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
43        let url = format!("/ip-pools?{}", query_);
44
45        let resp: crate::types::IpPoolResultsPage = self.client.get(&url, None).await?;
46
47        // Return our response data.
48        Ok(resp.items)
49    }
50
51    /**
52     * List IP pools.
53     *
54     * This function performs a `GET` to the `/ip-pools` endpoint.
55     *
56     * As opposed to `get`, this function returns all the pages of the request at once.
57     */
58    pub async fn get_all(
59        &self,
60        sort_by: crate::types::NameOrIdSortMode,
61    ) -> Result<Vec<crate::types::IpPool>> {
62        let mut query_args: Vec<(String, String)> = Default::default();
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!("/ip-pools?{}", query_);
68
69        let mut resp: crate::types::IpPoolResultsPage = self.client.get(&url, None).await?;
70
71        let mut items = resp.items;
72        let mut page = resp.next_page;
73
74        // Paginate if we should.
75        while !page.is_empty() {
76            if !url.contains('?') {
77                resp = self
78                    .client
79                    .get(&format!("{}?page={}", url, page), None)
80                    .await?;
81            } else {
82                resp = self
83                    .client
84                    .get(&format!("{}&page={}", url, page), None)
85                    .await?;
86            }
87
88            items.append(&mut resp.items);
89
90            if !resp.next_page.is_empty() && resp.next_page != page {
91                page = resp.next_page.to_string();
92            } else {
93                page = "".to_string();
94            }
95        }
96
97        // Return our response data.
98        Ok(items)
99    }
100
101    /**
102     * Create an IP pool.
103     *
104     * This function performs a `POST` to the `/ip-pools` endpoint.
105     */
106    pub async fn post(&self, body: &crate::types::IpPoolCreate) -> Result<crate::types::IpPool> {
107        let url = "/ip-pools".to_string();
108        self.client
109            .post(&url, Some(reqwest::Body::from(serde_json::to_vec(body)?)))
110            .await
111    }
112
113    /**
114     * Fetch an IP pool.
115     *
116     * This function performs a `GET` to the `/ip-pools/{pool_name}` endpoint.
117     *
118     * **Parameters:**
119     *
120     * * `pool_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.
121     */
122    pub async fn get_pool(&self, pool_name: &str) -> Result<crate::types::IpPool> {
123        let url = format!(
124            "/ip-pools/{}",
125            crate::progenitor_support::encode_path(pool_name),
126        );
127
128        self.client.get(&url, None).await
129    }
130
131    /**
132     * Update an IP Pool.
133     *
134     * This function performs a `PUT` to the `/ip-pools/{pool_name}` endpoint.
135     *
136     * **Parameters:**
137     *
138     * * `pool_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.
139     */
140    pub async fn put_pool(
141        &self,
142        pool_name: &str,
143        body: &crate::types::IpPoolUpdate,
144    ) -> Result<crate::types::IpPool> {
145        let url = format!(
146            "/ip-pools/{}",
147            crate::progenitor_support::encode_path(pool_name),
148        );
149
150        self.client
151            .put(&url, Some(reqwest::Body::from(serde_json::to_vec(body)?)))
152            .await
153    }
154
155    /**
156     * Delete an IP Pool.
157     *
158     * This function performs a `DELETE` to the `/ip-pools/{pool_name}` endpoint.
159     *
160     * **Parameters:**
161     *
162     * * `pool_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.
163     */
164    pub async fn delete_pool(&self, pool_name: &str) -> Result<()> {
165        let url = format!(
166            "/ip-pools/{}",
167            crate::progenitor_support::encode_path(pool_name),
168        );
169
170        self.client.delete(&url, None).await
171    }
172
173    /**
174     * List ranges for an IP pool.
175     *
176     * This function performs a `GET` to the `/ip-pools/{pool_name}/ranges` endpoint.
177     *
178     * Ranges are ordered by their first address.
179     *
180     * **Parameters:**
181     *
182     * * `pool_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.
183     * * `limit: u32` -- Maximum number of items returned by a single call.
184     * * `page_token: &str` -- Token returned by previous call to retrieve the subsequent page.
185     */
186    pub async fn ranges_get(
187        &self,
188        limit: u32,
189        page_token: &str,
190        pool_name: &str,
191    ) -> Result<Vec<crate::types::IpPoolRange>> {
192        let mut query_args: Vec<(String, String)> = Default::default();
193        if !limit.to_string().is_empty() {
194            query_args.push(("limit".to_string(), limit.to_string()));
195        }
196        if !page_token.is_empty() {
197            query_args.push(("page_token".to_string(), page_token.to_string()));
198        }
199        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
200        let url = format!(
201            "/ip-pools/{}/ranges?{}",
202            crate::progenitor_support::encode_path(pool_name),
203            query_
204        );
205
206        let resp: crate::types::IpPoolRangeResultsPage = self.client.get(&url, None).await?;
207
208        // Return our response data.
209        Ok(resp.items)
210    }
211
212    /**
213     * List ranges for an IP pool.
214     *
215     * This function performs a `GET` to the `/ip-pools/{pool_name}/ranges` endpoint.
216     *
217     * As opposed to `ranges_get`, this function returns all the pages of the request at once.
218     *
219     * Ranges are ordered by their first address.
220     */
221    pub async fn ranges_get_all(&self, pool_name: &str) -> Result<Vec<crate::types::IpPoolRange>> {
222        let url = format!(
223            "/ip-pools/{}/ranges",
224            crate::progenitor_support::encode_path(pool_name),
225        );
226
227        let mut resp: crate::types::IpPoolRangeResultsPage = self.client.get(&url, None).await?;
228
229        let mut items = resp.items;
230        let mut page = resp.next_page;
231
232        // Paginate if we should.
233        while !page.is_empty() {
234            if !url.contains('?') {
235                resp = self
236                    .client
237                    .get(&format!("{}?page={}", url, page), None)
238                    .await?;
239            } else {
240                resp = self
241                    .client
242                    .get(&format!("{}&page={}", url, page), None)
243                    .await?;
244            }
245
246            items.append(&mut resp.items);
247
248            if !resp.next_page.is_empty() && resp.next_page != page {
249                page = resp.next_page.to_string();
250            } else {
251                page = "".to_string();
252            }
253        }
254
255        // Return our response data.
256        Ok(items)
257    }
258
259    /**
260     * Add a range to an IP pool.
261     *
262     * This function performs a `POST` to the `/ip-pools/{pool_name}/ranges/add` endpoint.
263     *
264     * **Parameters:**
265     *
266     * * `pool_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.
267     */
268    pub async fn ranges_add(
269        &self,
270        pool_name: &str,
271        body: &crate::types::IpRange,
272    ) -> Result<crate::types::IpPoolRange> {
273        let url = format!(
274            "/ip-pools/{}/ranges/add",
275            crate::progenitor_support::encode_path(pool_name),
276        );
277
278        self.client
279            .post(&url, Some(reqwest::Body::from(serde_json::to_vec(body)?)))
280            .await
281    }
282
283    /**
284     * Remove a range from an IP pool.
285     *
286     * This function performs a `POST` to the `/ip-pools/{pool_name}/ranges/remove` endpoint.
287     *
288     * **Parameters:**
289     *
290     * * `pool_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.
291     */
292    pub async fn ranges_delete(&self, pool_name: &str, body: &crate::types::IpRange) -> Result<()> {
293        let url = format!(
294            "/ip-pools/{}/ranges/remove",
295            crate::progenitor_support::encode_path(pool_name),
296        );
297
298        self.client
299            .post(&url, Some(reqwest::Body::from(serde_json::to_vec(body)?)))
300            .await
301    }
302
303    /**
304     * Fetch an IP pool used for Oxide services.
305     *
306     * This function performs a `GET` to the `/ip-pools-service/{rack_id}` endpoint.
307     *
308     * **Parameters:**
309     *
310     * * `rack_id: &str`
311     */
312    pub async fn service_view(&self, rack_id: &str) -> Result<crate::types::IpPool> {
313        let url = format!(
314            "/ip-pools-service/{}",
315            crate::progenitor_support::encode_path(rack_id),
316        );
317
318        self.client.get(&url, None).await
319    }
320
321    /**
322     * List ranges for an IP pool used for Oxide services.
323     *
324     * This function performs a `GET` to the `/ip-pools-service/{rack_id}/ranges` endpoint.
325     *
326     * Ranges are ordered by their first address.
327     *
328     * **Parameters:**
329     *
330     * * `rack_id: &str`
331     * * `limit: u32` -- Maximum number of items returned by a single call.
332     * * `page_token: &str` -- Token returned by previous call to retrieve the subsequent page.
333     */
334    pub async fn service_range_list(
335        &self,
336        limit: u32,
337        page_token: &str,
338        rack_id: &str,
339    ) -> Result<Vec<crate::types::IpPoolRange>> {
340        let mut query_args: Vec<(String, String)> = Default::default();
341        if !limit.to_string().is_empty() {
342            query_args.push(("limit".to_string(), limit.to_string()));
343        }
344        if !page_token.is_empty() {
345            query_args.push(("page_token".to_string(), page_token.to_string()));
346        }
347        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
348        let url = format!(
349            "/ip-pools-service/{}/ranges?{}",
350            crate::progenitor_support::encode_path(rack_id),
351            query_
352        );
353
354        let resp: crate::types::IpPoolRangeResultsPage = self.client.get(&url, None).await?;
355
356        // Return our response data.
357        Ok(resp.items)
358    }
359
360    /**
361     * List ranges for an IP pool used for Oxide services.
362     *
363     * This function performs a `GET` to the `/ip-pools-service/{rack_id}/ranges` endpoint.
364     *
365     * As opposed to `service_range_list`, this function returns all the pages of the request at once.
366     *
367     * Ranges are ordered by their first address.
368     */
369    pub async fn service_range_list_all(
370        &self,
371        rack_id: &str,
372    ) -> Result<Vec<crate::types::IpPoolRange>> {
373        let url = format!(
374            "/ip-pools-service/{}/ranges",
375            crate::progenitor_support::encode_path(rack_id),
376        );
377
378        let mut resp: crate::types::IpPoolRangeResultsPage = self.client.get(&url, None).await?;
379
380        let mut items = resp.items;
381        let mut page = resp.next_page;
382
383        // Paginate if we should.
384        while !page.is_empty() {
385            if !url.contains('?') {
386                resp = self
387                    .client
388                    .get(&format!("{}?page={}", url, page), None)
389                    .await?;
390            } else {
391                resp = self
392                    .client
393                    .get(&format!("{}&page={}", url, page), None)
394                    .await?;
395            }
396
397            items.append(&mut resp.items);
398
399            if !resp.next_page.is_empty() && resp.next_page != page {
400                page = resp.next_page.to_string();
401            } else {
402                page = "".to_string();
403            }
404        }
405
406        // Return our response data.
407        Ok(items)
408    }
409
410    /**
411     * Add a range to an IP pool used for Oxide services.
412     *
413     * This function performs a `POST` to the `/ip-pools-service/{rack_id}/ranges/add` endpoint.
414     *
415     * **Parameters:**
416     *
417     * * `rack_id: &str`
418     */
419    pub async fn service_range_add(
420        &self,
421        rack_id: &str,
422        body: &crate::types::IpRange,
423    ) -> Result<crate::types::IpPoolRange> {
424        let url = format!(
425            "/ip-pools-service/{}/ranges/add",
426            crate::progenitor_support::encode_path(rack_id),
427        );
428
429        self.client
430            .post(&url, Some(reqwest::Body::from(serde_json::to_vec(body)?)))
431            .await
432    }
433
434    /**
435     * Remove a range from an IP pool used for Oxide services.
436     *
437     * This function performs a `POST` to the `/ip-pools-service/{rack_id}/ranges/remove` endpoint.
438     *
439     * **Parameters:**
440     *
441     * * `rack_id: &str`
442     */
443    pub async fn service_range_remove(
444        &self,
445        rack_id: &str,
446        body: &crate::types::IpRange,
447    ) -> Result<()> {
448        let url = format!(
449            "/ip-pools-service/{}/ranges/remove",
450            crate::progenitor_support::encode_path(rack_id),
451        );
452
453        self.client
454            .post(&url, Some(reqwest::Body::from(serde_json::to_vec(body)?)))
455            .await
456    }
457}