Skip to main content

tapis_apps/apis/
permissions_api.rs

1/*
2 * Tapis Applications API
3 *
4 * The Tapis Applications API provides for management of Tapis applications including permissions.
5 *
6 * The version of the OpenAPI document: 26Q1.0
7 * Contact: cicsupport@tacc.utexas.edu
8 * Generated by: https://openapi-generator.tech
9 */
10
11use super::{ContentType, Error, configuration};
12use crate::{apis::ResponseContent, models};
13use reqwest;
14use serde::{Deserialize, Serialize, de::Error as _};
15
16/// struct for typed errors of method [`get_user_perms`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum GetUserPermsError {
20    Status400(models::RespBasic),
21    Status403(models::RespBasic),
22    Status404(models::RespBasic),
23    Status500(models::RespBasic),
24    UnknownValue(serde_json::Value),
25}
26
27/// struct for typed errors of method [`grant_user_perms`]
28#[derive(Debug, Clone, Serialize, Deserialize)]
29#[serde(untagged)]
30pub enum GrantUserPermsError {
31    Status400(models::RespBasic),
32    Status403(models::RespBasic),
33    Status500(models::RespBasic),
34    UnknownValue(serde_json::Value),
35}
36
37/// struct for typed errors of method [`revoke_user_perm`]
38#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(untagged)]
40pub enum RevokeUserPermError {
41    Status400(models::RespBasic),
42    Status403(models::RespBasic),
43    Status500(models::RespBasic),
44    UnknownValue(serde_json::Value),
45}
46
47/// struct for typed errors of method [`revoke_user_perms`]
48#[derive(Debug, Clone, Serialize, Deserialize)]
49#[serde(untagged)]
50pub enum RevokeUserPermsError {
51    Status403(models::RespBasic),
52    Status500(models::RespBasic),
53    UnknownValue(serde_json::Value),
54}
55
56/// Retrieve all application related permissions for a given application and user.
57pub async fn get_user_perms(
58    configuration: &configuration::Configuration,
59    app_id: &str,
60    user_name: &str,
61) -> Result<models::RespNameArray, Error<GetUserPermsError>> {
62    // add a prefix to parameters to efficiently prevent name collisions
63    let p_path_app_id = app_id;
64    let p_path_user_name = user_name;
65
66    let uri_str = format!(
67        "{}/v3/apps/perms/{appId}/user/{userName}",
68        configuration.base_path,
69        appId = crate::apis::urlencode(p_path_app_id),
70        userName = crate::apis::urlencode(p_path_user_name)
71    );
72    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
73
74    if let Some(ref user_agent) = configuration.user_agent {
75        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
76    }
77    if let Some(ref apikey) = configuration.api_key {
78        let key = apikey.key.clone();
79        let value = match apikey.prefix {
80            Some(ref prefix) => format!("{} {}", prefix, key),
81            None => key,
82        };
83        req_builder = req_builder.header("X-Tapis-Token", value);
84    };
85
86    let req = req_builder.build()?;
87    let resp = configuration.client.execute(req).await?;
88
89    let status = resp.status();
90    let content_type = resp
91        .headers()
92        .get("content-type")
93        .and_then(|v| v.to_str().ok())
94        .unwrap_or("application/octet-stream");
95    let content_type = super::ContentType::from(content_type);
96
97    if !status.is_client_error() && !status.is_server_error() {
98        let content = resp.text().await?;
99        match content_type {
100            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
101            ContentType::Text => Err(Error::from(serde_json::Error::custom(
102                "Received `text/plain` content type response that cannot be converted to `models::RespNameArray`",
103            ))),
104            ContentType::Unsupported(unknown_type) => {
105                Err(Error::from(serde_json::Error::custom(format!(
106                    "Received `{unknown_type}` content type response that cannot be converted to `models::RespNameArray`"
107                ))))
108            }
109        }
110    } else {
111        let content = resp.text().await?;
112        let entity: Option<GetUserPermsError> = serde_json::from_str(&content).ok();
113        Err(Error::ResponseError(ResponseContent {
114            status,
115            content,
116            entity,
117        }))
118    }
119}
120
121/// Create permissions in the Security Kernel for a user. Requester must be owner. Permissions are READ, MODIFY, EXECUTE.
122pub async fn grant_user_perms(
123    configuration: &configuration::Configuration,
124    app_id: &str,
125    user_name: &str,
126    req_perms: models::ReqPerms,
127) -> Result<models::RespBasic, Error<GrantUserPermsError>> {
128    // add a prefix to parameters to efficiently prevent name collisions
129    let p_path_app_id = app_id;
130    let p_path_user_name = user_name;
131    let p_body_req_perms = req_perms;
132
133    let uri_str = format!(
134        "{}/v3/apps/perms/{appId}/user/{userName}",
135        configuration.base_path,
136        appId = crate::apis::urlencode(p_path_app_id),
137        userName = crate::apis::urlencode(p_path_user_name)
138    );
139    let mut req_builder = configuration
140        .client
141        .request(reqwest::Method::POST, &uri_str);
142
143    if let Some(ref user_agent) = configuration.user_agent {
144        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
145    }
146    if let Some(ref apikey) = configuration.api_key {
147        let key = apikey.key.clone();
148        let value = match apikey.prefix {
149            Some(ref prefix) => format!("{} {}", prefix, key),
150            None => key,
151        };
152        req_builder = req_builder.header("X-Tapis-Token", value);
153    };
154    req_builder = req_builder.json(&p_body_req_perms);
155
156    let req = req_builder.build()?;
157    let resp = configuration.client.execute(req).await?;
158
159    let status = resp.status();
160    let content_type = resp
161        .headers()
162        .get("content-type")
163        .and_then(|v| v.to_str().ok())
164        .unwrap_or("application/octet-stream");
165    let content_type = super::ContentType::from(content_type);
166
167    if !status.is_client_error() && !status.is_server_error() {
168        let content = resp.text().await?;
169        match content_type {
170            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
171            ContentType::Text => Err(Error::from(serde_json::Error::custom(
172                "Received `text/plain` content type response that cannot be converted to `models::RespBasic`",
173            ))),
174            ContentType::Unsupported(unknown_type) => {
175                Err(Error::from(serde_json::Error::custom(format!(
176                    "Received `{unknown_type}` content type response that cannot be converted to `models::RespBasic`"
177                ))))
178            }
179        }
180    } else {
181        let content = resp.text().await?;
182        let entity: Option<GrantUserPermsError> = serde_json::from_str(&content).ok();
183        Err(Error::ResponseError(ResponseContent {
184            status,
185            content,
186            entity,
187        }))
188    }
189}
190
191/// Remove user permission from the Security Kernel. Requester must be owner. Permissions are READ, MODIFY, EXECUTE.
192pub async fn revoke_user_perm(
193    configuration: &configuration::Configuration,
194    app_id: &str,
195    user_name: &str,
196    permission: &str,
197) -> Result<models::RespBasic, Error<RevokeUserPermError>> {
198    // add a prefix to parameters to efficiently prevent name collisions
199    let p_path_app_id = app_id;
200    let p_path_user_name = user_name;
201    let p_path_permission = permission;
202
203    let uri_str = format!(
204        "{}/v3/apps/perms/{appId}/user/{userName}/{permission}",
205        configuration.base_path,
206        appId = crate::apis::urlencode(p_path_app_id),
207        userName = crate::apis::urlencode(p_path_user_name),
208        permission = crate::apis::urlencode(p_path_permission)
209    );
210    let mut req_builder = configuration
211        .client
212        .request(reqwest::Method::DELETE, &uri_str);
213
214    if let Some(ref user_agent) = configuration.user_agent {
215        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
216    }
217    if let Some(ref apikey) = configuration.api_key {
218        let key = apikey.key.clone();
219        let value = match apikey.prefix {
220            Some(ref prefix) => format!("{} {}", prefix, key),
221            None => key,
222        };
223        req_builder = req_builder.header("X-Tapis-Token", value);
224    };
225
226    let req = req_builder.build()?;
227    let resp = configuration.client.execute(req).await?;
228
229    let status = resp.status();
230    let content_type = resp
231        .headers()
232        .get("content-type")
233        .and_then(|v| v.to_str().ok())
234        .unwrap_or("application/octet-stream");
235    let content_type = super::ContentType::from(content_type);
236
237    if !status.is_client_error() && !status.is_server_error() {
238        let content = resp.text().await?;
239        match content_type {
240            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
241            ContentType::Text => Err(Error::from(serde_json::Error::custom(
242                "Received `text/plain` content type response that cannot be converted to `models::RespBasic`",
243            ))),
244            ContentType::Unsupported(unknown_type) => {
245                Err(Error::from(serde_json::Error::custom(format!(
246                    "Received `{unknown_type}` content type response that cannot be converted to `models::RespBasic`"
247                ))))
248            }
249        }
250    } else {
251        let content = resp.text().await?;
252        let entity: Option<RevokeUserPermError> = serde_json::from_str(&content).ok();
253        Err(Error::ResponseError(ResponseContent {
254            status,
255            content,
256            entity,
257        }))
258    }
259}
260
261/// Remove permissions from the Security Kernel for a user. Requester must be owner. Permissions are READ, MODIFY, EXECUTE.
262pub async fn revoke_user_perms(
263    configuration: &configuration::Configuration,
264    app_id: &str,
265    user_name: &str,
266    req_perms: models::ReqPerms,
267) -> Result<models::RespBasic, Error<RevokeUserPermsError>> {
268    // add a prefix to parameters to efficiently prevent name collisions
269    let p_path_app_id = app_id;
270    let p_path_user_name = user_name;
271    let p_body_req_perms = req_perms;
272
273    let uri_str = format!(
274        "{}/v3/apps/perms/{appId}/user/{userName}/revoke",
275        configuration.base_path,
276        appId = crate::apis::urlencode(p_path_app_id),
277        userName = crate::apis::urlencode(p_path_user_name)
278    );
279    let mut req_builder = configuration
280        .client
281        .request(reqwest::Method::POST, &uri_str);
282
283    if let Some(ref user_agent) = configuration.user_agent {
284        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
285    }
286    if let Some(ref apikey) = configuration.api_key {
287        let key = apikey.key.clone();
288        let value = match apikey.prefix {
289            Some(ref prefix) => format!("{} {}", prefix, key),
290            None => key,
291        };
292        req_builder = req_builder.header("X-Tapis-Token", value);
293    };
294    req_builder = req_builder.json(&p_body_req_perms);
295
296    let req = req_builder.build()?;
297    let resp = configuration.client.execute(req).await?;
298
299    let status = resp.status();
300    let content_type = resp
301        .headers()
302        .get("content-type")
303        .and_then(|v| v.to_str().ok())
304        .unwrap_or("application/octet-stream");
305    let content_type = super::ContentType::from(content_type);
306
307    if !status.is_client_error() && !status.is_server_error() {
308        let content = resp.text().await?;
309        match content_type {
310            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
311            ContentType::Text => Err(Error::from(serde_json::Error::custom(
312                "Received `text/plain` content type response that cannot be converted to `models::RespBasic`",
313            ))),
314            ContentType::Unsupported(unknown_type) => {
315                Err(Error::from(serde_json::Error::custom(format!(
316                    "Received `{unknown_type}` content type response that cannot be converted to `models::RespBasic`"
317                ))))
318            }
319        }
320    } else {
321        let content = resp.text().await?;
322        let entity: Option<RevokeUserPermsError> = serde_json::from_str(&content).ok();
323        Err(Error::ResponseError(ResponseContent {
324            status,
325            content,
326            entity,
327        }))
328    }
329}