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