1use serde::{Deserialize, Serialize};
16
17use crate::client::{get_owner, Client, Response};
18use crate::error::Result;
19use crate::serde_util::null_to_default;
20
21#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
24#[serde(rename_all = "camelCase", default)]
25pub struct Role {
26 pub owner: String,
27 pub name: String,
28 pub created_time: String,
29 pub display_name: String,
30 pub description: String,
31
32 #[serde(deserialize_with = "null_to_default")]
33 pub users: Vec<String>,
34 #[serde(deserialize_with = "null_to_default")]
35 pub groups: Vec<String>,
36 #[serde(deserialize_with = "null_to_default")]
37 pub roles: Vec<String>,
38 #[serde(deserialize_with = "null_to_default")]
39 pub domains: Vec<String>,
40 pub is_enabled: bool,
41 #[serde(deserialize_with = "null_to_default")]
42 pub source_groups: Vec<String>,
43}
44
45impl Client {
46 pub async fn get_roles(&self) -> Result<Vec<Role>> {
48 self.get_list("get-roles", &[("owner", &self.config.organization_name)])
49 .await
50 }
51
52 pub async fn get_pagination_roles(
55 &self,
56 p: i32,
57 page_size: i32,
58 query: &[(&str, &str)],
59 ) -> Result<(Vec<Role>, i32)> {
60 self.get_pagination(
61 "get-roles",
62 &self.config.organization_name,
63 p,
64 page_size,
65 query,
66 )
67 .await
68 }
69
70 pub async fn get_role(&self, name: &str) -> Result<Option<Role>> {
72 self.get_object("get-role", &[("id", &self.get_id(name))])
73 .await
74 }
75
76 pub async fn add_role(&self, role: &Role) -> Result<bool> {
78 Ok(self.modify_role("add-role", role, &[]).await?.1)
79 }
80
81 pub async fn update_role(&self, role: &Role) -> Result<bool> {
83 Ok(self.modify_role("update-role", role, &[]).await?.1)
84 }
85
86 pub async fn update_role_for_columns(&self, role: &Role, columns: &[&str]) -> Result<bool> {
88 Ok(self.modify_role("update-role", role, columns).await?.1)
89 }
90
91 pub async fn delete_role(&self, role: &Role) -> Result<bool> {
93 Ok(self.modify_role("delete-role", role, &[]).await?.1)
94 }
95
96 async fn modify_role(
97 &self,
98 action: &str,
99 role: &Role,
100 columns: &[&str],
101 ) -> Result<(Response, bool)> {
102 let mut role = role.clone();
103 role.owner = get_owner(&role.owner, &self.config.organization_name);
104
105 let id = format!("{}/{}", role.owner, role.name);
106 self.modify_object(action, &id, &role, columns).await
107 }
108}