oxide_api/
organizations.rs

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